| @@ -1,10 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace YayMail\Emails; |
| 4 | 4 | |
| 5 | +use YayMail\Utils\Helpers; | |
| 5 | 6 | use YayMail\Utils\SingletonTrait; |
| 7 | +use YayMail\Utils\TemplateHelpers; | |
| 6 | 8 | use YayMail\YayMailEmails; |
| 9 | +use YayMail\YayMailTemplate; | |
| 7 | 10 | |
| 8 | 11 | /** |
| 9 | 12 | * EmailsLoader Class |
| 10 | 13 | * |
| @@ -55,21 +58,108 @@ | ||
| 55 | 58 | */ |
| 56 | 59 | |
| 57 | 60 | $yaymail_emails->register( \YayMail\Emails\CustomerPOSCompletedOrder::get_instance() ); |
| 58 | 61 | $yaymail_emails->register( \YayMail\Emails\CustomerPOSRefundedOrder::get_instance() ); |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Fulfillment emails (WC 10.1, `fulfillments` feature) and | |
| 65 | + * review request email (`customer_review_request` feature) | |
| 66 | + * | |
| 67 | + * @since 4.4.7 | |
| 68 | + */ | |
| 69 | + $yaymail_emails->register( \YayMail\Emails\CustomerFulfillmentCreated::get_instance() ); | |
| 70 | + $yaymail_emails->register( \YayMail\Emails\CustomerFulfillmentUpdated::get_instance() ); | |
| 71 | + $yaymail_emails->register( \YayMail\Emails\CustomerFulfillmentDeleted::get_instance() ); | |
| 72 | + $yaymail_emails->register( \YayMail\Emails\CustomerReviewRequest::get_instance() ); | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Core Back-in-Stock emails (WC 10.2, `customer_stock_notifications` feature) | |
| 76 | + * | |
| 77 | + * @since 4.4.7 | |
| 78 | + */ | |
| 79 | + $yaymail_emails->register( \YayMail\Emails\CustomerStockNotificationVerify::get_instance() ); | |
| 80 | + $yaymail_emails->register( \YayMail\Emails\CustomerStockNotificationVerified::get_instance() ); | |
| 81 | + $yaymail_emails->register( \YayMail\Emails\CustomerStockNotification::get_instance() ); | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Confirm email address (WC 11.0) and | |
| 85 | + * customer order withdrawal request (WC 11.2, `order_withdrawal` feature) | |
| 86 | + * | |
| 87 | + * @since 4.4.7 | |
| 88 | + */ | |
| 89 | + $yaymail_emails->register( \YayMail\Emails\CustomerVerifyEmail::get_instance() ); | |
| 90 | + $yaymail_emails->register( \YayMail\Emails\CustomerOrderWithdrawalRequested::get_instance() ); | |
| 59 | 91 | }//end if |
| 60 | 92 | |
| 61 | 93 | $yaymail_emails->register( \YayMail\Emails\GlobalHeaderFooter::get_instance() ); |
| 62 | 94 | |
| 63 | 95 | do_action( 'yaymail_register_emails', $yaymail_emails ); |
| 96 | + | |
| 97 | + $this->register_subject_shortcode_filters( $yaymail_emails ); | |
| 64 | 98 | } |
| 65 | 99 | |
| 100 | + /** | |
| 101 | + * Wire one woocommerce_email_subject_{id} filter per registered YayMail email so | |
| 102 | + * YayMail shortcodes typed into the subject resolve at send time. | |
| 103 | + * | |
| 104 | + * @param YayMailEmails $yaymail_emails Registered emails collection. | |
| 105 | + */ | |
| 106 | + private function register_subject_shortcode_filters( $yaymail_emails ) { | |
| 107 | + foreach ( $yaymail_emails->get_emails() as $email ) { | |
| 108 | + $email_id = $email->get_id(); | |
| 109 | + if ( empty( $email_id ) ) { | |
| 110 | + continue; | |
| 111 | + } | |
| 112 | + | |
| 113 | + add_filter( | |
| 114 | + 'woocommerce_email_subject_' . $email_id, | |
| 115 | + function( $subject, $object = null ) use ( $email_id ) { | |
| 116 | + return self::render_subject_shortcodes( $subject, $object, $email_id ); | |
| 117 | + }, | |
| 118 | + 10, | |
| 119 | + 2 | |
| 120 | + ); | |
| 121 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * Resolve YayMail shortcodes in a subject, but only when the email's YayMail | |
| 126 | + * template is active. Uses real order context only - never the sample-data | |
| 127 | + * fallback - so a real outgoing subject never leaks placeholder values. | |
| 128 | + * | |
| 129 | + * @param string $subject Subject after WooCommerce format_string(). | |
| 130 | + * @param mixed $object Email object; a WC_Order for order emails. | |
| 131 | + * @param string $email_id WooCommerce email id (also the YayMail template name). | |
| 132 | + * @return string | |
| 133 | + */ | |
| 134 | + public static function render_subject_shortcodes( $subject, $object, $email_id ) { | |
| 135 | + if ( ! is_string( $subject ) || false === strpos( $subject, '[' ) ) { | |
| 136 | + return $subject; | |
| 137 | + } | |
| 138 | + | |
| 139 | + $template = new YayMailTemplate( $email_id ); | |
| 140 | + if ( ! $template->is_enabled() ) { | |
| 141 | + return $subject; | |
| 142 | + } | |
| 143 | + | |
| 144 | + $order = ( $object instanceof \WC_Order ) ? $object : null; | |
| 145 | + $executor_data = [ | |
| 146 | + 'template' => $template, | |
| 147 | + 'render_data' => $order ? [ 'order' => $order ] : [], | |
| 148 | + 'settings' => yaymail_settings(), | |
| 149 | + // Real send: resolve to actual values, never editor placeholder tokens. | |
| 150 | + 'is_placeholder' => false, | |
| 151 | + ]; | |
| 152 | + | |
| 153 | + return TemplateHelpers::process_email_subject( $subject, $template, $executor_data ); | |
| 154 | + } | |
| 155 | + | |
| 66 | 156 | public function before_email_content( $template ) { |
| 67 | - include YAYMAIL_PLUGIN_PATH . 'templates/emails/before-email-content.php'; | |
| 157 | + include Helpers::get_plugin_path() . 'templates/emails/before-email-content.php'; | |
| 68 | 158 | } |
| 69 | 159 | |
| 70 | 160 | public function after_email_content( $template ) { |
| 71 | - include YAYMAIL_PLUGIN_PATH . 'templates/emails/after-email-content.php'; | |
| 161 | + include Helpers::get_plugin_path() . 'templates/emails/after-email-content.php'; | |
| 72 | 162 | } |
| 73 | 163 | |
| 74 | 164 | public function filter_safe_style_css( $default_array ) { |
| 75 | 165 | $additional_allowed_css_attributes = [ 'display', 'background-repeat', 'word-wrap' ]; |