Changeset 57920 for trunk/src/wp-includes/option.php
- Timestamp:
- 04/03/2024 09:29:13 PM (15 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/option.php
r57110 r57920 393 393 394 394 $grouped_options = array( 395 ' yes'=> array(),396 ' no'=> array(),395 'on' => array(), 396 'off' => array(), 397 397 ); 398 398 $results = array(); 399 399 foreach ( $options as $option => $autoload ) { 400 400 wp_protect_special_option( $option ); // Ensure only valid options can be passed. 401 if ( ' no' === $autoload || false === $autoload ) { // Sanitize autoload value and categorize accordingly.402 $grouped_options[' no'][] = $option;401 if ( 'off' === $autoload || 'no' === $autoload || false === $autoload ) { // Sanitize autoload value and categorize accordingly. 402 $grouped_options['off'][] = $option; 403 403 } else { 404 $grouped_options[' yes'][] = $option;404 $grouped_options['on'][] = $option; 405 405 } 406 406 $results[ $option ] = false; // Initialize result value. … … 466 466 467 467 /* 468 * If any options were changed to ' yes', delete their individual caches, and delete 'alloptions' cache so that it468 * If any options were changed to 'on', delete their individual caches, and delete 'alloptions' cache so that it 469 469 * is refreshed as needed. 470 * If no options were changed to ' yes' but any options were changed to 'no', delete them from the 'alloptions'471 * cache. This is not necessary when options were changed to ' yes', since in that situation the entire cache is470 * If no options were changed to 'on' but any options were changed to 'no', delete them from the 'alloptions' 471 * cache. This is not necessary when options were changed to 'on', since in that situation the entire cache is 472 472 * deleted anyway. 473 473 */ 474 if ( $grouped_options[' yes'] ) {475 wp_cache_delete_multiple( $grouped_options[' yes'], 'options' );474 if ( $grouped_options['on'] ) { 475 wp_cache_delete_multiple( $grouped_options['on'], 'options' ); 476 476 wp_cache_delete( 'alloptions', 'options' ); 477 } elseif ( $grouped_options[' no'] ) {477 } elseif ( $grouped_options['off'] ) { 478 478 $alloptions = wp_load_alloptions( true ); 479 479 480 foreach ( $grouped_options[' no'] as $option ) {480 foreach ( $grouped_options['off'] as $option ) { 481 481 if ( isset( $alloptions[ $option ] ) ) { 482 482 unset( $alloptions[ $option ] ); … … 607 607 if ( ! $alloptions ) { 608 608 $suppress = $wpdb->suppress_errors(); 609 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload = 'yes'" ); 609 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE autoload IN ( '" . implode( "', '", wp_autoload_values_to_autoload() ) . "' )" ); 610 610 611 if ( ! $alloptions_db ) { 611 612 $alloptions_db = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options" ); … … 706 707 * @global wpdb $wpdb WordPress database abstraction object. 707 708 * 708 * @param string $option Name of the option to update. Expected to not be SQL-escaped. 709 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. 710 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. For existing options, 711 * `$autoload` can only be updated using `update_option()` if `$value` is also changed. 712 * Accepts 'yes'|true to enable or 'no'|false to disable. 713 * Autoloading too many options can lead to performance problems, especially if the 714 * options are not frequently used. For options which are accessed across several places 715 * in the frontend, it is recommended to autoload them, by using 'yes'|true. 716 * For options which are accessed only on few specific URLs, it is recommended 717 * to not autoload them, by using 'no'|false. For non-existent options, the default value 718 * is 'yes'. Default null. 709 * @param string $option Name of the option to update. Expected to not be SQL-escaped. 710 * @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped. 711 * @param bool|null $autoload Optional. Whether to load the option when WordPress starts up. 712 * Accepts a boolean, or `null` to stick with the initial value or, if no initial value is set, 713 * to leave the decision up to default heuristics in WordPress. 714 * For existing options, 715 * `$autoload` can only be updated using `update_option()` if `$value` is also changed. 716 * For backward compatibility 'yes' and 'no' are also accepted. 717 * Autoloading too many options can lead to performance problems, especially if the 718 * options are not frequently used. For options which are accessed across several places 719 * in the frontend, it is recommended to autoload them, by using true. 720 * For options which are accessed only on few specific URLs, it is recommended 721 * to not autoload them, by using false. 722 * For non-existent options, the default is null, which means WordPress will determine 723 * the autoload value. 719 724 * @return bool True if the value was updated, false otherwise. 720 725 */ … … 802 807 /** This filter is documented in wp-includes/option.php */ 803 808 if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) { 804 // Default setting for new options is 'yes'.805 if ( null === $autoload ) {806 $autoload = 'yes';807 }808 809 809 return add_option( $option, $value, '', $autoload ); 810 810 } … … 828 828 829 829 if ( null !== $autoload ) { 830 $update_args['autoload'] = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes'; 830 $update_args['autoload'] = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ); 831 } else { 832 // Retrieve the current autoload value to reevaluate it in case it was set automatically. 833 $raw_autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); 834 $allow_values = array( 'auto-on', 'auto-off', 'auto' ); 835 if ( in_array( $raw_autoload, $allow_values, true ) ) { 836 $autoload = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ); 837 if ( $autoload !== $raw_autoload ) { 838 $update_args['autoload'] = $autoload; 839 } 840 } 831 841 } 832 842 … … 854 864 wp_cache_set( $option, $serialized_value, 'options' ); 855 865 } 856 } elseif ( 'yes' === $update_args['autoload']) {866 } elseif ( in_array( $update_args['autoload'], wp_autoload_values_to_autoload(), true ) ) { 857 867 // Delete the individual cache, then set in alloptions cache. 858 868 wp_cache_delete( $option, 'options' ); … … 916 926 * 917 927 * @since 1.0.0 928 * @since 6.6.0 The $autoload parameter's default value was changed to null. 918 929 * 919 930 * @global wpdb $wpdb WordPress database abstraction object. 920 931 * 921 * @param string $option Name of the option to add. Expected to not be SQL-escaped. 922 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. 923 * Expected to not be SQL-escaped. 924 * @param string $deprecated Optional. Description. Not used anymore. 925 * @param string|bool $autoload Optional. Whether to load the option when WordPress starts up. 926 * Accepts 'yes'|true to enable or 'no'|false to disable. 927 * Autoloading too many options can lead to performance problems, especially if the 928 * options are not frequently used. For options which are accessed across several places 929 * in the frontend, it is recommended to autoload them, by using 'yes'|true. 930 * For options which are accessed only on few specific URLs, it is recommended 931 * to not autoload them, by using 'no'|false. Default 'yes'. 932 * @param string $option Name of the option to add. Expected to not be SQL-escaped. 933 * @param mixed $value Optional. Option value. Must be serializable if non-scalar. 934 * Expected to not be SQL-escaped. 935 * @param string $deprecated Optional. Description. Not used anymore. 936 * @param bool|null $autoload Optional. Whether to load the option when WordPress starts up. 937 * Accepts a boolean, or `null` to leave the decision up to default heuristics in WordPress. 938 * For backward compatibility 'yes' and 'no' are also accepted. 939 * Autoloading too many options can lead to performance problems, especially if the 940 * options are not frequently used. For options which are accessed across several places 941 * in the frontend, it is recommended to autoload them, by using 'yes'|true. 942 * For options which are accessed only on few specific URLs, it is recommended 943 * to not autoload them, by using false. 944 * Default is null, which means WordPress will determine the autoload value. 932 945 * @return bool True if the option was added, false otherwise. 933 946 */ 934 function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes') {947 function add_option( $option, $value = '', $deprecated = '', $autoload = null ) { 935 948 global $wpdb; 936 949 … … 992 1005 993 1006 $serialized_value = maybe_serialize( $value ); 994 $autoload = ( 'no' === $autoload || false === $autoload ) ? 'no' : 'yes'; 1007 1008 $autoload = wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ); 995 1009 996 1010 /** … … 1010 1024 1011 1025 if ( ! wp_installing() ) { 1012 if ( 'yes' === $autoload) {1026 if ( in_array( $autoload, wp_autoload_values_to_autoload(), true ) ) { 1013 1027 $alloptions = wp_load_alloptions( true ); 1014 1028 $alloptions[ $option ] = $serialized_value; … … 1094 1108 1095 1109 if ( ! wp_installing() ) { 1096 if ( 'yes' === $row->autoload) {1110 if ( in_array( $row->autoload, wp_autoload_values_to_autoload(), true ) ) { 1097 1111 $alloptions = wp_load_alloptions( true ); 1098 1112 … … 1132 1146 1133 1147 return false; 1148 } 1149 1150 /** 1151 * Determines the appropriate autoload value for an option based on input. 1152 * 1153 * This function checks the provided autoload value and returns a standardized value 1154 * ('on', 'off', 'auto-on', 'auto-off', or 'auto') based on specific conditions. 1155 * 1156 * If no explicit autoload value is provided, the function will check for certain heuristics around the given option. 1157 * It will return `auto-on` to indicate autoloading, `auto-off` to indicate not autoloading, or `auto` if no clear 1158 * decision could be made. 1159 * 1160 * @since 6.6.0 1161 * @access private 1162 * 1163 * @param string $option The name of the option. 1164 * @param mixed $value The value of the option to check its autoload value. 1165 * @param mixed $serialized_value The serialized value of the option to check its autoload value. 1166 * @param bool|null $autoload The autoload value to check. 1167 * Accepts 'on'|true to enable or 'off'|false to disable, or 1168 * 'auto-on', 'auto-off', or 'auto' for internal purposes. 1169 * Any other autoload value will be forced to either 'auto-on', 1170 * 'auto-off', or 'auto'. 1171 * 'yes' and 'no' are supported for backward compatibility. 1172 * @return string Returns the original $autoload value if explicit, or 'auto-on', 'auto-off', 1173 * or 'auto' depending on default heuristics. 1174 */ 1175 function wp_determine_option_autoload_value( $option, $value, $serialized_value, $autoload ) { 1176 1177 // Check if autoload is a boolean. 1178 if ( is_bool( $autoload ) ) { 1179 return $autoload ? 'on' : 'off'; 1180 } 1181 1182 switch ( $autoload ) { 1183 case 'on': 1184 case 'yes': 1185 return 'on'; 1186 case 'off': 1187 case 'no': 1188 return 'off'; 1189 } 1190 1191 /** 1192 * Allows to determine the default autoload value for an option where no explicit value is passed. 1193 * 1194 * @since 6.6.0 1195 * 1196 * @param bool|null $autoload The default autoload value to set. Returning true will be set as 'auto-on' in the 1197 * database, false will be set as 'auto-off', and null will be set as 'auto'. 1198 * @param string $option The passed option name. 1199 * @param mixed $value The passed option value to be saved. 1200 */ 1201 $autoload = apply_filters( 'wp_default_autoload_value', null, $option, $value, $serialized_value ); 1202 if ( is_bool( $autoload ) ) { 1203 return $autoload ? 'auto-on' : 'auto-off'; 1204 } 1205 1206 return 'auto'; 1207 } 1208 1209 /** 1210 * Filters the default autoload value to disable autoloading if the option value is too large. 1211 * 1212 * @since 6.6.0 1213 * @access private 1214 * 1215 * @param bool|null $autoload The default autoload value to set. 1216 * @param string $option The passed option name. 1217 * @param mixed $value The passed option value to be saved. 1218 * @param mixed $serialized_value The passed option value to be saved, in serialized form. 1219 * @return bool|null Potentially modified $default. 1220 */ 1221 function wp_filter_default_autoload_value_via_option_size( $autoload, $option, $value, $serialized_value ) { 1222 /** 1223 * Filters the maximum size of option value in bytes. 1224 * 1225 * @since 6.6.0 1226 * 1227 * @param int $max_option_size The option-size threshold, in bytes. Default 150000. 1228 * @param string $option The name of the option. 1229 */ 1230 $max_option_size = (int) apply_filters( 'wp_max_autoloaded_option_size', 150000, $option ); 1231 $size = ! empty( $serialized_value ) ? strlen( $serialized_value ) : 0; 1232 1233 if ( $size > $max_option_size ) { 1234 return false; 1235 } 1236 1237 return $autoload; 1134 1238 } 1135 1239 … … 2925 3029 return $registered[ $option ]['default']; 2926 3030 } 3031 3032 /** 3033 * Returns the values that trigger autoloading from the options table. 3034 * 3035 * @since 6.6.0 3036 * 3037 * @return array The values that trigger autoloading. 3038 */ 3039 function wp_autoload_values_to_autoload() { 3040 $autoload_values = array( 'yes', 'on', 'auto-on', 'auto' ); 3041 3042 /** 3043 * Filters the autoload values that should be considered for autoloading from the options table. 3044 * 3045 * The filter can only be used to remove autoload values from the default list. 3046 * 3047 * @since 6.6.0 3048 * 3049 * @param array $autoload_values Autoload values used to autoload option. 3050 * Default list contains 'yes', 'on', 'auto-on', and 'auto'. 3051 */ 3052 $filtered_values = apply_filters( 'wp_autoload_values_to_autoload', $autoload_values ); 3053 3054 return array_intersect( $filtered_values, $autoload_values ); 3055 }
Note: See TracChangeset
for help on using the changeset viewer.