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 |