|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class Google\Site_Kit\Core\Email_Reporting\Email_Log_Batch_Query |
| 4 | + * |
| 5 | + * @package Google\Site_Kit\Core\Email_Reporting |
| 6 | + * @copyright 2025 Google LLC |
| 7 | + * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 8 | + * @link https://sitekit.withgoogle.com |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Google\Site_Kit\Core\Email_Reporting; |
| 12 | + |
| 13 | +use WP_Query; |
| 14 | + |
| 15 | +/** |
| 16 | + * Helper for querying and updating email log batches. |
| 17 | + * |
| 18 | + * @since n.e.x.t |
| 19 | + * @access private |
| 20 | + * @ignore |
| 21 | + */ |
| 22 | +class Email_Log_Batch_Query { |
| 23 | + |
| 24 | + const MAX_ATTEMPTS = 3; |
| 25 | + |
| 26 | + /** |
| 27 | + * Retrieves IDs for pending logs within a batch. |
| 28 | + * |
| 29 | + * @since n.e.x.t |
| 30 | + * |
| 31 | + * @param string $batch_id Batch identifier. |
| 32 | + * @param int $max_attempts Maximum delivery attempts allowed. |
| 33 | + * @return array Pending post IDs that still require processing. |
| 34 | + */ |
| 35 | + public function get_pending_ids( $batch_id, $max_attempts = self::MAX_ATTEMPTS ) { |
| 36 | + $batch_id = (string) $batch_id; |
| 37 | + $max_attempts = (int) $max_attempts; |
| 38 | + |
| 39 | + $query = $this->get_batch_query( $batch_id ); |
| 40 | + |
| 41 | + $pending_ids = array(); |
| 42 | + |
| 43 | + foreach ( $query->posts as $post_id ) { |
| 44 | + $status = get_post_status( $post_id ); |
| 45 | + |
| 46 | + if ( Email_Log::STATUS_SENT === $status ) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + if ( Email_Log::STATUS_FAILED === $status ) { |
| 51 | + $attempts = (int) get_post_meta( $post_id, Email_Log::META_SEND_ATTEMPTS, true ); |
| 52 | + |
| 53 | + if ( $attempts >= $max_attempts ) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $pending_ids[] = (int) $post_id; |
| 59 | + } |
| 60 | + |
| 61 | + return $pending_ids; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Builds a batch query object limited to a specific batch ID. |
| 66 | + * |
| 67 | + * @since n.e.x.t |
| 68 | + * |
| 69 | + * @param string $batch_id Batch identifier. |
| 70 | + * @return WP_Query Query returning IDs only. |
| 71 | + */ |
| 72 | + private function get_batch_query( $batch_id ) { |
| 73 | + return new WP_Query( |
| 74 | + array( |
| 75 | + 'post_type' => Email_Log::POST_TYPE, |
| 76 | + 'post_status' => array( |
| 77 | + Email_Log::STATUS_SCHEDULED, |
| 78 | + Email_Log::STATUS_SENT, |
| 79 | + Email_Log::STATUS_FAILED, |
| 80 | + ), |
| 81 | + 'posts_per_page' => -1, |
| 82 | + 'fields' => 'ids', |
| 83 | + 'no_found_rows' => true, |
| 84 | + 'update_post_meta_cache' => false, |
| 85 | + 'update_post_term_cache' => false, |
| 86 | + // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query |
| 87 | + 'meta_query' => array( |
| 88 | + array( |
| 89 | + 'key' => Email_Log::META_BATCH_ID, |
| 90 | + 'value' => $batch_id, |
| 91 | + ), |
| 92 | + ), |
| 93 | + ) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Determines whether all posts in the batch completed delivery. |
| 99 | + * |
| 100 | + * @since n.e.x.t |
| 101 | + * |
| 102 | + * @param string $batch_id Batch identifier. |
| 103 | + * @param int $max_attempts Maximum delivery attempts allowed. |
| 104 | + * @return bool True if the batch has no remaining pending posts. |
| 105 | + */ |
| 106 | + public function is_complete( $batch_id, $max_attempts = self::MAX_ATTEMPTS ) { |
| 107 | + return empty( $this->get_pending_ids( $batch_id, $max_attempts ) ); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Increments the send attempt counter for a log post. |
| 112 | + * |
| 113 | + * @since n.e.x.t |
| 114 | + * |
| 115 | + * @param int $post_id Log post ID. |
| 116 | + * @return void Nothing returned. |
| 117 | + */ |
| 118 | + public function increment_attempt( $post_id ) { |
| 119 | + $post = get_post( $post_id ); |
| 120 | + |
| 121 | + if ( ! $post || Email_Log::POST_TYPE !== $post->post_type ) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + $current_attempts = (int) get_post_meta( $post_id, Email_Log::META_SEND_ATTEMPTS, true ); |
| 126 | + |
| 127 | + update_post_meta( $post_id, Email_Log::META_SEND_ATTEMPTS, $current_attempts + 1 ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Updates the post status for a log post. |
| 132 | + * |
| 133 | + * @since n.e.x.t |
| 134 | + * |
| 135 | + * @param int $post_id Log post ID. |
| 136 | + * @param string $status New status slug. |
| 137 | + * @return void Nothing returned. |
| 138 | + */ |
| 139 | + public function update_status( $post_id, $status ) { |
| 140 | + $post = get_post( $post_id ); |
| 141 | + |
| 142 | + if ( ! $post || Email_Log::POST_TYPE !== $post->post_type ) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + wp_update_post( |
| 147 | + array( |
| 148 | + 'ID' => $post_id, |
| 149 | + 'post_status' => $status, |
| 150 | + ) |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments