| 1 |
<?php |
| 2 |
namespace YayMail\Abstracts; |
| 3 |
|
| 4 |
use YayMail\Models\TemplateModel; |
| 5 |
use YayMail\YayMailTemplate; |
| 6 |
|
| 7 |
/** |
| 8 |
* Base Email Class |
| 9 |
*/ |
| 10 |
abstract class BaseEmail { |
| 11 |
|
| 12 |
/** |
| 13 |
* Contains id of the email |
| 14 |
* Id also means template name in some case |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $id; |
| 19 |
|
| 20 |
/** |
| 21 |
* Email name |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $title; |
| 26 |
|
| 27 |
/** |
| 28 |
* Contains recipient |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $recipient; |
| 33 |
|
| 34 |
/** |
| 35 |
* Which plugin email created by |
| 36 |
*/ |
| 37 |
protected $source = [ |
| 38 |
'plugin_id' => 'woocommerce', |
| 39 |
'plugin_name' => 'WooCommerce', |
| 40 |
]; |
| 41 |
|
| 42 |
protected $shortcodes = []; |
| 43 |
|
| 44 |
protected $root_email = null; |
| 45 |
|
| 46 |
protected $is_existed = true; |
| 47 |
|
| 48 |
/** |
| 49 |
* Example values: non_order, order, global_header_footer, ... |
| 50 |
*/ |
| 51 |
public $email_types = [ YAYMAIL_WITH_ORDER_EMAILS ]; |
| 52 |
|
| 53 |
/** |
| 54 |
* Indicate which template that process is working on |
| 55 |
*/ |
| 56 |
public $template = null; |
| 57 |
|
| 58 |
/** |
| 59 |
* Render priority |
| 60 |
* |
| 61 |
* @var int |
| 62 |
*/ |
| 63 |
protected $render_priority = YAYMAIL_EMAIL_RENDER_PRIORITY; |
| 64 |
|
| 65 |
/** |
| 66 |
* Callback for yaymail_emails hook |
| 67 |
* Return this email data |
| 68 |
*/ |
| 69 |
public function get_email_data() { |
| 70 |
return [ |
| 71 |
'id' => $this->id, |
| 72 |
'title' => $this->title, |
| 73 |
'recipient' => $this->recipient, |
| 74 |
'source' => $this->source, |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
abstract public function get_template_path(); |
| 79 |
|
| 80 |
abstract public function get_default_elements(); |
| 81 |
|
| 82 |
/** |
| 83 |
* Function check current template is WooCommerce email |
| 84 |
* Return boolean |
| 85 |
*/ |
| 86 |
protected function is_template_email( $email ) { |
| 87 |
return ! empty( $email->id ) && $email->id === $this->id; |
| 88 |
} |
| 89 |
|
| 90 |
public function get_language( $order ) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
public function get_id() { |
| 95 |
return $this->id; |
| 96 |
} |
| 97 |
|
| 98 |
public function get_template_file( $located, $template_name, $args ) { |
| 99 |
if ( ! isset( $args['email'] ) ) { |
| 100 |
return $located; |
| 101 |
} |
| 102 |
if ( ! $args['email'] instanceof \WC_Email || ! $this->is_template_email( $args['email'] ) ) { |
| 103 |
return $located; |
| 104 |
} |
| 105 |
$template_path = $this->get_template_path(); |
| 106 |
if ( ! file_exists( $template_path ) ) { |
| 107 |
return $located; |
| 108 |
} |
| 109 |
|
| 110 |
return $this->load_template( $args ) ? $template_path : $located; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Resolve the variant and load the YayMail template into $this->template. |
| 115 |
* The default design decides whether YayMail renders this email at all; |
| 116 |
* a variant only swaps the design when an addon returns one via `yaymail_email_get_variant`. |
| 117 |
* |
| 118 |
* @param array $args wc_get_template args. |
| 119 |
* @param string|null $template_name Template to load; defaults to this email's id. |
| 120 |
* @return bool True when a YayMail template should render. |
| 121 |
*/ |
| 122 |
protected function load_template( $args, $template_name = null ) { |
| 123 |
$template_name = $template_name ?? $this->id; |
| 124 |
$order = isset( $args['order'] ) ? $args['order'] : null; |
| 125 |
|
| 126 |
$this->template = new YayMailTemplate( $template_name ); |
| 127 |
if ( ! $this->template->is_enabled() ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
$variant = yaymail_sanitize_template_variant( apply_filters( 'yaymail_email_get_variant', '', $order, $args, $this, $template_name ) ); |
| 132 |
if ( '' !== $variant ) { |
| 133 |
$this->template = new YayMailTemplate( $template_name, '', $variant ); |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
public function get_title() { |
| 140 |
return $this->title ?? ''; |
| 141 |
} |
| 142 |
|
| 143 |
public function get_recipient() { |
| 144 |
return $this->recipient ?? ''; |
| 145 |
} |
| 146 |
|
| 147 |
public function get_source() { |
| 148 |
return $this->source; |
| 149 |
} |
| 150 |
|
| 151 |
public function register_shortcodes( $shortcodes ) { |
| 152 |
$this->shortcodes = array_merge( $this->shortcodes, $shortcodes ); |
| 153 |
} |
| 154 |
|
| 155 |
public function get_shortcodes() { |
| 156 |
return $this->shortcodes; |
| 157 |
} |
| 158 |
|
| 159 |
public function get_root_email() { |
| 160 |
return $this->root_email; |
| 161 |
} |
| 162 |
|
| 163 |
public function is_existed() { |
| 164 |
return ! empty( $this->id ); |
| 165 |
} |
| 166 |
|
| 167 |
public function maybe_disable_block_email_editor() { |
| 168 |
if ( ! \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'block_email_editor' ) ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
if ( ! $this->root_email || ! $this->root_email instanceof \WC_Email ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
// One batched lookup shared by all emails instead of a per-email query at init. |
| 175 |
if ( in_array( $this->id, TemplateModel::get_active_default_template_names(), true ) ) { |
| 176 |
$this->root_email->block_email_editor_enabled = false; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|