Geen omschrijving

class-wc-email.php 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. $this->placeholders = array_merge(
  208. array(
  209. '{site_title}' => $this->get_blogname(),
  210. '{site_address}' => wp_parse_url( home_url(), PHP_URL_HOST ),
  211. '{site_url}' => wp_parse_url( home_url(), PHP_URL_HOST ),
  212. ),
  213. $this->placeholders
  214. );
  215. // Init settings.
  216. $this->init_form_fields();
  217. $this->init_settings();
  218. // Default template base if not declared in child constructor.
  219. if ( is_null( $this->template_base ) ) {
  220. $this->template_base = WC()->plugin_path() . '/templates/';
  221. }
  222. $this->email_type = $this->get_option( 'email_type' );
  223. $this->enabled = $this->get_option( 'enabled' );
  224. add_action( 'phpmailer_init', array( $this, 'handle_multipart' ) );
  225. add_action( 'woocommerce_update_options_email_' . $this->id, array( $this, 'process_admin_options' ) );
  226. }
  227. /**
  228. * Handle multipart mail.
  229. *
  230. * @param PHPMailer $mailer PHPMailer object.
  231. * @return PHPMailer
  232. */
  233. public function handle_multipart( $mailer ) {
  234. if ( $this->sending && 'multipart' === $this->get_email_type() ) {
  235. $mailer->AltBody = wordwrap( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  236. preg_replace( $this->plain_search, $this->plain_replace, wp_strip_all_tags( $this->get_content_plain() ) )
  237. );
  238. $this->sending = false;
  239. }
  240. return $mailer;
  241. }
  242. /**
  243. * Format email string.
  244. *
  245. * @param mixed $string Text to replace placeholders in.
  246. * @return string
  247. */
  248. public function format_string( $string ) {
  249. $find = array_keys( $this->placeholders );
  250. $replace = array_values( $this->placeholders );
  251. // If using legacy find replace, add those to our find/replace arrays first. @todo deprecate in 4.0.0.
  252. $find = array_merge( (array) $this->find, $find );
  253. $replace = array_merge( (array) $this->replace, $replace );
  254. // Take care of blogname which is no longer defined as a valid placeholder.
  255. $find[] = '{blogname}';
  256. $replace[] = $this->get_blogname();
  257. // 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.
  258. if ( has_filter( 'woocommerce_email_format_string_replace' ) || has_filter( 'woocommerce_email_format_string_find' ) ) {
  259. $legacy_find = $this->find;
  260. $legacy_replace = $this->replace;
  261. foreach ( $this->placeholders as $find => $replace ) {
  262. $legacy_key = sanitize_title( str_replace( '_', '-', trim( $find, '{}' ) ) );
  263. $legacy_find[ $legacy_key ] = $find;
  264. $legacy_replace[ $legacy_key ] = $replace;
  265. }
  266. $string = str_replace( apply_filters( 'woocommerce_email_format_string_find', $legacy_find, $this ), apply_filters( 'woocommerce_email_format_string_replace', $legacy_replace, $this ), $string );
  267. }
  268. /**
  269. * Filter for main find/replace.
  270. *
  271. * @since 3.2.0
  272. */
  273. return apply_filters( 'woocommerce_email_format_string', str_replace( $find, $replace, $string ), $this );
  274. }
  275. /**
  276. * Set the locale to the store locale for customer emails to make sure emails are in the store language.
  277. */
  278. public function setup_locale() {
  279. if ( $this->is_customer_email() && apply_filters( 'woocommerce_email_setup_locale', true ) ) {
  280. wc_switch_to_site_locale();
  281. }
  282. }
  283. /**
  284. * Restore the locale to the default locale. Use after finished with setup_locale.
  285. */
  286. public function restore_locale() {
  287. if ( $this->is_customer_email() && apply_filters( 'woocommerce_email_restore_locale', true ) ) {
  288. wc_restore_locale();
  289. }
  290. }
  291. /**
  292. * Get email subject.
  293. *
  294. * @since 3.1.0
  295. * @return string
  296. */
  297. public function get_default_subject() {
  298. return $this->subject;
  299. }
  300. /**
  301. * Get email heading.
  302. *
  303. * @since 3.1.0
  304. * @return string
  305. */
  306. public function get_default_heading() {
  307. return $this->heading;
  308. }
  309. /**
  310. * Default content to show below main email content.
  311. *
  312. * @since 3.7.0
  313. * @return string
  314. */
  315. public function get_default_additional_content() {
  316. return '';
  317. }
  318. /**
  319. * Return content from the additional_content field.
  320. *
  321. * Displayed above the footer.
  322. *
  323. * @since 3.7.0
  324. * @return string
  325. */
  326. public function get_additional_content() {
  327. $content = $this->get_option( 'additional_content', '' );
  328. return apply_filters( 'woocommerce_email_additional_content_' . $this->id, $this->format_string( $content ), $this->object, $this );
  329. }
  330. /**
  331. * Get email subject.
  332. *
  333. * @return string
  334. */
  335. public function get_subject() {
  336. return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->get_option( 'subject', $this->get_default_subject() ) ), $this->object, $this );
  337. }
  338. /**
  339. * Get email heading.
  340. *
  341. * @return string
  342. */
  343. public function get_heading() {
  344. return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->get_option( 'heading', $this->get_default_heading() ) ), $this->object, $this );
  345. }
  346. /**
  347. * Get valid recipients.
  348. *
  349. * @return string
  350. */
  351. public function get_recipient() {
  352. $recipient = apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object, $this );
  353. $recipients = array_map( 'trim', explode( ',', $recipient ) );
  354. $recipients = array_filter( $recipients, 'is_email' );
  355. return implode( ', ', $recipients );
  356. }
  357. /**
  358. * Get email headers.
  359. *
  360. * @return string
  361. */
  362. public function get_headers() {
  363. $header = 'Content-Type: ' . $this->get_content_type() . "\r\n";
  364. if ( in_array( $this->id, array( 'new_order', 'cancelled_order', 'failed_order' ), true ) ) {
  365. if ( $this->object && $this->object->get_billing_email() && ( $this->object->get_billing_first_name() || $this->object->get_billing_last_name() ) ) {
  366. $header .= 'Reply-to: ' . $this->object->get_billing_first_name() . ' ' . $this->object->get_billing_last_name() . ' <' . $this->object->get_billing_email() . ">\r\n";
  367. }
  368. } elseif ( $this->get_from_address() && $this->get_from_name() ) {
  369. $header .= 'Reply-to: ' . $this->get_from_name() . ' <' . $this->get_from_address() . ">\r\n";
  370. }
  371. return apply_filters( 'woocommerce_email_headers', $header, $this->id, $this->object, $this );
  372. }
  373. /**
  374. * Get email attachments.
  375. *
  376. * @return array
  377. */
  378. public function get_attachments() {
  379. return apply_filters( 'woocommerce_email_attachments', array(), $this->id, $this->object, $this );
  380. }
  381. /**
  382. * Return email type.
  383. *
  384. * @return string
  385. */
  386. public function get_email_type() {
  387. return $this->email_type && class_exists( 'DOMDocument' ) ? $this->email_type : 'plain';
  388. }
  389. /**
  390. * Get email content type.
  391. *
  392. * @param string $default_content_type Default wp_mail() content type.
  393. * @return string
  394. */
  395. public function get_content_type( $default_content_type = '' ) {
  396. switch ( $this->get_email_type() ) {
  397. case 'html':
  398. $content_type = 'text/html';
  399. break;
  400. case 'multipart':
  401. $content_type = 'multipart/alternative';
  402. break;
  403. default:
  404. $content_type = 'text/plain';
  405. break;
  406. }
  407. return apply_filters( 'woocommerce_email_content_type', $content_type, $this, $default_content_type );
  408. }
  409. /**
  410. * Return the email's title
  411. *
  412. * @return string
  413. */
  414. public function get_title() {
  415. return apply_filters( 'woocommerce_email_title', $this->title, $this );
  416. }
  417. /**
  418. * Return the email's description
  419. *
  420. * @return string
  421. */
  422. public function get_description() {
  423. return apply_filters( 'woocommerce_email_description', $this->description, $this );
  424. }
  425. /**
  426. * Proxy to parent's get_option and attempt to localize the result using gettext.
  427. *
  428. * @param string $key Option key.
  429. * @param mixed $empty_value Value to use when option is empty.
  430. * @return string
  431. */
  432. public function get_option( $key, $empty_value = null ) {
  433. $value = parent::get_option( $key, $empty_value );
  434. return apply_filters( 'woocommerce_email_get_option', $value, $this, $value, $key, $empty_value );
  435. }
  436. /**
  437. * Checks if this email is enabled and will be sent.
  438. *
  439. * @return bool
  440. */
  441. public function is_enabled() {
  442. return apply_filters( 'woocommerce_email_enabled_' . $this->id, 'yes' === $this->enabled, $this->object, $this );
  443. }
  444. /**
  445. * Checks if this email is manually sent
  446. *
  447. * @return bool
  448. */
  449. public function is_manual() {
  450. return $this->manual;
  451. }
  452. /**
  453. * Checks if this email is customer focussed.
  454. *
  455. * @return bool
  456. */
  457. public function is_customer_email() {
  458. return $this->customer_email;
  459. }
  460. /**
  461. * Get WordPress blog name.
  462. *
  463. * @return string
  464. */
  465. public function get_blogname() {
  466. return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  467. }
  468. /**
  469. * Get email content.
  470. *
  471. * @return string
  472. */
  473. public function get_content() {
  474. $this->sending = true;
  475. if ( 'plain' === $this->get_email_type() ) {
  476. $email_content = wordwrap( preg_replace( $this->plain_search, $this->plain_replace, wp_strip_all_tags( $this->get_content_plain() ) ), 70 );
  477. } else {
  478. $email_content = $this->get_content_html();
  479. }
  480. return $email_content;
  481. }
  482. /**
  483. * Apply inline styles to dynamic content.
  484. *
  485. * We only inline CSS for html emails, and to do so we use Emogrifier library (if supported).
  486. *
  487. * @version 4.0.0
  488. * @param string|null $content Content that will receive inline styles.
  489. * @return string
  490. */
  491. public function style_inline( $content ) {
  492. if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) ) {
  493. ob_start();
  494. wc_get_template( 'emails/email-styles.php' );
  495. $css = apply_filters( 'woocommerce_email_styles', ob_get_clean(), $this );
  496. $emogrifier_class = 'Pelago\\Emogrifier';
  497. if ( $this->supports_emogrifier() && class_exists( $emogrifier_class ) ) {
  498. try {
  499. $emogrifier = new $emogrifier_class( $content, $css );
  500. do_action( 'woocommerce_emogrifier', $emogrifier, $this );
  501. $content = $emogrifier->emogrify();
  502. $html_prune = \Pelago\Emogrifier\HtmlProcessor\HtmlPruner::fromHtml( $content );
  503. $html_prune->removeElementsWithDisplayNone();
  504. $content = $html_prune->render();
  505. } catch ( Exception $e ) {
  506. $logger = wc_get_logger();
  507. $logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
  508. }
  509. } else {
  510. $content = '<style type="text/css">' . $css . '</style>' . $content;
  511. }
  512. }
  513. return $content;
  514. }
  515. /**
  516. * Return if emogrifier library is supported.
  517. *
  518. * @version 4.0.0
  519. * @since 3.5.0
  520. * @return bool
  521. */
  522. protected function supports_emogrifier() {
  523. return class_exists( 'DOMDocument' );
  524. }
  525. /**
  526. * Get the email content in plain text format.
  527. *
  528. * @return string
  529. */
  530. public function get_content_plain() {
  531. return '';
  532. }
  533. /**
  534. * Get the email content in HTML format.
  535. *
  536. * @return string
  537. */
  538. public function get_content_html() {
  539. return '';
  540. }
  541. /**
  542. * Get the from name for outgoing emails.
  543. *
  544. * @param string $from_name Default wp_mail() name associated with the "from" email address.
  545. * @return string
  546. */
  547. public function get_from_name( $from_name = '' ) {
  548. $from_name = apply_filters( 'woocommerce_email_from_name', get_option( 'woocommerce_email_from_name' ), $this, $from_name );
  549. return wp_specialchars_decode( esc_html( $from_name ), ENT_QUOTES );
  550. }
  551. /**
  552. * Get the from address for outgoing emails.
  553. *
  554. * @param string $from_email Default wp_mail() email address to send from.
  555. * @return string
  556. */
  557. public function get_from_address( $from_email = '' ) {
  558. $from_email = apply_filters( 'woocommerce_email_from_address', get_option( 'woocommerce_email_from_address' ), $this, $from_email );
  559. return sanitize_email( $from_email );
  560. }
  561. /**
  562. * Send an email.
  563. *
  564. * @param string $to Email to.
  565. * @param string $subject Email subject.
  566. * @param string $message Email message.
  567. * @param string $headers Email headers.
  568. * @param array $attachments Email attachments.
  569. * @return bool success
  570. */
  571. public function send( $to, $subject, $message, $headers, $attachments ) {
  572. add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  573. add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  574. add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  575. $message = apply_filters( 'woocommerce_mail_content', $this->style_inline( $message ) );
  576. $mail_callback = apply_filters( 'woocommerce_mail_callback', 'wp_mail', $this );
  577. $mail_callback_params = apply_filters( 'woocommerce_mail_callback_params', array( $to, $subject, $message, $headers, $attachments ), $this );
  578. $return = $mail_callback( ...$mail_callback_params );
  579. remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  580. remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  581. remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
  582. /**
  583. * Action hook fired when an email is sent.
  584. *
  585. * @since 5.6.0
  586. * @param bool $return Whether the email was sent successfully.
  587. * @param int $id Email ID.
  588. * @param WC_Email $this WC_Email instance.
  589. */
  590. do_action( 'woocommerce_email_sent', $return, $this->id, $this );
  591. return $return;
  592. }
  593. /**
  594. * Initialise Settings Form Fields - these are generic email options most will use.
  595. */
  596. public function init_form_fields() {
  597. /* translators: %s: list of placeholders */
  598. $placeholder_text = sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . esc_html( implode( '</code>, <code>', array_keys( $this->placeholders ) ) ) . '</code>' );
  599. $this->form_fields = array(
  600. 'enabled' => array(
  601. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  602. 'type' => 'checkbox',
  603. 'label' => __( 'Enable this email notification', 'woocommerce' ),
  604. 'default' => 'yes',
  605. ),
  606. 'subject' => array(
  607. 'title' => __( 'Subject', 'woocommerce' ),
  608. 'type' => 'text',
  609. 'desc_tip' => true,
  610. 'description' => $placeholder_text,
  611. 'placeholder' => $this->get_default_subject(),
  612. 'default' => '',
  613. ),
  614. 'heading' => array(
  615. 'title' => __( 'Email heading', 'woocommerce' ),
  616. 'type' => 'text',
  617. 'desc_tip' => true,
  618. 'description' => $placeholder_text,
  619. 'placeholder' => $this->get_default_heading(),
  620. 'default' => '',
  621. ),
  622. 'additional_content' => array(
  623. 'title' => __( 'Additional content', 'woocommerce' ),
  624. 'description' => __( 'Text to appear below the main email content.', 'woocommerce' ) . ' ' . $placeholder_text,
  625. 'css' => 'width:400px; height: 75px;',
  626. 'placeholder' => __( 'N/A', 'woocommerce' ),
  627. 'type' => 'textarea',
  628. 'default' => $this->get_default_additional_content(),
  629. 'desc_tip' => true,
  630. ),
  631. 'email_type' => array(
  632. 'title' => __( 'Email type', 'woocommerce' ),
  633. 'type' => 'select',
  634. 'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
  635. 'default' => 'html',
  636. 'class' => 'email_type wc-enhanced-select',
  637. 'options' => $this->get_email_type_options(),
  638. 'desc_tip' => true,
  639. ),
  640. );
  641. }
  642. /**
  643. * Email type options.
  644. *
  645. * @return array
  646. */
  647. public function get_email_type_options() {
  648. $types = array( 'plain' => __( 'Plain text', 'woocommerce' ) );
  649. if ( class_exists( 'DOMDocument' ) ) {
  650. $types['html'] = __( 'HTML', 'woocommerce' );
  651. $types['multipart'] = __( 'Multipart', 'woocommerce' );
  652. }
  653. return $types;
  654. }
  655. /**
  656. * Admin Panel Options Processing.
  657. */
  658. public function process_admin_options() {
  659. // Save regular options.
  660. parent::process_admin_options();
  661. $post_data = $this->get_post_data();
  662. // Save templates.
  663. if ( isset( $post_data['template_html_code'] ) ) {
  664. $this->save_template( $post_data['template_html_code'], $this->template_html );
  665. }
  666. if ( isset( $post_data['template_plain_code'] ) ) {
  667. $this->save_template( $post_data['template_plain_code'], $this->template_plain );
  668. }
  669. }
  670. /**
  671. * Get template.
  672. *
  673. * @param string $type Template type. Can be either 'template_html' or 'template_plain'.
  674. * @return string
  675. */
  676. public function get_template( $type ) {
  677. $type = basename( $type );
  678. if ( 'template_html' === $type ) {
  679. return $this->template_html;
  680. } elseif ( 'template_plain' === $type ) {
  681. return $this->template_plain;
  682. }
  683. return '';
  684. }
  685. /**
  686. * Save the email templates.
  687. *
  688. * @since 2.4.0
  689. * @param string $template_code Template code.
  690. * @param string $template_path Template path.
  691. */
  692. protected function save_template( $template_code, $template_path ) {
  693. if ( current_user_can( 'edit_themes' ) && ! empty( $template_code ) && ! empty( $template_path ) ) {
  694. $saved = false;
  695. $file = get_stylesheet_directory() . '/' . WC()->template_path() . $template_path;
  696. $code = wp_unslash( $template_code );
  697. if ( is_writeable( $file ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writeable
  698. $f = fopen( $file, 'w+' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
  699. if ( false !== $f ) {
  700. fwrite( $f, $code ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
  701. fclose( $f ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
  702. $saved = true;
  703. }
  704. }
  705. if ( ! $saved ) {
  706. $redirect = add_query_arg( 'wc_error', rawurlencode( __( 'Could not write to template file.', 'woocommerce' ) ) );
  707. wp_safe_redirect( $redirect );
  708. exit;
  709. }
  710. }
  711. }
  712. /**
  713. * Get the template file in the current theme.
  714. *
  715. * @param string $template Template name.
  716. *
  717. * @return string
  718. */
  719. public function get_theme_template_file( $template ) {
  720. return get_stylesheet_directory() . '/' . apply_filters( 'woocommerce_template_directory', 'woocommerce', $template ) . '/' . $template;
  721. }
  722. /**
  723. * Move template action.
  724. *
  725. * @param string $template_type Template type.
  726. */
  727. protected function move_template_action( $template_type ) {
  728. $template = $this->get_template( $template_type );
  729. if ( ! empty( $template ) ) {
  730. $theme_file = $this->get_theme_template_file( $template );
  731. if ( wp_mkdir_p( dirname( $theme_file ) ) && ! file_exists( $theme_file ) ) {
  732. // Locate template file.
  733. $core_file = $this->template_base . $template;
  734. $template_file = apply_filters( 'woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id );
  735. // Copy template file.
  736. copy( $template_file, $theme_file );
  737. /**
  738. * Action hook fired after copying email template file.
  739. *
  740. * @param string $template_type The copied template type
  741. * @param string $email The email object
  742. */
  743. do_action( 'woocommerce_copy_email_template', $template_type, $this );
  744. ?>
  745. <div class="updated">
  746. <p><?php echo esc_html__( 'Template file copied to theme.', 'woocommerce' ); ?></p>
  747. </div>
  748. <?php
  749. }
  750. }
  751. }
  752. /**
  753. * Delete template action.
  754. *
  755. * @param string $template_type Template type.
  756. */
  757. protected function delete_template_action( $template_type ) {
  758. $template = $this->get_template( $template_type );
  759. if ( $template ) {
  760. if ( ! empty( $template ) ) {
  761. $theme_file = $this->get_theme_template_file( $template );
  762. if ( file_exists( $theme_file ) ) {
  763. unlink( $theme_file ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink
  764. /**
  765. * Action hook fired after deleting template file.
  766. *
  767. * @param string $template The deleted template type
  768. * @param string $email The email object
  769. */
  770. do_action( 'woocommerce_delete_email_template', $template_type, $this );
  771. ?>
  772. <div class="updated">
  773. <p><?php echo esc_html__( 'Template file deleted from theme.', 'woocommerce' ); ?></p>
  774. </div>
  775. <?php
  776. }
  777. }
  778. }
  779. }
  780. /**
  781. * Admin actions.
  782. */
  783. protected function admin_actions() {
  784. // Handle any actions.
  785. if (
  786. ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) )
  787. && ( ! empty( $_GET['move_template'] ) || ! empty( $_GET['delete_template'] ) )
  788. && 'GET' === $_SERVER['REQUEST_METHOD'] // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
  789. ) {
  790. if ( empty( $_GET['_wc_email_nonce'] ) || ! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wc_email_nonce'] ) ), 'woocommerce_email_template_nonce' ) ) {
  791. wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
  792. }
  793. if ( ! current_user_can( 'edit_themes' ) ) {
  794. wp_die( esc_html__( 'You don&#8217;t have permission to do this.', 'woocommerce' ) );
  795. }
  796. if ( ! empty( $_GET['move_template'] ) ) {
  797. $this->move_template_action( wc_clean( wp_unslash( $_GET['move_template'] ) ) );
  798. }
  799. if ( ! empty( $_GET['delete_template'] ) ) {
  800. $this->delete_template_action( wc_clean( wp_unslash( $_GET['delete_template'] ) ) );
  801. }
  802. }
  803. }
  804. /**
  805. * Admin Options.
  806. *
  807. * Setup the email settings screen.
  808. * Override this in your email.
  809. *
  810. * @since 1.0.0
  811. */
  812. public function admin_options() {
  813. // Do admin actions.
  814. $this->admin_actions();
  815. ?>
  816. <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>
  817. <?php echo wpautop( wp_kses_post( $this->get_description() ) ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
  818. <?php
  819. /**
  820. * Action hook fired before displaying email settings.
  821. *
  822. * @param string $email The email object
  823. */
  824. do_action( 'woocommerce_email_settings_before', $this );
  825. ?>
  826. <table class="form-table">
  827. <?php $this->generate_settings_html(); ?>
  828. </table>
  829. <?php
  830. /**
  831. * Action hook fired after displaying email settings.
  832. *
  833. * @param string $email The email object
  834. */
  835. do_action( 'woocommerce_email_settings_after', $this );
  836. ?>
  837. <?php
  838. if ( current_user_can( 'edit_themes' ) && ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) ) ) {
  839. ?>
  840. <div id="template">
  841. <?php
  842. $templates = array(
  843. 'template_html' => __( 'HTML template', 'woocommerce' ),
  844. 'template_plain' => __( 'Plain text template', 'woocommerce' ),
  845. );
  846. foreach ( $templates as $template_type => $title ) :
  847. $template = $this->get_template( $template_type );
  848. if ( empty( $template ) ) {
  849. continue;
  850. }
  851. $local_file = $this->get_theme_template_file( $template );
  852. $core_file = $this->template_base . $template;
  853. $template_file = apply_filters( 'woocommerce_locate_core_template', $core_file, $template, $this->template_base, $this->id );
  854. $template_dir = apply_filters( 'woocommerce_template_directory', 'woocommerce', $template );
  855. ?>
  856. <div class="template <?php echo esc_attr( $template_type ); ?>">
  857. <h4><?php echo wp_kses_post( $title ); ?></h4>
  858. <?php if ( file_exists( $local_file ) ) : ?>
  859. <p>
  860. <a href="#" class="button toggle_editor"></a>
  861. <?php if ( is_writable( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable ?>
  862. <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">
  863. <?php esc_html_e( 'Delete template file', 'woocommerce' ); ?>
  864. </a>
  865. <?php endif; ?>
  866. <?php
  867. /* translators: %s: Path to template file */
  868. 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>' );
  869. ?>
  870. </p>
  871. <div class="editor" style="display:none">
  872. <textarea class="code" cols="25" rows="20"
  873. <?php
  874. if ( ! is_writable( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
  875. ?>
  876. readonly="readonly" disabled="disabled"
  877. <?php else : ?>
  878. data-name="<?php echo esc_attr( $template_type ) . '_code'; ?>"<?php endif; ?>><?php echo esc_html( file_get_contents( $local_file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents ?></textarea>
  879. </div>
  880. <?php elseif ( file_exists( $template_file ) ) : ?>
  881. <p>
  882. <a href="#" class="button toggle_editor"></a>
  883. <?php
  884. $emails_dir = get_stylesheet_directory() . '/' . $template_dir . '/emails';
  885. $templates_dir = get_stylesheet_directory() . '/' . $template_dir;
  886. $theme_dir = get_stylesheet_directory();
  887. if ( is_dir( $emails_dir ) ) {
  888. $target_dir = $emails_dir;
  889. } elseif ( is_dir( $templates_dir ) ) {
  890. $target_dir = $templates_dir;
  891. } else {
  892. $target_dir = $theme_dir;
  893. }
  894. if ( is_writable( $target_dir ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
  895. ?>
  896. <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">
  897. <?php esc_html_e( 'Copy file to theme', 'woocommerce' ); ?>
  898. </a>
  899. <?php endif; ?>
  900. <?php
  901. /* translators: 1: Path to template file 2: Path to theme folder */
  902. 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>' );
  903. ?>
  904. </p>
  905. <div class="editor" style="display:none">
  906. <textarea class="code" readonly="readonly" disabled="disabled" cols="25" rows="20"><?php echo esc_html( file_get_contents( $template_file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents ?></textarea>
  907. </div>
  908. <?php else : ?>
  909. <p><?php esc_html_e( 'File was not found.', 'woocommerce' ); ?></p>
  910. <?php endif; ?>
  911. </div>
  912. <?php endforeach; ?>
  913. </div>
  914. <?php
  915. wc_enqueue_js(
  916. "jQuery( 'select.email_type' ).on( 'change', function() {
  917. var val = jQuery( this ).val();
  918. jQuery( '.template_plain, .template_html' ).show();
  919. if ( val != 'multipart' && val != 'html' ) {
  920. jQuery('.template_html').hide();
  921. }
  922. if ( val != 'multipart' && val != 'plain' ) {
  923. jQuery('.template_plain').hide();
  924. }
  925. }).trigger( 'change' );
  926. var view = '" . esc_js( __( 'View template', 'woocommerce' ) ) . "';
  927. var hide = '" . esc_js( __( 'Hide template', 'woocommerce' ) ) . "';
  928. jQuery( 'a.toggle_editor' ).text( view ).on( 'click', function() {
  929. var label = hide;
  930. if ( jQuery( this ).closest(' .template' ).find( '.editor' ).is(':visible') ) {
  931. var label = view;
  932. }
  933. jQuery( this ).text( label ).closest(' .template' ).find( '.editor' ).slideToggle();
  934. return false;
  935. } );
  936. jQuery( 'a.delete_template' ).on( 'click', function() {
  937. if ( window.confirm('" . esc_js( __( 'Are you sure you want to delete this template file?', 'woocommerce' ) ) . "') ) {
  938. return true;
  939. }
  940. return false;
  941. });
  942. jQuery( '.editor textarea' ).on( 'change', function() {
  943. var name = jQuery( this ).attr( 'data-name' );
  944. if ( name ) {
  945. jQuery( this ).attr( 'name', name );
  946. }
  947. });"
  948. );
  949. }
  950. }
  951. }