Logger
11 months ago
Patterns
11 months ago
PersonalizationTags
2 days ago
Renderer
2 days ago
Templates
4 months ago
class-assets-manager.php
5 months ago
class-dependency-check.php
11 months ago
class-email-api-controller.php
6 months ago
class-email-editor.php
2 days ago
class-email-styles-schema.php
11 months ago
class-personalizer.php
2 days ago
class-send-preview-email.php
5 months ago
class-settings-controller.php
3 months ago
class-site-style-sync-controller.php
4 months ago
class-theme-controller.php
3 months ago
class-user-theme.php
11 months ago
content-editor.css
2 weeks ago
content-shared.css
11 months ago
index.php
11 months ago
theme.json
3 months ago
class-send-preview-email.php
92 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Engine\Renderer\Renderer; |
| 6 | class Send_Preview_Email { |
| 7 | private Renderer $renderer; |
| 8 | private Personalizer $personalizer; |
| 9 | public function __construct( |
| 10 | Renderer $renderer, |
| 11 | Personalizer $personalizer |
| 12 | ) { |
| 13 | $this->renderer = $renderer; |
| 14 | $this->personalizer = $personalizer; |
| 15 | } |
| 16 | public function send_preview_email( $data ): bool { |
| 17 | if ( is_bool( $data ) ) { |
| 18 | // preview mail already sent. Do not process again. |
| 19 | return $data; |
| 20 | } |
| 21 | $this->validate_data( $data ); |
| 22 | $email = $data['email']; |
| 23 | $post_id = $data['postId']; |
| 24 | $post = $this->fetch_post( $post_id ); |
| 25 | $subject = $this->get_preview_email_subject( $post ); |
| 26 | $email_html_content = $this->render_html( $post ); |
| 27 | return $this->send_email( $email, $subject, $email_html_content ); |
| 28 | } |
| 29 | public function render_html( $post ): string { |
| 30 | $subject = $this->get_preview_email_subject( $post ); |
| 31 | $language = get_bloginfo( 'language' ); |
| 32 | // Add filter to set preview context for block renderers. |
| 33 | add_filter( 'woocommerce_email_editor_rendering_email_context', array( $this, 'add_preview_context' ) ); |
| 34 | $rendered_data = $this->renderer->render( |
| 35 | $post, |
| 36 | $subject, |
| 37 | __( 'Preview', 'woocommerce' ), |
| 38 | $language |
| 39 | ); |
| 40 | // Remove filter after rendering. |
| 41 | remove_filter( 'woocommerce_email_editor_rendering_email_context', array( $this, 'add_preview_context' ) ); |
| 42 | $rendered_data = apply_filters( 'woocommerce_email_editor_send_preview_email_rendered_data', $rendered_data, $post ); |
| 43 | return $this->set_personalize_content( $rendered_data['html'] ); |
| 44 | } |
| 45 | public function get_preview_email_subject( $post ): string { |
| 46 | $subject = (string) apply_filters( 'woocommerce_email_editor_send_preview_email_subject', $post->post_title, $post ); |
| 47 | return $subject; |
| 48 | } |
| 49 | public function add_preview_context( $email_context ): array { |
| 50 | $email_context['is_user_preview'] = true; |
| 51 | return $email_context; |
| 52 | } |
| 53 | public function set_personalize_content( string $content ): string { |
| 54 | $current_user = wp_get_current_user(); |
| 55 | $subscriber = ! empty( $current_user->ID ) ? $current_user : null; |
| 56 | $personalizer_context = array( |
| 57 | 'recipient_email' => $subscriber ? $subscriber->user_email : null, |
| 58 | 'is_user_preview' => true, |
| 59 | ); |
| 60 | $personalizer_context = apply_filters( 'woocommerce_email_editor_send_preview_email_personalizer_context', $personalizer_context ); |
| 61 | $this->personalizer->set_context( $personalizer_context ); |
| 62 | return $this->personalizer->personalize_content( $content ); |
| 63 | } |
| 64 | public function send_email( string $to, string $subject, string $body ): bool { |
| 65 | do_action( 'woocommerce_email_editor_send_preview_email_before_wp_mail', $to, $subject, $body ); |
| 66 | add_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) ); |
| 67 | $result = wp_mail( $to, $subject, $body ); |
| 68 | // Reset content-type to avoid conflicts. |
| 69 | remove_filter( 'wp_mail_content_type', array( $this, 'set_mail_content_type' ) ); |
| 70 | do_action( 'woocommerce_email_editor_send_preview_email_after_wp_mail', $to, $subject, $body, $result ); |
| 71 | return $result; |
| 72 | } |
| 73 | public function set_mail_content_type( string $content_type ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 74 | return 'text/html'; |
| 75 | } |
| 76 | private function validate_data( array $data ) { |
| 77 | if ( empty( $data['email'] ) || empty( $data['postId'] ) ) { |
| 78 | throw new \InvalidArgumentException( esc_html__( 'Missing required data', 'woocommerce' ) ); |
| 79 | } |
| 80 | if ( ! is_email( $data['email'] ) ) { |
| 81 | throw new \InvalidArgumentException( esc_html__( 'Invalid email', 'woocommerce' ) ); |
| 82 | } |
| 83 | } |
| 84 | private function fetch_post( $post_id ): \WP_Post { |
| 85 | $post = get_post( intval( $post_id ) ); |
| 86 | if ( ! $post instanceof \WP_Post ) { |
| 87 | throw new \Exception( esc_html__( 'Invalid post', 'woocommerce' ) ); |
| 88 | } |
| 89 | return $post; |
| 90 | } |
| 91 | } |
| 92 |