PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.3.3
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.3.3
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.3.3, at includes/Services/QuoteExpirationService.php

100 lines 2.8 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 // Find quotes that are past their due date and not already expired
47 $expired_quotes = $wpdb->get_results(
48 $wpdb->prepare(
49 "SELECT p.ID, p.post_title
50 FROM {$wpdb->posts} p
51 INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
52 WHERE p.post_type = 'easy_quote'
53 AND p.post_status IN ('publish', 'draft')
54 AND pm.meta_key = '_easy_invoice_quote_expiry_date'
55 AND pm.meta_value < %s
56 AND p.ID NOT IN (
57 SELECT post_id
58 FROM {$wpdb->postmeta}
59 WHERE meta_key = '_easy_invoice_quote_status'
60 AND meta_value = 'expired'
61 )",
62 $current_date
63 )
64 );
65
66 if ( empty( $expired_quotes ) ) {
67 return;
68 }
69
70 $updated_count = 0;
71
72 foreach ( $expired_quotes as $quote ) {
73 // Update the quote status to expired
74 update_post_meta( $quote->ID, '_easy_invoice_quote_status', 'expired' );
75
76 // Fire integration hook — addons (Webhooks, etc.) subscribe to this.
77 do_action( 'easy_invoice_quote_expired', $quote->ID );
78
79 $updated_count++;
80 }
81 }
82
83 /**
84 * Manually trigger quote expiration check
85 *
86 * @since 2.0.0
87 */
88 public static function manual_check_expired_quotes() {
89 self::check_expired_quotes();
90 }
91
92 /**
93 * Clear the scheduled event on plugin deactivation
94 *
95 * @since 2.0.0
96 */
97 public static function clear_schedule() {
98 wp_clear_scheduled_hook( 'easy_invoice_quote_expiration_check' );
99 }
100 }