EmailPatterns
10 months ago
EmailTemplates
2 months ago
PersonalizationTags
3 months ago
WCTransactionalEmails
4 weeks ago
BlockEmailRenderer.php
7 months ago
EmailApiController.php
4 weeks ago
Integration.php
4 weeks ago
Logger.php
10 months ago
Package.php
1 year ago
PageRenderer.php
10 months ago
PersonalizationTagManager.php
1 year ago
TransactionalEmailPersonalizer.php
4 months ago
WooContentProcessor.php
2 months ago
TransactionalEmailPersonalizer.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for handling transactional email personalization. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\Internal\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types = 1); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\EmailEditor; |
| 11 | |
| 12 | use Automattic\WooCommerce\EmailEditor\Email_Editor_Container; |
| 13 | use Automattic\WooCommerce\EmailEditor\Engine\Personalizer; |
| 14 | |
| 15 | /** |
| 16 | * Class TransactionalEmailPersonalizer that internally uses the Personalizer class. |
| 17 | * The inheritance is not used here because Personalizer needs to pass Personalization_Tags_Registry and |
| 18 | * the combination of two different dependency injection containers is not possible. |
| 19 | */ |
| 20 | class TransactionalEmailPersonalizer { |
| 21 | /** |
| 22 | * Personalizer instance for handling email content personalization. |
| 23 | * |
| 24 | * @var Personalizer |
| 25 | */ |
| 26 | private Personalizer $personalizer; |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | $editor_container = Email_Editor_Container::container(); |
| 33 | $this->personalizer = $editor_container->get( Personalizer::class ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Personalize transactional email content with specific handling. |
| 38 | * |
| 39 | * @param string $content The content to personalize. |
| 40 | * @param \WC_Email $email The WooCommerce email object. |
| 41 | * @return string The personalized content. |
| 42 | */ |
| 43 | public function personalize_transactional_content( string $content, \WC_Email $email ): string { |
| 44 | $this->configure_context_by_email( $email ); |
| 45 | return $this->personalizer->personalize_content( $content ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Configure personalization context based on WooCommerce email object. |
| 50 | * |
| 51 | * @param \WC_Email $email The WooCommerce email object. |
| 52 | * @return void |
| 53 | */ |
| 54 | public function configure_context_by_email( \WC_Email $email ): void { |
| 55 | $prepared_context = $this->prepare_context_data( $this->personalizer->get_context(), $email ); |
| 56 | $this->personalizer->set_context( $prepared_context ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Prepare context data for email personalization. |
| 61 | * Adds new order specific context data. |
| 62 | * |
| 63 | * @param array $previous_context Previous version of context data. |
| 64 | * @param \WC_Email $email The WooCommerce email object. |
| 65 | * @return array Context data for personalization |
| 66 | */ |
| 67 | public function prepare_context_data( array $previous_context, \WC_Email $email ): array { |
| 68 | $context = $previous_context; |
| 69 | |
| 70 | $context['recipient_email'] = $email->get_recipient(); |
| 71 | $context['order'] = $email->object instanceof \WC_Order ? $email->object : null; |
| 72 | // For emails of type new_user or reset_password we want to set user directly from the object. |
| 73 | if ( $email->object instanceof \WP_User ) { |
| 74 | $context['wp_user'] = $email->object; |
| 75 | } elseif ( $email->object instanceof \WC_Order ) { |
| 76 | $context['wp_user'] = $email->object->get_user(); |
| 77 | } else { |
| 78 | $context['wp_user'] = null; |
| 79 | } |
| 80 | $context['wc_email'] = $email; |
| 81 | |
| 82 | $core_context = $context; |
| 83 | |
| 84 | /** |
| 85 | * Filters the context data for email personalization. |
| 86 | * |
| 87 | * This filter fires after core defaults are set, allowing extensions |
| 88 | * to override values like wp_user for custom email types (e.g., WooCommerce Bookings). |
| 89 | * |
| 90 | * @since 10.5.0 |
| 91 | * @param array $context Context data including core defaults. |
| 92 | * @param \WC_Email $email The WooCommerce email object. |
| 93 | * @return array Context data for personalization |
| 94 | */ |
| 95 | $context = apply_filters( 'woocommerce_email_editor_integration_personalizer_context_data', $context, $email ); |
| 96 | |
| 97 | if ( ! is_array( $context ) ) { |
| 98 | $context = $core_context; |
| 99 | } |
| 100 | |
| 101 | return $context; |
| 102 | } |
| 103 | } |
| 104 |