| @@ -16,17 +16,24 @@ | ||
| 16 | 16 | * Upgrades the configuration of charts, if required. |
| 17 | 17 | */ |
| 18 | 18 | public static function upgrade() { |
| 19 | 19 | $last_version = get_option( 'visualizer-upgraded', '0.0.0' ); |
| 20 | + $upgraded = false; | |
| 20 | 21 | |
| 21 | - switch ( $last_version ) { | |
| 22 | - case '0.0.0': | |
| 23 | - self::makeAllTableChartsTabular(); | |
| 24 | - break; | |
| 25 | - default: | |
| 26 | - return; | |
| 22 | + if ( version_compare( $last_version, '3.4.3', '<' ) ) { | |
| 23 | + self::makeAllTableChartsTabular(); | |
| 24 | + $upgraded = true; | |
| 27 | 25 | } |
| 28 | 26 | |
| 27 | + if ( wp_next_scheduled( 'visualizer_schedule_refresh_db' ) ) { | |
| 28 | + self::migrate_action_scheduler(); | |
| 29 | + $upgraded = true; | |
| 30 | + } | |
| 31 | + | |
| 32 | + if ( ! $upgraded ) { | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + | |
| 29 | 36 | update_option( 'visualizer-upgraded', Visualizer_Plugin::VERSION ); |
| 30 | 37 | |
| 31 | 38 | // this will help in debugging to know which version this was upgraded from. |
| 32 | 39 | update_option( 'visualizer-upgraded-from', $last_version ); |
| @@ -70,7 +77,43 @@ | ||
| 70 | 77 | AND pm.meta_key = '" . Visualizer_Plugin::CF_CHART_TYPE . "' |
| 71 | 78 | AND pm.meta_value IN ( 'dataTable', 'table' )" |
| 72 | 79 | ); |
| 73 | 80 | // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared |
| 81 | + } | |
| 74 | 82 | |
| 83 | + /** | |
| 84 | + * Migrate recurring WP-Cron jobs to Action Scheduler. | |
| 85 | + */ | |
| 86 | + private static function migrate_action_scheduler(): void { | |
| 87 | + if ( ! function_exists( 'as_schedule_recurring_action' ) || ! function_exists( 'as_next_scheduled_action' ) ) { | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $hook = 'visualizer_schedule_refresh_db'; | |
| 92 | + $group = 'visualizer'; | |
| 93 | + $interval_key = apply_filters( 'visualizer_chart_schedule_interval', 'visualizer_ten_minutes' ); | |
| 94 | + $interval = self::get_schedule_interval_seconds( $interval_key ); | |
| 95 | + $timestamp = strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS; | |
| 96 | + | |
| 97 | + $next = as_next_scheduled_action( $hook, array(), $group ); | |
| 98 | + if ( false === $next ) { | |
| 99 | + as_schedule_recurring_action( $timestamp, $interval, $hook, array(), $group ); | |
| 100 | + } | |
| 101 | + | |
| 102 | + wp_clear_scheduled_hook( $hook ); | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Resolve a cron schedule key to seconds. | |
| 107 | + * | |
| 108 | + * @param string $interval_key Cron schedule key. | |
| 109 | + * @return int Interval in seconds. | |
| 110 | + */ | |
| 111 | + private static function get_schedule_interval_seconds( $interval_key ) { | |
| 112 | + $schedules = wp_get_schedules(); | |
| 113 | + if ( isset( $schedules[ $interval_key ]['interval'] ) ) { | |
| 114 | + return (int) $schedules[ $interval_key ]['interval']; | |
| 115 | + } | |
| 116 | + | |
| 117 | + return 600; | |
| 75 | 118 | } |
| 76 | 119 | } |