| @@ -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,35 @@ | ||
| 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 ); | |
| 41 | - $notifications = apply_filters( 'elementor/core/admin/notifications', $notifications ); | |
| 33 | + $notifications = self::get_transient( '_elementor_notifications_data' ); | |
| 42 | 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 | + | |
| 43 | 41 | return $notifications; |
| 44 | 42 | } |
| 45 | 43 | |
| 44 | + private static function fetch_data() : array { | |
| 45 | + $response = wp_remote_get( self::NOTIFICATIONS_URL ); | |
| 46 | + | |
| 47 | + if ( is_wp_error( $response ) ) { | |
| 48 | + return []; | |
| 49 | + } | |
| 50 | + | |
| 51 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 52 | + | |
| 53 | + if ( empty( $data['notifications'] ) || ! is_array( $data['notifications'] ) ) { | |
| 54 | + return []; | |
| 55 | + } | |
| 56 | + | |
| 57 | + return $data['notifications']; | |
| 58 | + } | |
| 59 | + | |
| 46 | 60 | private static function add_to_array( $filtered_notifications, $notification ) { |
| 47 | 61 | foreach ( $filtered_notifications as $filtered_notification ) { |
| 48 | 62 | if ( $filtered_notification['id'] === $notification['id'] ) { |
| 49 | 63 | return $filtered_notifications; |
| @@ -73,9 +87,9 @@ | ||
| 73 | 87 | foreach ( $group as $condition ) { |
| 74 | 88 | // Reset results for each condition. |
| 75 | 89 | $result = false; |
| 76 | 90 | switch ( $condition['type'] ) { |
| 77 | - case 'wordpress': // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText | |
| 91 | + case 'wordpress': // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled | |
| 78 | 92 | // include an unmodified $wp_version |
| 79 | 93 | include ABSPATH . WPINC . '/version.php'; |
| 80 | 94 | $result = version_compare( $wp_version, $condition['version'], $condition['operator'] ); |
| 81 | 95 | break; |
| @@ -87,9 +101,9 @@ | ||
| 87 | 101 | $result = 'in' === $condition['operator'] ? $in_array : ! $in_array; |
| 88 | 102 | break; |
| 89 | 103 | case 'plugin': |
| 90 | 104 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 91 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 105 | + require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); | |
| 92 | 106 | } |
| 93 | 107 | |
| 94 | 108 | $is_plugin_active = is_plugin_active( $condition['plugin'] ); |
| 95 | 109 | |
| @@ -137,6 +151,29 @@ | ||
| 137 | 151 | } |
| 138 | 152 | } |
| 139 | 153 | |
| 140 | 154 | return $result; |
| 155 | + } | |
| 156 | + | |
| 157 | + private static function get_transient( $cache_key ) { | |
| 158 | + $cache = get_option( $cache_key ); | |
| 159 | + | |
| 160 | + if ( empty( $cache['timeout'] ) ) { | |
| 161 | + return false; | |
| 162 | + } | |
| 163 | + | |
| 164 | + if ( current_time( 'timestamp' ) > $cache['timeout'] ) { | |
| 165 | + return false; | |
| 166 | + } | |
| 167 | + | |
| 168 | + return json_decode( $cache['value'], true ); | |
| 169 | + } | |
| 170 | + | |
| 171 | + private static function set_transient( $cache_key, $value, $expiration = '+12 hours' ) { | |
| 172 | + $data = [ | |
| 173 | + 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ), | |
| 174 | + 'value' => json_encode( $value ), | |
| 175 | + ]; | |
| 176 | + | |
| 177 | + return update_option( $cache_key, $data, false ); | |
| 141 | 178 | } |
| 142 | 179 | } |