| @@ -1,27 +1,65 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Modules\Home; |
| 3 | 3 | |
| 4 | -use Elementor\Includes\EditorAssetsAPI; | |
| 5 | 4 | use Elementor\Modules\Home\Classes\Transformations_Manager; |
| 6 | 5 | |
| 7 | 6 | class API { |
| 8 | - protected EditorAssetsAPI $editor_assets_api; | |
| 9 | 7 | |
| 10 | - public function __construct( EditorAssetsAPI $editor_assets_api ) { | |
| 11 | - $this->editor_assets_api = $editor_assets_api; | |
| 12 | - } | |
| 8 | + const HOME_SCREEN_DATA_URL = 'https://assets.elementor.com/home-screen/v1/home-screen.json'; | |
| 13 | 9 | |
| 14 | - public function get_home_screen_items( $force_request = false ): array { | |
| 15 | - $assets_data = $this->editor_assets_api->get_assets_data( $force_request ); | |
| 10 | + public static function get_home_screen_items( $force_request = false ): array { | |
| 11 | + $home_screen_data = self::get_transient( '_elementor_home_screen_data' ); | |
| 16 | 12 | |
| 17 | - $assets_data = apply_filters( 'elementor/core/admin/homescreen', $assets_data ); | |
| 13 | + if ( $force_request || false === $home_screen_data ) { | |
| 14 | + $home_screen_data = static::fetch_data(); | |
| 15 | + static::set_transient( '_elementor_home_screen_data', $home_screen_data, '+1 hour' ); | |
| 16 | + } | |
| 18 | 17 | |
| 19 | - return $this->transform_home_screen_data( $assets_data ); | |
| 18 | + return self::transform_home_screen_data( $home_screen_data ); | |
| 20 | 19 | } |
| 21 | 20 | |
| 22 | - private function transform_home_screen_data( $json_data ): array { | |
| 21 | + private static function transform_home_screen_data( $json_data ): array { | |
| 23 | 22 | $transformers = new Transformations_Manager( $json_data ); |
| 24 | 23 | |
| 25 | 24 | return $transformers->run_transformations(); |
| 25 | + } | |
| 26 | + | |
| 27 | + private static function fetch_data(): array { | |
| 28 | + $response = wp_remote_get( self::HOME_SCREEN_DATA_URL ); | |
| 29 | + | |
| 30 | + if ( is_wp_error( $response ) ) { | |
| 31 | + return []; | |
| 32 | + } | |
| 33 | + | |
| 34 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 35 | + | |
| 36 | + if ( empty( $data['home-screen'] ) || ! is_array( $data['home-screen'] ) ) { | |
| 37 | + return []; | |
| 38 | + } | |
| 39 | + | |
| 40 | + return $data['home-screen']; | |
| 41 | + } | |
| 42 | + | |
| 43 | + private static function get_transient( $cache_key ) { | |
| 44 | + $cache = get_option( $cache_key ); | |
| 45 | + | |
| 46 | + if ( empty( $cache['timeout'] ) ) { | |
| 47 | + return false; | |
| 48 | + } | |
| 49 | + | |
| 50 | + if ( current_time( 'timestamp' ) > $cache['timeout'] ) { | |
| 51 | + return false; | |
| 52 | + } | |
| 53 | + | |
| 54 | + return json_decode( $cache['value'], true ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + private static function set_transient( $cache_key, $value, $expiration = '+12 hours' ): bool { | |
| 58 | + $data = [ | |
| 59 | + 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ), | |
| 60 | + 'value' => json_encode( $value ), | |
| 61 | + ]; | |
| 62 | + | |
| 63 | + return update_option( $cache_key, $data, false ); | |
| 26 | 64 | } |
| 27 | 65 | } |