/var/www/html_it/wp-content/plugins/woocommerce/src/Internal/EmailEditor/BlockEmailRenderer.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare( strict_types=);

namespace 
Automattic\WooCommerce\Internal\EmailEditor;

use 
Automattic\WooCommerce\EmailEditor\Email_Editor_Container;
use 
Automattic\WooCommerce\EmailEditor\Engine\Personalizer;
use 
Automattic\WooCommerce\EmailEditor\Engine\Renderer\Renderer as EmailRenderer;
use 
Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmailPostsManager;

/**
 * Class responsible for rendering block-based emails.
 */
class BlockEmailRenderer {
    const 
WOO_EMAIL_CONTENT_PLACEHOLDER '##WOO_CONTENT##';

    
/**
     * Service for rendering block emails
     *
     * @var EmailRenderer
     */
    
private $renderer;

    
/**
     * Service for personalization of emails
     * It replaces personalization tags with actual values
     *
     * @var Personalizer
     */
    
private $personalizer;

    
/**
     * Service for extracting WooCommerce content from WC_Email object.
     *
     * @var WooContentProcessor
     */
    
private $woo_content_processor;

    
/**
     * WooCommerce Email Template Manager instance.
     *
     * @var WCTransactionalEmailPostsManager
     */
    
private $template_manager;

    
/**
     * Constructor.
     */
    
public function __construct() {
        
$editor_container       Email_Editor_Container::container();
        
$this->renderer         $editor_container->getEmailRenderer::class );
        
$this->personalizer     $editor_container->getPersonalizer::class );
        
$this->template_manager WCTransactionalEmailPostsManager::get_instance();
    }

    
/**
     * Initialize the renderer.
     *
     * @param WooContentProcessor $woo_content_processor Service for extracting WooCommerce content from WC_Email object.
     * @internal
     */
    
final public function initWooContentProcessor $woo_content_processor ): void {
        
$this->woo_content_processor $woo_content_processor;
        
add_action'woocommerce_email_blocks_renderer_initialized', array( $this'register_block_renderers' ) );
    }

    
/**
     * Maybe render block-based email content.
     *
     * @param \WC_Email $wc_email WooCommerce email.
     * @return string|null Modified email content
     */
    
public function maybe_render_block_email\WC_Email $wc_email ): ?string {
        
$email_post $this->get_email_post_by_wc_email$wc_email );
        if ( ! 
$email_post ) {
            return 
null;
        }

        
$woo_content $this->woo_content_processor->get_woo_content$wc_email );
        return 
$this->render_block_email$email_post$woo_content$wc_email );
    }

    
/**
     * Maybe render block-based email content.
     *
     * @param \WP_Post  $email_post Email post.
     * @param string    $woo_content WooCommerce email content.
     * @param \WC_Email $wc_email WooCommerce email.
     * @return string Modified email content
     */
    
private function render_block_email\WP_Post $email_poststring $woo_content\WC_Email $wc_email ): ?string {
        try {
            
$subject             $wc_email->get_subject(); // We will get subject from $email_post after we add it to the editor.
            
$preheader           $wc_email->get_preheader();
            
$rendered_email_data $this->renderer->render$email_post$subject$preheader'en' );
            
$personalized_email  $this->personalizer->personalize_content$rendered_email_data['html'] );
            
$rendered_email      str_replaceself::WOO_EMAIL_CONTENT_PLACEHOLDER$woo_content$personalized_email );
            
add_filter'woocommerce_email_styles', array( $this->woo_content_processor'prepare_css' ), 10);
            return 
$rendered_email;
        } catch ( 
\Exception $e ) {
            
wc_caught_exception$e__METHOD__, array( $email_post$woo_content$wc_email ) );
            return 
null;
        }
    }

    
/**
     * Get the email post for a given WC_Email.
     *
     * @param \WC_Email $email WooCommerce email.
     * @return \WP_Post|null
     */
    
private function get_email_post_by_wc_email\WC_Email $email ): ?\WP_Post {
        return 
$this->template_manager->get_email_post$email->id );
    }
}