PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.9
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.9
4.0.9 4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 All 150 releases
← All changes | classes/Visualizer/Module/Setup.php +220 -24 3.10.15 → 4.0.9 View file →
@@ -31,8 +31,28 @@
31 31
32 32 const NAME = __CLASS__;
33 33
34 34 /**
35 + * Hook that refreshes database charts.
36 + */
37 + const REFRESH_DB_HOOK = 'visualizer_schedule_refresh_db';
38 +
39 + /**
40 + * Action Scheduler group that owns the refresh.
41 + */
42 + const REFRESH_DB_GROUP = 'visualizer';
43 +
44 + /**
45 + * Marks the refresh trigger as checked recently.
46 + */
47 + const REFRESH_DB_CHECK_TRANSIENT = 'visualizer-refresh-db-checked';
48 +
49 + /**
50 + * Seconds a check stays valid; matches Action Scheduler's timeout for a killed run.
51 + */
52 + const REFRESH_DB_CHECK_WINDOW = 300;
53 +
54 + /**
35 55 * Constructor.
36 56 *
37 57 * @since 1.0.0
38 58 *
@@ -43,13 +63,16 @@
43 63 parent::__construct( $plugin );
44 64
45 65 register_activation_hook( VISUALIZER_BASEFILE, array( $this, 'activate' ) );
46 66 register_deactivation_hook( VISUALIZER_BASEFILE, array( $this, 'deactivate' ) );
47 - $this->_addAction( 'visualizer_schedule_refresh_db', 'refreshDbChart' );
67 + $this->_addAction( self::REFRESH_DB_HOOK, 'refreshDbChart' );
68 + $this->_addAction( 'init', 'maybe_reschedule_refresh_db' );
69 + $this->_addAction( 'action_scheduler_ensure_recurring_actions', 'ensure_refresh_db_action' );
48 70 $this->_addFilter( 'visualizer_schedule_refresh_chart', 'refresh_db_for_chart', 10, 3 );
49 71
50 72 $this->_addAction( 'admin_init', 'adminInit' );
51 73 $this->_addAction( 'init', 'setupCustomPostTypes' );
74 + $this->_addFilter( 'cron_schedules', 'custom_cron_schedules' );
52 75 $this->_addAction( 'plugins_loaded', 'loadTextDomain' );
53 76 $this->_addFilter( 'visualizer_logger_data', 'getLoggerData' );
54 77 $this->_addFilter( 'visualizer_get_chart_counts', 'getUsage', 10, 2 );
55 78
@@ -111,9 +134,9 @@
111 134 $charts['sources'][ $source ] = isset( $charts['sources'][ $source ] ) ? $charts['sources'][ $source ] + 1 : 1;
112 135 $lib = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
113 136 $charts['library'][ $lib ] = isset( $charts['library'][ $lib ] ) ? $charts['library'][ $lib ] + 1 : 1;
114 137 $settings = get_post_meta( $chart_id, Visualizer_Plugin::CF_SETTINGS, true );
115 - if ( array_key_exists( 'manual', $settings ) && ! empty( $settings['manual'] ) ) {
138 + if ( is_array( $settings ) && ! empty( $settings['manual'] ) ) {
116 139 $charts['manual_config'] = $charts['manual_config'] + 1;
117 140 }
118 141
119 142 // phpcs:ignore WordPress.PHP.StrictInArray.FoundNonStrictFalse
@@ -121,10 +144,10 @@
121 144 $charts['scheduled'] = $charts['scheduled'] + 1;
122 145 }
123 146
124 147 if ( Visualizer_Module::is_pro() ) {
125 - $permissions = get_post_meta( $chart_id, Visualizer_PRO::CF_PERMISSIONS, true );
126 - if ( empty( $permissions ) ) {
148 + $permissions = get_post_meta( $chart_id, Visualizer_Pro::CF_PERMISSIONS, true );
149 + if ( ! is_array( $permissions ) || empty( $permissions['permissions'] ) || ! is_array( $permissions['permissions'] ) ) {
127 150 continue;
128 151 }
129 152 $permissions = $permissions['permissions'];
130 153 $customized = false;
@@ -130,9 +153,9 @@
130 153 $customized = false;
131 154 foreach ( $default_perms as $key => $val ) {
132 155 if ( ! is_array( $val ) && ! is_null( $val ) && isset( $permissions[ $key ] ) && $permissions[ $key ] !== $val ) {
133 156 $customized = true;
134 - } elseif ( is_array( $val ) && ! is_null( $val ) && isset( $permissions[ $key ] ) && count( $permissions[ $key ] ) !== count( $val ) ) {
157 + } elseif ( is_array( $val ) && ! is_null( $val ) && isset( $permissions[ $key ] ) && is_array( $permissions[ $key ] ) && count( $permissions[ $key ] ) !== count( $val ) ) {
135 158 $customized = true;
136 159 }
137 160 }
138 161 if ( $customized ) {
@@ -207,13 +230,12 @@
207 230 /**
208 231 * Activates the plugin on a particular blog instance (supports multisite and single site).
209 232 */
210 233 private function activate_on_site() {
211 - wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' );
212 - wp_schedule_event( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS, apply_filters( 'visualizer_chart_schedule_interval', 'hourly' ), 'visualizer_schedule_refresh_db' );
234 + $this->schedule_refresh_db_action();
213 235 add_option( 'visualizer-activated', true );
214 236 $is_fresh_install = get_option( 'visualizer_fresh_install', false );
215 - if ( ! defined( 'TI_CYPRESS_TESTING' ) && false === $is_fresh_install ) {
237 + if ( ! defined( 'TI_E2E_TESTING' ) && false === $is_fresh_install ) {
216 238 update_option( 'visualizer_fresh_install', '1' );
217 239 }
218 240 }
219 241
@@ -235,9 +257,9 @@
235 257 /**
236 258 * Deactivates the plugin on a particular blog instance (supports multisite and single site).
237 259 */
238 260 private function deactivate_on_site() {
239 - wp_clear_scheduled_hook( 'visualizer_schedule_refresh_db' );
261 + $this->unschedule_refresh_db_action();
240 262 delete_option( 'visualizer-activated', true );
241 263 }
242 264
243 265 /**
@@ -251,12 +273,23 @@
251 273 define( 'VISUALIZER_SURVEY', Visualizer_Module::is_pro() ? 'https://forms.gle/7Zo7FuZbvQ8DTvRi6' : 'https://forms.gle/muMtbcyvHn1aTvmJ7' );
252 274 // fire any upgrades necessary.
253 275 Visualizer_Module_Upgrade::upgrade();
254 276
255 - if ( get_option( 'visualizer-activated' ) ) {
277 + $activated_flag = get_option( 'visualizer-activated' );
278 + $fresh_install = get_option( 'visualizer_fresh_install', false );
279 + $is_pro = Visualizer_Module::is_pro();
280 + if ( $activated_flag ) {
281 + if ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) {
282 + // Defer redirect until a normal admin request.
283 + return;
284 + }
285 + if ( wp_doing_cron() ) {
286 + // Defer redirect during cron requests.
287 + return;
288 + }
256 289 delete_option( 'visualizer-activated' );
257 290 if ( ! headers_sent() ) {
258 - if ( ! Visualizer_Module::is_pro() && ! empty( get_option( 'visualizer_fresh_install', false ) ) ) {
291 + if ( ! $is_pro && ! empty( $fresh_install ) ) {
259 292 $redirect_url = array(
260 293 'page' => 'visualizer-setup-wizard',
261 294 'tab' => '#step-1',
262 295 );
@@ -280,11 +313,11 @@
280 313
281 314 /**
282 315 * Refresh the specific chart from the db.
283 316 *
284 - * @param WP_Post $chart The chart object.
285 - * @param int $chart_id The chart id.
286 - * @param bool $force If this is true, then the chart data will be force refreshed. If false, data will be refreshed only if the chart requests live data.
317 + * @param WP_Post|null $chart The chart object.
318 + * @param int $chart_id The chart id.
319 + * @param bool $force If this is true, then the chart data will be force refreshed. If false, data will be refreshed only if the chart requests live data.
287 320 *
288 321 * @access public
289 322 */
290 323 public function refresh_db_for_chart( $chart, $chart_id, $force = false ) {
@@ -390,17 +423,19 @@
390 423 return $chart;
391 424 }
392 425
393 426 /**
394 - * Refresh the db chart.
427 + * Refresh the Database chart type.
395 428 *
396 429 * @access public
397 430 */
398 431 public function refreshDbChart() {
399 - $schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
400 - if ( ! $schedules ) {
432 + // NOTE: This use a different key from normal schedule. Updated only by Database chart. Check `visualizer_schedule_import` action.
433 + $chart_schedules = get_option( Visualizer_Plugin::CF_DB_SCHEDULE, array() );
434 + if ( ! $chart_schedules ) {
401 435 return;
402 436 }
437 +
403 438 if ( ! defined( 'VISUALIZER_DO_NOT_DIE' ) ) {
404 439 // define this so that the ajax call does not die
405 440 // this means that if the new version of pro and the old version of free are installed, only the first chart will be updated
406 441 define( 'VISUALIZER_DO_NOT_DIE', true );
@@ -406,24 +441,33 @@
406 441 define( 'VISUALIZER_DO_NOT_DIE', true );
407 442 }
408 443
409 444 $new_schedules = array();
410 - $now = time();
411 - foreach ( $schedules as $chart_id => $time ) {
412 - $new_schedules[ $chart_id ] = $time;
413 - if ( $time > $now ) {
445 + $current_time = time();
446 + foreach ( $chart_schedules as $chart_id => $scheduled_time ) {
447 +
448 + // Skip deleted charts.
449 + if ( false === get_post_status( $chart_id ) ) {
414 450 continue;
415 451 }
416 452
417 - // if the time is nigh, we force an update.
453 + $new_schedules[ $chart_id ] = $scheduled_time;
454 +
455 + // Should we do an update?
456 + if ( $scheduled_time > $current_time ) {
457 + continue;
458 + }
459 +
418 460 $this->refresh_db_for_chart( null, $chart_id, true );
461 +
419 462 // Clear existing chart cache.
420 463 $cache_key = Visualizer_Plugin::CF_CHART_CACHE . '_' . $chart_id;
421 464 if ( get_transient( $cache_key ) ) {
422 465 delete_transient( $cache_key );
423 466 }
424 - $hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
425 - $new_schedules[ $chart_id ] = time() + $hours * HOUR_IN_SECONDS;
467 +
468 + $scheduled_hours = get_post_meta( $chart_id, Visualizer_Plugin::CF_DB_SCHEDULE, true );
469 + $new_schedules[ $chart_id ] = $current_time + $scheduled_hours * HOUR_IN_SECONDS;
426 470 }
427 471 update_option( Visualizer_Plugin::CF_DB_SCHEDULE, $new_schedules );
428 472 }
429 473
@@ -442,5 +486,157 @@
442 486 update_option( 'visualizer-new-user', ! empty( $charts ) ? 'no' : 'yes' );
443 487 }
444 488 }
445 489
490 + /**
491 + * Add custom cron schedules.
492 + *
493 + * @param array $schedules The current schedules options.
494 + * @return array The modified schedules options.
495 + */
496 + public function custom_cron_schedules( $schedules ) {
497 + $schedules['visualizer_ten_minutes'] = array(
498 + 'interval' => 600,
499 + 'display' => __( 'Every 10 minutes', 'visualizer' ),
500 + );
501 +
502 + return $schedules;
503 + }
504 +
505 + /**
506 + * Schedule the recurring DB refresh action.
507 + */
508 + private function schedule_refresh_db_action(): void {
509 + $hook = self::REFRESH_DB_HOOK;
510 + $group = self::REFRESH_DB_GROUP;
511 + $schedules = wp_get_schedules();
512 + $interval_key = apply_filters( 'visualizer_chart_schedule_interval', 'visualizer_ten_minutes' );
513 +
514 + // wp_schedule_event() refuses an unregistered schedule.
515 + if ( ! isset( $schedules[ $interval_key ]['interval'] ) ) {
516 + $interval_key = 'visualizer_ten_minutes';
517 + }
518 +
519 + $interval = isset( $schedules[ $interval_key ]['interval'] ) ? (int) $schedules[ $interval_key ]['interval'] : 600;
520 + // gmt_offset can be fractional, and WP-Cron keys its array by this value.
521 + $timestamp = (int) ( strtotime( 'midnight' ) - get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
522 +
523 + // West of UTC that midnight is still ahead; start from the previous one.
524 + if ( $timestamp > time() ) {
525 + $timestamp -= DAY_IN_SECONDS;
526 + }
527 +
528 + if (
529 + visualizer_can_use_action_scheduler()
530 + && function_exists( 'as_next_scheduled_action' )
531 + && function_exists( 'as_schedule_recurring_action' )
532 + ) {
533 + $next = as_next_scheduled_action( $hook, array(), $group );
534 + if ( false === $next ) {
535 + // Unique: a concurrent request can arrive while nothing is pending.
536 + as_schedule_recurring_action( $timestamp, $interval, $hook, array(), $group, true );
537 +
538 + // Returns 0 on failure, so ask the store.
539 + $next = as_next_scheduled_action( $hook, array(), $group );
540 + }
541 +
542 + // Drop the WP-Cron fallback only once the action exists.
543 + if ( false !== $next ) {
544 + wp_clear_scheduled_hook( $hook );
545 + return;
546 + }
547 + }
548 +
549 + // Re-arming a live event would pin it to a past timestamp and keep it due.
550 + $event = wp_get_scheduled_event( $hook );
551 + if ( $event && $event->schedule === $interval_key ) {
552 + return;
553 + }
554 +
555 + // Schedule first so a refused replacement keeps the old event, then remove the old one
556 + // by its timestamp: wp_clear_scheduled_hook() would take the new one too.
557 + if ( false === wp_schedule_event( $timestamp, $interval_key, $hook ) ) {
558 + return;
559 + }
560 +
561 + // A matching timestamp was already overwritten in place.
562 + if ( $event && $event->timestamp !== $timestamp ) {
563 + wp_unschedule_event( $event->timestamp, $hook );
564 + }
565 + }
566 +
567 + /**
568 + * Check once per window, on init, that something still fires the refresh.
569 + */
570 + public function maybe_reschedule_refresh_db(): void {
571 + if ( get_transient( self::REFRESH_DB_CHECK_TRANSIENT ) ) {
572 + return;
573 + }
574 +
575 + $this->ensure_refresh_db_action();
576 +
577 + // Cache only a check that left a trigger; a failed one retries next request.
578 + if ( $this->has_refresh_db_trigger() ) {
579 + set_transient( self::REFRESH_DB_CHECK_TRANSIENT, 1, self::REFRESH_DB_CHECK_WINDOW );
580 + }
581 + }
582 +
583 + /**
584 + * Keep the DB refresh scheduled.
585 + *
586 + * A killed run never reaches schedule_next_instance(), so Action Scheduler's chain ends there.
587 + */
588 + public function ensure_refresh_db_action(): void {
589 + if ( ! $this->refresh_db_is_settled() ) {
590 + $this->schedule_refresh_db_action();
591 + }
592 + }
593 +
594 + /**
595 + * Whether the refresh is on Action Scheduler with no WP-Cron event beside it.
596 + *
597 + * @return bool
598 + */
599 + private function refresh_db_is_settled(): bool {
600 + $hook = self::REFRESH_DB_HOOK;
601 +
602 + if (
603 + visualizer_can_use_action_scheduler()
604 + && function_exists( 'as_next_scheduled_action' )
605 + && function_exists( 'as_schedule_recurring_action' )
606 + ) {
607 + return false !== as_next_scheduled_action( $hook, array(), self::REFRESH_DB_GROUP )
608 + && ! wp_next_scheduled( $hook );
609 + }
610 +
611 + return (bool) wp_next_scheduled( $hook );
612 + }
613 +
614 + /**
615 + * Whether anything will fire the refresh hook again.
616 + *
617 + * @return bool
618 + */
619 + private function has_refresh_db_trigger(): bool {
620 + $hook = self::REFRESH_DB_HOOK;
621 +
622 + if ( visualizer_can_use_action_scheduler() && function_exists( 'as_next_scheduled_action' ) ) {
623 + if ( false !== as_next_scheduled_action( $hook, array(), self::REFRESH_DB_GROUP ) ) {
624 + return true;
625 + }
626 + }
627 +
628 + return (bool) wp_next_scheduled( $hook );
629 + }
630 +
631 + /**
632 + * Unschedule the recurring DB refresh action.
633 + */
634 + private function unschedule_refresh_db_action(): void {
635 + $hook = self::REFRESH_DB_HOOK;
636 + $group = self::REFRESH_DB_GROUP;
637 + if ( function_exists( 'as_unschedule_all_actions' ) ) {
638 + as_unschedule_all_actions( $hook, array(), $group );
639 + }
640 + wp_clear_scheduled_hook( $hook );
641 + }
446 642 }