| 1 |
<?php |
| 2 |
namespace Elementor\Includes; |
| 3 |
|
| 4 |
class EditorAssetsAPI { |
| 5 |
protected array $config; |
| 6 |
|
| 7 |
const ASSETS_DATA_TRANSIENT_KEY = 'ASSETS_DATA_TRANSIENT_KEY'; |
| 8 |
|
| 9 |
const ASSETS_DATA_URL = 'ASSETS_DATA_URL'; |
| 10 |
|
| 11 |
const ASSETS_DATA_KEY = 'ASSETS_DATA_KEY'; |
| 12 |
|
| 13 |
const ASSETS_DATA_EXPIRATION = 'ASSETS_DATA_EXPIRATION'; |
| 14 |
|
| 15 |
const DEFAULT_EXPIRATION_TIME = '+1 hour'; |
| 16 |
|
| 17 |
public function __construct( array $config ) { |
| 18 |
$this->config = $config; |
| 19 |
} |
| 20 |
|
| 21 |
public function config( $key ): string { |
| 22 |
return $this->config[ $key ] ?? ''; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_assets_data( $force_request = false ): array { |
| 26 |
$assets_data = $this->get_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ) ); |
| 27 |
|
| 28 |
if ( $force_request || false === $assets_data ) { |
| 29 |
$fresh_data = $this->fetch_data(); |
| 30 |
|
| 31 |
if ( empty( $fresh_data ) ) { |
| 32 |
return ! empty( $assets_data ) ? $assets_data : []; |
| 33 |
} |
| 34 |
|
| 35 |
$assets_data = $fresh_data; |
| 36 |
$this->set_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ), $assets_data, $this->get_expiration_time() ); |
| 37 |
} |
| 38 |
|
| 39 |
return $assets_data; |
| 40 |
} |
| 41 |
|
| 42 |
private function fetch_data(): array { |
| 43 |
$response = wp_remote_get( $this->config( static::ASSETS_DATA_URL ) ); |
| 44 |
|
| 45 |
if ( is_wp_error( $response ) || \WP_Http::OK !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 46 |
return []; |
| 47 |
} |
| 48 |
|
| 49 |
$data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 50 |
|
| 51 |
if ( ! $this->has_valid_data( $data ) ) { |
| 52 |
return []; |
| 53 |
} |
| 54 |
|
| 55 |
return $data[ $this->config( static::ASSETS_DATA_KEY ) ]; |
| 56 |
} |
| 57 |
|
| 58 |
private function get_transient( $cache_key ) { |
| 59 |
$cache = get_option( $cache_key ); |
| 60 |
|
| 61 |
if ( empty( $cache['timeout'] ) ) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
if ( current_time( 'timestamp' ) > $cache['timeout'] ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
return json_decode( $cache['value'], true ); |
| 70 |
} |
| 71 |
|
| 72 |
private function set_transient( $cache_key, $value, $expiration ): bool { |
| 73 |
$data = [ |
| 74 |
'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ), |
| 75 |
'value' => wp_json_encode( $value ), |
| 76 |
]; |
| 77 |
|
| 78 |
return update_option( $cache_key, $data, false ); |
| 79 |
} |
| 80 |
|
| 81 |
private function get_expiration_time(): string { |
| 82 |
$expiration = $this->config( static::ASSETS_DATA_EXPIRATION ); |
| 83 |
return $expiration ? $expiration : static::DEFAULT_EXPIRATION_TIME; |
| 84 |
} |
| 85 |
|
| 86 |
private function has_valid_data( $data ): bool { |
| 87 |
if ( ! is_array( $data ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
$key = $this->config( static::ASSETS_DATA_KEY ); |
| 92 |
return static::is_non_empty_array( $data[ $key ] ?? null ); |
| 93 |
} |
| 94 |
|
| 95 |
public static function has_valid_nested_array( $data, array $nested_array_path ): bool { |
| 96 |
$current = $data; |
| 97 |
|
| 98 |
foreach ( $nested_array_path as $nested_key ) { |
| 99 |
if ( ! is_array( $current ) || ! array_key_exists( $nested_key, $current ) ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
$current = $current[ $nested_key ]; |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! static::is_non_empty_array( $current ) ) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
public static function is_valid_data( $data ): bool { |
| 113 |
return static::is_non_empty_array( $data ); |
| 114 |
} |
| 115 |
|
| 116 |
private static function is_non_empty_array( $value ): bool { |
| 117 |
return is_array( $value ) && ! empty( $value ); |
| 118 |
} |
| 119 |
} |
| 120 |
|