PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.4.1
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.4.1
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Services / QuoteExpirationService.php

QuoteExpirationService.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.4.1, at includes/Services/QuoteExpirationService.php

101 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EasyInvoice\Services;
4
5 /**
6 * Quote Expiration Service
7 *
8 * Handles background process for checking and updating expired quotes
9 *
10 * @since 2.0.0
11 */
12 class QuoteExpirationService {
13
14 /**
15 * Initialize the service
16 *
17 * @since 2.0.0
18 */
19 public static function init() {
20 add_action( 'easy_invoice_quote_expiration_check', [ __CLASS__, 'check_expired_quotes' ] );
21 add_action( 'init', [ __CLASS__, 'schedule_expiration_check' ] );
22 }
23
24 /**
25 * Schedule the quote expiration check
26 *
27 * @since 2.0.0
28 */
29 public static function schedule_expiration_check() {
30 if ( ! wp_next_scheduled( 'easy_invoice_quote_expiration_check' ) ) {
31 wp_schedule_event( time(), 'daily', 'easy_invoice_quote_expiration_check' );
32 }
33 }
34
35 /**
36 * Check for expired quotes and update their status
37 *
38 * @since 2.0.0
39 */
40 public static function check_expired_quotes() {
41 global $wpdb;
42
43 // Get current date in Y-m-d format
44 $current_date = current_time( 'Y-m-d' );
45
46 // Quotes still open (draft, available, sent) whose expiry date has passed.
47 // Accepted, declined, converted and cancelled quotes are left alone —
48 // the old query (which also named a post type that does not exist)
49 // would have expired an accepted quote the day after its date.
50 $expired_quotes = $wpdb->get_results(
51 $wpdb->prepare(
52 "SELECT p.ID, p.post_title
53 FROM {$wpdb->posts} p
54 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
55 INNER JOIN {$wpdb->postmeta} ps ON p.ID = ps.post_id AND ps.meta_key = '_easy_invoice_quote_status'
56 WHERE p.post_type = %s
57 AND p.post_status IN ('publish', 'draft')
58 AND pm.meta_key = '_easy_invoice_quote_expiry_date'
59 AND pm.meta_value <> ''
60 AND pm.meta_value < %s
61 AND ps.meta_value IN ('draft', 'available', 'sent')",
62 \EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE,
63 $current_date
64 )
65 );
66
67 if ( empty( $expired_quotes ) ) {
68 return;
69 }
70
71 $updated_count = 0;
72
73 foreach ( $expired_quotes as $quote ) {
74 // Update the quote status to expired
75 update_post_meta( $quote->ID, '_easy_invoice_quote_status', 'expired' );
76
77 // Fire integration hook — addons (Webhooks, etc.) subscribe to this.
78 do_action( 'easy_invoice_quote_expired', $quote->ID );
79
80 $updated_count++;
81 }
82 }
83
84 /**
85 * Manually trigger quote expiration check
86 *
87 * @since 2.0.0
88 */
89 public static function manual_check_expired_quotes() {
90 self::check_expired_quotes();
91 }
92
93 /**
94 * Clear the scheduled event on plugin deactivation
95 *
96 * @since 2.0.0
97 */
98 public static function clear_schedule() {
99 wp_clear_scheduled_hook( 'easy_invoice_quote_expiration_check' );
100 }
101 }