From 707fbe1f04635ef9e8cdcd2c3ec4dabb16cfa5aa Mon Sep 17 00:00:00 2001 From: paul bearne Date: Mon, 8 Jul 2024 17:16:04 -0400 Subject: [PATCH] Add autoload parameter to `register_setting` function This commit introduces an `autoload` parameter in the `register_setting` function, allowing users to specify if a setting should be autoloaded. Autoloaded settings are loaded on every page, improving the performance for settings that are frequently used. The change includes both the addition of the `autoload` parameter in the registration function as well as the corresponding changes in the database layout to store this information. This also includes a modification to `wp_default_autoload_value` filter to reflect the new autoload state. Changes are carried out across multiple files like `theme-options.php`, `option.php`, `featured-content.php` etc. for proper implementation. --- .../themes/twentyeleven/inc/theme-options.php | 5 ++- .../twentyfourteen/inc/featured-content.php | 9 +++- src/wp-includes/blocks/site-logo.php | 2 + src/wp-includes/option.php | 31 ++++++++++++- tests/phpunit/tests/option/registration.php | 43 +++++++++++++++++++ 5 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/wp-content/themes/twentyeleven/inc/theme-options.php b/src/wp-content/themes/twentyeleven/inc/theme-options.php index dbadd29763689..c8af5cbfae65e 100644 --- a/src/wp-content/themes/twentyeleven/inc/theme-options.php +++ b/src/wp-content/themes/twentyeleven/inc/theme-options.php @@ -39,7 +39,10 @@ function twentyeleven_theme_options_init() { register_setting( 'twentyeleven_options', // Options group, see settings_fields() call in twentyeleven_theme_options_render_page(). 'twentyeleven_theme_options', // Database option, see twentyeleven_get_theme_options(). - 'twentyeleven_theme_options_validate' // The sanitization callback, see twentyeleven_theme_options_validate(). + array( + 'callable' => 'twentyeleven_theme_options_validate', // The sanitization callback, see twentyeleven_theme_options_validate(). + 'autoload' => true, + ) ); // Register our settings field group. diff --git a/src/wp-content/themes/twentyfourteen/inc/featured-content.php b/src/wp-content/themes/twentyfourteen/inc/featured-content.php index 8b36b12c31519..385286e73374b 100644 --- a/src/wp-content/themes/twentyfourteen/inc/featured-content.php +++ b/src/wp-content/themes/twentyfourteen/inc/featured-content.php @@ -364,7 +364,14 @@ public static function hide_the_featured_term( $terms, $id, $taxonomy ) { * @since Twenty Fourteen 1.0 */ public static function register_setting() { - register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) ); + register_setting( + 'featured-content', + 'featured-content', + array( + 'callable' => array( __CLASS__, 'validate_settings' ), + 'autoload' => true, + ) + ); } /** diff --git a/src/wp-includes/blocks/site-logo.php b/src/wp-includes/blocks/site-logo.php index ea6d9f1f8d97c..65fd55311e795 100644 --- a/src/wp-includes/blocks/site-logo.php +++ b/src/wp-includes/blocks/site-logo.php @@ -76,6 +76,7 @@ function register_block_core_site_logo_setting() { 'type' => 'integer', 'label' => __( 'Logo' ), 'description' => __( 'Site logo.' ), + 'autoload' => true, ) ); } @@ -96,6 +97,7 @@ function register_block_core_site_icon_setting() { 'type' => 'integer', 'label' => __( 'Icon' ), 'description' => __( 'Site icon.' ), + 'autoload' => true, ) ); } diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index 4dee6ce1188e9..4fd513744bc70 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -1260,6 +1260,8 @@ function delete_option( $option ) { * @since 6.6.0 * @access private * + * @global array $wp_registered_settings + * * @param string $option The name of the option. * @param mixed $value The value of the option to check its autoload value. * @param mixed $serialized_value The serialized value of the option to check its autoload value. @@ -1288,6 +1290,13 @@ function wp_determine_option_autoload_value( $option, $value, $serialized_value, return 'off'; } + $wp_registered_settings = get_registered_settings(); + if ( isset( $wp_registered_settings[ $option ]['autoload'] ) ) { + $default_autoload_value = ( bool ) $wp_registered_settings[ $option ]['autoload']; + } else { + $default_autoload_value = null; + } + /** * Allows to determine the default autoload value for an option where no explicit value is passed. * @@ -1298,7 +1307,7 @@ function wp_determine_option_autoload_value( $option, $value, $serialized_value, * @param string $option The passed option name. * @param mixed $value The passed option value to be saved. */ - $autoload = apply_filters( 'wp_default_autoload_value', null, $option, $value, $serialized_value ); + $autoload = apply_filters( 'wp_default_autoload_value',$default_autoload_value , $option, $value, $serialized_value ); if ( is_bool( $autoload ) ) { return $autoload ? 'auto-on' : 'auto-off'; } @@ -2661,6 +2670,7 @@ function register_initial_settings() { 'type' => 'string', 'label' => __( 'Title' ), 'description' => __( 'Site title.' ), + 'autoload' => true, ) ); @@ -2674,6 +2684,7 @@ function register_initial_settings() { 'type' => 'string', 'label' => __( 'Tagline' ), 'description' => __( 'Site tagline.' ), + 'autoload' => false, ) ); @@ -2690,6 +2701,7 @@ function register_initial_settings() { ), 'type' => 'string', 'description' => __( 'Site URL.' ), + 'autoload' => false, ) ); } @@ -2707,6 +2719,7 @@ function register_initial_settings() { ), 'type' => 'string', 'description' => __( 'This address is used for admin purposes, like new user notification.' ), + 'autoload' => false, ) ); } @@ -2720,6 +2733,7 @@ function register_initial_settings() { ), 'type' => 'string', 'description' => __( 'A city in the same timezone as you.' ), + 'autoload' => true, ) ); @@ -2730,6 +2744,7 @@ function register_initial_settings() { 'show_in_rest' => true, 'type' => 'string', 'description' => __( 'A date format for all date strings.' ), + 'autoload' => true, ) ); @@ -2740,6 +2755,7 @@ function register_initial_settings() { 'show_in_rest' => true, 'type' => 'string', 'description' => __( 'A time format for all time strings.' ), + 'autoload' => false, ) ); @@ -2750,6 +2766,7 @@ function register_initial_settings() { 'show_in_rest' => true, 'type' => 'integer', 'description' => __( 'A day number of the week that the week should start on.' ), + 'autoload' => false, ) ); @@ -2763,6 +2780,7 @@ function register_initial_settings() { 'type' => 'string', 'description' => __( 'WordPress locale code.' ), 'default' => 'en_US', + 'autoload' => true, ) ); @@ -2774,6 +2792,7 @@ function register_initial_settings() { 'type' => 'boolean', 'description' => __( 'Convert emoticons like :-) and :-P to graphics on display.' ), 'default' => true, + 'autoload' => true, ) ); @@ -2784,6 +2803,7 @@ function register_initial_settings() { 'show_in_rest' => true, 'type' => 'integer', 'description' => __( 'Default post category.' ), + 'autoload' => false, ) ); @@ -2794,6 +2814,7 @@ function register_initial_settings() { 'show_in_rest' => true, 'type' => 'string', 'description' => __( 'Default post format.' ), + 'autoload' => false, ) ); @@ -2806,6 +2827,7 @@ function register_initial_settings() { 'label' => __( 'Maximum posts per page' ), 'description' => __( 'Blog pages show at most.' ), 'default' => 10, + 'autoload' => true, ) ); @@ -2817,6 +2839,7 @@ function register_initial_settings() { 'type' => 'string', 'label' => __( 'Show on front' ), 'description' => __( 'What to show on the front page' ), + 'autoload' => true, ) ); @@ -2828,6 +2851,7 @@ function register_initial_settings() { 'type' => 'integer', 'label' => __( 'Page on front' ), 'description' => __( 'The ID of the page that should be displayed on the front page' ), + 'autoload' => true, ) ); @@ -2838,6 +2862,7 @@ function register_initial_settings() { 'show_in_rest' => true, 'type' => 'integer', 'description' => __( 'The ID of the page that should display the latest posts' ), + 'autoload' => true, ) ); @@ -2852,6 +2877,7 @@ function register_initial_settings() { ), 'type' => 'string', 'description' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles.' ), + 'autoload' => true, ) ); @@ -2867,6 +2893,7 @@ function register_initial_settings() { 'type' => 'string', 'label' => __( 'Allow comments on new posts' ), 'description' => __( 'Allow people to submit comments on new posts.' ), + 'autoload' => true, ) ); } @@ -2901,6 +2928,7 @@ function register_initial_settings() { * When registering complex settings, this argument may optionally be an * array with a 'schema' key. * @type mixed $default Default value when calling `get_option()`. + * @type bool $autoload Should this setting be set to autoload for all pages, default is false * } */ function register_setting( $option_group, $option_name, $args = array() ) { @@ -2919,6 +2947,7 @@ function register_setting( $option_group, $option_name, $args = array() ) { 'description' => '', 'sanitize_callback' => null, 'show_in_rest' => false, + 'autoload' => false, ); // Back-compat: old sanitize callback is added. diff --git a/tests/phpunit/tests/option/registration.php b/tests/phpunit/tests/option/registration.php index 9b0e418c91c57..7b62c8c33800a 100644 --- a/tests/phpunit/tests/option/registration.php +++ b/tests/phpunit/tests/option/registration.php @@ -163,4 +163,47 @@ public function test_unregister_invalid_setting_does_not_raise_php_notice_warnin unregister_setting( $setting, $setting ); $this->assertFalse( has_filter( 'default_option_' . $setting, 'filter_default_option' ) ); } + + /** + * @ticket 61522 + * + * @covers ::register_setting + * + * @dataProvider data_provider_register_setting_sets_autoload + */ + public function test_register_setting_sets_autoload( $autoload, $expected ) { + global $wpdb; + + register_setting( + 'test_group', + 'test_default', + array( + 'autoload' => $autoload, + ) + ); + $added = add_option( 'test_default', 'Autoload test' ); + $this->assertTrue( $added ); + + $actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", 'test_default' ) ); + $this->assertSame( $expected, $actual->autoload ); + + $added = update_option( 'test_default', 'Autoload tested' ); + $this->assertTrue( $added ); + + $actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", 'test_default' ) ); + $this->assertSame( $expected, $actual->autoload ); + } + + public function data_provider_register_setting_sets_autoload() { + return array( + 'autoloaded' => array( + 'autoload' => true, + 'expected' => 'auto-on', + ), + 'not_autoloaded' => array( + 'autoload' => false, + 'expected' => 'auto-off', + ), + ); + } }