| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Home; |
| 3 |
|
| 4 |
use Elementor\Modules\Home\Classes\Transformations_Manager; |
| 5 |
|
| 6 |
class API { |
| 7 |
|
| 8 |
const HOME_SCREEN_DATA_URL = 'https://assets.elementor.com/home-screen/v1/home-screen.json'; |
| 9 |
|
| 10 |
public static function get_home_screen_items( $force_request = false ): array { |
| 11 |
$home_screen_data = self::get_transient( '_elementor_home_screen_data' ); |
| 12 |
|
| 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 |
} |
| 17 |
|
| 18 |
return self::transform_home_screen_data( $home_screen_data ); |
| 19 |
} |
| 20 |
|
| 21 |
private static function transform_home_screen_data( $json_data ): array { |
| 22 |
$transformers = new Transformations_Manager( $json_data ); |
| 23 |
|
| 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 ); |
| 64 |
} |
| 65 |
} |
| 66 |
|