| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* General module to upgrade configuration/charts. |
| 5 |
* |
| 6 |
* @category Visualizer |
| 7 |
* @package Module |
| 8 |
* |
| 9 |
* @since 3.4.3 |
| 10 |
*/ |
| 11 |
class Visualizer_Module_Upgrade extends Visualizer_Module { |
| 12 |
|
| 13 |
const NAME = __CLASS__; |
| 14 |
|
| 15 |
/** |
| 16 |
* Upgrades the configuration of charts, if required. |
| 17 |
*/ |
| 18 |
public static function upgrade() { |
| 19 |
$last_version = get_option( 'visualizer-upgraded', '0.0.0' ); |
| 20 |
$upgraded = false; |
| 21 |
|
| 22 |
if ( version_compare( $last_version, '3.4.3', '<' ) ) { |
| 23 |
self::makeAllTableChartsTabular(); |
| 24 |
$upgraded = true; |
| 25 |
} |
| 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 |
|
| 36 |
update_option( 'visualizer-upgraded', Visualizer_Plugin::VERSION ); |
| 37 |
|
| 38 |
// this will help in debugging to know which version this was upgraded from. |
| 39 |
update_option( 'visualizer-upgraded-from', $last_version ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* All 'dataTable' and 'table' charts to become 'tabular'. |
| 44 |
* All charts that do not have a library, to get the default library. |
| 45 |
*/ |
| 46 |
private static function makeAllTableChartsTabular() { |
| 47 |
global $wpdb; |
| 48 |
|
| 49 |
// old table charts may not specify the library. |
| 50 |
$args = array( |
| 51 |
'post_type' => Visualizer_Plugin::CPT_VISUALIZER, |
| 52 |
'fields' => 'ids', |
| 53 |
'post_status' => 'publish', |
| 54 |
'meta_query' => array( |
| 55 |
array( |
| 56 |
'key' => Visualizer_Plugin::CF_CHART_LIBRARY, |
| 57 |
'compare' => 'NOT EXISTS', |
| 58 |
), |
| 59 |
array( |
| 60 |
'key' => Visualizer_Plugin::CF_CHART_TYPE, |
| 61 |
'value' => 'table', |
| 62 |
), |
| 63 |
), |
| 64 |
); |
| 65 |
$query = new WP_Query( $args ); |
| 66 |
while ( $query->have_posts() ) { |
| 67 |
$id = $query->next_post(); |
| 68 |
add_post_meta( $id, Visualizer_Plugin::CF_CHART_LIBRARY, 'GoogleCharts' ); |
| 69 |
} |
| 70 |
|
| 71 |
// make all dataTable and table chart types into tabular. |
| 72 |
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared |
| 73 |
$wpdb->query( |
| 74 |
"UPDATE $wpdb->postmeta pm, $wpdb->posts p SET pm.meta_value = 'tabular' |
| 75 |
WHERE p.post_type = '" . Visualizer_Plugin::CPT_VISUALIZER . "' |
| 76 |
AND p.id = pm.post_id |
| 77 |
AND pm.meta_key = '" . Visualizer_Plugin::CF_CHART_TYPE . "' |
| 78 |
AND pm.meta_value IN ( 'dataTable', 'table' )" |
| 79 |
); |
| 80 |
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared |
| 81 |
} |
| 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; |
| 118 |
} |
| 119 |
} |
| 120 |
|