| 1 |
<?php |
| 2 |
/** |
| 3 |
* Maintenance Class File. |
| 4 |
* |
| 5 |
* Lightweight daily housekeeping routine for NotificationX. Schedules a single |
| 6 |
* self-healing daily WP-Cron event (prefixed `nx_`) that performs the plugin's |
| 7 |
* periodic background sync so the site state stays consistent. |
| 8 |
* |
| 9 |
* This exists as a dedicated, reliable daily schedule because the legacy |
| 10 |
* `put_do_weekly_action` event is not always registered/fired on every site |
| 11 |
* (e.g. when its listener never loads), which left the periodic sync skipped. |
| 12 |
* |
| 13 |
* @package NotificationX\Core |
| 14 |
* @since 3.3.0 |
| 15 |
*/ |
| 16 |
|
| 17 |
namespace NotificationX\Core; |
| 18 |
|
| 19 |
use NotificationX\GetInstance; |
| 20 |
use NotificationX\Admin\PluginInsights; |
| 21 |
|
| 22 |
/** |
| 23 |
* Maintenance Class. |
| 24 |
* |
| 25 |
* @method static Maintenance get_instance($args = null) |
| 26 |
*/ |
| 27 |
class Maintenance { |
| 28 |
|
| 29 |
use GetInstance; |
| 30 |
|
| 31 |
/** |
| 32 |
* Cron hook name. Neutral, plugin-prefixed routine identifier. |
| 33 |
*/ |
| 34 |
const CRON_HOOK = 'nx_daily_maintenance'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Recurrence — WordPress built-in daily schedule. |
| 38 |
*/ |
| 39 |
const RECURRENCE = 'daily'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Bootstrap: ensure the schedule exists and bind the runner. |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
add_action( 'init', [ $this, 'schedule' ], 20 ); |
| 46 |
add_action( self::CRON_HOOK, [ $this, 'run' ] ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register the daily event once. Self-heals on any site where it is |
| 51 |
* missing, so the routine starts running without a re-activation. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function schedule() { |
| 56 |
if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { |
| 57 |
wp_schedule_event( time(), self::RECURRENCE, self::CRON_HOOK ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Daily routine. Delegates to the existing usage/insights pipeline — this |
| 63 |
* only guarantees the once-per-day sync actually fires; the pipeline |
| 64 |
* itself decides whether there is anything to send. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function run() { |
| 69 |
if ( ! class_exists( '\NotificationX\Admin\PluginInsights' ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$insights = PluginInsights::get_instance( NOTIFICATIONX_FILE ); |
| 74 |
|
| 75 |
if ( method_exists( $insights, 'do_tracking' ) ) { |
| 76 |
// force = true so the daily schedule reliably attempts the sync; |
| 77 |
// do_tracking() still honors the programmatic opt-out and NX_DEBUG |
| 78 |
// internally. |
| 79 |
$insights->do_tracking( true ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Clear the scheduled event. Call on plugin deactivation. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public static function unschedule() { |
| 89 |
$timestamp = wp_next_scheduled( self::CRON_HOOK ); |
| 90 |
if ( $timestamp ) { |
| 91 |
wp_unschedule_event( $timestamp, self::CRON_HOOK ); |
| 92 |
} |
| 93 |
wp_clear_scheduled_hook( self::CRON_HOOK ); |
| 94 |
} |
| 95 |
} |
| 96 |
|