| @@ -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 | * |
| @@ -60,16 +63,74 @@ | ||
| 60 | 63 | |
| 61 | 64 | $yaymail_emails->register( \YayMail\Emails\GlobalHeaderFooter::get_instance() ); |
| 62 | 65 | |
| 63 | 66 | do_action( 'yaymail_register_emails', $yaymail_emails ); |
| 67 | + | |
| 68 | + $this->register_subject_shortcode_filters( $yaymail_emails ); | |
| 64 | 69 | } |
| 65 | 70 | |
| 71 | + /** | |
| 72 | + * Wire one woocommerce_email_subject_{id} filter per registered YayMail email so | |
| 73 | + * YayMail shortcodes typed into the subject resolve at send time. | |
| 74 | + * | |
| 75 | + * @param YayMailEmails $yaymail_emails Registered emails collection. | |
| 76 | + */ | |
| 77 | + private function register_subject_shortcode_filters( $yaymail_emails ) { | |
| 78 | + foreach ( $yaymail_emails->get_emails() as $email ) { | |
| 79 | + $email_id = $email->get_id(); | |
| 80 | + if ( empty( $email_id ) ) { | |
| 81 | + continue; | |
| 82 | + } | |
| 83 | + | |
| 84 | + add_filter( | |
| 85 | + 'woocommerce_email_subject_' . $email_id, | |
| 86 | + function( $subject, $object = null ) use ( $email_id ) { | |
| 87 | + return self::render_subject_shortcodes( $subject, $object, $email_id ); | |
| 88 | + }, | |
| 89 | + 10, | |
| 90 | + 2 | |
| 91 | + ); | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Resolve YayMail shortcodes in a subject, but only when the email's YayMail | |
| 97 | + * template is active. Uses real order context only - never the sample-data | |
| 98 | + * fallback - so a real outgoing subject never leaks placeholder values. | |
| 99 | + * | |
| 100 | + * @param string $subject Subject after WooCommerce format_string(). | |
| 101 | + * @param mixed $object Email object; a WC_Order for order emails. | |
| 102 | + * @param string $email_id WooCommerce email id (also the YayMail template name). | |
| 103 | + * @return string | |
| 104 | + */ | |
| 105 | + public static function render_subject_shortcodes( $subject, $object, $email_id ) { | |
| 106 | + if ( ! is_string( $subject ) || false === strpos( $subject, '[' ) ) { | |
| 107 | + return $subject; | |
| 108 | + } | |
| 109 | + | |
| 110 | + $template = new YayMailTemplate( $email_id ); | |
| 111 | + if ( ! $template->is_enabled() ) { | |
| 112 | + return $subject; | |
| 113 | + } | |
| 114 | + | |
| 115 | + $order = ( $object instanceof \WC_Order ) ? $object : null; | |
| 116 | + $executor_data = [ | |
| 117 | + 'template' => $template, | |
| 118 | + 'render_data' => $order ? [ 'order' => $order ] : [], | |
| 119 | + 'settings' => yaymail_settings(), | |
| 120 | + // Real send: resolve to actual values, never editor placeholder tokens. | |
| 121 | + 'is_placeholder' => false, | |
| 122 | + ]; | |
| 123 | + | |
| 124 | + return TemplateHelpers::process_email_subject( $subject, $template, $executor_data ); | |
| 125 | + } | |
| 126 | + | |
| 66 | 127 | public function before_email_content( $template ) { |
| 67 | - include YAYMAIL_PLUGIN_PATH . 'templates/emails/before-email-content.php'; | |
| 128 | + include Helpers::get_plugin_path() . 'templates/emails/before-email-content.php'; | |
| 68 | 129 | } |
| 69 | 130 | |
| 70 | 131 | public function after_email_content( $template ) { |
| 71 | - include YAYMAIL_PLUGIN_PATH . 'templates/emails/after-email-content.php'; | |
| 132 | + include Helpers::get_plugin_path() . 'templates/emails/after-email-content.php'; | |
| 72 | 133 | } |
| 73 | 134 | |
| 74 | 135 | public function filter_safe_style_css( $default_array ) { |
| 75 | 136 | $additional_allowed_css_attributes = [ 'display', 'background-repeat', 'word-wrap' ]; |