Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 27 additions & 2 deletions src/class-protected-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, true>
*/
Expand All @@ -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;
}

Expand Down
66 changes: 66 additions & 0 deletions tests/phpunit/class-rest-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading