Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails

WCEmailTemplateAutoApplier::runpublic staticWC 10.8.0

Action Scheduler callback. Apply the canonical core render to every woo_email post whose status meta is core_updated_uncustomized.

Per-post try/catch ensures one bad post never breaks the rest of the batch (acceptance criterion). Status meta is never mutated by this method on failure — the next sweep re-classifies.

Declared void because Action Scheduler async-action callbacks discard the return value; a return type would just be noise (and trip PHPStan's return.void rule on add_action).

Метод класса: WCEmailTemplateAutoApplier{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$result = WCEmailTemplateAutoApplier::run(): void;

Список изменений

С версии 10.8.0 Введена.

Код WCEmailTemplateAutoApplier::run() WC 10.9.1

public static function run(): void {
	if ( 'yes' !== get_option( WCEmailTemplateDivergenceDetector::BACKFILL_COMPLETE_OPTION ) ) {
		return;
	}

	// `post_status=any` includes draft / private / pending / future and
	// excludes trash / auto-draft / inherit — anything not in the trash bucket
	// is fair game for auto-apply.
	$candidate_ids = get_posts(
		array(
			'post_type'      => \Automattic\WooCommerce\Internal\EmailEditor\Integration::EMAIL_POST_TYPE,
			'post_status'    => 'any',
			'posts_per_page' => -1,
			'fields'         => 'ids',
			'no_found_rows'  => true,
			'meta_query'     => array(  // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- bounded set; sync-registered emails only.
				array(
					'key'   => WCEmailTemplateDivergenceDetector::STATUS_META_KEY,
					'value' => WCEmailTemplateDivergenceDetector::STATUS_CORE_UPDATED_UNCUSTOMIZED,
				),
			),
		)
	);

	if ( empty( $candidate_ids ) ) {
		return;
	}

	$registry      = WCEmailTemplateSyncRegistry::get_sync_enabled_emails();
	$posts_manager = WCTransactionalEmailPostsManager::get_instance();
	$emails_by_id  = $posts_manager->get_emails_by_id();

	foreach ( $candidate_ids as $post_id ) {
		$post_id = (int) $post_id;
		try {
			$email_id = (string) $posts_manager->get_email_type_from_post_id( $post_id );
			if ( '' === $email_id || ! isset( $registry[ $email_id ] ) ) {
				continue;
			}

			$email = $emails_by_id[ $email_id ] ?? null;
			if ( ! $email instanceof \WC_Email ) {
				continue;
			}

			$result = self::apply_to_post( $email, $post_id );

			if ( is_wp_error( $result ) ) {
				self::log_apply_error( $result, $post_id, $email_id );
			}
		} catch ( \Throwable $e ) {
			self::get_logger()->error(
				sprintf(
					'Email template auto-apply failed for post %d: %s',
					$post_id,
					$e->getMessage()
				),
				array(
					'post_id' => $post_id,
					'context' => 'email_template_auto_applier',
				)
			);
			continue;
		}//end try
	}//end foreach
}