settings_controller = $settings_controller; $this->theme_controller = $theme_controller; $this->user_theme = $user_theme; $this->logger = $logger; } /** * Sets the path for the email editor assets. * * @param string $assets_path The path to the email editor assets directory. * @return void */ public function set_assets_path( string $assets_path ): void { $this->assets_path = $assets_path; } /** * Sets the URL for the email editor assets. * * @param string $assets_url The URL to the email editor assets directory. * @return void */ public function set_assets_url( string $assets_url ): void { $this->assets_url = $assets_url; } /** * Initialize the assets manager. */ public function initialize(): void { add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) ); } /** * Enqueue admin styles that are needed by the email editor. */ public function enqueue_admin_styles(): void { // Calling action that loads registered blockTypes. do_action( 'enqueue_block_editor_assets' ); // Load CSS from Post Editor. wp_enqueue_style( 'wp-edit-post' ); // Print the personalization tag chip styles right after core's editor styles by attaching them to the wp-edit-post handle. wp_add_inline_style( 'wp-edit-post', (string) file_get_contents( __DIR__ . '/rich-text-comment.css' ) ); // Load CSS for the format library - used for example in popover. wp_enqueue_style( 'wp-format-library' ); // Enqueue CSS containing --wp--preset variables. wp_enqueue_global_styles_css_custom_properties(); // Enqueue media library scripts. wp_enqueue_media(); } /** * Render the email editor's required HTML and admin header. * * Renders at most once per instance; repeated calls are no-ops. * * @param string $element_id Optional. The ID of the main container element. Default is 'woocommerce-email-editor'. */ public function render_email_editor_html( string $element_id = 'woocommerce-email-editor' ): void { // Integrations render from the `replace_editor` filter, which re-fires while // admin-header.php runs whenever a plugin calls WP_Screen::get() from // admin_enqueue_scripts; a second container echoed inside breaks the page. if ( $this->editor_html_rendered ) { return; } $this->editor_html_rendered = true; // @phpstan-ignore-next-line -- PHPStan tried to check if the file exists. require_once ABSPATH . 'wp-admin/admin-header.php'; echo '
'; } /** * Load editor assets. * * @param \WP_Post|\WP_Block_Template $edited_item The edited post or template. * @param string $script_name The name of the registered script. */ public function load_editor_assets( $edited_item, string $script_name ): void { $post_type = $edited_item instanceof \WP_Post ? $edited_item->post_type : 'wp_template'; $post_id = $edited_item instanceof \WP_Post ? $edited_item->ID : $edited_item->id; $email_editor_assets_path = rtrim( $this->assets_path, '/' ) . '/'; $email_editor_assets_url = rtrim( $this->assets_url, '/' ) . '/'; $assets_file = $email_editor_assets_path . 'style.asset.php'; if ( ! file_exists( $assets_file ) ) { $this->logger->error( 'Email editor assets file does not exist.', array( 'path' => $assets_file ) ); } else { $assets_file = require $assets_file; wp_enqueue_style( 'wc-admin-email-editor-integration', $email_editor_assets_url . 'style.css', array(), $assets_file['version'] ); } // The get_block_categories() function expects a WP_Post or WP_Block_Editor_Context object. // Therefore, we need to create an instance of WP_Block_Editor_Context when $edited_item is an instance of WP_Block_Template. if ( $edited_item instanceof \WP_Block_Template ) { $context = new \WP_Block_Editor_Context( array( 'post' => $edited_item, ) ); } else { $context = $edited_item; } // The email editor needs to load block categories to avoid warning and missing category names. // See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L116-L120. wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $context ), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ), 'after' ); // Preload server-registered block schemas to avoid warning about missing block titles. // See: https://github.com/WordPress/WordPress/blob/753817d462955eb4e40a89034b7b7c375a1e43f3/wp-admin/edit-form-blocks.php#L144C1-L148C3. wp_add_inline_script( 'wp-blocks', sprintf( 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions( %s );', wp_json_encode( get_block_editor_server_block_settings(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) ); $localization_data = array( 'current_post_type' => $post_type, 'current_post_id' => $post_id, 'current_wp_user_email' => wp_get_current_user()->user_email, 'editor_settings' => $this->settings_controller->get_settings(), 'editor_theme' => $this->theme_controller->get_base_theme()->get_raw_data(), 'user_theme_post_id' => $this->user_theme->get_user_theme_post()->ID, 'urls' => array( 'listings' => admin_url( 'admin.php?page=wc-settings&tab=email' ), 'send' => admin_url( 'admin.php?page=wc-settings&tab=email' ), 'back' => admin_url( 'admin.php?page=wc-settings&tab=email' ), 'createCoupon' => admin_url( 'post-new.php?post_type=shop_coupon' ), ), ); wp_localize_script( $script_name, 'WooCommerceEmailEditor', apply_filters( 'woocommerce_email_editor_script_localization_data', $localization_data ) ); $this->preload_rest_api_data( $post_id, $post_type ); } /** * Preload REST API data for the email editor. * * @param int|string $post_id The post ID. * @param string $post_type The post type. */ private function preload_rest_api_data( $post_id, string $post_type ): void { $email_post_type = $post_type; $user_theme_post_id = $this->user_theme->get_user_theme_post()->ID; $post = is_numeric( $post_id ) ? get_post( (int) $post_id ) : null; $template_slug = get_post_meta( (int) $post_id, '_wp_page_template', true ); $routes = array( "/wp/v2/{$email_post_type}/" . intval( $post_id ) . '?context=edit', "/wp/v2/types/{$email_post_type}?context=edit", '/wp/v2/global-styles/' . intval( $user_theme_post_id ) . '?context=view', // Global email styles. '/wp/v2/block-patterns/patterns', '/wp/v2/templates?context=view', '/wp/v2/block-patterns/categories', '/wp/v2/settings', '/wp/v2/types?context=view', '/wp/v2/taxonomies?context=view', ); if ( is_string( $template_slug ) && '' !== $template_slug ) { $routes[] = '/wp/v2/templates/lookup?slug=' . $template_slug; } // Recent emails listed by the template selection modal, which opens on emails with no content. if ( $post instanceof \WP_Post && '' === $post->post_content ) { $routes[] = "/wp/v2/{$email_post_type}?context=edit&per_page=30&status=publish,sent"; } /** * Filters the REST API routes preloaded for the email editor. * * @param string[] $routes The routes to preload. * @param string $post_type The edited post type. * @param int|string $post_id The edited post ID. * * @since 11.2.0 */ $routes = apply_filters( 'woocommerce_email_editor_preload_rest_api_routes', $routes, $post_type, $post_id ); if ( ! is_array( $routes ) ) { $routes = array(); } $preload_data = array_reduce( $routes, 'rest_preload_api_request', array() ); // Add inline script to set up preloading middleware. wp_add_inline_script( 'wp-blocks', sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', wp_json_encode( $preload_data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) ); } }