@@ -12,108 +12,108 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Daily_Maintenance { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Class constructor. |
|
| 17 | - */ |
|
| 18 | - public function __construct(){ |
|
| 19 | - |
|
| 20 | - // Clear deprecated events. |
|
| 21 | - add_action( 'wp', array( $this, 'maybe_clear_deprecated_events' ) ); |
|
| 22 | - |
|
| 23 | - // (Maybe) schedule a cron that runs daily. |
|
| 24 | - add_action( 'wp', array( $this, 'maybe_create_scheduled_event' ) ); |
|
| 25 | - |
|
| 26 | - // Fired everyday at 7 a.m (this might vary for sites with few visitors) |
|
| 27 | - add_action( 'getpaid_daily_maintenance', array( $this, 'log_cron_run' ) ); |
|
| 28 | - add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) ); |
|
| 29 | - add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) ); |
|
| 30 | - add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_update_geoip_databases' ) ); |
|
| 31 | - |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Schedules a cron to run every day at 7 a.m |
|
| 36 | - * |
|
| 37 | - */ |
|
| 38 | - public function maybe_create_scheduled_event() { |
|
| 39 | - |
|
| 40 | - if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) { |
|
| 41 | - $timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) ); |
|
| 42 | - wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' ); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Clears deprecated events. |
|
| 49 | - * |
|
| 50 | - */ |
|
| 51 | - public function maybe_clear_deprecated_events() { |
|
| 52 | - |
|
| 53 | - if ( ! get_option( 'wpinv_cleared_old_events' ) ) { |
|
| 54 | - wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' ); |
|
| 55 | - wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' ); |
|
| 56 | - update_option( 'wpinv_cleared_old_events', 1 ); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Fires the old hook for backwards compatibility. |
|
| 63 | - * |
|
| 64 | - */ |
|
| 65 | - public function backwards_compat() { |
|
| 66 | - do_action( 'wpinv_register_schedule_event_daily' ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Expires expired subscriptions. |
|
| 71 | - * |
|
| 72 | - */ |
|
| 73 | - public function maybe_expire_subscriptions() { |
|
| 74 | - |
|
| 75 | - // Fetch expired subscriptions (skips those that expire today). |
|
| 76 | - $args = array( |
|
| 77 | - 'number' => -1, |
|
| 78 | - 'count_total' => false, |
|
| 79 | - 'status' => 'trialling active failing cancelled', |
|
| 80 | - 'date_expires_query' => array( |
|
| 81 | - 'before' => 'today', |
|
| 82 | - 'inclusive' => false, |
|
| 83 | - ), |
|
| 84 | - ); |
|
| 85 | - |
|
| 86 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 87 | - |
|
| 88 | - foreach ( $subscriptions->get_results() as $subscription ) { |
|
| 89 | - if ( apply_filters( 'getpaid_daily_maintenance_should_expire_subscription', true, $subscription ) ) { |
|
| 90 | - $subscription->set_status( 'expired' ); |
|
| 91 | - $subscription->save(); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Logs cron runs. |
|
| 99 | - * |
|
| 100 | - */ |
|
| 101 | - public function log_cron_run() { |
|
| 102 | - wpinv_error_log( 'GetPaid Daily Cron', false ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Updates GeoIP databases. |
|
| 107 | - * |
|
| 108 | - */ |
|
| 109 | - public function maybe_update_geoip_databases() { |
|
| 110 | - $updated = get_transient( 'getpaid_updated_geoip_databases' ); |
|
| 111 | - |
|
| 112 | - if ( false === $updated ) { |
|
| 113 | - set_transient( 'getpaid_updated_geoip_databases', 1, 15 * DAY_IN_SECONDS ); |
|
| 114 | - do_action( 'getpaid_update_geoip_databases' ); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - } |
|
| 15 | + /** |
|
| 16 | + * Class constructor. |
|
| 17 | + */ |
|
| 18 | + public function __construct(){ |
|
| 19 | + |
|
| 20 | + // Clear deprecated events. |
|
| 21 | + add_action( 'wp', array( $this, 'maybe_clear_deprecated_events' ) ); |
|
| 22 | + |
|
| 23 | + // (Maybe) schedule a cron that runs daily. |
|
| 24 | + add_action( 'wp', array( $this, 'maybe_create_scheduled_event' ) ); |
|
| 25 | + |
|
| 26 | + // Fired everyday at 7 a.m (this might vary for sites with few visitors) |
|
| 27 | + add_action( 'getpaid_daily_maintenance', array( $this, 'log_cron_run' ) ); |
|
| 28 | + add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) ); |
|
| 29 | + add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) ); |
|
| 30 | + add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_update_geoip_databases' ) ); |
|
| 31 | + |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Schedules a cron to run every day at 7 a.m |
|
| 36 | + * |
|
| 37 | + */ |
|
| 38 | + public function maybe_create_scheduled_event() { |
|
| 39 | + |
|
| 40 | + if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) { |
|
| 41 | + $timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) ); |
|
| 42 | + wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' ); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Clears deprecated events. |
|
| 49 | + * |
|
| 50 | + */ |
|
| 51 | + public function maybe_clear_deprecated_events() { |
|
| 52 | + |
|
| 53 | + if ( ! get_option( 'wpinv_cleared_old_events' ) ) { |
|
| 54 | + wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' ); |
|
| 55 | + wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' ); |
|
| 56 | + update_option( 'wpinv_cleared_old_events', 1 ); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Fires the old hook for backwards compatibility. |
|
| 63 | + * |
|
| 64 | + */ |
|
| 65 | + public function backwards_compat() { |
|
| 66 | + do_action( 'wpinv_register_schedule_event_daily' ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Expires expired subscriptions. |
|
| 71 | + * |
|
| 72 | + */ |
|
| 73 | + public function maybe_expire_subscriptions() { |
|
| 74 | + |
|
| 75 | + // Fetch expired subscriptions (skips those that expire today). |
|
| 76 | + $args = array( |
|
| 77 | + 'number' => -1, |
|
| 78 | + 'count_total' => false, |
|
| 79 | + 'status' => 'trialling active failing cancelled', |
|
| 80 | + 'date_expires_query' => array( |
|
| 81 | + 'before' => 'today', |
|
| 82 | + 'inclusive' => false, |
|
| 83 | + ), |
|
| 84 | + ); |
|
| 85 | + |
|
| 86 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 87 | + |
|
| 88 | + foreach ( $subscriptions->get_results() as $subscription ) { |
|
| 89 | + if ( apply_filters( 'getpaid_daily_maintenance_should_expire_subscription', true, $subscription ) ) { |
|
| 90 | + $subscription->set_status( 'expired' ); |
|
| 91 | + $subscription->save(); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Logs cron runs. |
|
| 99 | + * |
|
| 100 | + */ |
|
| 101 | + public function log_cron_run() { |
|
| 102 | + wpinv_error_log( 'GetPaid Daily Cron', false ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Updates GeoIP databases. |
|
| 107 | + * |
|
| 108 | + */ |
|
| 109 | + public function maybe_update_geoip_databases() { |
|
| 110 | + $updated = get_transient( 'getpaid_updated_geoip_databases' ); |
|
| 111 | + |
|
| 112 | + if ( false === $updated ) { |
|
| 113 | + set_transient( 'getpaid_updated_geoip_databases', 1, 15 * DAY_IN_SECONDS ); |
|
| 114 | + do_action( 'getpaid_update_geoip_databases' ); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | 119 | } |