2012-09-17 00:53:17 +00:00
< ? php
2018-03-07 17:27:56 +00:00
/**
* Class WC_Email file .
*
* @ package WooCommerce\Emails
*/
2015-05-21 04:52:38 +00:00
2015-02-13 16:54:12 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
2017-02-16 11:46:01 +00:00
if ( class_exists ( 'WC_Email' , false ) ) {
2015-02-13 16:54:12 +00:00
return ;
}
/**
2015-11-03 14:57:39 +00:00
* Email Class
*
* WooCommerce Email Class which is extended by specific email template classes to add emails to WooCommerce
*
* @ class WC_Email
* @ version 2.5 . 0
* @ package WooCommerce / Classes / Emails
* @ extends WC_Settings_API
2015-02-13 16:54:12 +00:00
*/
2014-09-23 16:20:28 +00:00
class WC_Email extends WC_Settings_API {
2012-09-17 00:53:17 +00:00
2014-12-03 03:30:28 +00:00
/**
* Email method ID .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var String
*/
2014-08-31 05:49:58 +00:00
public $id ;
2012-09-17 00:53:17 +00:00
2014-12-03 03:30:28 +00:00
/**
* Email method title .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $title ;
2012-09-17 00:53:17 +00:00
2014-12-03 03:30:28 +00:00
/**
* 'yes' if the method is enabled .
2018-03-07 14:15:17 +00:00
*
2015-11-03 15:40:10 +00:00
* @ var string yes , no
2014-12-03 03:30:28 +00:00
*/
2014-08-31 05:49:58 +00:00
public $enabled ;
2012-09-17 00:53:17 +00:00
2014-12-03 03:30:28 +00:00
/**
* Description for the email .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $description ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
2017-06-02 17:25:05 +00:00
* Default heading .
*
* Supported for backwards compatibility but we recommend overloading the
2017-07-17 10:10:52 +00:00
* get_default_x methods instead so localization can be done when needed .
2017-06-02 17:25:05 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2017-06-02 17:25:05 +00:00
public $heading = '' ;
2012-09-17 00:53:17 +00:00
2014-12-03 03:30:28 +00:00
/**
2017-06-02 17:25:05 +00:00
* Default subject .
*
* Supported for backwards compatibility but we recommend overloading the
2017-07-17 10:10:52 +00:00
* get_default_x methods instead so localization can be done when needed .
2017-06-02 17:25:05 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2017-06-02 17:25:05 +00:00
public $subject = '' ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
2017-06-02 17:25:05 +00:00
* Plain text template path .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2017-06-02 17:25:05 +00:00
public $template_plain ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
2017-06-02 17:25:05 +00:00
* HTML template path .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2017-06-02 17:25:05 +00:00
public $template_html ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
2017-06-02 17:25:05 +00:00
* Template path .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2017-06-02 17:25:05 +00:00
public $template_base ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
2017-06-02 17:25:05 +00:00
* Recipients for the email .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2017-06-02 17:25:05 +00:00
public $recipient ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Object this email is for , for example a customer , product , or email .
2018-03-07 14:15:17 +00:00
*
2016-06-07 10:22:36 +00:00
* @ var object | bool
2014-12-03 03:30:28 +00:00
*/
2014-08-31 05:49:58 +00:00
public $object ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Mime boundary ( for multipart emails ) .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $mime_boundary ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Mime boundary header ( for multipart emails ) .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $mime_boundary_header ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* True when email is being sent .
2018-03-07 14:15:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ var bool
*/
2014-08-31 05:49:58 +00:00
public $sending ;
2012-12-13 09:52:20 +00:00
2015-11-03 15:40:10 +00:00
/**
* True when the email notification is sent manually only .
2018-03-07 14:15:17 +00:00
*
2015-11-03 15:40:10 +00:00
* @ var bool
*/
protected $manual = false ;
2015-11-02 16:27:00 +00:00
/**
* True when the email notification is sent to customers .
2018-03-07 14:15:17 +00:00
*
2015-11-02 16:27:00 +00:00
* @ var bool
*/
protected $customer_email = false ;
2012-09-17 00:53:17 +00:00
/**
2014-08-31 05:49:58 +00:00
* List of preg * regular expression patterns to search for ,
2015-11-03 14:57:39 +00:00
* used in conjunction with $plain_replace .
2014-08-31 05:49:58 +00:00
* https :// raw . github . com / ushahidi / wp - silcc / master / class . html2text . inc
2018-03-07 14:15:17 +00:00
*
2015-11-03 14:57:39 +00:00
* @ var array $plain_search
* @ see $plain_replace
2014-08-31 05:49:58 +00:00
*/
public $plain_search = array (
2018-03-07 14:15:17 +00:00
" / \r / " , // Non-legal carriage return.
'/&(nbsp|#0*160);/i' , // Non-breaking space.
'/&(quot|rdquo|ldquo|#0*8220|#0*8221|#0*147|#0*148);/i' , // Double quotes.
'/&(apos|rsquo|lsquo|#0*8216|#0*8217);/i' , // Single quotes.
'/>/i' , // Greater-than.
'/</i' , // Less-than.
'/�*38;/i' , // Ampersand.
'/&/i' , // Ampersand.
'/&(copy|#0*169);/i' , // Copyright.
'/&(trade|#0*8482|#0*153);/i' , // Trademark.
'/&(reg|#0*174);/i' , // Registered.
'/&(mdash|#0*151|#0*8212);/i' , // mdash.
'/&(ndash|minus|#0*8211|#0*8722);/i' , // ndash.
'/&(bull|#0*149|#0*8226);/i' , // Bullet.
'/&(pound|#0*163);/i' , // Pound sign.
'/&(euro|#0*8364);/i' , // Euro sign.
'/&(dollar|#0*36);/i' , // Dollar sign.
'/&[^&\s;]+;/i' , // Unknown/unhandled entities.
'/[ ]{2,}/' , // Runs of spaces, post-handling.
2014-08-31 05:49:58 +00:00
);
/**
* List of pattern replacements corresponding to patterns searched .
2018-03-07 14:15:17 +00:00
*
2015-11-03 14:57:39 +00:00
* @ var array $plain_replace
* @ see $plain_search
2014-08-31 05:49:58 +00:00
*/
public $plain_replace = array (
2018-03-07 14:15:17 +00:00
'' , // Non-legal carriage return.
' ' , // Non-breaking space.
'"' , // Double quotes.
" ' " , // Single quotes.
'>' , // Greater-than.
'<' , // Less-than.
'&' , // Ampersand.
'&' , // Ampersand.
'(c)' , // Copyright.
'(tm)' , // Trademark.
'(R)' , // Registered.
'--' , // mdash.
'-' , // ndash.
'*' , // Bullet.
'£' , // Pound sign.
'EUR' , // Euro sign. € ?.
'$' , // Dollar sign.
'' , // Unknown/unhandled entities.
' ' , // Runs of spaces, post-handling.
2014-08-31 05:49:58 +00:00
);
2017-07-19 13:25:00 +00:00
/**
* Strings to find / replace in subjects / headings .
*
* @ var array
*/
protected $placeholders = array ();
2018-03-07 14:15:17 +00:00
/**
2017-09-07 07:58:54 +00:00
* Strings to find in subjects / headings .
*
* @ deprecated 3.2 . 0 in favour of placeholders
* @ var array
*/
public $find = array ();
/**
* Strings to replace in subjects / headings .
*
* @ deprecated 3.2 . 0 in favour of placeholders
* @ var array
*/
public $replace = array ();
2014-08-31 05:49:58 +00:00
/**
2015-11-03 13:31:20 +00:00
* Constructor .
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function __construct () {
2018-03-07 14:15:17 +00:00
// Find/replace.
2019-03-04 15:30:56 +00:00
$this -> placeholders = array_merge (
array (
'{site_title}' => $this -> get_blogname (),
'{site_address}' => wp_parse_url ( home_url (), PHP_URL_HOST ),
),
$this -> placeholders
);
2017-07-19 13:25:00 +00:00
2018-03-07 14:15:17 +00:00
// Init settings.
2013-01-10 15:17:33 +00:00
$this -> init_form_fields ();
2012-09-17 00:53:17 +00:00
$this -> init_settings ();
2012-11-27 16:22:47 +00:00
2018-03-07 14:15:17 +00:00
// Default template base if not declared in child constructor.
2013-11-24 10:24:12 +00:00
if ( is_null ( $this -> template_base ) ) {
2013-11-25 14:01:32 +00:00
$this -> template_base = WC () -> plugin_path () . '/templates/' ;
2013-11-24 10:24:12 +00:00
}
2013-02-21 09:23:32 +00:00
2017-06-14 16:13:43 +00:00
$this -> email_type = $this -> get_option ( 'email_type' );
$this -> enabled = $this -> get_option ( 'enabled' );
2012-11-27 16:22:47 +00:00
2016-02-25 11:14:15 +00:00
add_action ( 'phpmailer_init' , array ( $this , 'handle_multipart' ) );
2017-07-19 13:25:00 +00:00
add_action ( 'woocommerce_update_options_email_' . $this -> id , array ( $this , 'process_admin_options' ) );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2016-01-05 18:42:18 +00:00
* Handle multipart mail .
2012-11-27 16:22:47 +00:00
*
2018-03-07 17:27:56 +00:00
* @ param PHPMailer $mailer PHPMailer object .
2013-11-28 12:54:19 +00:00
* @ return PHPMailer
2012-09-17 00:53:17 +00:00
*/
2016-08-27 04:53:52 +00:00
public function handle_multipart ( $mailer ) {
2015-11-03 14:57:39 +00:00
if ( $this -> sending && 'multipart' === $this -> get_email_type () ) {
2018-03-07 17:27:56 +00:00
$mailer -> AltBody = wordwrap ( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
2019-03-04 16:33:05 +00:00
preg_replace ( $this -> plain_search , $this -> plain_replace , wp_strip_all_tags ( $this -> get_content_plain () ) )
2018-03-07 17:27:56 +00:00
);
2015-11-03 14:57:39 +00:00
$this -> sending = false ;
2012-09-17 00:53:17 +00:00
}
return $mailer ;
}
/**
2016-01-05 18:42:18 +00:00
* Format email string .
2012-11-27 16:22:47 +00:00
*
2017-09-05 09:15:32 +00:00
* @ param mixed $string Text to replace placeholders in .
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function format_string ( $string ) {
2017-09-05 09:15:32 +00:00
$find = array_keys ( $this -> placeholders );
$replace = array_values ( $this -> placeholders );
// If using legacy find replace, add those to our find/replace arrays first. @todo deprecate in 4.0.0.
2017-09-15 00:37:23 +00:00
$find = array_merge ( ( array ) $this -> find , $find );
$replace = array_merge ( ( array ) $this -> replace , $replace );
2017-09-05 09:15:32 +00:00
2017-10-13 16:42:12 +00:00
// Take care of blogname which is no longer defined as a valid placeholder.
$find [] = '{blogname}' ;
$replace [] = $this -> get_blogname ();
2017-09-05 09:15:32 +00:00
// 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.
2017-08-31 11:21:02 +00:00
if ( has_filter ( 'woocommerce_email_format_string_replace' ) || has_filter ( 'woocommerce_email_format_string_find' ) ) {
2017-09-07 07:58:54 +00:00
$legacy_find = $this -> find ;
$legacy_replace = $this -> replace ;
2017-08-31 11:21:02 +00:00
foreach ( $this -> placeholders as $find => $replace ) {
$legacy_key = sanitize_title ( str_replace ( '_' , '-' , trim ( $find , '{}' ) ) );
$legacy_find [ $legacy_key ] = $find ;
$legacy_replace [ $legacy_key ] = $replace ;
}
$string = str_replace ( apply_filters ( 'woocommerce_email_format_string_find' , $legacy_find , $this ), apply_filters ( 'woocommerce_email_format_string_replace' , $legacy_replace , $this ), $string );
}
2017-09-05 09:15:32 +00:00
/**
2018-03-07 17:27:56 +00:00
* Filter for main find / replace .
2017-09-05 09:15:32 +00:00
*
* @ since 3.2 . 0
*/
return apply_filters ( 'woocommerce_email_format_string' , str_replace ( $find , $replace , $string ), $this );
2012-09-17 00:53:17 +00:00
}
2014-08-31 05:49:58 +00:00
2017-05-16 01:09:24 +00:00
/**
* Set the locale to the store locale for customer emails to make sure emails are in the store language .
*/
2017-05-15 21:34:37 +00:00
public function setup_locale () {
2017-08-03 11:58:44 +00:00
if ( $this -> is_customer_email () && apply_filters ( 'woocommerce_email_setup_locale' , true ) ) {
2017-06-05 13:18:39 +00:00
wc_switch_to_site_locale ();
2017-05-15 21:34:37 +00:00
}
}
2017-05-16 01:09:24 +00:00
/**
* Restore the locale to the default locale . Use after finished with setup_locale .
*/
2017-05-16 13:31:52 +00:00
public function restore_locale () {
2017-08-03 11:58:44 +00:00
if ( $this -> is_customer_email () && apply_filters ( 'woocommerce_email_restore_locale' , true ) ) {
2017-06-05 13:18:39 +00:00
wc_restore_locale ();
2017-05-15 21:34:37 +00:00
}
}
2017-06-02 17:25:05 +00:00
/**
* Get email subject .
*
* @ since 3.1 . 0
* @ return string
*/
public function get_default_subject () {
return $this -> subject ;
}
/**
* Get email heading .
*
* @ since 3.1 . 0
* @ return string
*/
public function get_default_heading () {
return $this -> heading ;
}
2019-03-04 16:33:05 +00:00
/**
* Default content to show below main email content .
*
* @ since 3.6 . 0
* @ return string
*/
public function get_default_additional_content () {
return '' ;
}
/**
* Return content from the additional_content field .
*
* Displayed above the footer .
*
* @ since 3.6 . 0
* @ return string
*/
public function get_additional_content () {
$content = $this -> get_option ( 'additional_content' , '' );
return apply_filters ( 'woocommerce_email_additional_content_' . $this -> id , $this -> format_string ( $content ), $this -> object );
}
2012-09-17 00:53:17 +00:00
/**
2016-01-05 18:42:18 +00:00
* Get email subject .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_subject () {
2019-04-09 13:19:21 +00:00
return apply_filters ( 'woocommerce_email_subject_' . $this -> id , $this -> format_string ( $this -> get_option ( 'subject' , $this -> get_default_subject () ) ), $this -> object , $this );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2016-01-05 18:42:18 +00:00
* Get email heading .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_heading () {
2019-04-09 13:19:21 +00:00
return apply_filters ( 'woocommerce_email_heading_' . $this -> id , $this -> format_string ( $this -> get_option ( 'heading' , $this -> get_default_heading () ) ), $this -> object , $this );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2015-11-02 16:27:00 +00:00
* Get valid recipients .
2018-03-07 14:15:17 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_recipient () {
2019-04-09 13:19:21 +00:00
$recipient = apply_filters ( 'woocommerce_email_recipient_' . $this -> id , $this -> recipient , $this -> object , $this );
2015-11-02 16:27:00 +00:00
$recipients = array_map ( 'trim' , explode ( ',' , $recipient ) );
$recipients = array_filter ( $recipients , 'is_email' );
2015-11-03 15:40:10 +00:00
return implode ( ', ' , $recipients );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2016-01-05 18:42:18 +00:00
* Get email headers .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_headers () {
2018-03-07 14:15:17 +00:00
$header = 'Content-Type: ' . $this -> get_content_type () . " \r \n " ;
2016-08-27 01:46:45 +00:00
2019-03-08 15:55:56 +00:00
if ( in_array ( $this -> id , array ( 'new_order' , 'cancelled_order' , 'failed_order' ), true ) ) {
if ( $this -> object && $this -> object -> get_billing_email () && ( $this -> object -> get_billing_first_name () || $this -> object -> get_billing_last_name () ) ) {
$header .= 'Reply-to: ' . $this -> object -> get_billing_first_name () . ' ' . $this -> object -> get_billing_last_name () . ' <' . $this -> object -> get_billing_email () . " > \r \n " ;
}
} elseif ( $this -> get_from_address () && $this -> get_from_name () ) {
$header .= 'Reply-to: ' . $this -> get_from_name () . ' <' . $this -> get_from_address () . " > \r \n " ;
2016-08-15 11:01:12 +00:00
}
2016-11-08 21:00:57 +00:00
2019-04-09 13:19:21 +00:00
return apply_filters ( 'woocommerce_email_headers' , $header , $this -> id , $this -> object , $this );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2016-01-05 18:42:18 +00:00
* Get email attachments .
2012-11-27 16:22:47 +00:00
*
2017-10-10 15:31:16 +00:00
* @ return array
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function get_attachments () {
2019-04-09 13:19:21 +00:00
return apply_filters ( 'woocommerce_email_attachments' , array (), $this -> id , $this -> object , $this );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2018-03-07 17:27:56 +00:00
* Return email type .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_email_type () {
2015-02-13 14:54:10 +00:00
return $this -> email_type && class_exists ( 'DOMDocument' ) ? $this -> email_type : 'plain' ;
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2016-01-05 18:42:18 +00:00
* Get email content type .
2012-11-27 16:22:47 +00:00
*
2013-11-28 12:54:19 +00:00
* @ return string
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function get_content_type () {
2012-09-17 00:53:17 +00:00
switch ( $this -> get_email_type () ) {
2018-03-07 14:15:17 +00:00
case 'html' :
2012-09-17 00:53:17 +00:00
return 'text/html' ;
2018-03-07 14:15:17 +00:00
case 'multipart' :
2012-09-17 00:53:17 +00:00
return 'multipart/alternative' ;
2018-03-07 14:15:17 +00:00
default :
2012-09-17 00:53:17 +00:00
return 'text/plain' ;
}
}
2012-11-27 16:22:47 +00:00
2015-11-03 14:57:39 +00:00
/**
* Return the email ' s title
2018-03-07 14:15:17 +00:00
*
2015-11-03 14:57:39 +00:00
* @ return string
*/
public function get_title () {
return apply_filters ( 'woocommerce_email_title' , $this -> title , $this );
}
/**
* Return the email ' s description
2018-03-07 14:15:17 +00:00
*
2015-11-03 14:57:39 +00:00
* @ return string
*/
public function get_description () {
return apply_filters ( 'woocommerce_email_description' , $this -> description , $this );
}
2013-04-04 16:21:03 +00:00
/**
2013-11-28 17:59:09 +00:00
* Proxy to parent ' s get_option and attempt to localize the result using gettext .
2014-08-31 05:49:58 +00:00
*
2018-03-07 17:27:56 +00:00
* @ param string $key Option key .
* @ param mixed $empty_value Value to use when option is empty .
2013-11-28 17:59:09 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_option ( $key , $empty_value = null ) {
2015-02-18 02:28:26 +00:00
$value = parent :: get_option ( $key , $empty_value );
2016-02-05 12:44:43 +00:00
return apply_filters ( 'woocommerce_email_get_option' , $value , $this , $value , $key , $empty_value );
2013-04-04 16:21:03 +00:00
}
2012-09-17 00:53:17 +00:00
/**
* Checks if this email is enabled and will be sent .
2018-03-07 14:15:17 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return bool
*/
2014-08-31 05:49:58 +00:00
public function is_enabled () {
2019-04-09 13:19:21 +00:00
return apply_filters ( 'woocommerce_email_enabled_' . $this -> id , 'yes' === $this -> enabled , $this -> object , $this );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2015-11-03 15:40:10 +00:00
/**
* Checks if this email is manually sent
2018-03-07 14:15:17 +00:00
*
2015-11-03 15:40:10 +00:00
* @ return bool
*/
public function is_manual () {
return $this -> manual ;
}
2015-11-02 16:27:00 +00:00
/**
* Checks if this email is customer focussed .
2018-03-07 14:15:17 +00:00
*
2015-11-02 16:27:00 +00:00
* @ return bool
*/
public function is_customer_email () {
return $this -> customer_email ;
}
2012-09-17 00:53:17 +00:00
/**
2016-01-05 18:42:18 +00:00
* Get WordPress blog name .
2012-11-27 16:22:47 +00:00
*
2013-11-27 18:20:31 +00:00
* @ return string
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function get_blogname () {
2012-09-17 00:53:17 +00:00
return wp_specialchars_decode ( get_option ( 'blogname' ), ENT_QUOTES );
}
/**
2016-01-05 18:42:18 +00:00
* Get email content .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_content () {
2012-09-17 00:53:17 +00:00
$this -> sending = true ;
2012-11-27 16:22:47 +00:00
2015-11-02 12:41:33 +00:00
if ( 'plain' === $this -> get_email_type () ) {
2019-03-04 16:33:05 +00:00
$email_content = wordwrap ( preg_replace ( $this -> plain_search , $this -> plain_replace , wp_strip_all_tags ( $this -> get_content_plain () ) ), 70 );
2012-09-17 00:53:17 +00:00
} else {
2014-10-08 21:34:31 +00:00
$email_content = $this -> get_content_html ();
2012-09-17 00:53:17 +00:00
}
2018-07-06 20:29:01 +00:00
return $email_content ;
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-14 17:44:58 +00:00
/**
* Apply inline styles to dynamic content .
2012-11-27 16:22:47 +00:00
*
2018-06-15 11:43:27 +00:00
* We only inline CSS for html emails , and to do so we use Emogrifier library ( if supported ) .
*
2018-03-07 17:27:56 +00:00
* @ param string | null $content Content that will receive inline styles .
2013-11-27 18:20:31 +00:00
* @ return string
2012-11-14 17:44:58 +00:00
*/
2014-08-31 05:49:58 +00:00
public function style_inline ( $content ) {
2018-06-15 11:43:27 +00:00
if ( in_array ( $this -> get_content_type (), array ( 'text/html' , 'multipart/alternative' ), true ) ) {
2014-10-08 21:34:31 +00:00
ob_start ();
wc_get_template ( 'emails/email-styles.php' );
2019-03-14 06:43:55 +00:00
$css = apply_filters ( 'woocommerce_email_styles' , ob_get_clean (), $this );
2012-11-27 16:22:47 +00:00
2018-06-15 11:43:27 +00:00
if ( $this -> supports_emogrifier () ) {
2019-03-27 11:35:45 +00:00
$emogrifier_class = '\\Pelago\\Emogrifier' ;
if ( ! class_exists ( $emogrifier_class ) ) {
2018-06-15 11:43:27 +00:00
include_once dirname ( dirname ( __FILE__ ) ) . '/libraries/class-emogrifier.php' ;
}
try {
2019-03-27 11:35:45 +00:00
$emogrifier = new $emogrifier_class ( $content , $css );
2018-06-15 11:43:27 +00:00
$content = $emogrifier -> emogrify ();
} catch ( Exception $e ) {
$logger = wc_get_logger ();
$logger -> error ( $e -> getMessage (), array ( 'source' => 'emogrifier' ) );
}
} else {
$content = '<style type="text/css">' . $css . '</style>' . $content ;
2015-03-08 23:45:05 +00:00
}
2013-08-20 22:11:43 +00:00
}
2012-11-14 17:44:58 +00:00
return $content ;
}
2012-11-27 16:22:47 +00:00
2018-06-15 11:43:27 +00:00
/**
* Return if emogrifier library is supported .
*
* @ since 3.5 . 0
* @ return bool
*/
protected function supports_emogrifier () {
return class_exists ( 'DOMDocument' ) && version_compare ( PHP_VERSION , '5.5' , '>=' );
}
2012-09-17 00:53:17 +00:00
/**
2015-11-03 14:57:39 +00:00
* Get the email content in plain text format .
2018-03-07 14:15:17 +00:00
*
2013-11-27 18:20:31 +00:00
* @ return string
2012-09-17 00:53:17 +00:00
*/
2018-03-07 14:15:17 +00:00
public function get_content_plain () {
return '' ; }
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2015-11-03 14:57:39 +00:00
* Get the email content in HTML format .
2018-03-07 14:15:17 +00:00
*
2013-11-27 18:20:31 +00:00
* @ return string
2012-09-17 00:53:17 +00:00
*/
2018-03-07 14:15:17 +00:00
public function get_content_html () {
return '' ; }
2012-09-17 00:53:17 +00:00
/**
2015-11-03 14:57:39 +00:00
* Get the from name for outgoing emails .
2018-03-07 14:15:17 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_from_name () {
2015-11-20 18:00:37 +00:00
$from_name = apply_filters ( 'woocommerce_email_from_name' , get_option ( 'woocommerce_email_from_name' ), $this );
return wp_specialchars_decode ( esc_html ( $from_name ), ENT_QUOTES );
2012-09-17 00:53:17 +00:00
}
/**
2015-11-03 14:57:39 +00:00
* Get the from address for outgoing emails .
2018-03-07 14:15:17 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_from_address () {
2015-11-20 18:00:37 +00:00
$from_address = apply_filters ( 'woocommerce_email_from_address' , get_option ( 'woocommerce_email_from_address' ), $this );
return sanitize_email ( $from_address );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
2015-11-03 14:57:39 +00:00
* Send an email .
2018-03-07 14:15:17 +00:00
*
2018-03-07 17:27:56 +00:00
* @ param string $to Email to .
* @ param string $subject Email subject .
* @ param string $message Email message .
* @ param string $headers Email headers .
* @ param array $attachments Email attachments .
2015-11-03 14:57:39 +00:00
* @ return bool success
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function send ( $to , $subject , $message , $headers , $attachments ) {
2012-12-15 11:53:32 +00:00
add_filter ( 'wp_mail_from' , array ( $this , 'get_from_address' ) );
add_filter ( 'wp_mail_from_name' , array ( $this , 'get_from_name' ) );
add_filter ( 'wp_mail_content_type' , array ( $this , 'get_content_type' ) );
2012-11-27 16:22:47 +00:00
2019-01-09 11:28:32 +00:00
$message = apply_filters ( 'woocommerce_mail_content' , $this -> style_inline ( $message ) );
$mail_callback = apply_filters ( 'woocommerce_mail_callback' , 'wp_mail' , $this );
$mail_callback_params = apply_filters ( 'woocommerce_mail_callback_params' , array ( $to , $subject , $message , $headers , $attachments ), $this );
$return = call_user_func_array ( $mail_callback , $mail_callback_params );
2012-09-17 00:53:17 +00:00
2012-12-15 11:53:32 +00:00
remove_filter ( 'wp_mail_from' , array ( $this , 'get_from_address' ) );
remove_filter ( 'wp_mail_from_name' , array ( $this , 'get_from_name' ) );
remove_filter ( 'wp_mail_content_type' , array ( $this , 'get_content_type' ) );
2014-08-31 05:49:58 +00:00
2014-03-23 17:09:01 +00:00
return $return ;
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2014-08-31 05:49:58 +00:00
/**
* Initialise Settings Form Fields - these are generic email options most will use .
*/
2015-02-13 14:16:58 +00:00
public function init_form_fields () {
2019-03-04 16:33:05 +00:00
/* translators: %s: list of placeholders */
$placeholder_text = sprintf ( __ ( 'Available placeholders: %s' , 'woocommerce' ), '<code>' . esc_html ( implode ( '</code>, <code>' , array_keys ( $this -> placeholders ) ) ) . '</code>' );
2018-03-07 14:15:17 +00:00
$this -> form_fields = array (
2019-03-04 16:33:05 +00:00
'enabled' => array (
2018-03-07 14:15:17 +00:00
'title' => __ ( 'Enable/Disable' , 'woocommerce' ),
'type' => 'checkbox' ,
'label' => __ ( 'Enable this email notification' , 'woocommerce' ),
'default' => 'yes' ,
2012-09-17 00:53:17 +00:00
),
2019-03-04 16:33:05 +00:00
'subject' => array (
2017-06-14 17:01:12 +00:00
'title' => __ ( 'Subject' , 'woocommerce' ),
2015-11-03 14:57:39 +00:00
'type' => 'text' ,
2018-03-07 14:15:17 +00:00
'desc_tip' => true ,
2019-03-04 16:33:05 +00:00
'description' => $placeholder_text ,
2017-06-14 16:13:43 +00:00
'placeholder' => $this -> get_default_subject (),
2015-11-03 14:57:39 +00:00
'default' => '' ,
2012-09-17 00:53:17 +00:00
),
2019-03-04 16:33:05 +00:00
'heading' => array (
2016-10-12 10:16:30 +00:00
'title' => __ ( 'Email heading' , 'woocommerce' ),
2015-11-03 14:57:39 +00:00
'type' => 'text' ,
2018-03-07 14:15:17 +00:00
'desc_tip' => true ,
2019-03-04 16:33:05 +00:00
'description' => $placeholder_text ,
2017-06-14 16:13:43 +00:00
'placeholder' => $this -> get_default_heading (),
2015-11-03 14:57:39 +00:00
'default' => '' ,
2012-09-17 00:53:17 +00:00
),
2019-03-04 16:33:05 +00:00
'additional_content' => array (
'title' => __ ( 'Additional content' , 'woocommerce' ),
'description' => __ ( 'Text to appear to appear below the main email content.' , 'woocommerce' ) . ' ' . $placeholder_text ,
'css' => 'width:400px; height: 75px;' ,
'placeholder' => __ ( 'N/A' , 'woocommerce' ),
'type' => 'textarea' ,
'default' => $this -> get_default_additional_content (),
'desc_tip' => true ,
),
'email_type' => array (
2016-10-12 10:16:30 +00:00
'title' => __ ( 'Email type' , 'woocommerce' ),
2015-11-03 14:57:39 +00:00
'type' => 'select' ,
'description' => __ ( 'Choose which format of email to send.' , 'woocommerce' ),
'default' => 'html' ,
'class' => 'email_type wc-enhanced-select' ,
'options' => $this -> get_email_type_options (),
2016-08-27 01:46:45 +00:00
'desc_tip' => true ,
),
2012-09-17 00:53:17 +00:00
);
2014-08-31 05:49:58 +00:00
}
2012-11-27 16:22:47 +00:00
2015-02-13 14:16:58 +00:00
/**
2015-11-03 14:57:39 +00:00
* Email type options .
2018-03-07 14:15:17 +00:00
*
2015-02-13 14:16:58 +00:00
* @ return array
*/
public function get_email_type_options () {
2015-11-03 14:57:39 +00:00
$types = array ( 'plain' => __ ( 'Plain text' , 'woocommerce' ) );
2015-02-13 14:16:58 +00:00
2015-02-13 14:54:10 +00:00
if ( class_exists ( 'DOMDocument' ) ) {
2015-11-03 14:57:39 +00:00
$types [ 'html' ] = __ ( 'HTML' , 'woocommerce' );
2015-02-13 14:16:58 +00:00
$types [ 'multipart' ] = __ ( 'Multipart' , 'woocommerce' );
}
return $types ;
}
2012-09-17 00:53:17 +00:00
/**
2015-11-03 14:57:39 +00:00
* Admin Panel Options Processing .
2012-09-17 00:53:17 +00:00
*/
2016-04-20 07:48:29 +00:00
public function process_admin_options () {
2018-03-07 14:15:17 +00:00
// Save regular options.
2016-04-20 07:48:29 +00:00
parent :: process_admin_options ();
$post_data = $this -> get_post_data ();
2012-11-27 16:22:47 +00:00
2018-03-07 14:15:17 +00:00
// Save templates.
2016-04-20 07:48:29 +00:00
if ( isset ( $post_data [ 'template_html_code' ] ) ) {
2016-03-24 17:26:40 +00:00
$this -> save_template ( $post_data [ 'template_html_code' ], $this -> template_html );
2015-05-29 16:40:53 +00:00
}
2016-04-20 07:48:29 +00:00
if ( isset ( $post_data [ 'template_plain_code' ] ) ) {
2016-03-24 17:26:40 +00:00
$this -> save_template ( $post_data [ 'template_plain_code' ], $this -> template_plain );
2015-05-29 16:40:53 +00:00
}
2015-03-27 15:51:30 +00:00
}
2014-08-31 05:49:58 +00:00
2015-03-27 15:51:30 +00:00
/**
* Get template .
*
2018-03-07 17:27:56 +00:00
* @ param string $type Template type . Can be either 'template_html' or 'template_plain' .
2015-03-27 15:51:30 +00:00
* @ return string
*/
public function get_template ( $type ) {
2015-11-03 14:57:39 +00:00
$type = basename ( $type );
2012-11-27 16:22:47 +00:00
2015-11-03 14:57:39 +00:00
if ( 'template_html' === $type ) {
2015-03-27 15:51:30 +00:00
return $this -> template_html ;
2015-11-03 14:57:39 +00:00
} elseif ( 'template_plain' === $type ) {
2015-03-27 15:51:30 +00:00
return $this -> template_plain ;
2012-09-17 00:53:17 +00:00
}
2015-03-27 15:51:30 +00:00
return '' ;
}
2012-11-27 16:22:47 +00:00
2015-03-27 15:51:30 +00:00
/**
2015-11-03 13:31:20 +00:00
* Save the email templates .
2015-03-27 15:51:30 +00:00
*
2015-05-29 16:40:53 +00:00
* @ since 2.4 . 0
2018-03-07 17:27:56 +00:00
* @ param string $template_code Template code .
* @ param string $template_path Template path .
2015-03-27 15:51:30 +00:00
*/
protected function save_template ( $template_code , $template_path ) {
2015-05-21 04:52:38 +00:00
if ( current_user_can ( 'edit_themes' ) && ! empty ( $template_code ) && ! empty ( $template_path ) ) {
2018-03-07 14:15:17 +00:00
$saved = false ;
$file = get_stylesheet_directory () . '/' . WC () -> template_path () . $template_path ;
$code = wp_unslash ( $template_code );
2012-11-27 16:22:47 +00:00
2018-03-07 17:27:56 +00:00
if ( is_writeable ( $file ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writeable
2019-03-04 16:33:05 +00:00
$f = fopen ( $file , 'w+' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen
2014-08-31 05:49:58 +00:00
2016-09-09 00:14:28 +00:00
if ( false !== $f ) {
2019-03-04 16:33:05 +00:00
fwrite ( $f , $code ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite
fclose ( $f ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
2014-08-31 05:49:58 +00:00
$saved = true ;
}
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( ! $saved ) {
2019-03-04 16:33:05 +00:00
$redirect = add_query_arg ( 'wc_error' , rawurlencode ( __ ( 'Could not write to template file.' , 'woocommerce' ) ) );
2015-11-03 14:57:39 +00:00
wp_safe_redirect ( $redirect );
2014-08-31 05:49:58 +00:00
exit ;
}
2012-09-17 00:53:17 +00:00
}
2014-08-31 05:49:58 +00:00
}
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Get the template file in the current theme .
*
2018-03-07 17:27:56 +00:00
* @ param string $template Template name .
2014-12-03 03:30:28 +00:00
*
* @ return string
*/
public function get_theme_template_file ( $template ) {
return get_stylesheet_directory () . '/' . apply_filters ( 'woocommerce_template_directory' , 'woocommerce' , $template ) . '/' . $template ;
}
/**
* Move template action .
*
2018-03-07 17:27:56 +00:00
* @ param string $template_type Template type .
2014-12-03 03:30:28 +00:00
*/
protected function move_template_action ( $template_type ) {
2018-03-07 17:27:56 +00:00
$template = $this -> get_template ( $template_type );
if ( ! empty ( $template ) ) {
$theme_file = $this -> get_theme_template_file ( $template );
if ( wp_mkdir_p ( dirname ( $theme_file ) ) && ! file_exists ( $theme_file ) ) {
// Locate template file.
$core_file = $this -> template_base . $template ;
$template_file = apply_filters ( 'woocommerce_locate_core_template' , $core_file , $template , $this -> template_base , $this -> id );
// Copy template file.
copy ( $template_file , $theme_file );
/**
* Action hook fired after copying email template file .
*
* @ param string $template_type The copied template type
* @ param string $email The email object
*/
do_action ( 'woocommerce_copy_email_template' , $template_type , $this );
?>
< div class = " updated " >
< p >< ? php echo esc_html__ ( 'Template file copied to theme.' , 'woocommerce' ); ?> </p>
</ div >
< ? php
2012-09-17 00:53:17 +00:00
}
2014-12-03 03:30:28 +00:00
}
}
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Delete template action .
*
2018-03-07 17:27:56 +00:00
* @ param string $template_type Template type .
2014-12-03 03:30:28 +00:00
*/
protected function delete_template_action ( $template_type ) {
2018-03-07 17:27:56 +00:00
$template = $this -> get_template ( $template_type );
2014-08-31 05:49:58 +00:00
2018-03-07 17:27:56 +00:00
if ( $template ) {
2014-12-03 03:30:28 +00:00
if ( ! empty ( $template ) ) {
$theme_file = $this -> get_theme_template_file ( $template );
2014-11-25 14:59:29 +00:00
2014-12-03 03:30:28 +00:00
if ( file_exists ( $theme_file ) ) {
2018-03-07 17:27:56 +00:00
unlink ( $theme_file ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink
2014-11-25 14:59:29 +00:00
2014-12-03 03:30:28 +00:00
/**
2018-03-07 17:27:56 +00:00
* Action hook fired after deleting template file .
2014-12-03 03:30:28 +00:00
*
* @ param string $template The deleted template type
* @ param string $email The email object
*/
do_action ( 'woocommerce_delete_email_template' , $template_type , $this );
2018-03-07 17:27:56 +00:00
?>
< div class = " updated " >
< p >< ? php echo esc_html__ ( 'Template file deleted from theme.' , 'woocommerce' ); ?> </p>
</ div >
< ? php
2012-09-17 00:53:17 +00:00
}
}
2014-12-03 03:30:28 +00:00
}
}
/**
* Admin actions .
*/
protected function admin_actions () {
2018-03-07 14:15:17 +00:00
// Handle any actions.
2015-05-20 16:55:35 +00:00
if (
( ! empty ( $this -> template_html ) || ! empty ( $this -> template_plain ) )
&& ( ! empty ( $_GET [ 'move_template' ] ) || ! empty ( $_GET [ 'delete_template' ] ) )
2019-03-04 15:30:56 +00:00
&& 'GET' === $_SERVER [ 'REQUEST_METHOD' ] // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
2015-05-20 16:55:35 +00:00
) {
2018-03-07 17:27:56 +00:00
if ( empty ( $_GET [ '_wc_email_nonce' ] ) || ! wp_verify_nonce ( wc_clean ( wp_unslash ( $_GET [ '_wc_email_nonce' ] ) ), 'woocommerce_email_template_nonce' ) ) {
wp_die ( esc_html__ ( 'Action failed. Please refresh the page and retry.' , 'woocommerce' ) );
2015-05-20 16:55:35 +00:00
}
2015-05-21 04:52:38 +00:00
if ( ! current_user_can ( 'edit_themes' ) ) {
2018-03-07 17:27:56 +00:00
wp_die ( esc_html__ ( 'You don’t have permission to do this.' , 'woocommerce' ) );
2015-05-20 16:55:35 +00:00
}
2014-12-03 03:30:28 +00:00
if ( ! empty ( $_GET [ 'move_template' ] ) ) {
2018-03-07 17:27:56 +00:00
$this -> move_template_action ( wc_clean ( wp_unslash ( $_GET [ 'move_template' ] ) ) );
2014-12-03 03:30:28 +00:00
}
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
if ( ! empty ( $_GET [ 'delete_template' ] ) ) {
2018-03-07 17:27:56 +00:00
$this -> delete_template_action ( wc_clean ( wp_unslash ( $_GET [ 'delete_template' ] ) ) );
2014-12-03 03:30:28 +00:00
}
2012-09-17 00:53:17 +00:00
}
2014-12-03 03:30:28 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Admin Options .
2014-12-03 03:30:28 +00:00
*
2015-03-27 16:11:25 +00:00
* Setup the email settings screen .
* Override this in your email .
2014-12-03 03:30:28 +00:00
*
* @ since 1.0 . 0
*/
public function admin_options () {
2015-03-27 16:10:00 +00:00
// Do admin actions.
2014-12-03 03:30:28 +00:00
$this -> admin_actions ();
2012-09-17 00:53:17 +00:00
?>
2016-10-12 10:16:30 +00:00
< 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>
2016-02-17 16:02:22 +00:00
2018-03-07 17:27:56 +00:00
< ? php echo wpautop ( wp_kses_post ( $this -> get_description () ) ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
2012-09-17 00:53:17 +00:00
2014-11-25 14:59:29 +00:00
< ? php
2018-03-07 14:15:17 +00:00
/**
2018-03-07 17:27:56 +00:00
* Action hook fired before displaying email settings .
2018-03-07 14:15:17 +00:00
*
* @ param string $email The email object
*/
do_action ( 'woocommerce_email_settings_before' , $this );
2014-11-25 14:59:29 +00:00
?>
2012-09-17 00:53:17 +00:00
< table class = " form-table " >
< ? php $this -> generate_settings_html (); ?>
</ table >
2012-11-27 16:22:47 +00:00
2014-11-25 14:59:29 +00:00
< ? php
2018-03-07 14:15:17 +00:00
/**
2018-03-07 17:27:56 +00:00
* Action hook fired after displaying email settings .
2018-03-07 14:15:17 +00:00
*
* @ param string $email The email object
*/
do_action ( 'woocommerce_email_settings_after' , $this );
2014-11-25 14:59:29 +00:00
?>
2018-03-07 14:15:17 +00:00
< ? php
if ( current_user_can ( 'edit_themes' ) && ( ! empty ( $this -> template_html ) || ! empty ( $this -> template_plain ) ) ) {
?>
2012-09-17 00:53:17 +00:00
< div id = " template " >
2018-03-07 14:15:17 +00:00
< ? php
2012-11-27 16:22:47 +00:00
$templates = array (
2014-12-03 03:30:28 +00:00
'template_html' => __ ( 'HTML template' , 'woocommerce' ),
2016-08-27 01:46:45 +00:00
'template_plain' => __ ( 'Plain text template' , 'woocommerce' ),
2012-09-17 00:53:17 +00:00
);
2014-08-31 05:49:58 +00:00
2014-12-03 03:30:28 +00:00
foreach ( $templates as $template_type => $title ) :
$template = $this -> get_template ( $template_type );
2014-08-31 05:49:58 +00:00
2014-12-03 03:30:28 +00:00
if ( empty ( $template ) ) {
2012-09-17 00:53:17 +00:00
continue ;
2013-11-24 10:24:12 +00:00
}
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
$local_file = $this -> get_theme_template_file ( $template );
$core_file = $this -> template_base . $template ;
2016-12-28 19:38:51 +00:00
$template_file = apply_filters ( 'woocommerce_locate_core_template' , $core_file , $template , $this -> template_base , $this -> id );
2014-12-03 03:30:28 +00:00
$template_dir = apply_filters ( 'woocommerce_template_directory' , 'woocommerce' , $template );
2012-09-17 00:53:17 +00:00
?>
2018-03-07 17:27:56 +00:00
< div class = " template <?php echo esc_attr( $template_type ); ?> " >
2012-10-17 12:46:35 +00:00
< h4 >< ? php echo wp_kses_post ( $title ); ?> </h4>
2012-11-27 16:22:47 +00:00
2018-03-07 14:15:17 +00:00
< ? php if ( file_exists ( $local_file ) ) : ?>
2012-09-17 00:53:17 +00:00
< p >
< a href = " # " class = " button toggle_editor " ></ a >
2012-11-27 16:22:47 +00:00
2018-03-07 17:27:56 +00:00
< ? php if ( is_writable ( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable ?>
< 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 " >
< ? php esc_html_e ( 'Delete template file' , 'woocommerce' ); ?>
</ a >
2012-09-17 00:53:17 +00:00
< ? php endif ; ?>
2018-03-07 17:27:56 +00:00
< ? php
/* translators: %s: Path to template file */
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>' );
?>
2012-09-17 00:53:17 +00:00
</ p >
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
< div class = " editor " style = " display:none " >
2018-03-07 14:15:17 +00:00
< textarea class = " code " cols = " 25 " rows = " 20 "
< ? php
2018-03-07 17:27:56 +00:00
if ( ! is_writable ( $local_file ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
2018-03-07 14:15:17 +00:00
?>
readonly = " readonly " disabled = " disabled "
< ? php else : ?>
2019-03-04 16:33:05 +00:00
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>
2012-09-17 00:53:17 +00:00
</ div >
2018-03-07 14:15:17 +00:00
< ? php elseif ( file_exists ( $template_file ) ) : ?>
2012-09-17 00:53:17 +00:00
< p >
< a href = " # " class = " button toggle_editor " ></ a >
2012-11-27 16:22:47 +00:00
2017-08-21 10:32:21 +00:00
< ? php
2018-03-07 14:15:17 +00:00
$emails_dir = get_stylesheet_directory () . '/' . $template_dir . '/emails' ;
$templates_dir = get_stylesheet_directory () . '/' . $template_dir ;
$theme_dir = get_stylesheet_directory ();
if ( is_dir ( $emails_dir ) ) {
$target_dir = $emails_dir ;
} elseif ( is_dir ( $templates_dir ) ) {
$target_dir = $templates_dir ;
} else {
$target_dir = $theme_dir ;
}
2018-03-07 17:27:56 +00:00
if ( is_writable ( $target_dir ) ) : // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
2018-03-07 14:15:17 +00:00
?>
2018-03-07 17:27:56 +00:00
< 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 " >
< ? php esc_html_e ( 'Copy file to theme' , 'woocommerce' ); ?>
</ a >
2018-03-07 14:15:17 +00:00
< ? php endif ; ?>
2012-09-17 00:53:17 +00:00
2018-03-07 17:27:56 +00:00
< ? php
/* translators: 1: Path to template file 2: Path to theme folder */
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>' );
?>
2012-09-17 00:53:17 +00:00
</ p >
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
< div class = " editor " style = " display:none " >
2019-03-04 16:33:05 +00:00
< 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>
2012-09-17 00:53:17 +00:00
</ div >
2018-03-07 14:15:17 +00:00
< ? php else : ?>
2018-03-07 17:27:56 +00:00
< p >< ? php esc_html_e ( 'File was not found.' , 'woocommerce' ); ?> </p>
2018-03-07 14:15:17 +00:00
< ? php endif ; ?>
2012-09-17 00:53:17 +00:00
</ div >
2018-03-07 14:15:17 +00:00
< ? php endforeach ; ?>
2012-09-17 00:53:17 +00:00
</ div >
2018-03-07 14:15:17 +00:00
2012-09-17 00:53:17 +00:00
< ? php
2018-03-07 14:15:17 +00:00
wc_enqueue_js (
" jQuery( 'select.email_type' ).change( function() {
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
var val = jQuery ( this ) . val ();
2012-11-27 16:22:47 +00:00
2015-08-05 18:57:15 +00:00
jQuery ( '.template_plain, .template_html' ) . show ();
2012-11-27 16:22:47 +00:00
2015-08-05 18:57:15 +00:00
if ( val != 'multipart' && val != 'html' ) {
2012-09-17 00:53:17 +00:00
jQuery ( '.template_html' ) . hide ();
2015-08-05 18:57:15 +00:00
}
2012-11-27 16:22:47 +00:00
2015-08-05 18:57:15 +00:00
if ( val != 'multipart' && val != 'plain' ) {
2012-09-17 00:53:17 +00:00
jQuery ( '.template_plain' ) . hide ();
2015-08-05 18:57:15 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
}) . change ();
2012-11-27 16:22:47 +00:00
2013-05-06 13:17:04 +00:00
var view = '" . esc_js( __( ' View template ', ' woocommerce ' ) ) . "' ;
var hide = '" . esc_js( __( ' Hide template ', ' woocommerce ' ) ) . "' ;
2012-11-27 16:22:47 +00:00
2015-08-05 18:57:15 +00:00
jQuery ( 'a.toggle_editor' ) . text ( view ) . toggle ( function () {
jQuery ( this ) . text ( hide ) . closest ( ' .template' ) . find ( '.editor' ) . slideToggle ();
2012-09-17 00:53:17 +00:00
return false ;
}, function () {
2015-08-05 18:57:15 +00:00
jQuery ( this ) . text ( view ) . closest ( '.template' ) . find ( '.editor' ) . slideToggle ();
2012-09-17 00:53:17 +00:00
return false ;
} );
2012-11-27 16:22:47 +00:00
2015-08-05 18:57:15 +00:00
jQuery ( 'a.delete_template' ) . click ( function () {
if ( window . confirm ( '" . esc_js( __( ' Are you sure you want to delete this template file ? ', ' woocommerce ' ) ) . "' ) ) {
2012-09-17 00:53:17 +00:00
return true ;
2015-08-05 18:57:15 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
return false ;
});
2012-11-27 16:22:47 +00:00
2015-08-05 18:57:15 +00:00
jQuery ( '.editor textarea' ) . change ( function () {
var name = jQuery ( this ) . attr ( 'data-name' );
2012-11-27 16:22:47 +00:00
2015-08-05 18:57:15 +00:00
if ( name ) {
jQuery ( this ) . attr ( 'name' , name );
}
2018-03-07 14:15:17 +00:00
}); "
);
2012-09-17 00:53:17 +00:00
}
}
2013-02-21 00:39:38 +00:00
}