base.php (5456B)
1 <?php 2 namespace Elementor\Core\Files; 3 4 use Elementor\Plugin; 5 6 if ( ! defined( 'ABSPATH' ) ) { 7 exit; // Exit if accessed directly 8 } 9 10 abstract class Base { 11 12 const UPLOADS_DIR = 'elementor/'; 13 14 const DEFAULT_FILES_DIR = 'css/'; 15 16 const META_KEY = ''; 17 18 private static $wp_uploads_dir = []; 19 20 private $files_dir; 21 22 private $file_name; 23 24 /** 25 * File path. 26 * 27 * Holds the file path. 28 * 29 * @access private 30 * 31 * @var string 32 */ 33 private $path; 34 35 /** 36 * Content. 37 * 38 * Holds the file content. 39 * 40 * @access private 41 * 42 * @var string 43 */ 44 private $content; 45 46 /** 47 * @since 2.1.0 48 * @access public 49 * @static 50 */ 51 public static function get_base_uploads_dir() { 52 $wp_upload_dir = self::get_wp_uploads_dir(); 53 54 return $wp_upload_dir['basedir'] . '/' . self::UPLOADS_DIR; 55 } 56 57 /** 58 * @since 2.1.0 59 * @access public 60 * @static 61 */ 62 public static function get_base_uploads_url() { 63 $wp_upload_dir = self::get_wp_uploads_dir(); 64 65 return $wp_upload_dir['baseurl'] . '/' . self::UPLOADS_DIR; 66 } 67 68 /** 69 * Use a create function for PhpDoc (@return static). 70 * 71 * @return static 72 */ 73 public static function create() { 74 return Plugin::$instance->files_manager->get( get_called_class(), func_get_args() ); 75 } 76 77 /** 78 * @since 2.1.0 79 * @access public 80 */ 81 public function __construct( $file_name ) { 82 /** 83 * Elementor File Name 84 * 85 * Filters the File name 86 * 87 * @since 2.3.0 88 * 89 * @param string $file_name 90 * @param object $this The file instance, which inherits Elementor\Core\Files 91 */ 92 $file_name = apply_filters( 'elementor/files/file_name', $file_name, $this ); 93 94 $this->set_file_name( $file_name ); 95 96 $this->set_files_dir( static::DEFAULT_FILES_DIR ); 97 98 $this->set_path(); 99 } 100 101 /** 102 * @since 2.1.0 103 * @access public 104 */ 105 public function set_files_dir( $files_dir ) { 106 $this->files_dir = $files_dir; 107 } 108 109 /** 110 * @since 2.1.0 111 * @access public 112 */ 113 public function set_file_name( $file_name ) { 114 $this->file_name = $file_name; 115 } 116 117 /** 118 * @since 2.1.0 119 * @access public 120 */ 121 public function get_file_name() { 122 return $this->file_name; 123 } 124 125 /** 126 * @since 2.1.0 127 * @access public 128 */ 129 public function get_url() { 130 $url = set_url_scheme( self::get_base_uploads_url() . $this->files_dir . $this->file_name ); 131 132 return add_query_arg( [ 'ver' => $this->get_meta( 'time' ) ], $url ); 133 } 134 135 /** 136 * @since 2.1.0 137 * @access public 138 */ 139 public function get_content() { 140 if ( ! $this->content ) { 141 $this->content = $this->parse_content(); 142 } 143 144 return $this->content; 145 } 146 147 /** 148 * @since 2.1.0 149 * @access public 150 */ 151 public function update() { 152 $this->update_file(); 153 154 $meta = $this->get_meta(); 155 156 $meta['time'] = time(); 157 158 $this->update_meta( $meta ); 159 } 160 161 /** 162 * @since 2.1.0 163 * @access public 164 */ 165 public function update_file() { 166 $this->content = $this->parse_content(); 167 168 if ( $this->content ) { 169 $this->write(); 170 } else { 171 $this->delete(); 172 } 173 } 174 175 /** 176 * @since 2.1.0 177 * @access public 178 */ 179 public function write() { 180 return file_put_contents( $this->path, $this->content ); 181 } 182 183 /** 184 * @since 2.1.0 185 * @access public 186 */ 187 public function delete() { 188 if ( file_exists( $this->path ) ) { 189 unlink( $this->path ); 190 } 191 192 $this->delete_meta(); 193 } 194 195 /** 196 * Get meta data. 197 * 198 * Retrieve the CSS file meta data. Returns an array of all the data, or if 199 * custom property is given it will return the property value, or `null` if 200 * the property does not exist. 201 * 202 * @since 2.1.0 203 * @access public 204 * 205 * @param string $property Optional. Custom meta data property. Default is 206 * null. 207 * 208 * @return array|null An array of all the data, or if custom property is 209 * given it will return the property value, or `null` if 210 * the property does not exist. 211 */ 212 public function get_meta( $property = null ) { 213 $meta = array_merge( $this->get_default_meta(), (array) $this->load_meta() ); 214 215 if ( $property ) { 216 return isset( $meta[ $property ] ) ? $meta[ $property ] : null; 217 } 218 219 return $meta; 220 } 221 222 /** 223 * @since 2.1.0 224 * @access protected 225 * @abstract 226 */ 227 abstract protected function parse_content(); 228 229 /** 230 * Load meta. 231 * 232 * Retrieve the file meta data. 233 * 234 * @since 2.1.0 235 * @access protected 236 */ 237 protected function load_meta() { 238 return get_option( static::META_KEY ); 239 } 240 241 /** 242 * Update meta. 243 * 244 * Update the file meta data. 245 * 246 * @since 2.1.0 247 * @access protected 248 * 249 * @param array $meta New meta data. 250 */ 251 protected function update_meta( $meta ) { 252 update_option( static::META_KEY, $meta ); 253 } 254 255 /** 256 * Delete meta. 257 * 258 * Delete the file meta data. 259 * 260 * @since 2.1.0 261 * @access protected 262 */ 263 protected function delete_meta() { 264 delete_option( static::META_KEY ); 265 } 266 267 /** 268 * @since 2.1.0 269 * @access protected 270 */ 271 protected function get_default_meta() { 272 return [ 273 'time' => 0, 274 ]; 275 } 276 277 /** 278 * @since 2.1.0 279 * @access private 280 * @static 281 */ 282 private static function get_wp_uploads_dir() { 283 global $blog_id; 284 if ( empty( self::$wp_uploads_dir[ $blog_id ] ) ) { 285 self::$wp_uploads_dir[ $blog_id ] = wp_upload_dir( null, false ); 286 } 287 288 return self::$wp_uploads_dir[ $blog_id ]; 289 } 290 291 /** 292 * @since 2.1.0 293 * @access private 294 */ 295 private function set_path() { 296 $dir_path = self::get_base_uploads_dir() . $this->files_dir; 297 298 if ( ! is_dir( $dir_path ) ) { 299 wp_mkdir_p( $dir_path ); 300 } 301 302 $this->path = $dir_path . $this->file_name; 303 } 304 }