PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.5
Elementor Website Builder – more than just a page builder v4.3.5
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 All 458 releases
elementor / includes / editor-assets-api.php

editor-assets-api.php in Elementor Website Builder – more than just a page builder 4.3.5, at includes/editor-assets-api.php

170 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 const FAILED_FETCH_EXPIRATION = '+30 minutes';
17
18 const PRODUCTION_URL = 'https://assets.elementor.com';
19 const STAGING_URL = 'https://assets.stg.elementor.red';
20 const DEV_URL = 'https://assets.dev.builder.elementor.red';
21
22 private static ?array $allowed_hosts = null;
23
24 public function __construct( array $config ) {
25 $this->config = $config;
26 }
27
28 public function config( $key ): string {
29 return $this->config[ $key ] ?? '';
30 }
31
32 public function get_assets_data( $force_request = false ): array {
33 $assets_data = $this->get_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ) );
34
35 if ( $force_request || false === $assets_data ) {
36 $fresh_data = $this->fetch_data();
37
38 if ( empty( $fresh_data ) ) {
39 if ( false === $assets_data ) {
40 // Fetch failed and there is no existing cache. Write an empty
41 // entry with a short TTL so repeated page loads don't make
42 // blocking HTTP calls until the CDN recovers.
43 $this->set_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ), [], static::FAILED_FETCH_EXPIRATION );
44 }
45 return ! empty( $assets_data ) ? $assets_data : [];
46 }
47
48 $assets_data = $fresh_data;
49 $this->set_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ), $assets_data, $this->get_expiration_time() );
50 }
51
52 return $assets_data;
53 }
54
55 private function fetch_data(): array {
56 $url = $this->config( static::ASSETS_DATA_URL );
57 $data = self::do_safe_get_request( $url );
58
59 if ( ! $this->has_valid_data( $data ) ) {
60 return [];
61 }
62
63 return $data[ $this->config( static::ASSETS_DATA_KEY ) ];
64 }
65
66 private function get_transient( $cache_key ) {
67 $cache = get_option( $cache_key );
68
69 if ( empty( $cache['timeout'] ) ) {
70 return false;
71 }
72
73 if ( current_time( 'timestamp' ) > $cache['timeout'] ) {
74 return false;
75 }
76
77 return json_decode( $cache['value'], true );
78 }
79
80 private function set_transient( $cache_key, $value, $expiration ): bool {
81 $data = [
82 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ),
83 'value' => wp_json_encode( $value ),
84 ];
85
86 return update_option( $cache_key, $data, false );
87 }
88
89 private function get_expiration_time(): string {
90 $expiration = $this->config( static::ASSETS_DATA_EXPIRATION );
91 return $expiration ? $expiration : static::DEFAULT_EXPIRATION_TIME;
92 }
93
94 private function has_valid_data( $data ): bool {
95 if ( ! is_array( $data ) ) {
96 return false;
97 }
98
99 $key = $this->config( static::ASSETS_DATA_KEY );
100 return static::is_non_empty_array( $data[ $key ] ?? null );
101 }
102
103 public static function has_valid_nested_array( $data, array $nested_array_path ): bool {
104 $current = $data;
105
106 foreach ( $nested_array_path as $nested_key ) {
107 if ( ! is_array( $current ) || ! array_key_exists( $nested_key, $current ) ) {
108 return false;
109 }
110 $current = $current[ $nested_key ];
111 }
112
113 if ( ! static::is_non_empty_array( $current ) ) {
114 return false;
115 }
116
117 return true;
118 }
119
120 public static function is_valid_data( $data ): bool {
121 return static::is_non_empty_array( $data );
122 }
123
124 private static function is_non_empty_array( $value ): bool {
125 return is_array( $value ) && ! empty( $value );
126 }
127
128 private static function get_allowed_hosts(): array {
129 if ( null !== self::$allowed_hosts ) {
130 return self::$allowed_hosts;
131 }
132
133 self::$allowed_hosts = array_values( array_filter( array_map(
134 fn( $url ) => wp_parse_url( $url, PHP_URL_HOST ),
135 [ self::PRODUCTION_URL, self::STAGING_URL, self::DEV_URL ]
136 ) ) );
137
138 return self::$allowed_hosts;
139 }
140
141 public static function is_allowed_url( $url ): bool {
142 if ( ! is_string( $url ) || ! wp_http_validate_url( $url ) ) {
143 return false;
144 }
145
146 if ( 'https' !== wp_parse_url( $url, PHP_URL_SCHEME ) ) {
147 return false;
148 }
149
150 return in_array( wp_parse_url( $url, PHP_URL_HOST ), self::get_allowed_hosts(), true );
151 }
152
153 public static function do_safe_get_request( $url ) {
154 if ( ! self::is_allowed_url( $url ) ) {
155 return null;
156 }
157
158 $response = wp_safe_remote_get( $url, [
159 'timeout' => 5,
160 'limit_response_size' => 512 * KB_IN_BYTES,
161 ] );
162
163 if ( is_wp_error( $response ) || \WP_Http::OK !== (int) wp_remote_retrieve_response_code( $response ) ) {
164 return null;
165 }
166
167 return json_decode( wp_remote_retrieve_body( $response ), true );
168 }
169 }
170