Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/EmailEdi...
File: Integration.php
<?php
[0] Fix | Delete
[1] Fix | Delete
declare( strict_types=1 );
[2] Fix | Delete
[3] Fix | Delete
namespace Automattic\WooCommerce\Internal\EmailEditor;
[4] Fix | Delete
[5] Fix | Delete
use Automattic\WooCommerce\EmailEditor\Email_Editor_Container;
[6] Fix | Delete
use Automattic\WooCommerce\EmailEditor\Engine\Dependency_Check;
[7] Fix | Delete
use Automattic\WooCommerce\Internal\Admin\EmailPreview\EmailPreview;
[8] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\EmailPatterns\PatternsController;
[9] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\EmailTemplates\TemplatesController;
[10] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\Renderer\Blocks\WooContent;
[11] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmails;
[12] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmailPostsManager;
[13] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\EmailTemplates\TemplateApiController;
[14] Fix | Delete
use Automattic\WooCommerce\EmailEditor\Engine\Logger\Email_Editor_Logger;
[15] Fix | Delete
use WP_Post;
[16] Fix | Delete
[17] Fix | Delete
defined( 'ABSPATH' ) || exit;
[18] Fix | Delete
[19] Fix | Delete
/**
[20] Fix | Delete
* Integration class for the Email Editor functionality.
[21] Fix | Delete
*/
[22] Fix | Delete
class Integration {
[23] Fix | Delete
const EMAIL_POST_TYPE = 'woo_email';
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* The email editor page renderer instance.
[27] Fix | Delete
*
[28] Fix | Delete
* @var PageRenderer
[29] Fix | Delete
*/
[30] Fix | Delete
private PageRenderer $editor_page_renderer;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* The dependency check instance.
[34] Fix | Delete
*
[35] Fix | Delete
* @var Dependency_Check
[36] Fix | Delete
*/
[37] Fix | Delete
private Dependency_Check $dependency_check;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* The template API controller instance.
[41] Fix | Delete
*
[42] Fix | Delete
* @var TemplateApiController
[43] Fix | Delete
*/
[44] Fix | Delete
private TemplateApiController $template_api_controller;
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* The email data API controller instance.
[48] Fix | Delete
*
[49] Fix | Delete
* @var EmailApiController
[50] Fix | Delete
*/
[51] Fix | Delete
private EmailApiController $email_api_controller;
[52] Fix | Delete
[53] Fix | Delete
/**
[54] Fix | Delete
* Constructor.
[55] Fix | Delete
*/
[56] Fix | Delete
public function __construct() {
[57] Fix | Delete
$editor_container = Email_Editor_Container::container();
[58] Fix | Delete
$this->dependency_check = $editor_container->get( Dependency_Check::class );
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* Initialize the integration.
[63] Fix | Delete
*
[64] Fix | Delete
* @internal
[65] Fix | Delete
*/
[66] Fix | Delete
final public function init(): void {
[67] Fix | Delete
if ( ! $this->dependency_check->are_dependencies_met() ) {
[68] Fix | Delete
// If dependencies are not met, do not initialize the email editor integration.
[69] Fix | Delete
return;
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
add_action( 'woocommerce_init', array( $this, 'initialize' ) );
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Initialize the integration.
[77] Fix | Delete
*/
[78] Fix | Delete
public function initialize() {
[79] Fix | Delete
$this->init_logger();
[80] Fix | Delete
$this->init_hooks();
[81] Fix | Delete
$this->register_blocks();
[82] Fix | Delete
$this->extend_post_api();
[83] Fix | Delete
$this->extend_template_post_api();
[84] Fix | Delete
$this->register_hooks();
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* Initialize the logger.
[89] Fix | Delete
*/
[90] Fix | Delete
public function init_logger() {
[91] Fix | Delete
$editor_container = Email_Editor_Container::container();
[92] Fix | Delete
$logger = $editor_container->get( Email_Editor_Logger::class );
[93] Fix | Delete
[94] Fix | Delete
// Register the WooCommerce logger with the email editor package.
[95] Fix | Delete
$logger->set_logger( new Logger( wc_get_logger() ) );
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Initialize hooks for required classes.
[100] Fix | Delete
*/
[101] Fix | Delete
public function init_hooks() {
[102] Fix | Delete
$container = wc_get_container();
[103] Fix | Delete
$container->get( PatternsController::class );
[104] Fix | Delete
$container->get( TemplatesController::class );
[105] Fix | Delete
$container->get( PersonalizationTagManager::class );
[106] Fix | Delete
$container->get( BlockEmailRenderer::class );
[107] Fix | Delete
$container->get( WCTransactionalEmails::class );
[108] Fix | Delete
$this->editor_page_renderer = $container->get( PageRenderer::class );
[109] Fix | Delete
$this->template_api_controller = $container->get( TemplateApiController::class );
[110] Fix | Delete
$this->email_api_controller = $container->get( EmailApiController::class );
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* Register hooks for the integration.
[115] Fix | Delete
*/
[116] Fix | Delete
public function register_hooks() {
[117] Fix | Delete
add_filter( 'woocommerce_email_editor_post_types', array( $this, 'add_email_post_type' ) );
[118] Fix | Delete
add_filter( 'woocommerce_is_email_editor_page', array( $this, 'is_editor_page' ), 10, 1 );
[119] Fix | Delete
add_filter( 'replace_editor', array( $this, 'replace_editor' ), 10, 2 );
[120] Fix | Delete
add_action( 'before_delete_post', array( $this, 'delete_email_template_associated_with_email_editor_post' ), 10, 2 );
[121] Fix | Delete
add_filter( 'woocommerce_email_editor_send_preview_email_rendered_data', array( $this, 'update_send_preview_email_rendered_data' ) );
[122] Fix | Delete
add_filter( 'woocommerce_email_editor_send_preview_email_personalizer_context', array( $this, 'update_send_preview_email_personalizer_context' ) );
[123] Fix | Delete
add_filter( 'woocommerce_email_editor_preview_post_template_html', array( $this, 'update_preview_post_template_html_data' ), 100, 1 );
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
/**
[127] Fix | Delete
* Registers blocks for the integration.
[128] Fix | Delete
*/
[129] Fix | Delete
public function register_blocks(): void {
[130] Fix | Delete
$woo_content = new WooContent();
[131] Fix | Delete
$woo_content->register();
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* Add WooCommerce email post type to the list of supported post types.
[136] Fix | Delete
*
[137] Fix | Delete
* @param array $post_types List of post types.
[138] Fix | Delete
* @return array Modified list of post types.
[139] Fix | Delete
*/
[140] Fix | Delete
public function add_email_post_type( array $post_types ): array {
[141] Fix | Delete
$post_types[] = array(
[142] Fix | Delete
'name' => self::EMAIL_POST_TYPE,
[143] Fix | Delete
'args' => array(
[144] Fix | Delete
'labels' => array(
[145] Fix | Delete
'name' => __( 'Emails', 'woocommerce' ),
[146] Fix | Delete
'singular_name' => __( 'Email', 'woocommerce' ),
[147] Fix | Delete
'add_new_item' => __( 'Add Email', 'woocommerce' ),
[148] Fix | Delete
'edit_item' => __( 'Edit Email', 'woocommerce' ),
[149] Fix | Delete
'new_item' => __( 'New Email', 'woocommerce' ),
[150] Fix | Delete
'view_item' => __( 'View Email', 'woocommerce' ),
[151] Fix | Delete
'search_items' => __( 'Search Emails', 'woocommerce' ),
[152] Fix | Delete
),
[153] Fix | Delete
'rewrite' => array( 'slug' => self::EMAIL_POST_TYPE ),
[154] Fix | Delete
'supports' => array(
[155] Fix | Delete
'title',
[156] Fix | Delete
'editor' => array(
[157] Fix | Delete
'default-mode' => 'template-locked',
[158] Fix | Delete
),
[159] Fix | Delete
'excerpt',
[160] Fix | Delete
),
[161] Fix | Delete
),
[162] Fix | Delete
);
[163] Fix | Delete
return $post_types;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
/**
[167] Fix | Delete
* Check if current page is email editor page.
[168] Fix | Delete
*
[169] Fix | Delete
* @param bool $is_editor_page Current editor page status.
[170] Fix | Delete
* @return bool Whether current page is email editor page.
[171] Fix | Delete
*/
[172] Fix | Delete
public function is_editor_page( bool $is_editor_page ): bool {
[173] Fix | Delete
if ( $is_editor_page ) {
[174] Fix | Delete
return $is_editor_page;
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
// We need to check early if we are on the email editor page. The check runs early so we can't use current_screen() here.
[178] Fix | Delete
if ( is_admin() && isset( $_GET['post'] ) && isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We are not verifying the nonce here because we are not using the nonce in the function and the data is okay in this context (WP-admin errors out gracefully).
[179] Fix | Delete
$post = get_post( (int) $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We are not verifying the nonce here because we are not using the nonce in the function and the data is okay in this context (WP-admin errors out gracefully).
[180] Fix | Delete
return $post && self::EMAIL_POST_TYPE === $post->post_type;
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
return false;
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
/**
[187] Fix | Delete
* Replace the default editor with our custom email editor.
[188] Fix | Delete
*
[189] Fix | Delete
* @param bool $replace Whether to replace the editor.
[190] Fix | Delete
* @param WP_Post $post Post object.
[191] Fix | Delete
* @return bool Whether the editor was replaced.
[192] Fix | Delete
*/
[193] Fix | Delete
public function replace_editor( $replace, $post ) {
[194] Fix | Delete
$current_screen = get_current_screen();
[195] Fix | Delete
if ( self::EMAIL_POST_TYPE === $post->post_type && $current_screen ) {
[196] Fix | Delete
$this->editor_page_renderer->render();
[197] Fix | Delete
return true;
[198] Fix | Delete
}
[199] Fix | Delete
return $replace;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
/**
[203] Fix | Delete
* Delete the email template associated with the email editor post when the post is permanently deleted.
[204] Fix | Delete
*
[205] Fix | Delete
* @param int $post_id The post ID.
[206] Fix | Delete
* @param WP_Post $post The post object.
[207] Fix | Delete
*/
[208] Fix | Delete
public function delete_email_template_associated_with_email_editor_post( $post_id, $post ) {
[209] Fix | Delete
if ( self::EMAIL_POST_TYPE !== $post->post_type ) {
[210] Fix | Delete
return;
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
$post_manager = WCTransactionalEmailPostsManager::get_instance();
[214] Fix | Delete
[215] Fix | Delete
$email_type = $post_manager->get_email_type_from_post_id( $post_id );
[216] Fix | Delete
[217] Fix | Delete
if ( empty( $email_type ) ) {
[218] Fix | Delete
return;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
$post_manager->delete_email_template( $email_type );
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
/**
[225] Fix | Delete
* Extend the post API for the wp_template post type to add and save the woocommerce_data field.
[226] Fix | Delete
*/
[227] Fix | Delete
public function extend_template_post_api(): void {
[228] Fix | Delete
register_rest_field(
[229] Fix | Delete
'wp_template',
[230] Fix | Delete
'woocommerce_data',
[231] Fix | Delete
array(
[232] Fix | Delete
'get_callback' => array( $this->template_api_controller, 'get_template_data' ),
[233] Fix | Delete
'update_callback' => array( $this->template_api_controller, 'save_template_data' ),
[234] Fix | Delete
'schema' => $this->template_api_controller->get_template_data_schema(),
[235] Fix | Delete
)
[236] Fix | Delete
);
[237] Fix | Delete
}
[238] Fix | Delete
[239] Fix | Delete
/**
[240] Fix | Delete
* Filter email preview data to replace placeholders with actual content.
[241] Fix | Delete
*
[242] Fix | Delete
* This method retrieves the appropriate email type based on the request,
[243] Fix | Delete
* generates the email content using the WooContentProcessor, and replaces
[244] Fix | Delete
* the placeholder in the preview HTML.
[245] Fix | Delete
*
[246] Fix | Delete
* @param string $data The preview data.
[247] Fix | Delete
* @param string $email_type The email type identifier (e.g., 'customer_processing_order').
[248] Fix | Delete
* @return string The updated preview data with placeholders replaced.
[249] Fix | Delete
*/
[250] Fix | Delete
private function update_email_preview_data( $data, string $email_type ) {
[251] Fix | Delete
$type_param = EmailPreview::DEFAULT_EMAIL_TYPE;
[252] Fix | Delete
[253] Fix | Delete
if ( ! empty( $email_type ) ) {
[254] Fix | Delete
$type_param = WCTransactionalEmailPostsManager::get_instance()->get_email_type_class_name_from_template_name( $email_type );
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
$email_preview = wc_get_container()->get( EmailPreview::class );
[258] Fix | Delete
[259] Fix | Delete
try {
[260] Fix | Delete
$message = $email_preview->generate_placeholder_content( $type_param );
[261] Fix | Delete
} catch ( \InvalidArgumentException $e ) {
[262] Fix | Delete
// If the provided type was invalid, fall back to the default.
[263] Fix | Delete
try {
[264] Fix | Delete
$message = $email_preview->generate_placeholder_content( EmailPreview::DEFAULT_EMAIL_TYPE );
[265] Fix | Delete
} catch ( \Throwable $e ) {
[266] Fix | Delete
return $data;
[267] Fix | Delete
}
[268] Fix | Delete
} catch ( \Throwable $e ) {
[269] Fix | Delete
return $data;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return str_replace( BlockEmailRenderer::WOO_EMAIL_CONTENT_PLACEHOLDER, $message, $data );
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Filter email preview data used when sending a preview email.
[277] Fix | Delete
*
[278] Fix | Delete
* @param string $data The preview data.
[279] Fix | Delete
* @return string The updated preview data with placeholders replaced.
[280] Fix | Delete
*/
[281] Fix | Delete
public function update_send_preview_email_rendered_data( $data ) {
[282] Fix | Delete
$email_type = '';
[283] Fix | Delete
$post_body = file_get_contents( 'php://input' );
[284] Fix | Delete
[285] Fix | Delete
if ( $post_body ) {
[286] Fix | Delete
$decoded_body = json_decode( $post_body );
[287] Fix | Delete
[288] Fix | Delete
if ( json_last_error() === JSON_ERROR_NONE && isset( $decoded_body->postId ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
[289] Fix | Delete
$post_id = absint( $decoded_body->postId ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
[290] Fix | Delete
[291] Fix | Delete
$email_type = WCTransactionalEmailPostsManager::get_instance()->get_email_type_from_post_id( $post_id );
[292] Fix | Delete
if ( ! empty( $email_type ) ) {
[293] Fix | Delete
return $this->update_email_preview_data( $data, $email_type );
[294] Fix | Delete
}
[295] Fix | Delete
}
[296] Fix | Delete
}
[297] Fix | Delete
return $data;
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
/**
[301] Fix | Delete
* Update the personalizer context for the send preview email.
[302] Fix | Delete
*
[303] Fix | Delete
* @param array $context The personalizer context.
[304] Fix | Delete
* @return array The updated personalizer context.
[305] Fix | Delete
*/
[306] Fix | Delete
public function update_send_preview_email_personalizer_context( $context ) {
[307] Fix | Delete
$post_manager = WCTransactionalEmailPostsManager::get_instance();
[308] Fix | Delete
$email_type_template_name = $post_manager->get_email_type_from_post_id( get_the_ID() );
[309] Fix | Delete
$email_type = $email_type_template_name ? $post_manager->get_email_type_class_name_from_template_name( $email_type_template_name ) : EmailPreview::DEFAULT_EMAIL_TYPE;
[310] Fix | Delete
$email_preview = wc_get_container()->get( EmailPreview::class );
[311] Fix | Delete
[312] Fix | Delete
try {
[313] Fix | Delete
$email_preview->set_email_type( $email_type );
[314] Fix | Delete
} catch ( \InvalidArgumentException $e ) {
[315] Fix | Delete
// If the email type is invalid, return the context data as is.
[316] Fix | Delete
return $context;
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
$email = $email_preview->get_email();
[320] Fix | Delete
$email->recipient = $context['recipient_email'] ?? '';
[321] Fix | Delete
$personalizer = wc_get_container()->get( TransactionalEmailPersonalizer::class );
[322] Fix | Delete
[323] Fix | Delete
return $personalizer->prepare_context_data( $context, $email );
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Filter email preview data used when previewing the email in new tab.
[328] Fix | Delete
*
[329] Fix | Delete
* @param string $data The preview HTML string.
[330] Fix | Delete
* @return string The updated preview HTML with placeholders replaced.
[331] Fix | Delete
*/
[332] Fix | Delete
public function update_preview_post_template_html_data( $data ) {
[333] Fix | Delete
// phpcs:disable WordPress.Security.NonceVerification.Recommended
[334] Fix | Delete
// Nonce verification is disabled here because the preview action doesn't modify data,
[335] Fix | Delete
// and the check caused issues with the 'Preview in new tab' feature due to context changes.
[336] Fix | Delete
$type_param = isset( $_GET['woo_email'] ) ? sanitize_text_field( wp_unslash( $_GET['woo_email'] ) ) : '';
[337] Fix | Delete
// phpcs:enable
[338] Fix | Delete
return $this->update_email_preview_data( $data, $type_param );
[339] Fix | Delete
}
[340] Fix | Delete
[341] Fix | Delete
/**
[342] Fix | Delete
* Extend the post API for the woo_email post type to add and save the woocommerce_data field.
[343] Fix | Delete
*/
[344] Fix | Delete
public function extend_post_api(): void {
[345] Fix | Delete
register_rest_field(
[346] Fix | Delete
self::EMAIL_POST_TYPE,
[347] Fix | Delete
'woocommerce_data',
[348] Fix | Delete
array(
[349] Fix | Delete
'get_callback' => array( $this->email_api_controller, 'get_email_data' ),
[350] Fix | Delete
'update_callback' => array( $this->email_api_controller, 'save_email_data' ),
[351] Fix | Delete
'schema' => $this->email_api_controller->get_email_data_schema(),
[352] Fix | Delete
)
[353] Fix | Delete
);
[354] Fix | Delete
}
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function