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
$subject = $wc_email->get_subject(); // We will get subject from $email_post after we add it to the editor.
[92] Fix | Delete
$preheader = $wc_email->get_preheader();
[93] Fix | Delete
$rendered_email_data = $this->renderer->render( $email_post, $subject, $preheader, 'en' );
[94] Fix | Delete
$personalized_email = $this->personalizer->personalize_content( $rendered_email_data['html'] );
[95] Fix | Delete
$rendered_email = str_replace( self::WOO_EMAIL_CONTENT_PLACEHOLDER, $woo_content, $personalized_email );
[96] Fix | Delete
add_filter( 'woocommerce_email_styles', array( $this->woo_content_processor, 'prepare_css' ), 10, 2 );
[97] Fix | Delete
return $rendered_email;
[98] Fix | Delete
} catch ( \Exception $e ) {
[99] Fix | Delete
wc_caught_exception( $e, __METHOD__, array( $email_post, $woo_content, $wc_email ) );
[100] Fix | Delete
return null;
[101] Fix | Delete
}
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Get the email post for a given WC_Email.
[106] Fix | Delete
*
[107] Fix | Delete
* @param \WC_Email $email WooCommerce email.
[108] Fix | Delete
* @return \WP_Post|null
[109] Fix | Delete
*/
[110] Fix | Delete
private function get_email_post_by_wc_email( \WC_Email $email ): ?\WP_Post {
[111] Fix | Delete
return $this->template_manager->get_email_post( $email->id );
[112] Fix | Delete
}
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function