| @@ -13,14 +13,8 @@ | ||
| 13 | 13 | const ASSETS_DATA_EXPIRATION = 'ASSETS_DATA_EXPIRATION'; |
| 14 | 14 | |
| 15 | 15 | const DEFAULT_EXPIRATION_TIME = '+1 hour'; |
| 16 | 16 | |
| 17 | - const PRODUCTION_URL = 'https://assets.elementor.com'; | |
| 18 | - const STAGING_URL = 'https://assets.stg.elementor.red'; | |
| 19 | - const DEV_URL = 'https://assets.dev.builder.elementor.red'; | |
| 20 | - | |
| 21 | - private static ?array $allowed_hosts = null; | |
| 22 | - | |
| 23 | 17 | public function __construct( array $config ) { |
| 24 | 18 | $this->config = $config; |
| 25 | 19 | } |
| 26 | 20 | |
| @@ -45,11 +39,16 @@ | ||
| 45 | 39 | return $assets_data; |
| 46 | 40 | } |
| 47 | 41 | |
| 48 | 42 | private function fetch_data(): array { |
| 49 | - $url = $this->config( static::ASSETS_DATA_URL ); | |
| 50 | - $data = self::do_safe_get_request( $url ); | |
| 43 | + $response = wp_remote_get( $this->config( static::ASSETS_DATA_URL ) ); | |
| 51 | 44 | |
| 45 | + if ( is_wp_error( $response ) || \WP_Http::OK !== (int) wp_remote_retrieve_response_code( $response ) ) { | |
| 46 | + return []; | |
| 47 | + } | |
| 48 | + | |
| 49 | + $data = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 50 | + | |
| 52 | 51 | if ( ! $this->has_valid_data( $data ) ) { |
| 53 | 52 | return []; |
| 54 | 53 | } |
| 55 | 54 | |
| @@ -115,48 +114,6 @@ | ||
| 115 | 114 | } |
| 116 | 115 | |
| 117 | 116 | private static function is_non_empty_array( $value ): bool { |
| 118 | 117 | return is_array( $value ) && ! empty( $value ); |
| 119 | - } | |
| 120 | - | |
| 121 | - private static function get_allowed_hosts(): array { | |
| 122 | - if ( null !== self::$allowed_hosts ) { | |
| 123 | - return self::$allowed_hosts; | |
| 124 | - } | |
| 125 | - | |
| 126 | - self::$allowed_hosts = array_values( array_filter( array_map( | |
| 127 | - fn( $url ) => wp_parse_url( $url, PHP_URL_HOST ), | |
| 128 | - [ self::PRODUCTION_URL, self::STAGING_URL, self::DEV_URL ] | |
| 129 | - ) ) ); | |
| 130 | - | |
| 131 | - return self::$allowed_hosts; | |
| 132 | - } | |
| 133 | - | |
| 134 | - public static function is_allowed_url( $url ): bool { | |
| 135 | - if ( ! is_string( $url ) || ! wp_http_validate_url( $url ) ) { | |
| 136 | - return false; | |
| 137 | - } | |
| 138 | - | |
| 139 | - if ( 'https' !== wp_parse_url( $url, PHP_URL_SCHEME ) ) { | |
| 140 | - return false; | |
| 141 | - } | |
| 142 | - | |
| 143 | - return in_array( wp_parse_url( $url, PHP_URL_HOST ), self::get_allowed_hosts(), true ); | |
| 144 | - } | |
| 145 | - | |
| 146 | - public static function do_safe_get_request( $url ) { | |
| 147 | - if ( ! self::is_allowed_url( $url ) ) { | |
| 148 | - return null; | |
| 149 | - } | |
| 150 | - | |
| 151 | - $response = wp_safe_remote_get( $url, [ | |
| 152 | - 'timeout' => 5, | |
| 153 | - 'limit_response_size' => 512 * KB_IN_BYTES, | |
| 154 | - ] ); | |
| 155 | - | |
| 156 | - if ( is_wp_error( $response ) || \WP_Http::OK !== (int) wp_remote_retrieve_response_code( $response ) ) { | |
| 157 | - return null; | |
| 158 | - } | |
| 159 | - | |
| 160 | - return json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 161 | 118 | } |
| 162 | 119 | } |