get_col( $wpdb->prepare( "SELECT id FROM {$t['orders']} WHERE status = 'pending' AND reminded_at IS NULL AND pay_deadline IS NOT NULL AND pay_deadline > %s AND pay_deadline <= %s AND created_at <= %s LIMIT 100", paygrab_now(), paygrab_now( DAY_IN_SECONDS ), paygrab_now( -12 * HOUR_IN_SECONDS ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared foreach ( $ids as $id ) { $order = Paygrab_Orders::get( $id ); if ( $order && $order->is_pending() ) { // Only the run that marks the order sends the reminder (WP-Cron and a server cron may overlap). $marked = $wpdb->query( $wpdb->prepare( "UPDATE {$t['orders']} SET reminded_at = %s WHERE id = %d AND reminded_at IS NULL", paygrab_now(), $order->id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- the fixed table name of this plugin. if ( 1 === (int) $marked ) { Paygrab_Mailer::reminder( $order ); } } } } /** * Cancel orders whose payment deadline has passed. * * @return void */ public static function cancel_expired() { global $wpdb; $t = Paygrab_Install::tables(); $ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$t['orders']} WHERE status = 'pending' AND pay_deadline IS NOT NULL AND pay_deadline < %s LIMIT 200", paygrab_now() ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared foreach ( $ids as $id ) { $order = Paygrab_Orders::get( $id ); if ( $order ) { Paygrab_Orders::set_status( $order, 'cancelled', array( 'actor' => 0, 'note' => __( '(The payment deadline passed.)', 'paygrab' ), 'by' => 'expired', ) ); } } // Orders that never got a deadline (the payment was not started) are dropped after a day. $stale = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$t['orders']} WHERE status = 'pending' AND pay_deadline IS NULL AND created_at < %s LIMIT 200", paygrab_now( -DAY_IN_SECONDS ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared foreach ( $stale as $id ) { $order = Paygrab_Orders::get( $id ); if ( $order ) { Paygrab_Orders::set_status( $order, 'failed', array( 'actor' => 0, 'note' => __( '(The payment was not completed.)', 'paygrab' ), 'notify' => false, ) ); } } } }