| @@ -1,8 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Elementor\Modules\Notifications; |
| 3 | 3 | |
| 4 | -use Elementor\Includes\EditorAssetsAPI; | |
| 5 | 4 | use Elementor\User; |
| 6 | 5 | |
| 7 | 6 | class API { |
| 8 | 7 | |
| @@ -30,20 +29,37 @@ | ||
| 30 | 29 | return $filtered_notifications; |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | 32 | private static function get_notifications( $force_request = false ) { |
| 34 | - $editor_assets_api = new EditorAssetsAPI( [ | |
| 35 | - EditorAssetsAPI::ASSETS_DATA_URL => self::NOTIFICATIONS_URL, | |
| 36 | - EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_notifications_data', | |
| 37 | - EditorAssetsAPI::ASSETS_DATA_KEY => 'notifications', | |
| 38 | - EditorAssetsAPI::ASSETS_DATA_EXPIRATION => '+12 hours', | |
| 39 | - ] ); | |
| 40 | - $notifications = $editor_assets_api->get_assets_data( $force_request ); | |
| 33 | + $notifications = self::get_transient( '_elementor_notifications_data' ); | |
| 34 | + | |
| 35 | + if ( $force_request || false === $notifications ) { | |
| 36 | + $notifications = static::fetch_data(); | |
| 37 | + | |
| 38 | + static::set_transient( '_elementor_notifications_data', $notifications, '+1 hour' ); | |
| 39 | + } | |
| 40 | + | |
| 41 | 41 | $notifications = apply_filters( 'elementor/core/admin/notifications', $notifications ); |
| 42 | 42 | |
| 43 | 43 | return $notifications; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | + private static function fetch_data(): array { | |
| 47 | + $response = wp_remote_get( self::NOTIFICATIONS_URL ); | |
| 48 | + | |
| 49 | + if ( is_wp_error( $response ) ) { | |
| 50 | + return []; | |
| 51 | + } | |
| 52 | + | |
| 53 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 54 | + | |
| 55 | + if ( empty( $data['notifications'] ) || ! is_array( $data['notifications'] ) ) { | |
| 56 | + return []; | |
| 57 | + } | |
| 58 | + | |
| 59 | + return $data['notifications']; | |
| 60 | + } | |
| 61 | + | |
| 46 | 62 | private static function add_to_array( $filtered_notifications, $notification ) { |
| 47 | 63 | foreach ( $filtered_notifications as $filtered_notification ) { |
| 48 | 64 | if ( $filtered_notification['id'] === $notification['id'] ) { |
| 49 | 65 | return $filtered_notifications; |
| @@ -137,6 +153,29 @@ | ||
| 137 | 153 | } |
| 138 | 154 | } |
| 139 | 155 | |
| 140 | 156 | return $result; |
| 157 | + } | |
| 158 | + | |
| 159 | + private static function get_transient( $cache_key ) { | |
| 160 | + $cache = get_option( $cache_key ); | |
| 161 | + | |
| 162 | + if ( empty( $cache['timeout'] ) ) { | |
| 163 | + return false; | |
| 164 | + } | |
| 165 | + | |
| 166 | + if ( current_time( 'timestamp' ) > $cache['timeout'] ) { | |
| 167 | + return false; | |
| 168 | + } | |
| 169 | + | |
| 170 | + return json_decode( $cache['value'], true ); | |
| 171 | + } | |
| 172 | + | |
| 173 | + private static function set_transient( $cache_key, $value, $expiration = '+12 hours' ) { | |
| 174 | + $data = [ | |
| 175 | + 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ), | |
| 176 | + 'value' => json_encode( $value ), | |
| 177 | + ]; | |
| 178 | + | |
| 179 | + return update_option( $cache_key, $data, false ); | |
| 141 | 180 | } |
| 142 | 181 | } |