Skip to content
Draft
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
86 changes: 86 additions & 0 deletions src/wp-includes/class-wp-option-autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Option API: WP_Option_Autoload class
*
* @package WordPress
* @subpackage Option
* @since 7.3.0
*/

/**
* Holds the values stored in the `autoload` column of the options table.
*
* Functions such as add_option() and update_option() accept a boolean for the
* `$autoload` parameter. These constants name the strings that are stored in
* the database, so core code does not repeat them as literals.
*
* @since 7.3.0
*/
final class WP_Option_Autoload {

/**
* The option is always autoloaded.
*
* @since 7.3.0
* @var string
*/
const ON = 'on';

/**
* The option is never autoloaded.
*
* @since 7.3.0
* @var string
*/
const OFF = 'off';

/**
* No explicit value was given, and the default behavior applies.
*
* Options with this value are autoloaded.
*
* @since 7.3.0
* @var string
*/
const AUTO = 'auto';

/**
* No explicit value was given, and the `wp_default_autoload_value` filter
* decided to autoload the option.
*
* @since 7.3.0
* @var string
*/
const AUTO_ON = 'auto-on';

/**
* No explicit value was given, and the `wp_default_autoload_value` filter
* decided not to autoload the option, for example because its value is large.
*
* @since 7.3.0
* @var string
*/
const AUTO_OFF = 'auto-off';

/**
* Legacy value for an autoloaded option.
*
* Deprecated since 6.7.0, but still accepted and read for backward
* compatibility. Use ON for new code.
*
* @since 7.3.0
* @var string
*/
const LEGACY_YES = 'yes';

/**
* Legacy value for an option that is not autoloaded.
*
* Deprecated since 6.7.0, but still accepted and read for backward
* compatibility. Use OFF for new code.
*
* @since 7.3.0
* @var string
*/
const LEGACY_NO = 'no';
}
1 change: 1 addition & 0 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
exit;
}

require ABSPATH . WPINC . '/class-wp-option-autoload.php';
require ABSPATH . WPINC . '/option.php';

/**
Expand Down
47 changes: 26 additions & 21 deletions src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ function wp_set_option_autoload_values( array $options ) {
}

$grouped_options = array(
'on' => array(),
'off' => array(),
WP_Option_Autoload::ON => array(),
WP_Option_Autoload::OFF => array(),
);
$results = array();
foreach ( $options as $option => $autoload ) {
Expand All @@ -413,10 +413,10 @@ function wp_set_option_autoload_values( array $options ) {
* Sanitize autoload value and categorize accordingly.
* The values 'yes', 'no', 'on', and 'off' are supported for backward compatibility.
*/
if ( 'off' === $autoload || 'no' === $autoload || false === $autoload ) {
$grouped_options['off'][] = $option;
if ( WP_Option_Autoload::OFF === $autoload || WP_Option_Autoload::LEGACY_NO === $autoload || false === $autoload ) {
$grouped_options[ WP_Option_Autoload::OFF ][] = $option;
} else {
$grouped_options['on'][] = $option;
$grouped_options[ WP_Option_Autoload::ON ][] = $option;
}
$results[ $option ] = false; // Initialize result value.
}
Expand Down Expand Up @@ -446,7 +446,7 @@ function wp_set_option_autoload_values( array $options ) {
return $results;
}

