Base_Email.php
204 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Emails; |
| 25 | |
| 26 | use WooCommerce\Square\Framework\Square_Helper; |
| 27 | |
| 28 | defined( 'ABSPATH' ) || exit; |
| 29 | |
| 30 | /** |
| 31 | * Base email class. |
| 32 | * |
| 33 | * @since 2.1.0 |
| 34 | */ |
| 35 | class Base_Email extends \WC_Email { |
| 36 | /** |
| 37 | * Whether the email is enabled by default. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | protected $enabled_default = 'no'; |
| 42 | |
| 43 | /** |
| 44 | * Plain text template path. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $template_plain = 'emails/plain/square-email.php'; |
| 49 | |
| 50 | /** |
| 51 | * HTML template path. |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | public $template_html = 'emails/square-email.php'; |
| 56 | |
| 57 | /** |
| 58 | * Template path. |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | public $template_base; |
| 63 | |
| 64 | /** |
| 65 | * Email constructor. |
| 66 | * |
| 67 | * @since 2.1.0 |
| 68 | */ |
| 69 | public function __construct() { |
| 70 | $this->template_base = wc_square()->get_plugin_path() . '/templates/'; |
| 71 | |
| 72 | // call parent constructor |
| 73 | parent::__construct(); |
| 74 | |
| 75 | // set default recipient |
| 76 | $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Initializes the email settings form fields. |
| 81 | * |
| 82 | * Extends and overrides parent method. |
| 83 | * |
| 84 | * @since 2.1.0 |
| 85 | */ |
| 86 | public function init_form_fields() { |
| 87 | // initialize the default fields from parent email object |
| 88 | parent::init_form_fields(); |
| 89 | |
| 90 | $form_fields = $this->form_fields; |
| 91 | |
| 92 | // set email disabled by default |
| 93 | if ( isset( $form_fields['enabled'] ) ) { |
| 94 | $form_fields['enabled']['default'] = $this->enabled_default; |
| 95 | } |
| 96 | |
| 97 | // the email has no customizable body or heading via input field |
| 98 | unset( $form_fields['body'], $form_fields['heading'] ); |
| 99 | |
| 100 | // adjust email subject field |
| 101 | if ( isset( $form_fields['subject'] ) ) { |
| 102 | /* translators: Placeholder: %s - default email subject text */ |
| 103 | $form_fields['subject']['description'] = sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: %s', 'woocommerce-square' ), '<code>' . $this->get_default_subject() . '</code>' ); |
| 104 | $form_fields['subject']['desc_tip'] = false; |
| 105 | $form_fields['subject']['default'] = $this->subject; |
| 106 | } |
| 107 | |
| 108 | // add a recipient field |
| 109 | $form_fields = Square_Helper::array_insert_after( |
| 110 | $form_fields, |
| 111 | isset( $form_fields['enabled'] ) ? 'enabled' : key( $form_fields ), |
| 112 | array( |
| 113 | 'recipient' => array( |
| 114 | 'title' => __( 'Recipient(s)', 'woocommerce-square' ), |
| 115 | 'type' => 'text', |
| 116 | /* translatorsPlaceholder: %s default email address */ |
| 117 | 'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to admin email: %s', 'woocommerce-square' ), '<code>' . esc_attr( get_option( 'admin_email' ) ) . '</code>' ), |
| 118 | 'placeholder' => get_bloginfo( 'admin_email' ), |
| 119 | 'default' => get_bloginfo( 'admin_email' ), |
| 120 | ), |
| 121 | ) |
| 122 | ); |
| 123 | |
| 124 | // set the updated fields |
| 125 | $this->form_fields = $form_fields; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Gets the default email subject. |
| 130 | * |
| 131 | * @since 2.1.0 |
| 132 | * |
| 133 | * @return string |
| 134 | */ |
| 135 | public function get_default_subject() { |
| 136 | return $this->subject; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Gets the email body. |
| 141 | * |
| 142 | * @since 2.1.0 |
| 143 | * |
| 144 | * @return string may contain HTML |
| 145 | */ |
| 146 | protected function get_default_body() { |
| 147 | return $this->body; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Determines if the email has valid recipients. |
| 152 | * |
| 153 | * @since 2.1.0 |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | protected function has_recipients() { |
| 158 | return ! empty( $this->get_recipient() ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Gets the arguments that should be passed to an email template. |
| 163 | * |
| 164 | * @since 2.1.0 |
| 165 | * |
| 166 | * @param array $args optional associative array with additional arguments |
| 167 | * @return array |
| 168 | */ |
| 169 | protected function get_template_args( $args = array() ) { |
| 170 | return array_merge( |
| 171 | $args, |
| 172 | array( |
| 173 | 'email' => $this, |
| 174 | 'email_heading' => '', |
| 175 | 'email_body' => '', |
| 176 | ) |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Gets the email HTML content. |
| 182 | * |
| 183 | * @since 2.0.0 |
| 184 | * |
| 185 | * @return string HTML |
| 186 | */ |
| 187 | public function get_content_html() { |
| 188 | $args = array( 'plain_text' => false ); |
| 189 | return wc_get_template_html( $this->template_html, array_merge( $args, $this->get_template_args( $args ) ) ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Gets the email plain text content. |
| 194 | * |
| 195 | * @since 2.0.0 |
| 196 | * |
| 197 | * @return string plain text |
| 198 | */ |
| 199 | public function get_content_plain() { |
| 200 | $args = array( 'plain_text' => true ); |
| 201 | return wc_get_template_html( $this->template_plain, array_merge( $args, $this->get_template_args( $args ) ) ); |
| 202 | } |
| 203 | } |
| 204 |