Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/EmailEdi...
File: BlockEmailRenderer.php
<?php
[0] Fix | Delete
declare( strict_types=1 );
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\Internal\EmailEditor;
[3] Fix | Delete
[4] Fix | Delete
use Automattic\WooCommerce\EmailEditor\Email_Editor_Container;
[5] Fix | Delete
use Automattic\WooCommerce\EmailEditor\Engine\Personalizer;
[6] Fix | Delete
use Automattic\WooCommerce\EmailEditor\Engine\Renderer\Renderer as EmailRenderer;
[7] Fix | Delete
use Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmailPostsManager;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Class responsible for rendering block-based emails.
[11] Fix | Delete
*/
[12] Fix | Delete
class BlockEmailRenderer {
[13] Fix | Delete
const WOO_EMAIL_CONTENT_PLACEHOLDER = '##WOO_CONTENT##';
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Service for rendering block emails
[17] Fix | Delete
*
[18] Fix | Delete
* @var EmailRenderer
[19] Fix | Delete
*/
[20] Fix | Delete
private $renderer;
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Service for personalization of emails
[24] Fix | Delete
* It replaces personalization tags with actual values
[25] Fix | Delete
*
[26] Fix | Delete
* @var Personalizer
[27] Fix | Delete
*/
[28] Fix | Delete
private $personalizer;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Service for extracting WooCommerce content from WC_Email object.
[32] Fix | Delete
*
[33] Fix | Delete
* @var WooContentProcessor
[34] Fix | Delete
*/
[35] Fix | Delete
private $woo_content_processor;
[36] Fix | Delete
[37] Fix | Delete
/**
[38] Fix | Delete
* WooCommerce Email Template Manager instance.
[39] Fix | Delete
*
[40] Fix | Delete
* @var WCTransactionalEmailPostsManager
[41] Fix | Delete
*/
[42] Fix | Delete
private $template_manager;
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Constructor.
[46] Fix | Delete
*/
[47] Fix | Delete
public function __construct() {
[48] Fix | Delete
$editor_container = Email_Editor_Container::container();
[49] Fix | Delete
$this->renderer = $editor_container->get( EmailRenderer::class );
[50] Fix | Delete
$this->personalizer = $editor_container->get( Personalizer::class );
[51] Fix | Delete
$this->template_manager = WCTransactionalEmailPostsManager::get_instance();
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Initialize the renderer.
[56] Fix | Delete
*
[57] Fix | Delete
* @param WooContentProcessor $woo_content_processor Service for extracting WooCommerce content from WC_Email object.
[58] Fix | Delete
* @internal
[59] Fix | Delete
*/
[60] Fix | Delete
final public function init( WooContentProcessor $woo_content_processor ): void {
[61] Fix | Delete
$this->woo_content_processor = $woo_content_processor;
[62] Fix | Delete
add_action( 'woocommerce_email_blocks_renderer_initialized', array( $this, 'register_block_renderers' ) );
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* Maybe render block-based email content.
[67] Fix | Delete
*
[68] Fix | Delete
* @param \WC_Email $wc_email WooCommerce email.
[69] Fix | Delete
* @return string|null Modified email content
[70] Fix | Delete
*/
[71] Fix | Delete
public function maybe_render_block_email( \WC_Email $wc_email ): ?string {
[72] Fix | Delete
$email_post = $this->get_email_post_by_wc_email( $wc_email );
[73] Fix | Delete
if ( ! $email_post ) {
[74] Fix | Delete
return null;
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
$woo_content = $this->woo_content_processor->get_woo_content( $wc_email );
[78] Fix | Delete
return $this->render_block_email( $email_post, $woo_content, $wc_email );
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Maybe render block-based email content.
[83] Fix | Delete
*
[84] Fix | Delete
* @param \WP_Post $email_post Email post.
[85] Fix | Delete
* @param string $woo_content WooCommerce email content.
[86] Fix | Delete
* @param \WC_Email $wc_email WooCommerce email.
[87] Fix | Delete
* @return string Modified email content
[88] Fix | Delete
*/
[89] Fix | Delete
private function render_block_email( \WP_Post $email_post, string $woo_content, \WC_Email $wc_email ): ?string {
[90] Fix | Delete
try {
[91] Fix | Delete
// Set email context before rendering so blocks can access it.
[92] Fix | Delete
$filter_callback = function ( $context = array() ) use ( $wc_email ) {
[93] Fix | Delete
return array_merge( $context, $this->build_email_context( $wc_email ) );
[94] Fix | Delete
};
[95] Fix | Delete
add_filter( 'woocommerce_email_editor_rendering_email_context', $filter_callback, 10, 1 );
[96] Fix | Delete
[97] Fix | Delete
$subject = $wc_email->get_subject(); // We will get subject from $email_post after we add it to the editor.
[98] Fix | Delete
$preheader = $wc_email->get_preheader();
[99] Fix | Delete
$rendered_email_data = $this->renderer->render( $email_post, $subject, $preheader, 'en' );
[100] Fix | Delete
$personalized_email = $this->personalizer->personalize_content( $rendered_email_data['html'] );
[101] Fix | Delete
$rendered_email = str_replace( self::WOO_EMAIL_CONTENT_PLACEHOLDER, $woo_content, $personalized_email );
[102] Fix | Delete
[103] Fix | Delete
// Remove the filter after rendering to prevent context leakage.
[104] Fix | Delete
remove_filter( 'woocommerce_email_editor_rendering_email_context', $filter_callback );
[105] Fix | Delete
[106] Fix | Delete
add_filter( 'woocommerce_email_styles', array( $this->woo_content_processor, 'prepare_css' ), 10, 2 );
[107] Fix | Delete
return $rendered_email;
[108] Fix | Delete
} catch ( \Exception $e ) {
[109] Fix | Delete
wc_caught_exception( $e, __METHOD__, array( $email_post, $woo_content, $wc_email ) );
[110] Fix | Delete
// Remove the filter in case of exception.
[111] Fix | Delete
if ( isset( $filter_callback ) ) {
[112] Fix | Delete
remove_filter( 'woocommerce_email_editor_rendering_email_context', $filter_callback );
[113] Fix | Delete
}
[114] Fix | Delete
return null;
[115] Fix | Delete
}
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* Get the email post for a given WC_Email.
[120] Fix | Delete
*
[121] Fix | Delete
* @param \WC_Email $email WooCommerce email.
[122] Fix | Delete
* @return \WP_Post|null
[123] Fix | Delete
*/
[124] Fix | Delete
private function get_email_post_by_wc_email( \WC_Email $email ): ?\WP_Post {
[125] Fix | Delete
return $this->template_manager->get_email_post( $email->id );
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
/**
[129] Fix | Delete
* Build email context from WC_Email object.
[130] Fix | Delete
*
[131] Fix | Delete
* Extracts relevant context data from the WC_Email object that can be used
[132] Fix | Delete
* by blocks during rendering, such as user ID, email address, order information, etc.
[133] Fix | Delete
*
[134] Fix | Delete
* Blocks that need cart product information can derive it from the user_id or email
[135] Fix | Delete
* using CartCheckoutUtils::get_cart_product_ids_for_user().
[136] Fix | Delete
*
[137] Fix | Delete
* @param \WC_Email $wc_email WooCommerce email object.
[138] Fix | Delete
* @return array Email context data.
[139] Fix | Delete
*/
[140] Fix | Delete
private function build_email_context( \WC_Email $wc_email ): array {
[141] Fix | Delete
$recipient_raw = $wc_email->get_recipient();
[142] Fix | Delete
$emails = array_values( array_filter( array_map( 'sanitize_email', array_map( 'trim', explode( ',', $recipient_raw ) ) ) ) );
[143] Fix | Delete
$context = array(
[144] Fix | Delete
'recipient_email' => $emails[0] ?? null,
[145] Fix | Delete
);
[146] Fix | Delete
[147] Fix | Delete
// Extract order-related context if the email object is an order.
[148] Fix | Delete
if ( isset( $wc_email->object ) && $wc_email->object instanceof \WC_Order ) {
[149] Fix | Delete
$order = $wc_email->object;
[150] Fix | Delete
$context['user_id'] = $order->get_customer_id();
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
return $context;
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function