| @@ -42,24 +42,25 @@ | ||
| 42 | 42 | |
| 43 | 43 | // Get current date in Y-m-d format |
| 44 | 44 | $current_date = current_time( 'Y-m-d' ); |
| 45 | 45 | |
| 46 | - // Find quotes that are past their due date and not already expired | |
| 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. | |
| 47 | 50 | $expired_quotes = $wpdb->get_results( |
| 48 | 51 | $wpdb->prepare( |
| 49 | 52 | "SELECT p.ID, p.post_title |
| 50 | 53 | FROM {$wpdb->posts} p |
| 51 | 54 | INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id |
| 52 | - WHERE p.post_type = 'easy_quote' | |
| 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 | |
| 53 | 57 | AND p.post_status IN ('publish', 'draft') |
| 54 | 58 | AND pm.meta_key = '_easy_invoice_quote_expiry_date' |
| 59 | + AND pm.meta_value <> '' | |
| 55 | 60 | 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 | - )", | |
| 61 | + AND ps.meta_value IN ('draft', 'available', 'sent')", | |
| 62 | + \EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE, | |
| 62 | 63 | $current_date |
| 63 | 64 | ) |
| 64 | 65 | ); |
| 65 | 66 | |
| @@ -70,10 +71,13 @@ | ||
| 70 | 71 | $updated_count = 0; |
| 71 | 72 | |
| 72 | 73 | foreach ( $expired_quotes as $quote ) { |
| 73 | 74 | // Update the quote status to expired |
| 74 | - update_post_meta( $quote->ID, '_easy_invoice_quote_status', 'expired' ); | |
| 75 | - | |
| 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 | + | |
| 76 | 80 | $updated_count++; |
| 77 | 81 | } |
| 78 | 82 | } |
| 79 | 83 | |