Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support changing autoload value for largest autoloaded options in Site Health check #1048

Merged
merged 20 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 37 additions & 5 deletions modules/database/audit-autoloaded-options/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function perflab_aao_autoloaded_options_test() {
'<p>' . esc_html( $base_description ) . ' ' . __( 'Your site has %1$s autoloaded options (size: %2$s) in the options table, which could cause your site to be slow. You can reduce the number of autoloaded options by cleaning up your site\'s options table.', 'performance-lab' ) . '</p>',
$autoloaded_options_count,
size_format( $autoloaded_options_size )
) . perflab_aao_get_autoloaded_options_table();
) . perflab_aao_get_autoloaded_options_table() . perflab_aao_get_disabled_autoloaded_options_table();

/**
* Filters description to be shown on Site Health warning when threshold is met.
Expand Down Expand Up @@ -144,13 +144,45 @@ function perflab_aao_get_autoloaded_options_table() {
$autoload_summary = perflab_aao_query_autoloaded_options();

$html_table = sprintf(
'<table class="widefat striped"><thead><tr><th scope="col">%s</th><th scope="col">%s</th></tr></thead><tbody>',
__( 'Option Name', 'performance-lab' ),
__( 'Size', 'performance-lab' )
'<table class="widefat striped"><thead><tr><th scope="col">%s</th><th scope="col">%s</th><th scope="col">%s</th></tr></thead><tbody>',
esc_html__( 'Option Name', 'performance-lab' ),
esc_html__( 'Size', 'performance-lab' ),
esc_html__( 'Action', 'performance-lab' )
);

foreach ( $autoload_summary as $value ) {
$html_table .= sprintf( '<tr><td>%s</td><td>%s</td></tr>', $value->option_name, size_format( $value->option_value_length, 2 ) );
$disable_button = sprintf( '<a class="button update-autoload" data-option-name="%s" data-autoload="no" data-value="%s" href="#">%s</a>', esc_attr( $value->option_name ), esc_attr( $value->option_value_length ), esc_html__( 'Disable Autoload', 'performance-lab' ) );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$html_table .= sprintf( '<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $value->option_name, size_format( $value->option_value_length, 2 ), $disable_button );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
}
$html_table .= '</tbody></table>';

return $html_table;
}

/**
* Gets disabled autoload options table.
*
* @since n.e.x.t
*
* @return string HTML formatted table.
*/
function perflab_aao_get_disabled_autoloaded_options_table() {
$perflab_aao_disabled_options = get_option( 'perflab_aao_modified_options', array() );

if ( empty( $perflab_aao_disabled_options ) ) {
return;
}

$html_table = sprintf(
'<br><br><table class="widefat striped"><thead><tr><th scope="col">%s</th><th scope="col">%s</th><th scope="col">%s</th></tr></thead><tbody>',
esc_html__( 'Option Name', 'performance-lab' ),
esc_html__( 'Size', 'performance-lab' ),
esc_html__( 'Action', 'performance-lab' )
);

foreach ( $perflab_aao_disabled_options as $option_name => $value ) {
$disable_button = sprintf( '<a class="button update-autoload" data-option-name="%s" data-autoload="yes" href="#">%s</a>', esc_attr( $option_name ), esc_html__( 'Revert to Autoload', 'performance-lab' ) );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$html_table .= sprintf( '<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $option_name, size_format( $value, 2 ), $disable_button );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
}
$html_table .= '</tbody></table>';

Expand Down
112 changes: 111 additions & 1 deletion modules/database/audit-autoloaded-options/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,119 @@
*/
function perflab_aao_add_autoloaded_options_test( $tests ) {
$tests['direct']['autoloaded_options'] = array(
'label' => __( 'Autoloaded options', 'performance-lab' ),
'label' => esc_html__( 'Autoloaded options', 'performance-lab' ),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
'test' => 'perflab_aao_autoloaded_options_test',
);
return $tests;
}
add_filter( 'site_status_tests', 'perflab_aao_add_autoloaded_options_test' );

/**
* Register custom REST API route for updating autoload.
*
* @since n.e.x.t
*/
function perflab_aao_register_rest_route() {
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
register_rest_route(
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
'perflab-aao/v1',
'/update-autoload',
array(
'methods' => 'POST',
'callback' => 'perflab_aao_update_autoload_rest',
)
);
}
add_action( 'rest_api_init', 'perflab_aao_register_rest_route' );

/**
* Callback for updating autoload via REST API.
*
* @since n.e.x.t
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
function perflab_aao_update_autoload_rest( $request ) {
$option_name = $request->get_param( 'option_name' );
$autoload = $request->get_param( 'autoload' );
$value = $request->get_param( 'value' );

if ( empty( $option_name ) || empty( $autoload ) ) {
return new WP_REST_Response(
array(
'success' => false,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
'message' => esc_html__( 'Invalid option name or autoload value.', 'performance-lab' ),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
),
400
);
}

// Check if the option exists.
if ( false === get_option( $option_name ) ) {
// Option doesn't exist, return an error or handle the situation accordingly.
return new WP_REST_Response(
array(
'success' => false,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
'message' => esc_html__( 'The option does not exist.', 'performance-lab' ),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
),
400
);
}

mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$result = wp_set_option_autoload( $option_name, $autoload );

if ( $result ) {
// Update modified options list.
$modified_options = get_option( 'perflab_aao_modified_options', array() );
if ( 'yes' === $autoload && isset( $modified_options[ $option_name ] ) ) {
unset( $modified_options[ $option_name ] );
} else {
$modified_options[ $option_name ] = $value;
}
update_option( 'perflab_aao_modified_options', $modified_options );

return new WP_REST_Response(
array(
'success' => true,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
'message' => sprintf(
/* translators: 1: Autoload value, 2: Option name */
esc_html__( 'Autoload %1$s for option %2$s.', 'performance-lab' ),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$autoload,
$option_name
),
),
200
);
} else {
return new WP_REST_Response(
array(
'success' => false,
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
'message' => esc_html__( 'Failed to update autoload status.', 'performance-lab' ),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
),
500
);
}
}

