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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / packages / email-editor / src / Engine / class-send-preview-email.php
woocommerce / packages / email-editor / src / Engine Last commit date
Logger 10 months ago Patterns 10 months ago PersonalizationTags 7 months ago Renderer 1 month ago Templates 2 months ago class-assets-manager.php 4 months ago class-dependency-check.php 1 year ago class-email-api-controller.php 4 months ago class-email-editor.php 4 months ago class-email-styles-schema.php 1 year ago class-personalizer.php 4 months ago class-send-preview-email.php 3 months ago class-settings-controller.php 2 months ago class-site-style-sync-controller.php 2 months ago class-theme-controller.php 2 months ago class-user-theme.php 1 year ago content-editor.css 9 months ago content-shared.css 10 months ago theme.json 2 months ago
class-send-preview-email.php
228 lines
1 <?php
2 /**
3 * This file is part of the WooCommerce Email Editor package.
4 *
5 * @package Automattic\WooCommerce\EmailEditor
6 */
7
8 declare( strict_types = 1 );
9
10 namespace Automattic\WooCommerce\EmailEditor\Engine;
11
12 use Automattic\WooCommerce\EmailEditor\Engine\Renderer\Renderer;
13
14 /**
15 * Class Send_Preview_Email
16 *
17 * This class is responsible for handling the functionality to send preview emails.
18 * It is part of the email editor integrations utilities.
19 *
20 * @package Automattic\WooCommerce\EmailEditor\Integrations\Utils
21 */
22 class Send_Preview_Email {
23
24 /**
25 * Instance of the Renderer class used for rendering the editor emails.
26 *
27 * @var Renderer $renderer
28 */
29 private Renderer $renderer;
30
31 /**
32 * Instance of the Personalizer class used for rendering personalization tags.
33 *
34 * @var Personalizer $personalizer
35 */
36 private Personalizer $personalizer;
37
38 /**
39 * Send_Preview_Email constructor.
40 *
41 * @param Renderer $renderer renderer instance.
42 * @param Personalizer $personalizer personalizer instance.
43 */
44 public function __construct(
45 Renderer $renderer,
46 Personalizer $personalizer
47 ) {
48 $this->renderer = $renderer;
49 $this->personalizer = $personalizer;
50 }
51
52 /**
53 * Sends a preview email.
54 *
55 * @param array $data The data required to send the preview email.
56 * @return bool Returns true if the preview email was sent successfully, false otherwise.
57 * @throws \Exception If the data is invalid.
58 */
59 public function send_preview_email( $data ): bool {
60
61 if ( is_bool( $data ) ) {
62 // preview mail already sent. Do not process again.
63 return $data;
64 }
65
66 $this->validate_data( $data );
67
68 $email = $data['email'];
69 $post_id = $data['postId'];
70
71 $post = $this->fetch_post( $post_id );
72 $subject = $this->get_preview_email_subject( $post );
73
74 $email_html_content = $this->render_html( $post );
75
76 return $this->send_email( $email, $subject, $email_html_content );
77 }
78
79 /**
80 * Renders the HTML content of the post
81 *
82 * @param \WP_Post $post The WordPress post object.
83 * @return string
84 */
85 public function render_html( $post ): string {
86 $subject = $this->get_preview_email_subject( $post );
87 $language = get_bloginfo( 'language' );
88
89 // Add filter to set preview context for block renderers.
90 add_filter( 'woocommerce_email_editor_rendering_email_context', array( $this, 'add_preview_context' ) );
91
92 $rendered_data = $this->renderer->render(
93 $post,
94 $subject,
95 __( 'Preview', 'woocommerce' ),
96 $language
97 );
98
99 // Remove filter after rendering.
100 remove_filter( 'woocommerce_email_editor_rendering_email_context', array( $this, 'add_preview_context' ) );
101
102 $rendered_data = apply_filters( 'woocommerce_email_editor_send_preview_email_rendered_data', $rendered_data, $post );
103
104 return $this->set_personalize_content( $rendered_data['html'] );
105 }
106
107 /**
108 * Get the subject of the preview email.
109 *
110 * @param \WP_Post $post The WordPress post object.
111 * @return string
112 */
113 public function get_preview_email_subject( $post ): string {
114 /**
115 * Filters the subject of the preview email before it is sent or rendered.
116 *
117 * @param string $subject The email subject, defaults to the post title.
118 * @param \WP_Post $post The email post object.
119 *
120 * @since 2.9.0
121 */
122 $subject = (string) apply_filters( 'woocommerce_email_editor_send_preview_email_subject', $post->post_title, $post );
123 return $subject;
124 }
125
126 /**
127 * Add preview context to email rendering.
128 *
129 * This filter callback adds the is_user_preview flag and current user information
130 * to the rendering context, allowing block renderers to show appropriate preview content.
131 *
132 * @param array $email_context Email context data.
133 * @return array Modified email context with preview flag.
134 */
135 public function add_preview_context( $email_context ): array {
136 $email_context['is_user_preview'] = true;
137 return $email_context;
138 }
139
140 /**
141 * Personalize the content.
142 *
143 * @param string $content HTML content.
144 * @return string
145 */
146 public function set_personalize_content( string $content ): string {
147 $current_user = wp_get_current_user();
148 $subscriber = ! empty( $current_user->ID ) ? $current_user : null;
149
150 $personalizer_context = array(
151 'recipient_email' => $subscriber ? $subscriber->user_email : null,
152 'is_user_preview' => true,
153 );
154 $personalizer_context = apply_filters( 'woocommerce_email_editor_send_preview_email_personalizer_context', $personalizer_context );
155
156 $this->personalizer->set_context( $personalizer_context );
157 return $this->personalizer->personalize_content( $content );
158 }
159
160 /**
161 * Sends an email preview.
162 *
163 * @param string $to The recipient email address.
164 * @param string $subject The subject of the email.
165 * @param string $body The body content of the email.
166 * @return bool Returns true if the email was sent successfully, false otherwise.
167 */
168 public function send_email( string $to, string $subject, string $body ): bool {
169 do_action( 'woocommerce_email_editor_send_preview_email_before_wp_mail', $to, $subject, $body );
170
171 add_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) );
172
173 $result = wp_mail( $to, $subject, $body );
174
175 // Reset content-type to avoid conflicts.
176 remove_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) );
177
178 do_action( 'woocommerce_email_editor_send_preview_email_after_wp_mail', $to, $subject, $body, $result );
179
180 return $result;
181 }
182
183
184 /**
185 * Sets the mail content type. Used by $this->send_email.
186 *
187 * @param string $content_type The content type to be set for the mail.
188 * @return string The content type that was set.
189 */
190 public function set_mail_content_type( string $content_type ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
191 return 'text/html';
192 }
193
194 /**
195 * Validates the provided data array.
196 *
197 * @param array $data The data array to be validated.
198 *
199 * @return void
200 * @throws \InvalidArgumentException If the data is invalid.
201 */
202 private function validate_data( array $data ) {
203 if ( empty( $data['email'] ) || empty( $data['postId'] ) ) {
204 throw new \InvalidArgumentException( esc_html__( 'Missing required data', 'woocommerce' ) );
205 }
206
207 if ( ! is_email( $data['email'] ) ) {
208 throw new \InvalidArgumentException( esc_html__( 'Invalid email', 'woocommerce' ) );
209 }
210 }
211
212
213 /**
214 * Fetches a post_id post object based on the provided post ID.
215 *
216 * @param int $post_id The ID of the post to fetch.
217 * @return \WP_Post The WordPress post object.
218 * @throws \Exception If the post is invalid.
219 */
220 private function fetch_post( $post_id ): \WP_Post {
221 $post = get_post( intval( $post_id ) );
222 if ( ! $post instanceof \WP_Post ) {
223 throw new \Exception( esc_html__( 'Invalid post', 'woocommerce' ) );
224 }
225 return $post;
226 }
227 }
228