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