Skip to content

Commit f4f89fa

Browse files
ilicfilipclaude
andcommitted
Fetch known-plugins.json from wp.org with daily refresh
Adds a Known_Plugins loader that caches a remote copy of the mapping and falls back to the bundled JSON. A daily WP-Cron event refreshes the cache from https://ps.w.org/aaa-option-optimizer/assets/known-plugins.json. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d82fac1 commit f4f89fa

4 files changed

Lines changed: 123 additions & 3 deletions

File tree

aaa-option-optimizer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ function aaa_option_optimizer_activation() {
3737
// Create the custom table.
3838
Progress_Planner\OptionOptimizer\Database::create_table();
3939

40+
// Schedule daily refresh of the known-plugins mapping.
41+
if ( ! wp_next_scheduled( Progress_Planner\OptionOptimizer\Known_Plugins::CRON_HOOK ) ) {
42+
wp_schedule_event( time(), 'daily', Progress_Planner\OptionOptimizer\Known_Plugins::CRON_HOOK );
43+
}
44+
4045
$autoload_values = \wp_autoload_values_to_autoload();
4146
$placeholders = implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) );
4247

@@ -73,6 +78,8 @@ function aaa_option_optimizer_activation() {
7378
function aaa_option_optimizer_deactivation() {
7479
$aaa_option_value = get_option( 'option_optimizer' );
7580
update_option( 'option_optimizer', $aaa_option_value, false );
81+
82+
wp_clear_scheduled_hook( Progress_Planner\OptionOptimizer\Known_Plugins::CRON_HOOK );
7683
}
7784

7885
/**

src/class-known-plugins.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

src/class-map-plugin-to-options.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Map_Plugin_To_Options {
1616
/**
1717
* List of plugins we can recognize.
1818
*
19-
* @var object[]
19+
* @var array<string, array<string, mixed>>
2020
*/
2121
private $plugins_list = [];
2222

@@ -28,9 +28,9 @@ class Map_Plugin_To_Options {
2828
* @return string
2929
*/
3030
public function get_plugin_name( string $option ): string {
31-
$plugins_list = [];
3231
if ( empty( $this->plugins_list ) ) {
33-
$this->plugins_list = json_decode( file_get_contents( plugin_dir_path( AAA_OPTION_OPTIMIZER_FILE ) . 'known-plugins/known-plugins.json' ), true );
32+
$known = new Known_Plugins();
33+
$this->plugins_list = $known->get();
3434
}
3535

3636
// for each plugin in the list, check if the option starts with the prefix.

src/class-plugin.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public function register_hooks() {
7979
// Use the shutdown action to update the option with tracked data.
8080
\add_action( 'shutdown', [ $this, 'update_tracked_options' ] );
8181

82+
// Daily refresh of the known-plugins mapping.
83+
\add_action( Known_Plugins::CRON_HOOK, [ $this, 'refresh_known_plugins' ] );
84+
if ( ! \wp_next_scheduled( Known_Plugins::CRON_HOOK ) ) {
85+
\wp_schedule_event( \time(), 'daily', Known_Plugins::CRON_HOOK );
86+
}
87+
8288
// Register the REST routes.
8389
$rest = new REST();
8490
$rest->register_hooks();
@@ -160,6 +166,15 @@ protected function add_option_usage( $option_name ) {
160166
++$this->accessed_options[ $option_name ];
161167
}
162168

169+
/**
170+
* Refresh the cached known-plugins mapping from wp.org.
171+
*
172+
* @return void
173+
*/
174+
public function refresh_known_plugins() {
175+
( new Known_Plugins() )->refresh();
176+
}
177+
163178
/**
164179
* Update the tracked options at the end of the page load.
165180
*

0 commit comments

Comments
 (0)