/var/www/html_it/wp-content/plugins/woocommerce/src/Internal/EmailEditor/Integration.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php

declare( strict_types=);

namespace 
Automattic\WooCommerce\Internal\EmailEditor;

use 
Automattic\WooCommerce\EmailEditor\Email_Editor_Container;
use 
Automattic\WooCommerce\EmailEditor\Engine\Dependency_Check;
use 
Automattic\WooCommerce\Internal\Admin\EmailPreview\EmailPreview;
use 
Automattic\WooCommerce\Internal\EmailEditor\EmailPatterns\PatternsController;
use 
Automattic\WooCommerce\Internal\EmailEditor\EmailTemplates\TemplatesController;
use 
Automattic\WooCommerce\Internal\EmailEditor\Renderer\Blocks\WooContent;
use 
Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmails;
use 
Automattic\WooCommerce\Internal\EmailEditor\WCTransactionalEmails\WCTransactionalEmailPostsManager;
use 
Automattic\WooCommerce\Internal\EmailEditor\EmailTemplates\TemplateApiController;
use 
Automattic\WooCommerce\EmailEditor\Engine\Logger\Email_Editor_Logger;
use 
WP_Post;

defined'ABSPATH' ) || exit;

/**
 * Integration class for the Email Editor functionality.
 */
class Integration {
    const 
EMAIL_POST_TYPE 'woo_email';

    
/**
     * The email editor page renderer instance.
     *
     * @var PageRenderer
     */
    
private PageRenderer $editor_page_renderer;

    
/**
     * The dependency check instance.
     *
     * @var Dependency_Check
     */
    
private Dependency_Check $dependency_check;

    
/**
     * The template API controller instance.
     *
     * @var TemplateApiController
     */
    
private TemplateApiController $template_api_controller;

    
/**
     * The email data API controller instance.
     *
     * @var EmailApiController
     */
    
private EmailApiController $email_api_controller;

    
/**
     * Constructor.
     */
    
public function __construct() {
        
$editor_container       Email_Editor_Container::container();
        
$this->dependency_check $editor_container->getDependency_Check::class );
    }

    
/**
     * Initialize the integration.
     *
     * @internal
     */
    
final public function init(): void {
        if ( ! 
$this->dependency_check->are_dependencies_met() ) {
            
// If dependencies are not met, do not initialize the email editor integration.
            
return;
        }

        
add_action'woocommerce_init', array( $this'initialize' ) );
    }

    
/**
     * Initialize the integration.
     */
    
public function initialize() {
        
$this->init_logger();
        
$this->init_hooks();
        
$this->extend_post_api();
        
$this->extend_template_post_api();
        
$this->register_hooks();
    }

    
/**
     * Initialize the logger.
     */
    
public function init_logger() {
        
$editor_container Email_Editor_Container::container();
        
$logger           $editor_container->getEmail_Editor_Logger::class );

        
// Register the WooCommerce logger with the email editor package.
        
$logger->set_logger( new Loggerwc_get_logger() ) );
    }

    
/**
     * Initialize hooks for required classes.
     */
    
public function init_hooks() {
        
$container wc_get_container();
        
$container->getPatternsController::class );
        
$container->getTemplatesController::class );
        
$container->getPersonalizationTagManager::class );
        
$container->getBlockEmailRenderer::class );
        
$container->getWCTransactionalEmails::class );
        
$this->editor_page_renderer    $container->getPageRenderer::class );
        
$this->template_api_controller $container->getTemplateApiController::class );
        
$this->email_api_controller    $container->getEmailApiController::class );
    }

    
/**
     * Register hooks for the integration.
     */
    
public function register_hooks() {
        
add_filter'woocommerce_email_editor_post_types', array( $this'add_email_post_type' ) );
        
add_filter'woocommerce_is_email_editor_page', array( $this'is_editor_page' ), 10);
        
add_filter'replace_editor', array( $this'replace_editor' ), 10);
        
add_action'before_delete_post', array( $this'delete_email_template_associated_with_email_editor_post' ), 10);
        
