| @@ -9,12 +9,8 @@ | ||
| 9 | 9 | const ASSETS_DATA_URL = 'ASSETS_DATA_URL'; |
| 10 | 10 | |
| 11 | 11 | const ASSETS_DATA_KEY = 'ASSETS_DATA_KEY'; |
| 12 | 12 | |
| 13 | - const ASSETS_DATA_EXPIRATION = 'ASSETS_DATA_EXPIRATION'; | |
| 14 | - | |
| 15 | - const DEFAULT_EXPIRATION_TIME = '+1 hour'; | |
| 16 | - | |
| 17 | 13 | public function __construct( array $config ) { |
| 18 | 14 | $this->config = $config; |
| 19 | 15 | } |
| 20 | 16 | |
| @@ -25,16 +21,10 @@ | ||
| 25 | 21 | public function get_assets_data( $force_request = false ): array { |
| 26 | 22 | $assets_data = $this->get_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ) ); |
| 27 | 23 | |
| 28 | 24 | if ( $force_request || false === $assets_data ) { |
| 29 | - $fresh_data = $this->fetch_data(); | |
| 30 | - | |
| 31 | - if ( empty( $fresh_data ) ) { | |
| 32 | - return ! empty( $assets_data ) ? $assets_data : []; | |
| 33 | - } | |
| 34 | - | |
| 35 | - $assets_data = $fresh_data; | |
| 36 | - $this->set_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ), $assets_data, $this->get_expiration_time() ); | |
| 25 | + $assets_data = $this->fetch_data(); | |
| 26 | + $this->set_transient( $this->config( static::ASSETS_DATA_TRANSIENT_KEY ), $assets_data, '+1 hour' ); | |
| 37 | 27 | } |
| 38 | 28 | |
| 39 | 29 | return $assets_data; |
| 40 | 30 | } |
| @@ -41,15 +31,15 @@ | ||
| 41 | 31 | |
| 42 | 32 | private function fetch_data(): array { |
| 43 | 33 | $response = wp_remote_get( $this->config( static::ASSETS_DATA_URL ) ); |
| 44 | 34 | |
| 45 | - if ( is_wp_error( $response ) || \WP_Http::OK !== (int) wp_remote_retrieve_response_code( $response ) ) { | |
| 35 | + if ( is_wp_error( $response ) ) { | |
| 46 | 36 | return []; |
| 47 | 37 | } |
| 48 | 38 | |
| 49 | 39 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 50 | 40 | |
| 51 | - if ( ! $this->has_valid_data( $data ) ) { | |
| 41 | + if ( empty( $data[ $this->config( static::ASSETS_DATA_KEY ) ] ) || ! is_array( $data[ $this->config( static::ASSETS_DATA_KEY ) ] ) ) { | |
| 52 | 42 | return []; |
| 53 | 43 | } |
| 54 | 44 | |
| 55 | 45 | return $data[ $this->config( static::ASSETS_DATA_KEY ) ]; |
| @@ -68,9 +58,9 @@ | ||
| 68 | 58 | |
| 69 | 59 | return json_decode( $cache['value'], true ); |
| 70 | 60 | } |
| 71 | 61 | |
| 72 | - private function set_transient( $cache_key, $value, $expiration ): bool { | |
| 62 | + private function set_transient( $cache_key, $value, $expiration = '+12 hours' ): bool { | |
| 73 | 63 | $data = [ |
| 74 | 64 | 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ), |
| 75 | 65 | 'value' => wp_json_encode( $value ), |
| 76 | 66 | ]; |
| @@ -75,45 +65,6 @@ | ||
| 75 | 65 | 'value' => wp_json_encode( $value ), |
| 76 | 66 | ]; |
| 77 | 67 | |
| 78 | 68 | return update_option( $cache_key, $data, false ); |
| 79 | - } | |
| 80 | - | |
| 81 | - private function get_expiration_time(): string { | |
| 82 | - $expiration = $this->config( static::ASSETS_DATA_EXPIRATION ); | |
| 83 | - return $expiration ? $expiration : static::DEFAULT_EXPIRATION_TIME; | |
| 84 | - } | |
| 85 | - | |
| 86 | - private function has_valid_data( $data ): bool { | |
| 87 | - if ( ! is_array( $data ) ) { | |
| 88 | - return false; | |
| 89 | - } | |
| 90 | - | |
| 91 | - $key = $this->config( static::ASSETS_DATA_KEY ); | |
| 92 | - return static::is_non_empty_array( $data[ $key ] ?? null ); | |
| 93 | - } | |
| 94 | - | |
| 95 | - public static function has_valid_nested_array( $data, array $nested_array_path ): bool { | |
| 96 | - $current = $data; | |
| 97 | - | |
| 98 | - foreach ( $nested_array_path as $nested_key ) { | |
| 99 | - if ( ! is_array( $current ) || ! array_key_exists( $nested_key, $current ) ) { | |
| 100 | - return false; | |
| 101 | - } | |
| 102 | - $current = $current[ $nested_key ]; | |
| 103 | - } | |
| 104 | - | |
| 105 | - if ( ! static::is_non_empty_array( $current ) ) { | |
| 106 | - return false; | |
| 107 | - } | |
| 108 | - | |
| 109 | - return true; | |
| 110 | - } | |
| 111 | - | |
| 112 | - public static function is_valid_data( $data ): bool { | |
| 113 | - return static::is_non_empty_array( $data ); | |
| 114 | - } | |
| 115 | - | |
| 116 | - private static function is_non_empty_array( $value ): bool { | |
| 117 | - return is_array( $value ) && ! empty( $value ); | |
| 118 | 69 | } |
| 119 | 70 | } |