MarketingConfirmation.php
195 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\WooCommerce\Emails; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 10 | |
| 11 | if (!defined('ABSPATH')) exit; |
| 12 | |
| 13 | // phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps -- WC_Email properties use snake case. |
| 14 | // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- WC_Email methods use snake case. |
| 15 | |
| 16 | /** |
| 17 | * Marketing Confirmation Email |
| 18 | * |
| 19 | * This email is sent to confirm marketing subscriptions. |
| 20 | * Only available in Garden environment. |
| 21 | */ |
| 22 | class MarketingConfirmation extends \WC_Email { |
| 23 | /** |
| 24 | * Email group slug. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public $email_group; |
| 29 | |
| 30 | public function __construct() { |
| 31 | $this->id = 'mailpoet_marketing_confirmation'; |
| 32 | $this->title = __('MailPoet Marketing Confirmation', 'mailpoet'); |
| 33 | $this->description = __('Send an email for customers to confirm their subscription to marketing emails.', 'mailpoet'); |
| 34 | $this->customer_email = true; |
| 35 | $this->email_group = 'marketing'; |
| 36 | $this->heading = $this->get_default_heading(); |
| 37 | $this->subject = $this->get_default_subject(); |
| 38 | $this->template_base = $this->get_template_base_path(); |
| 39 | $this->template_html = 'emails/marketing-confirmation.php'; |
| 40 | $this->template_plain = 'emails/plain/marketing-confirmation.php'; |
| 41 | $this->placeholders = [ |
| 42 | '{site_title}' => $this->get_blogname(), |
| 43 | '{activation_link}' => '', |
| 44 | '{subscriber_firstname}' => '', |
| 45 | ]; |
| 46 | |
| 47 | // Call parent constructor |
| 48 | parent::__construct(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get template base path. |
| 53 | */ |
| 54 | public function get_template_base_path() { |
| 55 | return __DIR__ . '/Templates/'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get email subject. |
| 60 | */ |
| 61 | public function get_default_subject() { |
| 62 | return __('Confirm your subscription to {site_title}', 'mailpoet'); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get email heading. |
| 67 | */ |
| 68 | public function get_default_heading() { |
| 69 | return __('Confirm your subscription to {site_title}', 'mailpoet'); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Trigger the sending of this email. |
| 74 | * |
| 75 | * @param string $to Email address to send to. |
| 76 | * @param string $activation_link Activation link for the subscriber. |
| 77 | * @param string $subscriber_firstname First name of the subscriber. |
| 78 | * @return bool True if the email was sent, false otherwise. |
| 79 | */ |
| 80 | public function trigger($to, $activation_link = '', $subscriber_firstname = '') { |
| 81 | $this->setup_locale(); |
| 82 | $sent = false; |
| 83 | |
| 84 | if ($this->is_enabled() && $to) { |
| 85 | $this->recipient = $to; |
| 86 | $this->placeholders['{activation_link}'] = $activation_link; |
| 87 | $this->placeholders['{subscriber_firstname}'] = $subscriber_firstname; |
| 88 | |
| 89 | $sent = $this->send($to, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); |
| 90 | } |
| 91 | |
| 92 | $this->restore_locale(); |
| 93 | return (bool)$sent; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get content html. |
| 98 | */ |
| 99 | public function get_content_html() { |
| 100 | return wc_get_template_html( |
| 101 | $this->template_html, |
| 102 | [ |
| 103 | 'email_heading' => $this->get_heading(), |
| 104 | 'additional_content' => $this->get_additional_content(), |
| 105 | 'email' => $this, |
| 106 | 'activation_link' => $this->placeholders['{activation_link}'], |
| 107 | 'subscriber_firstname' => $this->placeholders['{subscriber_firstname}'], |
| 108 | ], |
| 109 | '', |
| 110 | $this->template_base |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get content plain. |
| 116 | */ |
| 117 | public function get_content_plain() { |
| 118 | return wc_get_template_html( |
| 119 | $this->template_plain, |
| 120 | [ |
| 121 | 'email_heading' => $this->get_heading(), |
| 122 | 'additional_content' => $this->get_additional_content(), |
| 123 | 'email' => $this, |
| 124 | 'activation_link' => $this->placeholders['{activation_link}'], |
| 125 | 'subscriber_firstname' => $this->placeholders['{subscriber_firstname}'], |
| 126 | ], |
| 127 | '', |
| 128 | $this->template_base |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Initialize settings form fields. |
| 134 | */ |
| 135 | public function init_form_fields() { |
| 136 | $this->form_fields = [ |
| 137 | 'enabled' => [ |
| 138 | 'title' => __('Enable/Disable', 'mailpoet'), |
| 139 | 'type' => 'checkbox', |
| 140 | 'label' => __('Enable this email notification', 'mailpoet'), |
| 141 | 'default' => 'yes', |
| 142 | ], |
| 143 | 'subject' => [ |
| 144 | 'title' => __('Subject', 'mailpoet'), |
| 145 | 'type' => 'text', |
| 146 | 'desc_tip' => true, |
| 147 | 'description' => sprintf( |
| 148 | // translators: %s is a list of available placeholders |
| 149 | __('Available placeholders: %s', 'mailpoet'), |
| 150 | '<code>{site_title}, {activation_link}, {subscriber_firstname}</code>' |
| 151 | ), |
| 152 | 'placeholder' => $this->get_default_subject(), |
| 153 | 'default' => '', |
| 154 | ], |
| 155 | 'heading' => [ |
| 156 | 'title' => __('Email heading', 'mailpoet'), |
| 157 | 'type' => 'text', |
| 158 | 'desc_tip' => true, |
| 159 | 'description' => sprintf( |
| 160 | // translators: %s is a list of available placeholders |
| 161 | __('Available placeholders: %s', 'mailpoet'), |
| 162 | '<code>{site_title}, {activation_link}, {subscriber_firstname}</code>' |
| 163 | ), |
| 164 | 'placeholder' => $this->get_default_heading(), |
| 165 | 'default' => '', |
| 166 | ], |
| 167 | 'additional_content' => [ |
| 168 | 'title' => __('Additional content', 'mailpoet'), |
| 169 | 'description' => __('Text to appear below the main email content.', 'mailpoet'), |
| 170 | 'type' => 'textarea', |
| 171 | 'default' => '', |
| 172 | 'desc_tip' => true, |
| 173 | ], |
| 174 | 'email_type' => [ |
| 175 | 'title' => __('Email type', 'mailpoet'), |
| 176 | 'type' => 'select', |
| 177 | 'description' => __('Choose which format of email to send.', 'mailpoet'), |
| 178 | 'default' => 'html', |
| 179 | 'class' => 'email_type wc-enhanced-select', |
| 180 | 'options' => $this->get_email_type_options(), |
| 181 | 'desc_tip' => true, |
| 182 | ], |
| 183 | ]; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Check if this email should be available. |
| 188 | */ |
| 189 | public static function is_available() { |
| 190 | $dotcomHelperFunctions = new DotcomHelperFunctions(WPFunctions::get()); |
| 191 | // Only available in Garden environment. |
| 192 | return $dotcomHelperFunctions->isGarden(); |
| 193 | } |
| 194 | } |
| 195 |