5 * MINUTE_IN_SECONDS, 'display' => __( 'Every 5 minutes (TableEngine)', 'tableengine' ), ); return $schedules; } public static function schedule() { if ( ! wp_next_scheduled( 'tableengine_release_holds' ) ) { wp_schedule_event( time() + 60, 'tableengine_5min', 'tableengine_release_holds' ); } if ( ! wp_next_scheduled( 'tableengine_send_reminders' ) ) { wp_schedule_event( time() + 120, 'hourly', 'tableengine_send_reminders' ); } } public static function unschedule() { wp_clear_scheduled_hook( 'tableengine_release_holds' ); wp_clear_scheduled_hook( 'tableengine_send_reminders' ); wp_clear_scheduled_hook( 'tableengine_backfill_guests' ); } /** * Expire bookings whose checkout hold lapsed and cancel their pending payments. */ public static function release_holds() { global $wpdb; $t = Install::table( 'reservations' ); $rows = $wpdb->get_results( // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. $wpdb->prepare( "SELECT * FROM {$t} WHERE status = 'pending_payment' AND hold_expires_gmt < %s LIMIT 100", current_time( 'mysql', true ) ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Only the internal table name is interpolated. ARRAY_A ); foreach ( $rows as $row ) { $gateway = Payments::for_reservation( $row ); if ( $gateway && $gateway->on_hold_expired( $row ) ) { continue; } Reservations::update( $row['id'], array( 'payment_status' => 'cancelled' ) ); Reservations::set_status( $row['id'], 'expired', false ); } } public static function send_reminders() { // Extensions with their own reminder channels (e.g. SMS) can keep this running when email reminders are off. if ( ! apply_filters( 'tableengine_reminders_enabled', (bool) Settings::get( 'email_reminder_on' ) ) ) { return; } global $wpdb; $t = Install::table( 'reservations' ); $now = new \DateTimeImmutable( 'now', wp_timezone() ); $until = $now->modify( '+' . (int) Settings::get( 'reminder_hours' ) . ' hours' ); $rows = $wpdb->get_results( // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. $wpdb->prepare( "SELECT * FROM {$t} WHERE status = 'confirmed' AND reminder_sent = 0 AND start_at > %s AND start_at <= %s LIMIT 200", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Only the internal table name is interpolated. $now->format( 'Y-m-d H:i:s' ), $until->format( 'Y-m-d H:i:s' ) ), ARRAY_A ); foreach ( $rows as $row ) { // Don't remind people who booked within the reminder window minutes ago. if ( strtotime( $row['created_gmt'] . ' UTC' ) > time() - 2 * HOUR_IN_SECONDS ) { Reservations::update( $row['id'], array( 'reminder_sent' => 1 ) ); continue; } Notifications::dispatch( 'reminder', $row ); Reservations::update( $row['id'], array( 'reminder_sent' => 1 ) ); } } }