class-wc-email.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. <?php
  2. /**
  3. * Class WC_Email file.
  4. *
  5. * @package WooCommerce\Emails
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. if ( class_exists( 'WC_Email', false ) ) {
  11. return;
  12. }
  13. /**
  14. * Email Class
  15. *
  16. * WooCommerce Email Class which is extended by specific email template classes to add emails to WooCommerce
  17. *
  18. * @class WC_Email
  19. * @version 2.5.0
  20. * @package WooCommerce/Classes/Emails
  21. * @extends WC_Settings_API
  22. */
  23. class WC_Email extends WC_Settings_API {
  24. /**
  25. * Email method ID.
  26. *
  27. * @var String
  28. */
  29. public $id;
  30. /**
  31. * Email method title.
  32. *
  33. * @var string
  34. */
  35. public $title;
  36. /**
  37. * 'yes' if the method is enabled.
  38. *
  39. * @var string yes, no
  40. */
  41. public $enabled;
  42. /**
  43. * Description for the email.
  44. *
  45. * @var string
  46. */
  47. public $description;
  48. /**
  49. * Default heading.
  50. *
  51. * Supported for backwards compatibility but we recommend overloading the
  52. * get_default_x methods instead so localization can be done when needed.
  53. *
  54. * @var string
  55. */
  56. public $heading = '';
  57. /**
  58. * Default subject.
  59. *
  60. * Supported for backwards compatibility but we recommend overloading the
  61. * get_default_x methods instead so localization can be done when needed.
  62. *
  63. * @var string
  64. */
  65. public $subject = '';
  66. /**
  67. * Plain text template path.
  68. *
  69. * @var string
  70. */
  71. public $template_plain;
  72. /**
  73. * HTML template path.
  74. *
  75. * @var string
  76. */
  77. public $template_html;
  78. /**
  79. * Template path.
  80. *
  81. * @var string
  82. */
  83. public $template_base;
  84. /**
  85. * Recipients for the email.
  86. *
  87. * @var string
  88. */
  89. public $recipient;
  90. /**
  91. * Object this email is for, for example a customer, product, or email.
  92. *
  93. * @var object|bool
  94. */
  95. public $object;
  96. /**
  97. * Mime boundary (for multipart emails).
  98. *
  99. * @var string
  100. */
  101. public $mime_boundary;
  102. /**
  103. * Mime boundary header (for multipart emails).
  104. *
  105. * @var string
  106. */
  107. public $mime_boundary_header;
  108. /**
  109. * True when email is being sent.
  110. *
  111. * @var bool
  112. */
  113. public $sending;
  114. /**
  115. * True when the email notification is sent manually only.
  116. *
  117. * @var bool
  118. */
  119. protected $manual = false;
  120. /**
  121. * True when the email notification is sent to customers.
  122. *
  123. * @var bool
  124. */
  125. protected $customer_email = false;
  126. /**
  127. * List of preg* regular expression patterns to search for,
  128. * used in conjunction with $plain_replace.
  129. * https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc
  130. *
  131. * @var array $plain_search
  132. * @see $plain_replace
  133. */
  134. public $plain_search = array(
  135. "/\r/", // Non-legal carriage return.
  136. '/&(nbsp|#0*160);/i', // Non-breaking space.
  137. '/&(quot|rdquo|ldquo|#0*8220|#0*8221|#0*147|#0*148);/i', // Double quotes.
  138. '/&(apos|rsquo|lsquo|#0*8216|#0*8217);/i', // Single quotes.
  139. '/&gt;/i', // Greater-than.
  140. '/&lt;/i', // Less-than.
  141. '/&#0*38;/i', // Ampersand.
  142. '/&amp;/i', // Ampersand.
  143. '/&(copy|#0*169);/i', // Copyright.
  144. '/&(trade|#0*8482|#0*153);/i', // Trademark.
  145. '/&(reg|#0*174);/i', // Registered.
  146. '/&(mdash|#0*151|#0*8212);/i', // mdash.
  147. '/&(ndash|minus|#0*8211|#0*8722);/i', // ndash.
  148. '/&(bull|#0*149|#0*8226);/i', // Bullet.
  149. '/&(pound|#0*163);/i', // Pound sign.
  150. '/&(euro|#0*8364);/i', // Euro sign.
  151. '/&(dollar|#0*36);/i', // Dollar sign.
  152. '/&[^&\s;]+;/i', // Unknown/unhandled entities.
  153. '/[ ]{2,}/', // Runs of spaces, post-handling.
  154. );
  155. /**
  156. * List of pattern replacements corresponding to patterns searched.
  157. *
  158. * @var array $plain_replace
  159. * @see $plain_search
  160. */
  161. public $plain_replace = array(
  162. '', // Non-legal carriage return.
  163. ' ', // Non-breaking space.
  164. '"', // Double quotes.
  165. "'", // Single quotes.
  166. '>', // Greater-than.
  167. '<', // Less-than.
  168. '&', // Ampersand.
  169. '&', // Ampersand.
  170. '(c)', // Copyright.
  171. '(tm)', // Trademark.
  172. '(R)', // Registered.
  173. '--', // mdash.
  174. '-', // ndash.
  175. '*', // Bullet.
  176. '£', // Pound sign.
  177. 'EUR', // Euro sign. € ?.
  178. '$', // Dollar sign.
  179. '', // Unknown/unhandled entities.
  180. ' ', // Runs of spaces, post-handling.
  181. );
  182. /**
  183. * Strings to find/replace in subjects/headings.
  184. *
  185. * @var array
  186. */
  187. protected $placeholders = array();
  188. /**
  189. * Strings to find in subjects/headings.
  190. *
  191. * @deprecated 3.2.0 in favour of placeholders
  192. * @var array
  193. */
  194. public $find = array();
  195. /**
  196. * Strings to replace in subjects/headings.
  197. *
  198. * @deprecated 3.2.0 in favour of placeholders
  199. * @var array
  200. */
  201. public $replace = array();
  202. /**
  203. * Constructor.
  204. */
  205. public function __construct() {
  206. // Find/replace.
  207. if ( empty( $this->placeholders ) ) {
  208. $this->placeholders = array(
  209. '{site_title}' => $this->get_blogname(),
  210. );
  211. }
  212. // Init settings.
  213. $this->init_form_fields();
  214. $this->init_settings();
  215. // Default template base if not declared in child constructor.
  216. if ( is_null( $this->template_base ) ) {
  217. $this->template_base = WC()->plugin_path() . '/templates/';
  218. }
  219. $this->email_type = $this->get_option( 'email_type' );
  220. $this->enabled = $this->get_option( 'enabled' );
  221. add_action( 'phpmailer_init', array( $this, 'handle_multipart' ) );
  222. add_action( 'woocommerce_update_options_email_' . $this->id, array( $this, 'process_admin_options' ) );
  223. }
  224. /**
  225. * Handle multipart mail.
  226. *
  227. * @param PHPMailer $mailer PHPMailer object.
  228. * @return PHPMailer
  229. */
  230. public function handle_multipart( $mailer ) {
  231. if ( $this->sending && 'multipart' === $this->get_email_type() ) {
  232. $mailer->AltBody = wordwrap( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
  233. preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) )
  234. );
  235. $this->sending = false;
  236. }
  237. return $mailer;
  238. }
  239. /**
  240. * Format email string.
  241. *
  242. * @param mixed $string Text to replace placeholders in.
  243. * @return string
  244. */
  245. public function format_string( $string ) {
  246. $find = array_keys( $this->placeholders );
  247. $replace = array_values( $this->placeholders );
  248. // If using legacy find replace, add those to our find/replace arrays first. @todo deprecate in 4.0.0.
  249. $find = array_merge( (array) $this->find, $find );
  250. $replace = array_merge( (array) $this->replace, $replace );
  251. // Take care of blogname which is no longer defined as a valid placeholder.
  252. $find[] = '{blogname}';
  253. $replace[] = $this->get_blogname();
  254. // If using the older style filters for find and replace, ensure the array is associative and then pass through filters. @todo deprecate in 4.0.0.
  255. if ( has_filter( 'woocommerce_email_format_string_replace' ) || has_filter( 'woocommerce_email_format_string_find' ) ) {
  256. $legacy_find = $this->find;
  257. $legacy_replace = $this->replace;
  258. foreach ( $this->placeholders as $find => $replace ) {
  259. $legacy_key = sanitize_title( str_replace( '_', '-', trim( $find, '{}' ) ) );
  260. $legacy_find[ $legacy_key ] = $find;
  261. $legacy_replace[ $legacy_key ] = $replace;
  262. }
  263. $string = str_replace( apply_filters( 'woocommerce_email_format_string_find', $legacy_find, $this ), apply_filters( 'woocommerce_email_format_string_replace', $legacy_replace, $this ), $string );
  264. }
  265. /**
  266. * Filter for main find/replace.
  267. *
  268. * @since 3.2.0
  269. */
  270. return apply_filters( 'woocommerce_email_format_string', str_replace( $find, $replace, $string ), $this );
  271. }
  272. /**
  273. * Set the locale to the store locale for customer emails to make sure emails are in the store language.
  274. */
  275. public function setup_locale() {
  276. if ( $this->is_customer_email() && apply_filters( 'woocommerce_email_setup_locale', true ) ) {
  277. wc_switch_to_site_locale();
  278. }
  279. }
  280. /**
  281. * Restore the locale to the default locale. Use after finished with setup_locale.
  282. */
  283. public function restore_locale() {
  284. if ( $this->is_customer_email() && apply_filters( 'woocommerce_email_restore_locale', true ) ) {
  285. wc_restore_locale();
  286. }
  287. }
  288. /**
  289. * Get email subject.
  290. *
  291. * @since 3.1.0
  292. * @return string
  293. */
  294. public function get_default_subject() {
  295. return $this->subject;
  296. }
  297. /**
  298. * Get email heading.
  299. *
  300. * @since 3.1.0
  301. * @return string
  302. */
  303. public function get_default_heading() {
  304. return $this->heading;
  305. }
  306. /**
  307. * Get email subject.
  308. *
  309. * @return string
  310. */
  311. public function get_subject() {
  312. return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->get_option( 'subject', $this->get_default_subject() ) ), $this->object );
  313. }
  314. /**
  315. * Get email heading.
  316. *
  317. * @return string
  318. */
  319. public function get_heading() {
  320. return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->get_option( 'heading', $this->get_default_heading() ) ), $this->object );
  321. }
  322. /**
  323. * Get valid recipients.
  324. *
  325. * @return string
  326. */
  327. public function get_recipient() {
  328. $recipient = apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object );
  329. $recipients = array_map( 'trim', explode( ',', $recipient ) );
  330. $recipients = array_filter( $recipients, 'is_email' );
  331. return implode( ', ', $recipients );
  332. }
  333. /**
  334. * Get email headers.
  335. *
  336. * @return string
  337. */
  338. public function get_headers() {
  339. $header = 'Content-Type: ' . $this->get_content_type() . "\r\n";
  340. if ( 'new_order' === $this->id && $this->object && $this->object->get_billing_email() && ( $this->object->get_billing_first_name() || $this->object->get_billing_last_name() ) ) {
  341. $header .= 'Reply-to: ' . $this->object->get_billing_first_name() . ' ' . $this->object->get_billing_last_name() . ' <' . $this->object->get_billing_email() . ">\r\n";
  342. }
  343. return apply_filters( 'woocommerce_email_headers', $header, $this->id, $this->object );
  344. }
  345. /**
  346. * Get email attachments.
  347. *
  348. * @return array
  349. */
  350. public function get_attachments() {
  351. return apply_filters( 'woocommerce_email_attachments', array(), $this->id, $this->object );
  352. }
  353. /**
  354. * Return email type.
  355. *
  356. * @return string
  357. */
  358. public function get_email_type() {
  359. return $this->email_type && class_exists( 'DOMDocument' ) ? $this->email_type : 'plain';
  360. }
  361. /**
  362. * Get email content type.
  363. *
  364. * @return string
  365. */
  366. public function get_content_type() {
  367. switch ( $this->get_email_type() ) {
  368. case 'html':
  369. return 'text/html';
  370. case 'multipart':
  371. return 'multipart/alternative';
  372. default:
  373. return 'text/plain';
  374. }
  375. }
  376. /**
  377. * Return the email's title
  378. *
  379. * @return string
  380. */
  381. public function get_title() {
  382. return apply_filters( 'woocommerce_email_title', $this->title, $this );
  383. }
  384. /**
  385. * Return the email's description
  386. *
  387. * @return string
  388. */
  389. public function get_description() {
  390. return apply_filters( 'woocommerce_email_description', $this->description, $this );
  391. }
  392. /**
  393. * Proxy to parent's get_option and attempt to localize the result using gettext.
  394. *
  395. * @param string $key Option key.
  396. * @param mixed $empty_value Value to use when option is empty.
  397. * @return string
  398. */
  399. public function get_option( $key, $empty_value = null ) {
  400. $value = parent::get_option( $key, $empty_value );
  401. return apply_filters( 'woocommerce_email_get_option', $value, $this, $value, $key, $empty_value );
  402. }
  403. /**
  404. * Checks if this email is enabled and will be sent.
  405. *
  406. * @return bool
  407. */
  408. public function is_enabled() {
  409. return apply_filters( 'woocommerce_email_enabled_' . $this->id, 'yes' === $this->enabled, $this->object );
  410. }
  411. /**
  412. * Checks if this email is manually sent
  413. *
  414. * @return bool
  415. */
  416. public function is_manual() {
  417. return $this->manual;
  418. }
  419. /**
  420. * Checks if this email is customer focussed.
  421. *
  422. * @return bool
  423. */
  424. public function is_customer_email() {
  425. return $this->customer_email;
  426. }
  427. /**
  428. * Get WordPress blog name.
  429. *
  430. * @return string
  431. */
  432. public function get_blogname() {
  433. return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  434. }
  435. /**
  436. * Get email content.
  437. *
  438. * @return string
  439. */
  440. public function get_content() {
  441. $this->sending = true;
  442. if ( 'plain' === $this->get_email_type() ) {
  443. $email_content = wordwrap( preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) ), 70 );
  444. } else {
  445. $email_content = $this->get_content_html();
  446. }
  447. return $email_content;
  448. }
  449. /**
  450. * Apply inline styles to dynamic content.
  451. *
  452. * @param string|null $content Content that will receive inline styles.
  453. * @return string
  454. */
  455. public function style_inline( $content ) {
  456. // make sure we only inline CSS for html emails.
  457. if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) && class_exists( 'DOMDocument' ) ) {
  458. ob_start();
  459. wc_get_template( 'emails/email-styles.php' );
  460. $css = apply_filters( 'woocommerce_email_styles', ob_get_clean() );
  461. // apply CSS styles inline for picky email clients.
  462. try {
  463. $emogrifier = new Emogrifier( $content, $css );
  464. $content = $emogrifier->emogrify();
  465. } catch ( Exception $e ) {
  466. $logger = wc_get_logger();
  467. $logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
  468. }
  469. }
  470. return $content;
  471. }
  472. /**
  473. * Get the email content in plain text format.
  474. *
  475. * @return string
  476. */
  477. public function get_content_plain() {
  478. return ''; }
  479. /**
  480. * Get the email content in HTML format.
  481. *
  482. * @return string
  483. */
  484. public function get_content_html() {
  485. return ''; }
  486. /**
  487. * Get the from name for outgoing emails.
  488. *
  489. * @return string
  490. */
  491. public function get_from_name() {
  492. $from_name = apply_filters( 'woocommerce_email_from_name', get_option( 'woocommerce_email_from_name' ), $this );
  493. return wp_specialchars_decode( esc_html( $from_name ), ENT_QUOTES );
  494. }
  495. /**
  496. * Get the from address for outgoing emails.
  497. *
  498. * @return string
  499. */
  500. public function get_from_address() {
  501. $from_address = apply_filters( 'woocommerce_email_from_address', get_option( 'woocommerce_email_from_address' ), $this );
  502. return sanitize_email( $from_address );
  503. }
  504. /**
  505. * Send an email.
  506. *
  507. * @param string $to Email to.
  508. * @param string $subject Email subject.
  509. * @param string $message Email message.
  510. * @param string $headers Email headers.
  511. * @param array $attachments Email attachments.
  512. * @return bool success
  513. */
  514. public function send( $to, $subject, $message, $headers, $attachments ) {
  515. add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  516. add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  517. add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  518. $message = apply_filters( 'woocommerce_mail_content', $this->style_inline( $message ) );
  519. $return = wp_mail( $to, $subject, $message, $headers, $attachments );
  520. remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  521. remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  522. remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  523. return $return;
  524. }
  525. /**
  526. * Initialise Settings Form Fields - these are generic email options most will use.
  527. */
  528. public function init_form_fields() {
  529. $this->form_fields = array(
  530. 'enabled' => array(
  531. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  532. 'type' => 'checkbox',
  533. 'label' => __( 'Enable this email notification', 'woocommerce' ),
  534. 'default' => 'yes',
  535. ),
  536. 'subject' => array(
  537. 'title' => __( 'Subject', 'woocommerce' ),
  538. 'type' => 'text',
  539. 'desc_tip' => true,
  540. /* translators: %s: list of placeholders */
  541. 'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' ),
  542. 'placeholder' => $this->get_default_subject(),
  543. 'default' => '',
  544. ),
  545. 'heading' => array(
  546. 'title' => __( 'Email heading', 'woocommerce' ),
  547. 'type' => 'text',
  548. 'desc_tip' => true,
  549. /* translators: %s: list of placeholders */
  550. 'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' ),
  551. 'placeholder' => $this->get_default_heading(),
  552. 'default' => '',
  553. ),
  554. 'email_type' => array(
  555. 'title' => __( 'Email type', 'woocommerce' ),
  556. 'type' => 'select',
  557. 'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
  558. 'default' => 'html',
  559. 'class' => 'email_type wc-enhanced-select',
  560. 'options' => $this->get_email_type_options(),
  561. 'desc_tip' => true,
  562. ),
  563. );
  564. }
  565. /**
  566. * Email type options.
  567. *
  568. * @return array
  569. */
  570. public function get_email_type_options() {
  571. $types = array( 'plain' => __( 'Plain text', 'woocommerce' ) );
  572. if ( class_exists( 'DOMDocument' ) ) {
  573. $types['html'] = __( 'HTML', 'woocommerce' );
  574. $types['multipart'] = __( 'Multipart', 'woocommerce' );
  575. }
  576. return $types;
  577. }
  578. /**
  579. * Admin Panel Options Processing.
  580. */
  581. public function process_admin_options() {
  582. // Save regular options.
  583. parent::process_admin_options();
  584. $post_data = $this->get_post_data();
  585. // Save templates.
  586. if ( isset( $post_data['template_html_code'] ) ) {
  587. $this->save_template( $post_data['template_html_code'], $this->template_html );
  588. }
  589. if ( isset( $post_data['template_plain_code'] ) ) {
  590. $this->save_template( $post_data['template_plain_code'], $this->template_plain );
  591. }
  592. }
  593. /**
  594. * Get template.
  595. *
  596. * @param string $type Template type. Can be either 'template_html' or 'template_plain'.
  597. * @return string
  598. */
  599. public function get_template( $type ) {
  600. $type = basename( $type );
  601. if ( 'template_html' === $type ) {
  602. return $this->template_html;
  603. } elseif ( 'template_plain' === $type ) {
  604. return $this->template_plain;
  605. }
  606. return '';
  607. }
  608. /**
  609. * Save the email templates.
  610. *
  611. * @since 2.4.0
  612. * @param string $template_code Template code.
  613. * @param string $template_path Template path.
  614. */
  615. protected function save_template( $template_code, $template_path ) {
  616. if ( current_user_can( 'edit_themes' ) && ! empty( $template_code ) && ! empty( $template_path ) ) {
  617. $saved = false;
  618. $file = get_stylesheet_directory() . '/' . WC()->template_path() . $template_path;
  619. $code = wp_unslash( $template_code );
  620. if ( is_writeable( $file ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writeable
  621. $f = fopen( $file, 'w+' );
  622. if ( false !== $f ) {
  623. fwrite( $f, $code );
  624. fclose( $f );
  625. $saved = true;
  626. }
  627. }
  628. if ( ! $saved ) {
  629. $redirect = add_query_arg( 'wc_error', urlencode( __( 'Could not write to template file.', 'woocommerce' ) ) );
  630. wp_safe_redirect( $redirect );
  631. exit;
  632. }
  633. }
  634. }
  635. /**
  636. * Get the template file in the current theme.
  637. *
  638. * @param string $template Template name.
  639. *
  640. * @return string
  641. */
  642. public function get_theme_template_file( $template ) {
  643. return get_stylesheet_directory() . '/' . apply_filters( 'woocommerce_template_directory', 'woocommerce', $template ) . '/' . $template;
  644. }
  645. /**
  646. * Move template action.
  647. *
  648. * @param string $template_type Template type.
  649. */
  650. protected function move_template_action( $template_type ) {
  651. $template = $this->get_template( $template_type );
  652. if ( ! empty( $template ) ) {
  653. $theme_file = $this->get_theme_template_file( $template );
  654. if ( wp_mkdir_p( dirname( $theme_file ) ) && ! file_exists( $theme_file ) ) {
  655. // Locate template file.
  656. $core_file = $this->template_base . $template;
  657. $template_file = apply_filters( 'woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id );
  658. // Copy template file.
  659. copy( $template_file, $theme_file );
  660. /**
  661. * Action hook fired after copying email template file.
  662. *
  663. * @param string $template_type The copied template type
  664. * @param string $email The email object
  665. */
  666. do_action( 'woocommerce_copy_email_template', $template_type, $this );
  667. ?>
  668. <div class="updated">
  669. <p><?php echo esc_html__( 'Template file copied to theme.', 'woocommerce' ); ?></p>
  670. </div>
  671. <?php
  672. }
  673. }
  674. }
  675. /**
  676. * Delete template action.
  677. *
  678. * @param string $template_type Template type.
  679. */
  680. protected function delete_template_action( $template_type ) {
  681. $template = $this->get_template( $template_type );
  682. if ( $template ) {
  683. if ( ! empty( $template ) ) {
  684. $theme_file = $this->get_theme_template_file( $template );
  685. if ( file_exists( $theme_file ) ) {
  686. unlink( $theme_file ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink
  687. /**
  688. * Action hook fired after deleting template file.
  689. *
  690. * @param string $template The deleted template type
  691. * @param string $email The email object
  692. */
  693. do_action( 'woocommerce_delete_email_template', $template_type, $this );
  694. ?>
  695. <div class="updated">
  696. <p><?php echo esc_html__( 'Template file deleted from theme.', 'woocommerce' ); ?></p>
  697. </div>
  698. <?php
  699. }
  700. }
  701. }
  702. }
  703. /**
  704. * Admin actions.
  705. */
  706. protected function admin_actions() {
  707. // Handle any actions.
  708. if (
  709. ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) )
  710. && ( ! empty( $_GET['move_template'] ) || ! empty( $_GET['delete_template'] ) )
  711. && 'GET' === $_SERVER['REQUEST_METHOD'] // phpcs:ignore WordPress.VIP.ValidatedSanitizedInput.InputNotValidated
  712. ) {
  713. if ( empty( $_GET['_wc_email_nonce'] ) || ! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wc_email_nonce'] ) ), 'woocommerce_email_template_nonce' ) ) {
  714. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  715. }
  716. if ( ! current_user_can( 'edit_themes' ) ) {
  717. wp_die( esc_html__( 'You don&#8217;t have permission to do this.', 'woocommerce' ) );
  718. }
  719. if ( ! empty( $_GET['move_template'] ) ) {
  720. $this->move_template_action( wc_clean( wp_unslash( $_GET['move_template'] ) ) );
  721. }
  722. if ( ! empty( $_GET['delete_template'] ) ) {
  723. $this->delete_template_action( wc_clean( wp_unslash( $_GET['delete_template'] ) ) );
  724. }
  725. }
  726. }
  727. /**
  728. * Admin Options.
  729. *
  730. * Setup the email settings screen.
  731. * Override this in your email.
  732. *
  733. * @since 1.0.0
  734. */
  735. public function admin_options() {
  736. // Do admin actions.
  737. $this->admin_actions();
  738. ?>
  739. <h2><?php echo esc_html( $this->get_title() ); ?> <?php wc_back_link( __( 'Return to emails', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=email' ) ); ?></h2>
  740. <?php echo wpautop( wp_kses_post( $this->get_description() ) ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
  741. <?php
  742. /**
  743. * Action hook fired before displaying email settings.
  744. *
  745. * @param string $email The email object
  746. */
  747. do_action( 'woocommerce_email_settings_before', $this );
  748. ?>
  749. <table class="form-table">
  750. <?php $this->generate_settings_html(); ?>
  751. </table>
  752. <?php
  753. /**
  754. * Action hook fired after displaying email settings.
  755. *
  756. * @param string $email The email object
  757. */
  758. do_action( 'woocommerce_email_settings_after', $this );
  759. ?>
  760. <?php
  761. if ( current_user_can( 'edit_themes' ) && ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) ) ) {
  762. ?>
  763. <div id="template">
  764. <?php
  765. $templates = array(
  766. 'template_html' => __( 'HTML template', 'woocommerce' ),
  767. 'template_plain' => __( 'Plain text template', 'woocommerce' ),
  768. );
  769. foreach ( $templates as $template_type => $title ) :
  770. $template = $this->get_template( $template_type );
  771. if ( empty( $template ) ) {
  772. continue;
  773. }
  774. $local_file = $this->get_theme_template_file( $template );
  775. $core_file = $this->template_base . $template;
  776. $template_file = apply_filters( 'woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id );
  777. $template_dir = apply_filters( 'woocommerce_template_directory', 'woocommerce', $template );
  778. ?>
  779. <div class="template <?php echo esc_attr( $template_type ); ?>">
  780. <h4><?php echo wp_kses_post( $title ); ?></h4>
  781. <?php if ( file_exists( $local_file ) ) : ?>
  782. <p>
  783. <a href="#" class="button toggle_editor"></a>
  784. <?php if ( is_writable( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable ?>
  785. <a href="<?php echo esc_url( wp_nonce_url( remove_query_arg( array( 'move_template', 'saved' ), add_query_arg( 'delete_template', $template_type ) ), 'woocommerce_email_template_nonce', '_wc_email_nonce' ) ); ?>" class="delete_template button">
  786. <?php esc_html_e( 'Delete template file', 'woocommerce' ); ?>
  787. </a>
  788. <?php endif; ?>
  789. <?php
  790. /* translators: %s: Path to template file */
  791. printf( esc_html__( 'This template has been overridden by your theme and can be found in: %s.', 'woocommerce' ), '<code>' . esc_html( trailingslashit( basename( get_stylesheet_directory() ) ) . $template_dir . '/' . $template ) . '</code>' );
  792. ?>
  793. </p>
  794. <div class="editor" style="display:none">
  795. <textarea class="code" cols="25" rows="20"
  796. <?php
  797. if ( ! is_writable( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
  798. ?>
  799. readonly="readonly" disabled="disabled"
  800. <?php else : ?>
  801. data-name="<?php echo esc_attr( $template_type ) . '_code'; ?>"<?php endif; ?>><?php echo esc_html( file_get_contents( $local_file ) ); ?></textarea>
  802. </div>
  803. <?php elseif ( file_exists( $template_file ) ) : ?>
  804. <p>
  805. <a href="#" class="button toggle_editor"></a>
  806. <?php
  807. $emails_dir = get_stylesheet_directory() . '/' . $template_dir . '/emails';
  808. $templates_dir = get_stylesheet_directory() . '/' . $template_dir;
  809. $theme_dir = get_stylesheet_directory();
  810. if ( is_dir( $emails_dir ) ) {
  811. $target_dir = $emails_dir;
  812. } elseif ( is_dir( $templates_dir ) ) {
  813. $target_dir = $templates_dir;
  814. } else {
  815. $target_dir = $theme_dir;
  816. }
  817. if ( is_writable( $target_dir ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
  818. ?>
  819. <a href="<?php echo esc_url( wp_nonce_url( remove_query_arg( array( 'delete_template', 'saved' ), add_query_arg( 'move_template', $template_type ) ), 'woocommerce_email_template_nonce', '_wc_email_nonce' ) ); ?>" class="button">
  820. <?php esc_html_e( 'Copy file to theme', 'woocommerce' ); ?>
  821. </a>
  822. <?php endif; ?>
  823. <?php
  824. /* translators: 1: Path to template file 2: Path to theme folder */
  825. printf( esc_html__( 'To override and edit this email template copy %1$s to your theme folder: %2$s.', 'woocommerce' ), '<code>' . esc_html( plugin_basename( $template_file ) ) . '</code>', '<code>' . esc_html( trailingslashit( basename( get_stylesheet_directory() ) ) . $template_dir . '/' . $template ) . '</code>' );
  826. ?>
  827. </p>
  828. <div class="editor" style="display:none">
  829. <textarea class="code" readonly="readonly" disabled="disabled" cols="25" rows="20"><?php echo esc_html( file_get_contents( $template_file ) ); ?></textarea>
  830. </div>
  831. <?php else : ?>
  832. <p><?php esc_html_e( 'File was not found.', 'woocommerce' ); ?></p>
  833. <?php endif; ?>
  834. </div>
  835. <?php endforeach; ?>
  836. </div>
  837. <?php
  838. wc_enqueue_js(
  839. "jQuery( 'select.email_type' ).change( function() {
  840. var val = jQuery( this ).val();
  841. jQuery( '.template_plain, .template_html' ).show();
  842. if ( val != 'multipart' && val != 'html' ) {
  843. jQuery('.template_html').hide();
  844. }
  845. if ( val != 'multipart' && val != 'plain' ) {
  846. jQuery('.template_plain').hide();
  847. }
  848. }).change();
  849. var view = '" . esc_js( __( 'View template', 'woocommerce' ) ) . "';
  850. var hide = '" . esc_js( __( 'Hide template', 'woocommerce' ) ) . "';
  851. jQuery( 'a.toggle_editor' ).text( view ).toggle( function() {
  852. jQuery( this ).text( hide ).closest(' .template' ).find( '.editor' ).slideToggle();
  853. return false;
  854. }, function() {
  855. jQuery( this ).text( view ).closest( '.template' ).find( '.editor' ).slideToggle();
  856. return false;
  857. } );
  858. jQuery( 'a.delete_template' ).click( function() {
  859. if ( window.confirm('" . esc_js( __( 'Are you sure you want to delete this template file?', 'woocommerce' ) ) . "') ) {
  860. return true;
  861. }
  862. return false;
  863. });
  864. jQuery( '.editor textarea' ).change( function() {
  865. var name = jQuery( this ).attr( 'data-name' );
  866. if ( name ) {
  867. jQuery( this ).attr( 'name', name );
  868. }
  869. });"
  870. );
  871. }
  872. }
  873. }