// Run UPDATE queries as needed (maximum 2) to update the relevant options' autoload values to 'yes' or 'no'.
// Run UPDATE queries as needed (maximum 2) to update the relevant options' autoload values to 'on' or 'off'.
foreach ( $grouped_options as $autoload => $options ) {
if ( ! $options ) {
continue;
Expand Down Expand Up @@ -486,13 +486,13 @@ function wp_set_option_autoload_values( array $options ) {
* cache. This is not necessary when options were changed to 'on', since in that situation the entire cache is
* deleted anyway.
*/
if ( $grouped_options['on'] ) {
wp_cache_delete_multiple( $grouped_options['on'], 'options' );
if ( $grouped_options[ WP_Option_Autoload::ON ] ) {
wp_cache_delete_multiple( $grouped_options[ WP_Option_Autoload::ON ], 'options' );
wp_cache_delete( 'alloptions', 'options' );
} elseif ( $grouped_options['off'] ) {
} elseif ( $grouped_options[ WP_Option_Autoload::OFF ] ) {
$alloptions = wp_load_alloptions( true );

foreach ( $grouped_options['off'] as $option ) {
foreach ( $grouped_options[ WP_Option_Autoload::OFF ] as $option ) {
if ( isset( $alloptions[ $option ] ) ) {
unset( $alloptions[ $option ] );
}
Expand Down Expand Up @@ -951,7 +951,7 @@ function update_option( $option, $value, $autoload = null ) {
} else {
// Retrieve the current autoload value to reevaluate it in case it was set automatically.
$raw_autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );
$allow_values = array( 'auto-on', 'auto-off', 'auto' );
$allow_values = array( WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO_OFF, WP_Option_Autoload::AUTO );
if ( in_array( $raw_autoload, $allow_values, true ) ) {
$autoload = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload );
if ( $autoload !== $raw_autoload ) {
Expand Down Expand Up @@ -1309,16 +1309,16 @@ function wp_determine_option_autoload_value( $option, $value, $serialized_value,

// Check if autoload is a boolean.
if ( is_bool( $autoload ) ) {
return $autoload ? 'on' : 'off';
return $autoload ? WP_Option_Autoload::ON : WP_Option_Autoload::OFF;
}

switch ( $autoload ) {
case 'on':
case 'yes':
return 'on';
case 'off':
case 'no':
return 'off';
case WP_Option_Autoload::ON:
case WP_Option_Autoload::LEGACY_YES:
return WP_Option_Autoload::ON;
case WP_Option_Autoload::OFF:
case WP_Option_Autoload::LEGACY_NO:
return WP_Option_Autoload::OFF;
}

/**
Expand All @@ -1334,10 +1334,10 @@ function wp_determine_option_autoload_value( $option, $value, $serialized_value,
*/
$autoload = apply_filters( 'wp_default_autoload_value', null, $option, $value, $serialized_value );
if ( is_bool( $autoload ) ) {
return $autoload ? 'auto-on' : 'auto-off';
return $autoload ? WP_Option_Autoload::AUTO_ON : WP_Option_Autoload::AUTO_OFF;
}

return 'auto';
return WP_Option_Autoload::AUTO;
}

/**
Expand Down Expand Up @@ -3348,7 +3348,12 @@ function filter_default_option( $default_value, $option, $passed_default ) {
* @return string[] The values that trigger autoloading.
*/
function wp_autoload_values_to_autoload() {
$autoload_values = array( 'yes', 'on', 'auto-on', 'auto' );
$autoload_values = array(
WP_Option_Autoload::LEGACY_YES,
WP_Option_Autoload::ON,
WP_Option_Autoload::AUTO_ON,
WP_Option_Autoload::AUTO,
);

/**
* Filters the autoload values that should be considered for autoloading from the options table.
Expand Down
36 changes: 18 additions & 18 deletions tests/phpunit/tests/option/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,18 @@ public function test_option_autoloading( $name, $autoload_value, $expected ) {
public static function data_option_autoloading() {
return array(
// Supported values.
array( 'autoload_true', true, 'on' ),
array( 'autoload_false', false, 'off' ),
array( 'autoload_null', null, 'auto' ),
array( 'autoload_true', true, WP_Option_Autoload::ON ),
array( 'autoload_false', false, WP_Option_Autoload::OFF ),
array( 'autoload_null', null, WP_Option_Autoload::AUTO ),

// Values supported for backward compatibility.
array( 'autoload_yes', 'yes', 'on' ),
array( 'autoload_no', 'no', 'off' ),
array( 'autoload_yes', 'yes', WP_Option_Autoload::ON ),
array( 'autoload_no', 'no', WP_Option_Autoload::OFF ),

// Technically unsupported values.
array( 'autoload_string', 'foo', 'auto' ),
array( 'autoload_int', 123456, 'auto' ),
array( 'autoload_array', array(), 'auto' ),
array( 'autoload_string', 'foo', WP_Option_Autoload::AUTO ),
array( 'autoload_int', 123456, WP_Option_Autoload::AUTO ),
array( 'autoload_array', array(), WP_Option_Autoload::AUTO ),
);
}

Expand All @@ -382,32 +382,32 @@ public function test_update_option_autoloading_large_option( $autoload, $expecte
public static function data_option_autoloading_large_option() {
return array(
'on' => array(
'autoload' => 'on',
'expected' => 'on',
'autoload' => WP_Option_Autoload::ON,
'expected' => WP_Option_Autoload::ON,
),
'off' => array(
'autoload' => 'off',
'expected' => 'off',
'autoload' => WP_Option_Autoload::OFF,
'expected' => WP_Option_Autoload::OFF,
),
'yes' => array(
'autoload' => 'yes',
'expected' => 'on',
'expected' => WP_Option_Autoload::ON,
),
'true' => array(
'autoload' => true,
'expected' => 'on',
'expected' => WP_Option_Autoload::ON,
),
'no' => array(
'autoload' => 'no',
'expected' => 'off',
'expected' => WP_Option_Autoload::OFF,
),
'false' => array(
'autoload' => false,
'expected' => 'off',
'expected' => WP_Option_Autoload::OFF,
),
'null' => array(
'autoload' => null,
'expected' => 'auto-off',
'expected' => WP_Option_Autoload::AUTO_OFF,
),
);
}
Expand All @@ -430,7 +430,7 @@ public function test_update_option_autoloading_small_option_auto() {
$this->assertTrue( $updated );

$actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $name ) );
$this->assertSame( 'auto', $actual->autoload );
$this->assertSame( WP_Option_Autoload::AUTO, $actual->autoload );
}

/**
Expand Down
14 changes: 7 additions & 7 deletions tests/phpunit/tests/option/wpAutoloadValuesToAutoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Tests_Option_wpAutoloadValuesToAutoload extends WP_UnitTestCase {
* @ticket 42441
*/
public function test_wp_autoload_values_to_autoload() {
$this->assertSameSets( array( 'yes', 'on', 'auto-on', 'auto' ), wp_autoload_values_to_autoload() );
$this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO ), wp_autoload_values_to_autoload() );
}

/**
Expand All @@ -24,11 +24,11 @@ public function test_wp_autoload_values_to_autoload_filter_remove() {
add_filter(
'wp_autoload_values_to_autoload',
static function () {
return array( 'yes' );
return array( WP_Option_Autoload::LEGACY_YES );
}
);

$this->assertSameSets( array( 'yes' ), wp_autoload_values_to_autoload() );
$this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES ), wp_autoload_values_to_autoload() );
}

/**
Expand All @@ -39,11 +39,11 @@ public function test_wp_autoload_values_to_autoload_filter_extra() {
add_filter(
'wp_autoload_values_to_autoload',
static function () {
return array( 'yes', 'on', 'auto-on', 'auto', 'extra' );
return array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO, 'extra' );
}
);

$this->assertSameSets( array( 'yes', 'on', 'auto-on', 'auto' ), wp_autoload_values_to_autoload() );
$this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, WP_Option_Autoload::AUTO ), wp_autoload_values_to_autoload() );
}

/**
Expand All @@ -54,10 +54,10 @@ public function test_wp_autoload_values_to_autoload_filter_replace() {
add_filter(
'wp_autoload_values_to_autoload',
static function () {
return array( 'yes', 'on', 'auto-on', 'extra' );
return array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON, 'extra' );
}
);

$this->assertSameSets( array( 'yes', 'on', 'auto-on' ), wp_autoload_values_to_autoload() );
$this->assertSameSets( array( WP_Option_Autoload::LEGACY_YES, WP_Option_Autoload::ON, WP_Option_Autoload::AUTO_ON ), wp_autoload_values_to_autoload() );
}
}
Loading
Loading