| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Cron' ) ) : |
| 7 |
|
| 8 |
final class WPSC_Cron { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
// Add custom cron intervals. |
| 18 |
add_filter( 'cron_schedules', array( __CLASS__, 'custom_interval' ) ); //phpcs:ignore |
| 19 |
|
| 20 |
// Schedule cron jobs. |
| 21 |
add_action( 'init', array( __CLASS__, 'schedule_events' ) ); |
| 22 |
|
| 23 |
// cron event callbacks. |
| 24 |
add_action( 'wpsc_auto_delete_closed_tickets', array( __CLASS__, 'auto_delete_closed_tickets' ) ); |
| 25 |
add_action( 'wpsc_permenently_delete_tickets', array( __CLASS__, 'permenently_delete_tickets' ) ); |
| 26 |
|
| 27 |
// run background processes. |
| 28 |
add_action( 'wp_ajax_wpsc_run_ajax_background_process', array( __CLASS__, 'run_background_process' ) ); |
| 29 |
add_action( 'wp_ajax_nopriv_wpsc_run_ajax_background_process', array( __CLASS__, 'run_background_process' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Custom cron job intervals for SupportCandy |
| 34 |
* |
| 35 |
* @param array $schedules - schedule time. |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public static function custom_interval( $schedules ) { |
| 39 |
|
| 40 |
$schedules['wpsc_1min'] = array( |
| 41 |
'interval' => 60, |
| 42 |
'display' => esc_attr__( 'Every one minute', 'supportcandy' ), |
| 43 |
); |
| 44 |
|
| 45 |
$schedules['wpsc_5min'] = array( |
| 46 |
'interval' => 300, |
| 47 |
'display' => esc_attr__( 'Every five minutes', 'supportcandy' ), |
| 48 |
); |
| 49 |
|
| 50 |
return $schedules; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Schedule cron job events for SupportCandy |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public static function schedule_events() { |
| 59 |
|
| 60 |
// Schedule cron job for every five minute events. |
| 61 |
if ( ! wp_next_scheduled( 'wpsc_cron_five_minute' ) ) { |
| 62 |
wp_schedule_event( |
| 63 |
time(), |
| 64 |
'wpsc_5min', |
| 65 |
'wpsc_cron_five_minute' |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
// Schedule cron job for daily events. |
| 70 |
if ( ! wp_next_scheduled( 'wpsc_cron_daily' ) ) { |
| 71 |
wp_schedule_event( |
| 72 |
self::get_midnight_timestamp(), |
| 73 |
'daily', |
| 74 |
'wpsc_cron_daily' |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
// license checker. |
| 79 |
if ( ! wp_next_scheduled( 'wpsc_license_checker' ) ) { |
| 80 |
wp_schedule_event( |
| 81 |
self::get_midnight_timestamp(), |
| 82 |
'daily', |
| 83 |
'wpsc_license_checker' |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
// Auto-delete closed tickets. |
| 88 |
if ( ! wp_next_scheduled( 'wpsc_auto_delete_closed_tickets' ) ) { |
| 89 |
wp_schedule_event( |
| 90 |
time(), |
| 91 |
'hourly', |
| 92 |
'wpsc_auto_delete_closed_tickets' |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
// Permenently delete tickets. |
| 97 |
if ( ! wp_next_scheduled( 'wpsc_permenently_delete_tickets' ) ) { |
| 98 |
wp_schedule_event( |
| 99 |
time(), |
| 100 |
'hourly', |
| 101 |
'wpsc_permenently_delete_tickets' |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
// Attachment garbage collector. |
| 106 |
if ( ! wp_next_scheduled( 'wpsc_attach_garbage_collector' ) ) { |
| 107 |
wp_schedule_event( |
| 108 |
time(), |
| 109 |
'hourly', |
| 110 |
'wpsc_attach_garbage_collector' |
| 111 |
); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Remove existing scheduled events. |
| 117 |
* Can be used while deactivation of plugin or resetting schedules after an update etc. |
| 118 |
* |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public static function unschedule_events() { |
| 122 |
|
| 123 |
// Remove every five minute cron. |
| 124 |
$timestamp = wp_next_scheduled( 'wpsc_cron_five_minute' ); |
| 125 |
if ( $timestamp ) { |
| 126 |
wp_unschedule_event( $timestamp, 'wpsc_cron_five_minute' ); |
| 127 |
} |
| 128 |
|
| 129 |
// Remove daily cron. |
| 130 |
$timestamp = wp_next_scheduled( 'wpsc_cron_daily' ); |
| 131 |
if ( $timestamp ) { |
| 132 |
wp_unschedule_event( $timestamp, 'wpsc_cron_daily' ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Provide mid-night unix timestamp |
| 138 |
* |
| 139 |
* @return String |
| 140 |
*/ |
| 141 |
public static function get_midnight_timestamp() { |
| 142 |
|
| 143 |
$tz = wp_timezone(); |
| 144 |
$date = new DateTime( 'now', $tz ); |
| 145 |
$date->setTime( 0, 0, 0 ); |
| 146 |
$date->add( new DateInterval( 'P1D' ) ); |
| 147 |
return $date->getTimestamp(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Auto delete closed ticket after x days/months/years |
| 152 |
* |
| 153 |
* @return void |
| 154 |
*/ |
| 155 |
public static function auto_delete_closed_tickets() { |
| 156 |
|
| 157 |
$tz = wp_timezone(); |
| 158 |
$today = new DateTime( 'now', $tz ); |
| 159 |
$transient_label = 'wpsc_auto_delete_closed_tickets_cron_' . $today->format( 'Y-m-d' ); |
| 160 |
$cron_status = get_transient( $transient_label ); |
| 161 |
if ( false === $cron_status ) { |
| 162 |
$cron_status = 'active'; |
| 163 |
} |
| 164 |
|
| 165 |
// return if today's tickets finished checking. |
| 166 |
if ( $cron_status == 'finished' ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$ad_settings = get_option( 'wpsc-tl-ms-advanced' ); |
| 171 |
$ms_settings = get_option( 'wpsc-ms-advanced-settings' ); |
| 172 |
|
| 173 |
if ( ! $ms_settings['auto-delete-tickets-time'] ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$age = clone $today; |
| 178 |
switch ( $ms_settings['auto-delete-tickets-unit'] ) { |
| 179 |
|
| 180 |
case 'days': |
| 181 |
$age->sub( new DateInterval( 'P' . $ms_settings['auto-delete-tickets-time'] . 'D' ) ); |
| 182 |
break; |
| 183 |
|
| 184 |
case 'month': |
| 185 |
$age->sub( new DateInterval( 'P' . $ms_settings['auto-delete-tickets-time'] . 'M' ) ); |
| 186 |
break; |
| 187 |
|
| 188 |
case 'year': |
| 189 |
$age->sub( new DateInterval( 'P' . $ms_settings['auto-delete-tickets-time'] . 'Y' ) ); |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
$tickets = WPSC_Ticket::find( |
| 194 |
array( |
| 195 |
'items_per_page' => 20, |
| 196 |
'orderby' => 'date_closed', |
| 197 |
'order' => 'ASC', |
| 198 |
'meta_query' => array( |
| 199 |
'relation' => 'AND', |
| 200 |
array( |
| 201 |
'slug' => 'status', |
| 202 |
'compare' => 'IN', |
| 203 |
'val' => $ad_settings['closed-ticket-statuses'], |
| 204 |
), |
| 205 |
array( |
| 206 |
'slug' => 'date_closed', |
| 207 |
'compare' => '<', |
| 208 |
'val' => $age->format( 'Y-m-d' ), |
| 209 |
), |
| 210 |
), |
| 211 |
) |
| 212 |
); |
| 213 |
|
| 214 |
// update cron status. |
| 215 |
delete_transient( $transient_label ); |
| 216 |
$cron_status = $tickets['has_next_page'] ? 'active' : 'finished'; |
| 217 |
set_transient( $transient_label, $cron_status, MINUTE_IN_SECONDS * 60 * 24 ); |
| 218 |
|
| 219 |
// delete applicable tickets. |
| 220 |
if ( $tickets['total_items'] > 0 ) { |
| 221 |
foreach ( $tickets['results'] as $ticket ) { |
| 222 |
WPSC_Individual_Ticket::$ticket = $ticket; |
| 223 |
WPSC_Individual_Ticket::delete_ticket(); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Permenently delete tickets after x days/months/years |
| 230 |
* |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
public static function permenently_delete_tickets() { |
| 234 |
|
| 235 |
$tz = wp_timezone(); |
| 236 |
$today = new DateTime( 'now', $tz ); |
| 237 |
$transient_label = 'wpsc_permenently_delete_tickets_cron_' . $today->format( 'Y-m-d' ); |
| 238 |
$cron_status = get_transient( $transient_label ); |
| 239 |
if ( false === $cron_status ) { |
| 240 |
$cron_status = 'active'; |
| 241 |
} |
| 242 |
|
| 243 |
// return if today's tickets finished checking. |
| 244 |
if ( $cron_status == 'finished' ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$ms_settings = get_option( 'wpsc-ms-advanced-settings' ); |
| 249 |
|
| 250 |
if ( ! $ms_settings['permanent-delete-tickets-time'] ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
$age = clone $today; |
| 255 |
switch ( $ms_settings['permanent-delete-tickets-unit'] ) { |
| 256 |
|
| 257 |
case 'days': |
| 258 |
$age->sub( new DateInterval( 'P' . $ms_settings['permanent-delete-tickets-time'] . 'D' ) ); |
| 259 |
break; |
| 260 |
|
| 261 |
case 'month': |
| 262 |
$age->sub( new DateInterval( 'P' . $ms_settings['permanent-delete-tickets-time'] . 'M' ) ); |
| 263 |
break; |
| 264 |
|
| 265 |
case 'year': |
| 266 |
$age->sub( new DateInterval( 'P' . $ms_settings['permanent-delete-tickets-time'] . 'Y' ) ); |
| 267 |
break; |
| 268 |
} |
| 269 |
|
| 270 |
$tickets = WPSC_Ticket::find( |
| 271 |
array( |
| 272 |
'items_per_page' => 5, |
| 273 |
'orderby' => 'date_closed', |
| 274 |
'order' => 'ASC', |
| 275 |
'is_active' => 0, |
| 276 |
'meta_query' => array( |
| 277 |
'relation' => 'AND', |
| 278 |
array( |
| 279 |
'slug' => 'date_updated', |
| 280 |
'compare' => '<', |
| 281 |
'val' => $age->format( 'Y-m-d' ), |
| 282 |
), |
| 283 |
), |
| 284 |
) |
| 285 |
); |
| 286 |
|
| 287 |
// update cron status. |
| 288 |
delete_transient( $transient_label ); |
| 289 |
$cron_status = $tickets['has_next_page'] ? 'active' : 'finished'; |
| 290 |
set_transient( $transient_label, $cron_status, MINUTE_IN_SECONDS * 60 * 24 ); |
| 291 |
|
| 292 |
// delete applicable tickets. |
| 293 |
if ( $tickets['total_items'] > 0 ) { |
| 294 |
foreach ( $tickets['results'] as $ticket ) { |
| 295 |
WPSC_Individual_Ticket::$ticket = $ticket; |
| 296 |
WPSC_Individual_Ticket::delete_permanently(); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Execute background processes |
| 303 |
* |
| 304 |
* @return void |
| 305 |
*/ |
| 306 |
public static function run_background_process() { |
| 307 |
|
| 308 |
do_action( 'wpsc_run_ajax_background_process' ); |
| 309 |
wp_die(); |
| 310 |
} |
| 311 |
} |
| 312 |
endif; |
| 313 |
|
| 314 |
WPSC_Cron::init(); |
| 315 |
|