/**
* Enqueue JavaScript for disabling autoload.
*
* @since n.e.x.t
*
* @param string $hook_suffix The current admin page.
*/
function perflab_aao_enqueue_script( $hook_suffix ) {
if ( 'site-health.php' !== $hook_suffix ) {
return;
}
wp_enqueue_script( 'perflab-aao-update-autoload', plugin_dir_url( __FILE__ ) . 'update-autoload.js', array( 'jquery' ), 'n.e.x.t', true );

wp_localize_script(
'perflab-aao-update-autoload',
'perflabAutoloadSettings',
array(
'root' => sanitize_url( get_rest_url() ),
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
)
);
}
add_action( 'admin_enqueue_scripts', 'perflab_aao_enqueue_script' );
45 changes: 45 additions & 0 deletions modules/database/audit-autoloaded-options/update-autoload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint camelcase: "off" */
/* global jQuery:false, perflabAutoloadSettings:false */

( function ( $, document ) {
$( document ).on( 'click', '.update-autoload', function ( e ) {
e.preventDefault();
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
const button = $( this );
const optionName = button.data( 'option-name' );
const autoload = button.data( 'autoload' );
const value = button.data( 'value' );
const data = {
option_name: optionName,
autoload,
value,
};

$.ajax( {
url:
perflabAutoloadSettings.root + 'perflab-aao/v1/update-autoload',
method: 'POST',
data,
success( response ) {
if ( response.success ) {
button.attr( 'disabled', true );
} else {
button.append(
'<span class=""> ' + response.message + '</span>'
);
$(
'<div style="margin-top:5px;border:1px solid #d63638;padding-left:5px;">' +
response.message +
'</div>'
).insertAfter( button );
}
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
},
error( error ) {
$(
'<div style="margin-top:5px;border:1px solid #d63638;padding-left:5px;">' +
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
error.responseJSON.message +
'</div>'
).insertAfter( button );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
},
} );
} );
} )( jQuery, document );