Skip to content
Open
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
2 changes: 1 addition & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"core": null,
"phpVersion": "7.4",
"phpVersion": "8.1",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PHP 7.4 wp-env image is based on Debian Bullseye, whose security repository release file has expired. As a result, apt-get update fails and the image build cannot complete.

Therefore, I bumped the PHP version to 8.1 so the test environment can build and the test case can run successfully. This change is only for the test environment and does not mean that the plugin's minimum supported PHP version has been increased to 8.1.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that readme.txt, composer.json, and CI still declare PHP 7.4 support. The new void return type is valid on PHP 7.4 and already exists in this class. The wp-env change does not alter runtime support.

"plugins": [
"."
],
Expand Down
4 changes: 4 additions & 0 deletions classes/Visualizer/Gutenberg/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ public function get_visualizer_data( $post ) {

// faetch and update settings
$data['visualizer-settings'] = get_post_meta( $post_id, Visualizer_Plugin::CF_SETTINGS, true );
if ( ! is_array( $data['visualizer-settings'] ) ) {
$data['visualizer-settings'] = array();
}

if ( empty( $data['visualizer-settings']['pagination'] ) ) {
$data['visualizer-settings']['pageSize'] = '';
}
Expand Down
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