Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion classes/Visualizer/Module/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct( Visualizer_Plugin $plugin ) {
register_activation_hook( VISUALIZER_BASEFILE, array( $this, 'activate' ) );
register_deactivation_hook( VISUALIZER_BASEFILE, array( $this, 'deactivate' ) );
$this->_addAction( 'visualizer_schedule_refresh_db', 'refreshDbChart' );
$this->_addAction( 'init', 'maybe_reschedule_refresh_db' );
$this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 );

$this->_addAction( 'admin_init', 'adminInit' );
Expand Down Expand Up @@ -490,7 +491,11 @@ private function schedule_refresh_db_action(): void {
$interval = $this->get_schedule_interval_seconds( $interval_key );
$timestamp = strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;

if ( function_exists( 'as_next_scheduled_action' ) && function_exists( 'as_schedule_recurring_action' ) ) {
if (
visualizer_can_use_action_scheduler()
&& function_exists( 'as_next_scheduled_action' )
&& function_exists( 'as_schedule_recurring_action' )
) {
$next = as_next_scheduled_action( $hook, array(), $group );
if ( false === $next ) {
as_schedule_recurring_action( $timestamp, $interval, $hook, array(), $group );
Expand All @@ -503,6 +508,29 @@ private function schedule_refresh_db_action(): void {
wp_schedule_event( $timestamp, $interval_key, $hook );
}

/**
* Keep the DB refresh scheduled when Action Scheduler is not available.
*
* The migration to Action Scheduler clears the WP-Cron event, so a site that
* already migrated and then lost the library would have nothing left running
* the refresh. Re-arms WP-Cron in that case; no-op whenever the library is up.
*/
public function maybe_reschedule_refresh_db(): void {
if (
visualizer_can_use_action_scheduler()
&& function_exists( 'as_next_scheduled_action' )
&& function_exists( 'as_schedule_recurring_action' )
) {
return;
}

if ( wp_next_scheduled( 'visualizer_schedule_refresh_db' ) ) {
return;
}

$this->schedule_refresh_db_action();
}

/**
* Unschedule the recurring DB refresh action.
*/
Expand Down
17 changes: 16 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function () {

$action_scheduler_file = VISUALIZER_ABSPATH . '/vendor/woocommerce/action-scheduler/action-scheduler.php';

if ( is_readable( $action_scheduler_file ) ) {
if ( visualizer_can_use_action_scheduler() && is_readable( $action_scheduler_file ) ) {
require_once $action_scheduler_file;
}

Expand Down Expand Up @@ -217,6 +217,21 @@ function ( $products ) {
}
}

/**
* Whether the bundled Action Scheduler can run against this wpdb.
*
* The bundled library calls wpdb::db_server_info() unguarded when it claims a
* queue batch, and core only added that method in WordPress 5.5. Loading it
* without the method available fatals, so those sites stay on WP-Cron instead.
*
* @return bool
*/
function visualizer_can_use_action_scheduler() {
global $wpdb;

return isset( $wpdb ) && is_callable( array( $wpdb, 'db_server_info' ) );
}

/**
* Registers with the SDK
*
Expand Down
Loading