add_filter'woocommerce_email_editor_send_preview_email_rendered_data', array( $this'update_send_preview_email_rendered_data' ) );
        
add_filter'woocommerce_email_editor_send_preview_email_personalizer_context', array( $this'update_send_preview_email_personalizer_context' ) );
        
add_filter'woocommerce_email_editor_preview_post_template_html', array( $this'update_preview_post_template_html_data' ), 100);
    }

    
/**
     * Add WooCommerce email post type to the list of supported post types.
     *
     * @param array $post_types List of post types.
     * @return array Modified list of post types.
     */
    
public function add_email_post_type( array $post_types ): array {
        
$post_types[] = array(
            
'name' => self::EMAIL_POST_TYPE,
            
'args' => array(
                
'labels'   => array(
                    
'name'          => __'Emails''woocommerce' ),
                    
'singular_name' => __'Email''woocommerce' ),
                    
'add_new_item'  => __'Add Email''woocommerce' ),
                    
'edit_item'     => __'Edit Email''woocommerce' ),
                    
'new_item'      => __'New Email''woocommerce' ),
                    
'view_item'     => __'View Email''woocommerce' ),
                    
'search_items'  => __'Search Emails''woocommerce' ),
                ),
                
'rewrite'  => array( 'slug' => self::EMAIL_POST_TYPE ),
                
'supports' => array(
                    
'title',
                    
'editor' => array(
                        
'default-mode' => 'template-locked',
                    ),
                    
'excerpt',
                ),
            ),
        );
        return 
$post_types;
    }

    
/**
     * Check if current page is email editor page.
     *
     * @param bool $is_editor_page Current editor page status.
     * @return bool Whether current page is email editor page.
     */
    
public function is_editor_pagebool $is_editor_page ): bool {
        if ( 
$is_editor_page ) {
            return 
$is_editor_page;
        }

        
// 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.
        
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).
            
$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).
            
return $post && self::EMAIL_POST_TYPE === $post->post_type;
        }

        return 
false;
    }

    
/**
     * Replace the default editor with our custom email editor.
     *
     * @param bool    $replace Whether to replace the editor.
     * @param WP_Post $post    Post object.
     * @return bool Whether the editor was replaced.
     */
    
public function replace_editor$replace$post ) {
        
$current_screen get_current_screen();
        if ( 
self::EMAIL_POST_TYPE === $post->post_type && $current_screen ) {
            
$this->editor_page_renderer->render();
            return 
true;
        }
        return 
$replace;
    }

    
/**
     * Delete the email template associated with the email editor post when the post is permanently deleted.
     *
     * @param int     $post_id The post ID.
     * @param WP_Post $post    The post object.
     */
    
public function delete_email_template_associated_with_email_editor_post$post_id$post ) {
        if ( 
self::EMAIL_POST_TYPE !== $post->post_type ) {
            return;
        }

        
$post_manager WCTransactionalEmailPostsManager::get_instance();

        
$email_type $post_manager->get_email_type_from_post_id$post_id );

        if ( empty( 
$email_type ) ) {
            return;
        }

        
$post_manager->delete_email_template$email_type );
    }

    
/**
     * Extend the post API for the wp_template post type to add and save the woocommerce_data field.
     */
    
public function extend_template_post_api(): void {
        
register_rest_field(
            
'wp_template',
            
'woocommerce_data',
            array(
                
'get_callback'    => array( $this->template_api_controller'get_template_data' ),
                
'update_callback' => array( $this->template_api_controller'save_template_data' ),
                
'schema'          => $this->template_api_controller->get_template_data_schema(),
            )
        );
    }

    
/**
     * Filter email preview data to replace placeholders with actual content.
     *
     * This method retrieves the appropriate email type based on the request,
     * generates the email content using the WooContentProcessor, and replaces
     * the placeholder in the preview HTML.
     *
     * @param string $data       The preview data.
     * @param string $email_type The email type identifier (e.g., 'customer_processing_order').
     * @return string The updated preview data with placeholders replaced.
     */
    
