← All changes
|
includes/template-library/sources/remote.php
+40
-32
4.1.0-dev2
→
3.13.3
View file →
| @@ -1,9 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\TemplateLibrary; |
| 3 | 3 | |
| 4 | 4 | use Elementor\Api; |
| 5 | -use Elementor\Core\Common\Modules\Connect\Module as ConnectModule; | |
| 6 | 5 | use Elementor\Plugin; |
| 7 | 6 | |
| 8 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | 8 | exit; // Exit if accessed directly. |
| @@ -22,8 +21,18 @@ | ||
| 22 | 21 | const API_TEMPLATES_URL = 'https://my.elementor.com/api/connect/v1/library/templates'; |
| 23 | 22 | |
| 24 | 23 | const TEMPLATES_DATA_TRANSIENT_KEY_PREFIX = 'elementor_remote_templates_data_'; |
| 25 | 24 | |
| 25 | + public function __construct() { | |
| 26 | + parent::__construct(); | |
| 27 | + | |
| 28 | + $this->add_actions(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function add_actions() { | |
| 32 | + add_action( 'elementor/experiments/feature-state-change/container', [ $this, 'clear_cache' ], 10, 0 ); | |
| 33 | + } | |
| 34 | + | |
| 26 | 35 | /** |
| 27 | 36 | * Get remote template ID. |
| 28 | 37 | * |
| 29 | 38 | * Retrieve the remote template ID. |
| @@ -223,13 +232,25 @@ | ||
| 223 | 232 | * |
| 224 | 233 | * @param bool $force_update |
| 225 | 234 | * @return array |
| 226 | 235 | */ |
| 227 | - protected function get_templates_data( bool $force_update ): array { | |
| 236 | + private function get_templates_data( bool $force_update ) : array { | |
| 237 | + $templates_data_cache_key = static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION; | |
| 238 | + | |
| 228 | 239 | $experiments_manager = Plugin::$instance->experiments; |
| 229 | 240 | $editor_layout_type = $experiments_manager->is_feature_active( 'container' ) ? 'container_flexbox' : ''; |
| 230 | 241 | |
| 231 | - return $this->get_templates( $editor_layout_type ); | |
| 242 | + if ( $force_update ) { | |
| 243 | + return $this->get_templates( $editor_layout_type ); | |
| 244 | + } | |
| 245 | + | |
| 246 | + $templates_data = get_transient( $templates_data_cache_key ); | |
| 247 | + | |
| 248 | + if ( empty( $templates_data ) ) { | |
| 249 | + return $this->get_templates( $editor_layout_type ); | |
| 250 | + } | |
| 251 | + | |
| 252 | + return $templates_data; | |
| 232 | 253 | } |
| 233 | 254 | |
| 234 | 255 | /** |
| 235 | 256 | * Get the templates from a remote server and set a transient. |
| @@ -236,12 +257,20 @@ | ||
| 236 | 257 | * |
| 237 | 258 | * @param string $editor_layout_type |
| 238 | 259 | * @return array |
| 239 | 260 | */ |
| 240 | - protected function get_templates( string $editor_layout_type ): array { | |
| 261 | + private function get_templates( string $editor_layout_type ): array { | |
| 262 | + $templates_data_cache_key = static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION; | |
| 263 | + | |
| 241 | 264 | $templates_data = $this->get_templates_remotely( $editor_layout_type ); |
| 242 | 265 | |
| 243 | - return empty( $templates_data ) ? [] : $templates_data; | |
| 266 | + if ( empty( $templates_data ) ) { | |
| 267 | + return []; | |
| 268 | + } | |
| 269 | + | |
| 270 | + set_transient( $templates_data_cache_key, $templates_data, 12 * HOUR_IN_SECONDS ); | |
| 271 | + | |
| 272 | + return $templates_data; | |
| 244 | 273 | } |
| 245 | 274 | |
| 246 | 275 | /** |
| 247 | 276 | * Fetch templates from the remote server. |
| @@ -248,11 +277,14 @@ | ||
| 248 | 277 | * |
| 249 | 278 | * @param string $editor_layout_type |
| 250 | 279 | * @return array|false |
| 251 | 280 | */ |
| 252 | - protected function get_templates_remotely( string $editor_layout_type ) { | |
| 281 | + private function get_templates_remotely( string $editor_layout_type ) { | |
| 253 | 282 | $response = wp_remote_get( static::API_TEMPLATES_URL, [ |
| 254 | - 'body' => $this->get_templates_body_args( $editor_layout_type ), | |
| 283 | + 'body' => [ | |
| 284 | + 'plugin_version' => ELEMENTOR_VERSION, | |
| 285 | + 'editor_layout_type' => $editor_layout_type, | |
| 286 | + ], | |
| 255 | 287 | ] ); |
| 256 | 288 | |
| 257 | 289 | if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 258 | 290 | return false; |
| @@ -267,37 +299,14 @@ | ||
| 267 | 299 | return $templates_data; |
| 268 | 300 | } |
| 269 | 301 | |
| 270 | 302 | /** |
| 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 | 303 | * @since 2.2.0 |
| 286 | 304 | * @access private |
| 287 | 305 | */ |
| 288 | - protected function prepare_template( array $template_data ) { | |
| 306 | + private function prepare_template( array $template_data ) { | |
| 289 | 307 | $favorite_templates = $this->get_user_meta( 'favorites' ); |
| 290 | 308 | |
| 291 | - // BC: Support legacy APIs that don't have access tiers. | |
| 292 | - if ( isset( $template_data['access_tier'] ) ) { | |
| 293 | - $access_tier = $template_data['access_tier']; | |
| 294 | - } else { | |
| 295 | - $access_tier = 0 === $template_data['access_level'] | |
| 296 | - ? ConnectModule::ACCESS_TIER_FREE | |
| 297 | - : ConnectModule::ACCESS_TIER_ESSENTIAL; | |
| 298 | - } | |
| 299 | - | |
| 300 | 309 | return [ |
| 301 | 310 | 'template_id' => $template_data['id'], |
| 302 | 311 | 'source' => $this->get_id(), |
| 303 | 312 | 'type' => $template_data['type'], |
| @@ -308,9 +317,8 @@ | ||
| 308 | 317 | 'author' => $template_data['author'], |
| 309 | 318 | 'tags' => json_decode( $template_data['tags'] ), |
| 310 | 319 | 'isPro' => ( '1' === $template_data['is_pro'] ), |
| 311 | 320 | 'accessLevel' => $template_data['access_level'], |
| 312 | - 'accessTier' => $access_tier, | |
| 313 | 321 | 'popularityIndex' => (int) $template_data['popularity_index'], |
| 314 | 322 | 'trendIndex' => (int) $template_data['trend_index'], |
| 315 | 323 | 'hasPageSettings' => ( '1' === $template_data['has_page_settings'] ), |
| 316 | 324 | 'url' => $template_data['url'], |