subscriptions->active_for_release( $release_id ); return $this->enqueue_subject( $subscription_ids, $event_key, 'release', 'release', $release_id ); } public function enqueue_feedback_status( int $feedback_id, string $status ): int { if ( get_post_type( $feedback_id ) !== Identifiers::POST_TYPE_FEEDBACK ) { return 0; } $event_key = hash( 'sha256', 'feedback_status:' . $feedback_id . ':' . $status ); $subscription_ids = $this->subscriptions->active_for_feedback( $feedback_id ); return $this->enqueue_subject( $subscription_ids, $event_key, 'feedback_status', 'feedback', $feedback_id ); } /** * @param int[] $subscription_ids */ private function enqueue_subject( array $subscription_ids, string $event_key, string $event_type, string $subject_type, int $subject_id ): int { $queued = 0; $table = "{$this->wpdb->prefix}senti_notifications"; foreach ( $subscription_ids as $subscription_id ) { $query = $this->wpdb->prepare( 'INSERT IGNORE INTO %i (subscription_id, event_key, event_type, subject_type, subject_id, state, attempt_count, scheduled_gmt, created_gmt) VALUES (%d, %s, %s, %s, %d, %s, 0, %s, %s)', $table, $subscription_id, $event_key, $event_type, $subject_type, $subject_id, 'pending', current_time( 'mysql', true ), current_time( 'mysql', true ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $query is prepared immediately above. $result = is_string( $query ) ? $this->wpdb->query( $query ) : false; $queued += $result === 1 ? 1 : 0; } if ( $queued > 0 ) { $this->schedule_worker( time() + MINUTE_IN_SECONDS ); } return $queued; } private function schedule_worker( int $timestamp ): void { $next = wp_next_scheduled( self::ACTION ); if ( $next !== false && $next <= $timestamp ) { return; } wp_clear_scheduled_hook( self::ACTION ); wp_schedule_single_event( $timestamp, self::ACTION ); } }