base.php (6547B)
1 <?php 2 namespace Elementor\Core\Page_Assets\Data_Managers; 3 4 if ( ! defined( 'ABSPATH' ) ) { 5 exit; // Exit if accessed directly. 6 } 7 8 /** 9 * Elementor Assets Data. 10 * 11 * @since 3.3.0 12 */ 13 abstract class Base { 14 const ASSETS_DATA_KEY = '_elementor_assets_data'; 15 16 /** 17 * @var array 18 */ 19 protected $assets_data; 20 21 /** 22 * @var string 23 */ 24 protected $content_type; 25 26 /** 27 * @var string 28 */ 29 protected $assets_category; 30 31 /** 32 * @var array 33 */ 34 private $assets_config; 35 36 /** 37 * @var array 38 */ 39 private $files_data; 40 41 /** 42 * Get Asset Content. 43 * 44 * Responsible for extracting the asset data from a certain file. 45 * Will be triggered automatically when the asset data does not exist, or when the asset version was changed. 46 * 47 * @since 3.3.0 48 * @access public 49 * 50 * @return string 51 */ 52 abstract protected function get_asset_content(); 53 54 /** 55 * Get Asset Key. 56 * 57 * The asset data will be saved in the DB under this key. 58 * 59 * @since 3.3.0 60 * @access protected 61 * 62 * @return string 63 */ 64 protected function get_key() { 65 return $this->assets_config['key']; 66 } 67 68 /** 69 * Get Relative Version. 70 * 71 * The asset data will be re-evaluated according the version number. 72 * 73 * @since 3.3.0 74 * @access protected 75 * 76 * @return string 77 */ 78 protected function get_version() { 79 return $this->assets_config['version']; 80 } 81 82 /** 83 * Get Asset Path. 84 * 85 * The asset data will be extracted from the file path. 86 * 87 * @since 3.3.0 88 * @access protected 89 * 90 * @return string 91 */ 92 protected function get_file_path() { 93 return $this->assets_config['file_path']; 94 } 95 96 /** 97 * Get Config Data. 98 * 99 * Holds a unique data relevant for the specific assets category type. 100 * 101 * @since 3.3.0 102 * @access protected 103 * 104 * @return string|array 105 */ 106 protected function get_config_data( $key = '' ) { 107 if ( isset( $this->assets_config['data'] ) ) { 108 if ( $key ) { 109 if ( isset( $this->assets_config['data'][ $key ] ) ) { 110 return $this->assets_config['data'][ $key ]; 111 } 112 113 return ''; 114 } 115 116 return $this->assets_config['data']; 117 } 118 119 return []; 120 } 121 122 /** 123 * Set Asset Data. 124 * 125 * Responsible for setting the current asset data. 126 * 127 * @since 3.3.0 128 * @access protected 129 * 130 * @return void 131 */ 132 protected function set_asset_data( $asset_key ) { 133 if ( ! isset( $this->assets_data[ $asset_key ] ) ) { 134 $this->assets_data[ $asset_key ] = []; 135 } 136 137 $this->assets_data[ $asset_key ]['content'] = $this->get_asset_content(); 138 $this->assets_data[ $asset_key ]['version'] = $this->get_version(); 139 140 $this->save_asset_data( $asset_key ); 141 } 142 143 /** 144 * Save Asset Data. 145 * 146 * Responsible for saving the asset data in the DB. 147 * 148 * @since 3.3.0 149 * @access protected 150 * 151 * @param string $asset_key 152 * 153 * @return void 154 */ 155 protected function save_asset_data( $asset_key ) { 156 $assets_data = $this->get_saved_assets_data(); 157 158 $content_type = $this->content_type; 159 $assets_category = $this->assets_category; 160 161 $assets_data[ $content_type ][ $assets_category ][ $asset_key ] = $this->assets_data[ $asset_key ]; 162 163 update_option( self::ASSETS_DATA_KEY, $assets_data ); 164 } 165 166 /** 167 * Is Asset Version Changed. 168 * 169 * Responsible for comparing the saved asset data version to the current relative version. 170 * 171 * @since 3.3.0 172 * @access protected 173 * 174 * @param string $asset_key 175 * 176 * @return boolean 177 */ 178 protected function is_asset_version_changed( $version ) { 179 return $this->get_version() !== $version; 180 } 181 182 /** 183 * Get File Data. 184 * 185 * Getting a file content or size. 186 * 187 * @since 3.3.0 188 * @access protected 189 * 190 * @param string $data_type (content|size) 191 * @param string $file_key - In case that the same file data is needed for multiple assets (like a JSON file), the file data key should be the same for all shared assets to make sure that the file is being read only once. 192 * 193 * @return string|number 194 */ 195 protected function get_file_data( $data_type, $file_key = '' ) { 196 $asset_key = $file_key ? $file_key : $this->get_key(); 197 198 if ( isset( $this->files_data[ $asset_key ][ $data_type ] ) ) { 199 return $this->files_data[ $asset_key ][ $data_type ]; 200 } 201 202 if ( ! isset( $this->files_data[ $asset_key ] ) ) { 203 $this->files_data[ $asset_key ] = []; 204 } 205 206 $asset_path = $this->get_file_path(); 207 208 if ( 'content' === $data_type ) { 209 $data = file_get_contents( $asset_path ); 210 211 if ( ! $data ) { 212 $data = ''; 213 } 214 } elseif ( 'size' === $data_type ) { 215 $data = file_exists( $asset_path ) ? filesize( $asset_path ) : 0; 216 } 217 218 $this->files_data[ $asset_key ][ $data_type ] = $data; 219 220 return $data; 221 } 222 223 /** 224 * Get Saved Assets Data. 225 * 226 * Getting the assets data from the DB. 227 * 228 * @since 3.3.0 229 * @access protected 230 * 231 * @return array 232 */ 233 protected function get_saved_assets_data() { 234 $assets_data = get_option( self::ASSETS_DATA_KEY, [] ); 235 236 $content_type = $this->content_type; 237 $assets_category = $this->assets_category; 238 239 if ( ! isset( $assets_data[ $content_type ] ) ) { 240 $assets_data[ $content_type ] = []; 241 } 242 243 if ( ! isset( $assets_data[ $content_type ][ $assets_category ] ) ) { 244 $assets_data[ $content_type ][ $assets_category ] = []; 245 } 246 return $assets_data; 247 } 248 249 /** 250 * Init Asset Data. 251 * 252 * Initialize the asset data and handles the asset content updates when needed. 253 * 254 * @since 3.3.0 255 * @access public 256 * 257 * @param array $config { 258 * @type string 'key' 259 * @type string 'version' 260 * @type string 'file_path' 261 * @type array 'data' 262 * } 263 * 264 * @return void 265 */ 266 public function init_asset_data( $config ) { 267 $this->assets_config = $config; 268 269 $asset_key = $config['key']; 270 271 $asset_data = isset( $this->assets_data[ $asset_key ] ) ? $this->assets_data[ $asset_key ] : []; 272 273 if ( ! $asset_data || $this->is_asset_version_changed( $asset_data['version'] ) ) { 274 $this->set_asset_data( $asset_key ); 275 } 276 } 277 278 /** 279 * Get Asset Data. 280 * 281 * Getting the asset data content. 282 * 283 * @since 3.3.0 284 * @access public 285 * 286 * @param array $config { 287 * @type string 'key' 288 * @type string 'version' 289 * @type string 'file_path' 290 * @type array 'data' 291 * } 292 * 293 * @return mixed 294 */ 295 public function get_asset_data( $config ) { 296 $this->init_asset_data( $config ); 297 298 $asset_key = $config['key']; 299 300 return $this->assets_data[ $asset_key ]['content']; 301 } 302 303 public function __construct() { 304 $assets_data = $this->get_saved_assets_data(); 305 306 $content_type = $this->content_type; 307 $assets_category = $this->assets_category; 308 309 $this->assets_data = $assets_data[ $content_type ][ $assets_category ]; 310 } 311 }