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

Fix missing AS missing DB tables issue #4897

Merged
26 changes: 26 additions & 0 deletions inc/Engine/Optimization/RUCSS/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ public function display_success_notice() {
);
}

/**
* Display admin notice when detecting any missed Action scheduler tables.
*
* @since 3.11.0.2
Tabrisrp marked this conversation as resolved.
Show resolved Hide resolved
*
* @return void
*/
public function display_as_missed_tables_notice() {
$as_tools_link = menu_page_url( 'action-scheduler', false );
$message = sprintf(
// translators: %1$s = plugin name, %2$s = opening anchor tag, %3$s = closing anchor tag.
__( '%1$s: We detected missing database table related to Action Scheduler. Please visit the following %2$sURL%3$s to recreate it, as it\'s needed for WP Rocket to work correctly.', 'rocket' ),
Tabrisrp marked this conversation as resolved.
Show resolved Hide resolved
'<strong>WP Rocket</strong>',
'<a href="' . $as_tools_link . '">',
'</a>'
);

rocket_notice_html(
[
'status' => 'error',
'message' => $message,
'id' => 'rocket-notice-as-missed-tables',
]
);
}

/**
* Checks if we can display the RUCSS notices
*
Expand Down
130 changes: 105 additions & 25 deletions inc/Engine/Optimization/RUCSS/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,49 +65,52 @@ public static function get_subscribed_events() : array {
$slug = rocket_get_constant( 'WP_ROCKET_SLUG', 'wp_rocket_settings' );

return [
'rocket_first_install_options' => 'add_options_first_time',
'rocket_input_sanitize' => [ 'sanitize_options', 14, 2 ],
'update_option_' . $slug => [
'rocket_first_install_options' => 'add_options_first_time',
'rocket_input_sanitize' => [ 'sanitize_options', 14, 2 ],
'update_option_' . $slug => [
[ 'clean_used_css_and_cache', 9, 2 ],
[ 'clean_used_css_with_cdn', 9, 2 ],
[ 'maybe_set_processing_transient', 50, 2 ],
],
'switch_theme' => 'truncate_used_css',
'wp_trash_post' => 'delete_used_css_on_update_or_delete',
'delete_post' => 'delete_used_css_on_update_or_delete',
'clean_post_cache' => 'delete_used_css_on_update_or_delete',
'wp_update_comment_count' => 'delete_used_css_on_update_or_delete',
'edit_term' => 'delete_term_used_css',
'pre_delete_term' => 'delete_term_used_css',
'init' => [
'switch_theme' => 'truncate_used_css',
'wp_trash_post' => 'delete_used_css_on_update_or_delete',
'delete_post' => 'delete_used_css_on_update_or_delete',
'clean_post_cache' => 'delete_used_css_on_update_or_delete',
'wp_update_comment_count' => 'delete_used_css_on_update_or_delete',
'edit_term' => 'delete_term_used_css',
'pre_delete_term' => 'delete_term_used_css',
'init' => [
[ 'schedule_clean_not_commonly_used_rows' ],
[ 'schedule_rucss_pending_jobs_cron' ],
[ 'initialize_rucss_queue_runner' ],
],
'rocket_rucss_clean_rows_time_event' => 'cron_clean_rows',
'admin_post_rocket_clear_usedcss' => 'truncate_used_css_handler',
'admin_post_rocket_clear_usedcss_url' => 'clear_url_usedcss',
'admin_notices' => [
'rocket_rucss_clean_rows_time_event' => 'cron_clean_rows',
'admin_post_rocket_clear_usedcss' => 'truncate_used_css_handler',
'admin_post_rocket_clear_usedcss_url' => 'clear_url_usedcss',
'admin_notices' => [
[ 'clear_usedcss_result' ],
[ 'display_processing_notice' ],
[ 'display_success_notice' ],
[ 'display_as_missed_tables_notice' ],
],
'rocket_admin_bar_items' => [
'rocket_admin_bar_items' => [
[ 'add_clean_used_css_menu_item' ],
[ 'add_clear_usedcss_bar_item' ],
],
'rocket_before_add_field_to_settings' => [
'rocket_before_add_field_to_settings' => [
[ 'set_optimize_css_delivery_value', 10, 1 ],
[ 'set_optimize_css_delivery_method_value', 10, 1 ],
],
'rocket_localize_admin_script' => 'add_localize_script_data',
'rocket_localize_admin_script' => 'add_localize_script_data',
'action_scheduler_queue_runner_concurrent_batches' => 'adjust_as_concurrent_batches',
'pre_update_option_wp_rocket_settings' => [ 'maybe_disable_combine_css', 11, 2 ],
'wp_rocket_upgrade' => [
'pre_update_option_wp_rocket_settings' => [ 'maybe_disable_combine_css', 11, 2 ],
'wp_rocket_upgrade' => [
[ 'set_option_on_update', 14, 2 ],
[ 'update_safelist_items', 15, 2 ],
],
'wp_ajax_rocket_spawn_cron' => 'spawn_cron',
'rocket_deactivation' => 'cancel_queues',
'wp_ajax_rocket_spawn_cron' => 'spawn_cron',
'rocket_deactivation' => 'cancel_queues',
'shutdown' => 'schedule_rucss_pending_jobs_cron',
'admin_head-tools_page_action-scheduler' => 'delete_as_tables_transient_on_tools_page',
];
}

Expand Down Expand Up @@ -164,6 +167,19 @@ public function schedule_clean_not_commonly_used_rows() {
* @return void
*/
public function schedule_rucss_pending_jobs_cron() {
$error = error_get_last();

// Delete the transient when any error happens.
if ( null !== $error ) {
delete_transient( 'rocket_rucss_as_tables_count' );

return;
}

if ( ! $this->is_valid_as_tables() ) {
return;
}

if ( ! $this->settings->is_enabled() ) {
if ( ! $this->queue->is_pending_jobs_cron_scheduled() ) {
return;
Expand All @@ -175,8 +191,6 @@ public function schedule_rucss_pending_jobs_cron() {
return;
}

RUCSSQueueRunner::instance()->init();

/**
* Filters the cron interval.
*
Expand All @@ -191,6 +205,44 @@ public function schedule_rucss_pending_jobs_cron() {
$this->queue->schedule_pending_jobs_cron( $interval );
}

/**
* Initialize the queue runner for our RUCSS.
*
* @return void
*/
public function initialize_rucss_queue_runner() {
if ( ! $this->settings->is_enabled() ) {
return;
}

RUCSSQueueRunner::instance()->init();
}

/**
* Checks if Action scheduler tables are there or not.
*
* @since 3.11.0.2
Tabrisrp marked this conversation as resolved.
Show resolved Hide resolved
*
* @return bool
*/
private function is_valid_as_tables() {
$cached_count = get_transient( 'rocket_rucss_as_tables_count' );
if ( false !== $cached_count && ! is_admin() ) { // Stop caching in admin UI.
return 4 === (int) $cached_count;
}

global $wpdb;

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$found_as_tables = $wpdb->get_col(
$wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'actionscheduler%' )
);

set_transient( 'rocket_rucss_as_tables_count', count( $found_as_tables ), rocket_get_constant( 'DAY_IN_SECONDS', 24 * 60 * 60 ) );

return 4 === count( $found_as_tables );
}

/**
* Delete used_css on Update Post or Delete post.
*
Expand Down Expand Up @@ -503,6 +555,25 @@ public function display_success_notice() {
$this->settings->display_success_notice();
}

/**
* Display admin notice when detecting any missed Action scheduler tables.
*
* @since 3.11.0.2
Tabrisrp marked this conversation as resolved.
Show resolved Hide resolved
*
* @return void
*/
public function display_as_missed_tables_notice() {
if ( function_exists( 'get_current_screen' ) && 'tools_page_action-scheduler' === get_current_screen()->id ) {
return;
}

if ( $this->is_valid_as_tables() ) {
return;
}

$this->settings->display_as_missed_tables_notice();
}

/**
* Adds the notice end time to WP Rocket localize script data
*
Expand Down Expand Up @@ -693,4 +764,13 @@ public function cancel_queues() {

wp_clear_scheduled_hook( 'rocket_rucss_clean_rows_time_event' );
}

/**
* Delete the transient for Action scheduler once admin visits the AS tools page.
*
* @return void
*/
public function delete_as_tables_transient_on_tools_page() {
delete_transient( 'rocket_rucss_as_tables_count' );
}
}