From 7b36f7e0d66d13e95ad651e9c223f4b24417e8d7 Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Wed, 18 Nov 2026 15:04:18 +0100 Subject: [PATCH] Add a filter to protect more options Ported from #79: the aaa_option_optimizer_protected_options filter lets other code add option names that can't be deleted, created with a false value, or have their autoload state changed through the plugin. Unlike the #79 version, the filter can only add names. Removing a built-in name has no effect, and a return value that is not an array of strings is ignored, so a broken callback can't switch off the built-in protection. Co-Authored-By: Claude Fable 5.2 --- README.md | 2 + src/class-protected-options.php | 29 +++++++++++++- tests/phpunit/class-rest-test.php | 66 +++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ba57c24..e4a381c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ Every REST route requires `manage_options`. Option values can include API keys, Essential WordPress options, including site URLs, active plugins, the active theme's settings, salts and keys stored in the database, and the current site's roles option, cannot be deleted, created with a false value, or have their autoload state changed through this plugin. Single and bulk deletion require confirmation in the admin screen. Autoload changes preserve the stored value and use WordPress's cache-aware autoload API. +To protect more options, add their names with the `aaa_option_optimizer_protected_options` filter. The filter can only add names; it cannot remove the built-in protection. + ### Why are unused-option recommendations paused? Tracking stores up to about 10,000 distinct names. The limit is checked without locking, so concurrent requests can push slightly past it. Once it is reached, names already recorded keep counting, but a new name is dropped, and the plugin then marks observations incomplete and pauses unused-option recommendations, because a dropped name could otherwise be reported as unused. Data migrated from older versions is kept in full, even past the limit. A failed tracking write does not pause recommendations; it loses at most that request's counts. Reset tracking to start a fresh observation period. diff --git a/src/class-protected-options.php b/src/class-protected-options.php index 25b100c..9c948be 100644 --- a/src/class-protected-options.php +++ b/src/class-protected-options.php @@ -174,8 +174,9 @@ private static function core_options() { /** * Lowercased protected names, keyed for lookups. * - * Names that depend on the current site (roles prefix, active theme) are added per call so - * the map stays correct after switch_to_blog() or a theme switch within the request. + * Names that depend on the current site (roles prefix, active theme) and names added through + * the filter are added per call, so the map stays correct after switch_to_blog(), a theme + * switch, or a filter added later in the request. * * @return array */ @@ -195,6 +196,30 @@ private static function names() { $names = self::$static_names; $names[ strtolower( $wpdb->prefix . 'user_roles' ) ] = true; $names[ strtolower( 'theme_mods_' . \get_stylesheet() ) ] = true; + + /** + * Filters the list of protected option names. + * + * Use this to protect options that other code depends on, for example from an MU plugin. + * Protected options can't be deleted, created with a false value, or have their autoload + * state changed through this plugin. Names are matched case-insensitively. + * + * The filter can only add names: removing a built-in name from the list has no effect. + * + * @param string[] $option_names Lowercased protected option names. + */ + $filtered = \apply_filters( 'aaa_option_optimizer_protected_options', array_keys( $names ) ); + + // @phpstan-ignore-next-line function.alreadyNarrowedType -- Other code controls the return value, so its type can't be trusted. + if ( is_array( $filtered ) ) { + foreach ( $filtered as $name ) { + // @phpstan-ignore-next-line function.alreadyNarrowedType -- Same as above. + if ( is_string( $name ) && '' !== $name ) { + $names[ strtolower( $name ) ] = true; + } + } + } + return $names; } diff --git a/tests/phpunit/class-rest-test.php b/tests/phpunit/class-rest-test.php index fe29bed..797fe9f 100644 --- a/tests/phpunit/class-rest-test.php +++ b/tests/phpunit/class-rest-test.php @@ -374,6 +374,72 @@ public function test_protected_options_keep_their_autoload_flag() { $this->assertAutoloaded( $other ); } + /** + * Other code can protect its own options through the filter. + * + * @return void + */ + public function test_filter_adds_protected_options() { + $this->log_in_as( 'administrator' ); + + $name = self::PREFIX . 'Filtered'; + \add_option( $name, 'value', '', true ); + + \add_filter( + 'aaa_option_optimizer_protected_options', + static function ( $names ) use ( $name ) { + $names[] = $name; + return $names; + } + ); + + $delete = $this->request( 'delete-option', [ 'option_name' => $name ] ); + $bulk = $this->request( 'delete-options', [ 'option_names' => [ $name ] ] ); + $flip = $this->request( + 'update-autoload', + [ + 'option_name' => $name, + 'autoload' => 'no', + ] + ); + + $this->assertSame( 403, $delete->get_status() ); + $this->assertSame( 403, $bulk->get_status() ); + $this->assertSame( 403, $flip->get_status() ); + $this->assertAutoloaded( $name ); + $this->assertSame( 'value', \get_option( $name ) ); + + // The list routes flag the option, so the admin screen hides its actions. + $rows = \wp_list_pluck( $this->request( 'all-options', [], 'GET' )->get_data()['data'], 'protected', 'name' ); + $this->assertTrue( $rows[ $name ] ); + } + + /** + * The filter can't remove built-in protection, and a broken return value is ignored. + * + * @return void + */ + public function test_filter_cannot_remove_built_in_protection() { + $this->log_in_as( 'administrator' ); + + \add_filter( 'aaa_option_optimizer_protected_options', '__return_empty_array' ); + $this->assertSame( 403, $this->request( 'delete-option', [ 'option_name' => 'siteurl' ] )->get_status() ); + \remove_filter( 'aaa_option_optimizer_protected_options', '__return_empty_array' ); + + \add_filter( 'aaa_option_optimizer_protected_options', '__return_false' ); + $this->assertSame( 403, $this->request( 'delete-option', [ 'option_name' => 'siteurl' ] )->get_status() ); + \remove_filter( 'aaa_option_optimizer_protected_options', '__return_false' ); + + \add_filter( + 'aaa_option_optimizer_protected_options', + static function () { + return [ 123, null, '', [ 'nested' ] ]; + } + ); + $this->assertSame( 403, $this->request( 'delete-option', [ 'option_name' => 'siteurl' ] )->get_status() ); + $this->assertNotNull( $this->get_autoload_flag( 'siteurl' ) ); + } + /** * Option names WordPress would trim, or that are not a list of strings, are rejected. *