← All changes
|
includes/template-library/sources/remote.php
+40
-21
4.1.0-dev2
→
3.23.0-dev3
View file →
| @@ -22,8 +22,18 @@ | ||
| 22 | 22 | const API_TEMPLATES_URL = 'https://my.elementor.com/api/connect/v1/library/templates'; |
| 23 | 23 | |
| 24 | 24 | const TEMPLATES_DATA_TRANSIENT_KEY_PREFIX = 'elementor_remote_templates_data_'; |
| 25 | 25 | |
| 26 | + public function __construct() { | |
| 27 | + parent::__construct(); | |
| 28 | + | |
| 29 | + $this->add_actions(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + public function add_actions() { | |
| 33 | + add_action( 'elementor/experiments/feature-state-change/container', [ $this, 'clear_cache' ], 10, 0 ); | |
| 34 | + } | |
| 35 | + | |
| 26 | 36 | /** |
| 27 | 37 | * Get remote template ID. |
| 28 | 38 | * |
| 29 | 39 | * Retrieve the remote template ID. |
| @@ -223,13 +233,25 @@ | ||
| 223 | 233 | * |
| 224 | 234 | * @param bool $force_update |
| 225 | 235 | * @return array |
| 226 | 236 | */ |
| 227 | - protected function get_templates_data( bool $force_update ): array { | |
| 237 | + private function get_templates_data( bool $force_update ) : array { | |
| 238 | + $templates_data_cache_key = static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION; | |
| 239 | + | |
| 228 | 240 | $experiments_manager = Plugin::$instance->experiments; |
| 229 | 241 | $editor_layout_type = $experiments_manager->is_feature_active( 'container' ) ? 'container_flexbox' : ''; |
| 230 | 242 | |
| 231 | - return $this->get_templates( $editor_layout_type ); | |
| 243 | + if ( $force_update ) { | |
| 244 | + return $this->get_templates( $editor_layout_type ); | |
| 245 | + } | |
| 246 | + | |
| 247 | + $templates_data = get_transient( $templates_data_cache_key ); | |
| 248 | + | |
| 249 | + if ( empty( $templates_data ) ) { | |
| 250 | + return $this->get_templates( $editor_layout_type ); | |
| 251 | + } | |
| 252 | + | |
| 253 | + return $templates_data; | |
| 232 | 254 | } |
| 233 | 255 | |
| 234 | 256 | /** |
| 235 | 257 | * Get the templates from a remote server and set a transient. |
| @@ -236,12 +258,20 @@ | ||
| 236 | 258 | * |
| 237 | 259 | * @param string $editor_layout_type |
| 238 | 260 | * @return array |
| 239 | 261 | */ |
| 240 | - protected function get_templates( string $editor_layout_type ): array { | |
| 262 | + private function get_templates( string $editor_layout_type ): array { | |
| 263 | + $templates_data_cache_key = static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION; | |
| 264 | + | |
| 241 | 265 | $templates_data = $this->get_templates_remotely( $editor_layout_type ); |
| 242 | 266 | |
| 243 | - return empty( $templates_data ) ? [] : $templates_data; | |
| 267 | + if ( empty( $templates_data ) ) { | |
| 268 | + return []; | |
| 269 | + } | |
| 270 | + | |
| 271 | + set_transient( $templates_data_cache_key, $templates_data, 12 * HOUR_IN_SECONDS ); | |
| 272 | + | |
| 273 | + return $templates_data; | |
| 244 | 274 | } |
| 245 | 275 | |
| 246 | 276 | /** |
| 247 | 277 | * Fetch templates from the remote server. |
| @@ -248,11 +278,14 @@ | ||
| 248 | 278 | * |
| 249 | 279 | * @param string $editor_layout_type |
| 250 | 280 | * @return array|false |
| 251 | 281 | */ |
| 252 | - protected function get_templates_remotely( string $editor_layout_type ) { | |
| 282 | + private function get_templates_remotely( string $editor_layout_type ) { | |
| 253 | 283 | $response = wp_remote_get( static::API_TEMPLATES_URL, [ |
| 254 | - 'body' => $this->get_templates_body_args( $editor_layout_type ), | |
| 284 | + 'body' => [ | |
| 285 | + 'plugin_version' => ELEMENTOR_VERSION, | |
| 286 | + 'editor_layout_type' => $editor_layout_type, | |
| 287 | + ], | |
| 255 | 288 | ] ); |
| 256 | 289 | |
| 257 | 290 | if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 258 | 291 | return false; |
| @@ -267,26 +300,12 @@ | ||
| 267 | 300 | return $templates_data; |
| 268 | 301 | } |
| 269 | 302 | |
| 270 | 303 | /** |
| 271 | - * Prepare the body arguments for the remote request. | |
| 272 | - * | |
| 273 | - * @param string $editor_layout_type | |
| 274 | - * | |
| 275 | - * @return array | |
| 276 | - */ | |
| 277 | - protected function get_templates_body_args( string $editor_layout_type ): array { | |
| 278 | - return [ | |
| 279 | - 'plugin_version' => ELEMENTOR_VERSION, | |
| 280 | - 'editor_layout_type' => $editor_layout_type, | |
| 281 | - ]; | |
| 282 | - } | |
| 283 | - | |
| 284 | - /** | |
| 285 | 304 | * @since 2.2.0 |
| 286 | 305 | * @access private |
| 287 | 306 | */ |
| 288 | - protected function prepare_template( array $template_data ) { | |
| 307 | + private function prepare_template( array $template_data ) { | |
| 289 | 308 | $favorite_templates = $this->get_user_meta( 'favorites' ); |
| 290 | 309 | |
| 291 | 310 | // BC: Support legacy APIs that don't have access tiers. |
| 292 | 311 | if ( isset( $template_data['access_tier'] ) ) { |