|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Loader for the known-plugins mapping. |
| 4 | + * |
| 5 | + * @package Progress_Planner\OptionOptimizer |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Progress_Planner\OptionOptimizer; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class Known_Plugins |
| 12 | + * |
| 13 | + * Fetches the latest known-plugins.json from wp.org and caches it locally. |
| 14 | + * Falls back to the JSON file bundled with the plugin when no fresh remote |
| 15 | + * copy is available. |
| 16 | + */ |
| 17 | +class Known_Plugins { |
| 18 | + |
| 19 | + const REMOTE_URL = 'https://ps.w.org/aaa-option-optimizer/assets/known-plugins.json'; |
| 20 | + const CACHE_KEY = 'aaa_option_optimizer_known_plugins'; |
| 21 | + const CRON_HOOK = 'aaa_option_optimizer_refresh_known_plugins'; |
| 22 | + |
| 23 | + /** |
| 24 | + * In-memory cache for the current request. |
| 25 | + * |
| 26 | + * @var array<string, array<string, mixed>>|null |
| 27 | + */ |
| 28 | + private $list; |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the known-plugins mapping. |
| 32 | + * |
| 33 | + * @return array<string, array<string, mixed>> |
| 34 | + */ |
| 35 | + public function get(): array { |
| 36 | + if ( null !== $this->list ) { |
| 37 | + return $this->list; |
| 38 | + } |
| 39 | + |
| 40 | + $cached = \get_option( self::CACHE_KEY ); |
| 41 | + if ( \is_array( $cached ) && ! empty( $cached ) ) { |
| 42 | + $this->list = $cached; |
| 43 | + return $this->list; |
| 44 | + } |
| 45 | + |
| 46 | + $this->list = $this->load_bundled(); |
| 47 | + return $this->list; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Refresh the cached mapping from the remote URL. |
| 52 | + * |
| 53 | + * @return bool True when a fresh copy was stored, false otherwise. |
| 54 | + */ |
| 55 | + public function refresh(): bool { |
| 56 | + $response = \wp_remote_get( |
| 57 | + self::REMOTE_URL, |
| 58 | + [ |
| 59 | + 'timeout' => 10, |
| 60 | + ] |
| 61 | + ); |
| 62 | + |
| 63 | + if ( \is_wp_error( $response ) ) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + if ( 200 !== \wp_remote_retrieve_response_code( $response ) ) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + $body = \wp_remote_retrieve_body( $response ); |
| 72 | + $data = \json_decode( $body, true ); |
| 73 | + |
| 74 | + if ( ! \is_array( $data ) || empty( $data ) ) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + \update_option( self::CACHE_KEY, $data, false ); |
| 79 | + $this->list = $data; |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Load the JSON file bundled with the plugin. |
| 85 | + * |
| 86 | + * @return array<string, array<string, mixed>> |
| 87 | + */ |
| 88 | + private function load_bundled(): array { |
| 89 | + $path = \plugin_dir_path( AAA_OPTION_OPTIMIZER_FILE ) . 'known-plugins/known-plugins.json'; |
| 90 | + if ( ! \file_exists( $path ) ) { |
| 91 | + return []; |
| 92 | + } |
| 93 | + |
| 94 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading a file bundled with the plugin. |
| 95 | + $data = \json_decode( (string) \file_get_contents( $path ), true ); |
| 96 | + return \is_array( $data ) ? $data : []; |
| 97 | + } |
| 98 | +} |
0 commit comments