300, PostaraMail_Sync::HOOK => 180, PostaraMail_Pending::HOOK => 120, ); foreach ( $events as $hook => $delay ) { if ( ! wp_next_scheduled( $hook ) ) { wp_schedule_event( time() + $delay, 'postaramail_15min', $hook ); } } } public static function schedules( $schedules ) { $schedules['postaramail_15min'] = array( 'interval' => 15 * MINUTE_IN_SECONDS, 'display' => 'Every 15 minutes (PostaraMail)', // Plain text on purpose: cron may ask for schedules before translations load. ); return $schedules; } public static function activate() { // Nothing to do here: ensure_events() schedules the tasks on the next page load, after translations are available. } public static function deactivate() { wp_clear_scheduled_hook( self::HOOK ); } public static function all() { $q = get_option( self::OPTION, array() ); return is_array( $q ) ? $q : array(); } public static function count() { return count( self::all() ); } public static function push( $item ) { $q = self::all(); if ( count( $q ) >= self::MAX_ITEM ) { array_shift( $q ); // Drop the oldest rather than growing forever. } $item['tries'] = 0; $item['added'] = time(); $q[] = $item; update_option( self::OPTION, $q, false ); } /** Queue now and ask WordPress to run cron soon (used where blocking would be rude, e.g. checkout). */ public static function push_and_spawn( $item ) { self::push( $item ); wp_schedule_single_event( time() + 5, self::HOOK ); if ( function_exists( 'spawn_cron' ) ) { spawn_cron(); } } public static function process() { if ( get_transient( 'postaramail_queue_lock' ) ) { return; } set_transient( 'postaramail_queue_lock', 1, 120 ); $queue = self::all(); $keep = array(); $done = 0; foreach ( $queue as $item ) { if ( $done >= 25 ) { $keep[] = $item; // Next run. continue; } ++$done; $r = PostaraMail_Subscriber::send( $item ); if ( $r['ok'] ) { continue; // Delivered. } if ( $r['retry'] ) { $item['tries'] = (int) $item['tries'] + 1; $too_old = ( time() - (int) $item['added'] ) > 3 * DAY_IN_SECONDS; if ( $item['tries'] >= self::MAX_TRY || $too_old ) { PostaraMail_Subscriber::remember_error( $r['message'] ? $r['message'] : $r['code'] ); continue; // Give up on this one. } $keep[] = $item; continue; } // Permanent answer from the API (for example the plan limit): don't retry, but tell the admin. PostaraMail_Subscriber::remember_error( $r['message'] ? $r['message'] : $r['code'] ); } update_option( self::OPTION, $keep, false ); delete_transient( 'postaramail_queue_lock' ); } }