* * @param string $email_type The email type. * @return int The post ID of the generated template. * @throws \Exception When post creation fails. */ public function generate_email_template_if_not_exists( $email_type ) { $email_data = $this->default_templates[ $email_type ]; if ( $this->template_manager->get_email_template_post_id( $email_type ) || empty( $email_data ) ) { return $this->template_manager->get_email_template_post_id( $email_type ); } return $this->generate_single_template( $email_type, $email_data ); } /** * Generate email templates. * * This function generates the email templates for the given email types. * * @param array $templates_to_generate The email types to generate. */ public function generate_email_templates( $templates_to_generate ) { global $wpdb; $core_emails = array_filter( $this->default_templates, function ( $email_id ) use ( $templates_to_generate ) { return in_array( $email_id, $templates_to_generate, true ); }, ARRAY_FILTER_USE_KEY ); if ( empty( $core_emails ) ) { return false; } // Start transaction. $wpdb->query( 'START TRANSACTION' ); try { foreach ( $core_emails as $email_type => $email_data ) { $this->generate_single_template( $email_type, $email_data ); } $wpdb->query( 'COMMIT' ); return true; } catch ( \Exception $e ) { $wpdb->query( 'ROLLBACK' ); return new \WP_Error( 'email_generation_failed', $e->getMessage() ); } } /** * Build the `wp_insert_post()` payload for a given email and apply the * `woocommerce_email_content_post_data` filter. * * Extracted so the generator and the divergence detector observe the exact * same pre-insert post payload, guaranteeing by construction that the hash * stamped in {@see self::generate_single_template()} and the hash recomputed * in `WCEmailTemplateDivergenceDetector` hash identical input. * * @param string $email_type The email type identifier (e.g. `customer_processing_order`). * @param \WC_Email $email The transactional email instance. * @return array The post data array after the `woocommerce_email_content_post_data` filter runs. * * @since 10.8.0 */ public static function build_filtered_post_data( string $email_type, $email ): array { $post_data = array( 'post_type' => Integration::EMAIL_POST_TYPE, 'post_status' => 'publish', 'post_name' => $email_type, 'post_title' => $email->get_title(), 'post_excerpt' => $email->get_description(), 'post_content' => self::render_block_template_html( $email ), 'meta_input' => array( '_wp_page_template' => ( new WooEmailTemplate() )->get_slug(), ), ); /** * Filter the email content post data before creating the post. * * Allows third-party integrators to modify the post data (title, content, meta, etc.) * before the email content post is created. * * @since 10.5.0 * @param array $post_data The post data array to be used for wp_insert_post(). * @param string $email_type The email type identifier (e.g., 'customer_processing_order'). * @param \WC_Email $email The WooCommerce email object. */ $filtered_post_data = apply_filters( 'woocommerce_email_content_post_data', $post_data, $email_type, $email ); return is_array( $filtered_post_data ) ? $filtered_post_data : $post_data; } /** * Compute the canonical `post_content` for a given email. * * Returns the `post_content` value that the generator would persist for this * email after the `woocommerce_email_content_post_data` filter runs, i.e. * the exact string whose sha1 is stamped into `_wc_email_template_source_hash`. * * Callers can hash the return value to obtain `currentCoreHash` for * divergence detection. * * @param \WC_Email $email The transactional email instance. * @return string The canonical post content. * * @since 10.8.0 */ public static function compute_canonical_post_content( $email ): string { $post_data = self::build_filtered_post_data( (string) $email->id, $email ); return (string) ( $post_data['post_content'] ?? '' ); } /** * Generate a single email template. * * This function generates a single email template post and sets its postmeta association. * * @param string $email_type The email type. * @param \WC_Email $email_data The transactional email data. * @return int The post ID of the generated template. * @throws \Exception When post creation fails. */ private function generate_single_template( $email_type, $email_data ) { $post_data = self::build_filtered_post_data( (string) $email_type, $email_data ); // Sync meta stamp for emails participating in template update propagation. $sync_config = WCEmailTemplateSyncRegistry::get_email_sync_config( (string) $email_data->id ); if ( null !== $sync_config ) { if ( ! isset( $post_data['meta_input'] ) || ! is_array( $post_data['meta_input'] ) ) { $post_data['meta_input'] = array(); } $post_data['meta_input'][ WCEmailTemplateDivergenceDetector::VERSION_META_KEY ] = (string) $sync_config['version']; $post_data['meta_input'][ WCEmailTemplateDivergenceDetector::SOURCE_HASH_META_KEY ] = sha1( (string) ( $post_data['post_content'] ?? '' ) ); $post_data['meta_input'][ WCEmailTemplateDivergenceDetector::LAST_SYNCED_AT_META_KEY ] = gmdate( 'Y-m-d H:i:s' ); $post_data['meta_input'][ WCEmailTemplateDivergenceDetector::LAST_CORE_RENDER_META_KEY ] = (string) ( $post_data['post_content'] ?? '' ); } $post_id = wp_insert_post( $post_data, true ); if ( is_wp_error( $post_id ) ) { throw new \Exception( esc_html( $post_id->get_error_message() ) ); } $this->template_manager->save_email_template_post_id( $email_type, $post_id ); return $post_id; } }