| @@ -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. |
| @@ -224,12 +234,24 @@ | ||
| 224 | 234 | * @param bool $force_update |
| 225 | 235 | * @return array |
| 226 | 236 | */ |
| 227 | 237 | protected 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. |
| @@ -237,11 +259,19 @@ | ||
| 237 | 259 | * @param string $editor_layout_type |
| 238 | 260 | * @return array |
| 239 | 261 | */ |
| 240 | 262 | protected 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. |