private function update_email_preview_data$datastring $email_type ) {
        
$type_param EmailPreview::DEFAULT_EMAIL_TYPE;

        if ( ! empty( 
$email_type ) ) {
            
$type_param WCTransactionalEmailPostsManager::get_instance()->get_email_type_class_name_from_template_name$email_type );
        }

        
$email_preview wc_get_container()->getEmailPreview::class );

        try {
            
$message $email_preview->generate_placeholder_content$type_param );
        } catch ( 
\InvalidArgumentException $e ) {
            
// If the provided type was invalid, fall back to the default.
            
try {
                
$message $email_preview->generate_placeholder_contentEmailPreview::DEFAULT_EMAIL_TYPE );
            } catch ( 
\Throwable $e ) {
                return 
$data;
            }
        } catch ( 
\Throwable $e ) {
            return 
$data;
        }

        return 
str_replaceBlockEmailRenderer::WOO_EMAIL_CONTENT_PLACEHOLDER$message$data );
    }

    
/**
     * Filter email preview data used when sending a preview email.
     *
     * @param string $data The preview data.
     * @return string The updated preview data with placeholders replaced.
     */
    
public function update_send_preview_email_rendered_data$data ) {
        
$email_type '';
        
$post_body  file_get_contents'php://input' );

        if ( 
$post_body ) {
            
$decoded_body json_decode$post_body );

            if ( 
json_last_error() === JSON_ERROR_NONE && isset( $decoded_body->postId ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
                
$post_id absint$decoded_body->postId ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

                
$email_type WCTransactionalEmailPostsManager::get_instance()->get_email_type_from_post_id$post_id );
                if ( ! empty( 
$email_type ) ) {
                    return 
$this->update_email_preview_data$data$email_type );
                }
            }
        }
        return 
$data;
    }

    
/**
     * Update the personalizer context for the send preview email.
     *
     * @param array $context The personalizer context.
     * @return array The updated personalizer context.
     */
    
public function update_send_preview_email_personalizer_context$context ) {
        
$post_manager             WCTransactionalEmailPostsManager::get_instance();
        
$email_type_template_name $post_manager->get_email_type_from_post_idget_the_ID() );
        
$email_type               $email_type_template_name $post_manager->get_email_type_class_name_from_template_name$email_type_template_name ) : EmailPreview::DEFAULT_EMAIL_TYPE;
        
$email_preview            wc_get_container()->getEmailPreview::class );

        try {
            
$email_preview->set_email_type$email_type );
        } catch ( 
\InvalidArgumentException $e ) {
            
// If the email type is invalid, return the context data as is.
            
return $context;
        }

        
$email            $email_preview->get_email();
        
$email->recipient $context['recipient_email'] ?? '';
        
$personalizer     wc_get_container()->getTransactionalEmailPersonalizer::class );

        return 
$personalizer->prepare_context_data$context$email );
    }

    
/**
     * Filter email preview data used when previewing the email in new tab.
     *
     * @param string $data The preview HTML string.
     * @return string The updated preview HTML with placeholders replaced.
     */
    
public function update_preview_post_template_html_data$data ) {
        
// phpcs:disable WordPress.Security.NonceVerification.Recommended
        // Nonce verification is disabled here because the preview action doesn't modify data,
        // and the check caused issues with the 'Preview in new tab' feature due to context changes.
        
$type_param = isset( $_GET['woo_email'] ) ? sanitize_text_fieldwp_unslash$_GET['woo_email'] ) ) : '';
        
// phpcs:enable
        
return $this->update_email_preview_data$data$type_param );
    }

    
/**
     * Extend the post API for the woo_email post type to add and save the woocommerce_data field.
     */
    
public function extend_post_api(): void {
        
register_rest_field(
            
self::EMAIL_POST_TYPE,
            
'woocommerce_data',
            array(
                
'get_callback'    => array( $this->email_api_controller'get_email_data' ),
                
'update_callback' => array( $this->email_api_controller'save_email_data' ),
                
'schema'          => $this->email_api_controller->get_email_data_schema(),
            )
        );
    }
}