Skip to content
File renamed without changes.
2 changes: 2 additions & 0 deletions .github/workflows/spell-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ jobs:
- uses: actions/checkout@v3
- name: Search for misspellings
uses: crate-ci/typos@master
with:
config: ./.github/typos.toml
40 changes: 38 additions & 2 deletions includes/site-health/audit-autoloaded-options/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@ function perflab_aao_autoloaded_options_test() {
*/
function perflab_aao_autoloaded_options_size() {
global $wpdb;
return (int) $wpdb->get_var( 'SELECT SUM(LENGTH(option_value)) FROM ' . $wpdb->prefix . 'options WHERE autoload = \'yes\'' );

$autoload_values = perflab_aao_get_autoload_values_to_autoload();

return (int) $wpdb->get_var(
$wpdb->prepare(
sprintf(
"SELECT SUM(LENGTH(option_value)) FROM $wpdb->options WHERE autoload IN (%s)",
implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) )
),
$autoload_values
)
);
}

/**
Expand Down Expand Up @@ -130,7 +141,17 @@ function perflab_aao_query_autoloaded_options() {
*/
$option_threshold = apply_filters( 'perflab_aao_autoloaded_options_table_threshold', 100 );

return $wpdb->get_results( $wpdb->prepare( "SELECT option_name, LENGTH(option_value) AS option_value_length FROM {$wpdb->options} WHERE autoload='yes' AND LENGTH(option_value) > %d ORDER BY option_value_length DESC LIMIT 20", $option_threshold ) );
$autoload_values = perflab_aao_get_autoload_values_to_autoload();

return $wpdb->get_results(
$wpdb->prepare(
sprintf(
"SELECT option_name, LENGTH(option_value) AS option_value_length FROM {$wpdb->options} WHERE autoload IN (%s)",
implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) )
) . ' AND LENGTH(option_value) > %d ORDER BY option_value_length DESC LIMIT 20',
array_merge( $autoload_values, array( $option_threshold ) )
)
);
}

/**
Expand Down Expand Up @@ -227,3 +248,18 @@ function perflab_aao_get_disabled_autoloaded_options_table() {

return $html_table;
}

/**
* Gets the autoload values in the database that should trigger their option to be autoloaded.
*
* @since n.e.x.t
*
* @return array List of autoload values.
*/
function perflab_aao_get_autoload_values_to_autoload() {
if ( function_exists( 'wp_autoload_values_to_autoload' ) ) {
return wp_autoload_values_to_autoload();
}

return array( 'yes' );
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,18 @@ public function test_perflab_aao_autoloaded_options_test_warning() {
*/
public function test_perflab_aao_autoloaded_options_size() {
global $wpdb;
$autoloaded_options_size = $wpdb->get_var( 'SELECT SUM(LENGTH(option_value)) FROM ' . $wpdb->prefix . 'options WHERE autoload = \'yes\'' );

$autoload_values = perflab_aao_get_autoload_values_to_autoload();

$autoloaded_options_size = $wpdb->get_var(
$wpdb->prepare(
sprintf(
"SELECT SUM(LENGTH(option_value)) FROM $wpdb->options WHERE autoload IN (%s)",
implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) )
),
$autoload_values
)
);
$this->assertEquals( $autoloaded_options_size, perflab_aao_autoloaded_options_size() );

// Add autoload option.
Expand Down Expand Up @@ -148,7 +159,9 @@ static function () {
*/
public static function set_autoloaded_option( $bytes = 800000 ) {
$heavy_option_string = wp_generate_password( $bytes );
add_option( self::AUTOLOADED_OPTION_KEY, $heavy_option_string );

// Force autoloading so that WordPress core does not override it. See https://core.trac.wordpress.org/changeset/57920.
add_option( self::AUTOLOADED_OPTION_KEY, $heavy_option_string, '', true );
}

/**
Expand Down