2012-09-17 00:53:17 +00:00
< ? php
2013-02-20 17:14:46 +00:00
2014-09-20 18:39:49 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
2013-02-20 17:14:46 +00:00
2012-09-17 00:53:17 +00:00
/**
2014-09-23 16:20:28 +00:00
* Email Class
2012-09-17 00:53:17 +00:00
*
* WooCommerce Email Class which is extended by specific email template classes to add emails to WooCommerce
*
2014-08-31 05:49:58 +00:00
* @ class WC_Email
* @ version 2.0 . 0
2014-09-23 16:20:28 +00:00
* @ package WooCommerce / Classes / Emails
2014-08-31 05:49:58 +00:00
* @ author WooThemes
* @ extends WC_Settings_API
2012-09-17 00:53:17 +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 .
*
* @ 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 .
*
* @ 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 .
*
* @ var string
*/
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 .
*
* @ 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
/**
* Plain text template path .
*
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $template_plain ;
2012-09-17 00:53:17 +00:00
2014-12-03 03:30:28 +00:00
/**
* HTML template path .
*
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $template_html ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Template path .
*
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $template_base ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Recipients for the email .
*
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $recipient ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Heading for the email content .
*
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $heading ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Subject for the email .
*
* @ var string
*/
2014-08-31 05:49:58 +00:00
public $subject ;
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 .
*
* @ var object
*/
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
/**
* Strings to find in subjects / headings .
*
* @ var array
*/
2014-08-31 05:49:58 +00:00
public $find ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Strings to replace in subjects / headings .
*
* @ var array
*/
2014-08-31 05:49:58 +00:00
public $replace ;
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
/**
* Mime boundary ( for multipart emails ) .
*
* @ 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 ) .
*
* @ 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 .
*
* @ var bool
*/
2014-08-31 05:49:58 +00:00
public $sending ;
2012-12-13 09:52:20 +00:00
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 ,
* used in conjunction with $replace .
* https :// raw . github . com / ushahidi / wp - silcc / master / class . html2text . inc
*
* @ var array $search
* @ see $replace
*/
public $plain_search = array (
" / \r / " , // Non-legal carriage return
'/&(nbsp|#160);/i' , // Non-breaking space
'/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i' , // Double quotes
'/&(apos|rsquo|lsquo|#8216|#8217);/i' , // Single quotes
'/>/i' , // Greater-than
'/</i' , // Less-than
'/&/i' , // Ampersand
'/&/i' , // Ampersand
'/&/i' , // Ampersand
'/&(copy|#169);/i' , // Copyright
'/&(trade|#8482|#153);/i' , // Trademark
'/&(reg|#174);/i' , // Registered
'/&(mdash|#151|#8212);/i' , // mdash
'/&(ndash|minus|#8211|#8722);/i' , // ndash
'/&(bull|#149|#8226);/i' , // Bullet
'/&(pound|#163);/i' , // Pound sign
'/&(euro|#8364);/i' , // Euro sign
'/$/' , // Dollar sign
'/&[^&;]+;/i' , // Unknown/unhandled entities
'/[ ]{2,}/' // Runs of spaces, post-handling
);
/**
* List of pattern replacements corresponding to patterns searched .
2012-09-17 00:53:17 +00:00
*
2014-08-31 05:49:58 +00:00
* @ var array $replace
* @ see $search
*/
public $plain_replace = array (
'' , // Non-legal carriage return
' ' , // Non-breaking space
'"' , // Double quotes
" ' " , // Single quotes
'>' , // Greater-than
'<' , // Less-than
'&' , // Ampersand
'&' , // 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
);
/**
* Constructor
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function __construct () {
2012-09-17 00:53: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
2012-09-17 00:53:17 +00:00
// Save settings hook
2012-12-15 11:53:32 +00:00
add_action ( 'woocommerce_update_options_email_' . $this -> id , array ( $this , 'process_admin_options' ) );
2012-09-17 00:53:17 +00:00
2013-02-27 10:10:36 +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
2012-09-17 00:53:17 +00:00
// Settings
2014-08-31 05:49:58 +00:00
$this -> heading = $this -> get_option ( 'heading' , $this -> heading );
$this -> subject = $this -> get_option ( 'subject' , $this -> subject );
$this -> email_type = $this -> get_option ( 'email_type' );
$this -> enabled = $this -> get_option ( 'enabled' );
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
// Find/replace
2014-05-30 09:42:47 +00:00
$this -> find [ 'blogname' ] = '{blogname}' ;
$this -> find [ 'site-title' ] = '{site_title}' ;
2014-08-31 05:49:58 +00:00
2014-05-30 09:42:47 +00:00
$this -> replace [ 'blogname' ] = $this -> get_blogname ();
$this -> replace [ 'site-title' ] = $this -> get_blogname ();
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
// For multipart messages
2012-12-15 11:53:32 +00:00
add_filter ( 'phpmailer_init' , array ( $this , 'handle_multipart' ) );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* handle_multipart function .
2012-11-27 16:22:47 +00:00
*
2013-11-28 12:54:19 +00:00
* @ param PHPMailer $mailer
* @ return PHPMailer
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function handle_multipart ( $mailer ) {
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( $this -> sending && $this -> get_email_type () == 'multipart' ) {
2012-12-13 09:52:20 +00:00
$mailer -> AltBody = wordwrap ( preg_replace ( $this -> plain_search , $this -> plain_replace , strip_tags ( $this -> get_content_plain () ) ) );
//$mailer->AltBody = wordwrap( html_entity_decode( strip_tags( $this->get_content_plain() ) ), 70 );
2012-09-17 00:53:17 +00:00
$this -> sending = false ;
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
return $mailer ;
}
/**
* format_string function .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ param mixed $string
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function format_string ( $string ) {
2012-09-17 00:53:17 +00:00
return str_replace ( $this -> find , $this -> replace , $string );
}
2014-08-31 05:49:58 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_subject function .
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 () {
2012-09-17 00:53:17 +00:00
return apply_filters ( 'woocommerce_email_subject_' . $this -> id , $this -> format_string ( $this -> subject ), $this -> object );
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_heading function .
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 () {
2012-09-17 00:53:17 +00:00
return apply_filters ( 'woocommerce_email_heading_' . $this -> id , $this -> format_string ( $this -> heading ), $this -> object );
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_recipient function .
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_recipient () {
2012-09-17 00:53:17 +00:00
return apply_filters ( 'woocommerce_email_recipient_' . $this -> id , $this -> recipient , $this -> object );
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_headers function .
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 () {
2012-09-17 00:53:17 +00:00
return apply_filters ( 'woocommerce_email_headers' , " Content-Type: " . $this -> get_content_type () . " \r \n " , $this -> id , $this -> object );
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_attachments function .
2012-11-27 16:22:47 +00:00
*
2014-09-08 15:35:37 +00:00
* @ return string | array
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function get_attachments () {
2014-01-20 15:33:33 +00:00
return apply_filters ( 'woocommerce_email_attachments' , array (), $this -> id , $this -> object );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_type function .
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 () {
2012-09-17 00:53:17 +00:00
return $this -> email_type ? $this -> email_type : 'plain' ;
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_content_type function .
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 () ) {
2015-02-10 12:51:44 +00:00
case 'html' :
2012-09-17 00:53:17 +00:00
return 'text/html' ;
2015-02-10 12:51:44 +00:00
case 'multipart' :
2012-09-17 00:53:17 +00:00
return 'multipart/alternative' ;
default :
return 'text/plain' ;
}
}
2012-11-27 16:22:47 +00:00
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
*
2013-11-28 17:59:09 +00:00
* @ param string $key
* @ param mixed $empty_value
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_option ( $key , $empty_value = null ) {
2015-02-10 12:51:44 +00:00
return apply_filters ( 'woocommerce_email_get_option' , __ ( parent :: get_option ( $key , $empty_value ) ), $this );
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 .
*
* @ return bool
*/
2014-08-31 05:49:58 +00:00
public function is_enabled () {
2015-02-10 12:51:44 +00:00
$enabled = $this -> enabled == 'yes' ? true : false ;
2013-02-27 10:10:36 +00:00
return apply_filters ( 'woocommerce_email_enabled_' . $this -> id , $enabled , $this -> object );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_blogname function .
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 );
}
/**
* get_content function .
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-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
$this -> sending = true ;
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( $this -> get_email_type () == 'plain' ) {
2012-12-13 09:52:20 +00:00
$email_content = preg_replace ( $this -> plain_search , $this -> plain_replace , strip_tags ( $this -> get_content_plain () ) );
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
}
2014-01-03 14:12:59 +00:00
return wordwrap ( $email_content , 70 );
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
*
2014-09-07 23:37:55 +00:00
* @ param string | null $content
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 ) {
2014-10-08 21:34:31 +00:00
// make sure we only inline CSS for html emails
2015-01-02 12:55:47 +00:00
if ( in_array ( $this -> get_content_type (), array ( 'text/html' , 'multipart/alternative' ) ) ) {
2012-11-27 16:22:47 +00:00
2014-10-08 21:34:31 +00:00
// get CSS styles
ob_start ();
wc_get_template ( 'emails/email-styles.php' );
$css = apply_filters ( 'woocommerce_email_styles' , ob_get_clean () );
2012-11-27 16:22:47 +00:00
2014-10-08 21:34:31 +00:00
// apply CSS styles inline for picky email clients
$emogrifier = new Emogrifier ( $content , $css );
$content = $emogrifier -> emogrify ();
2013-08-20 22:11:43 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-14 17:44:58 +00:00
return $content ;
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_content_plain function .
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_content_plain () {}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* get_content_html function .
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_content_html () {}
2012-09-17 00:53:17 +00:00
/**
* Get from name for email .
*
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_from_name () {
2013-09-04 12:24:01 +00:00
return wp_specialchars_decode ( esc_html ( get_option ( 'woocommerce_email_from_name' ) ), ENT_QUOTES );
2012-09-17 00:53:17 +00:00
}
/**
* Get from email address .
*
* @ return string
*/
2014-08-31 05:49:58 +00:00
public function get_from_address () {
2012-10-18 10:33:47 +00:00
return sanitize_email ( get_option ( 'woocommerce_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
/**
* Send the email .
*
2014-09-07 23:37:55 +00:00
* @ param string $to
* @ param string $subject
* @ param string $message
2012-09-17 00:53:17 +00:00
* @ param string $headers
* @ param string $attachments
2014-03-23 17:09:01 +00:00
* @ return bool
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
2014-10-21 15:18:59 +00:00
$message = apply_filters ( 'woocommerce_mail_content' , $this -> style_inline ( $message ) );
$return = wp_mail ( $to , $subject , $message , $headers , $attachments );
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 .
*/
function init_form_fields () {
$this -> form_fields = array (
2012-09-17 00:53:17 +00:00
'enabled' => array (
2014-08-31 05:49:58 +00:00
'title' => __ ( 'Enable/Disable' , 'woocommerce' ),
'type' => 'checkbox' ,
'label' => __ ( 'Enable this email notification' , 'woocommerce' ),
'default' => 'yes'
2012-09-17 00:53:17 +00:00
),
'subject' => array (
2014-08-31 05:49:58 +00:00
'title' => __ ( 'Email subject' , 'woocommerce' ),
'type' => 'text' ,
'description' => sprintf ( __ ( 'Defaults to <code>%s</code>' , 'woocommerce' ), $this -> subject ),
'placeholder' => '' ,
'default' => ''
2012-09-17 00:53:17 +00:00
),
'heading' => array (
2014-08-31 05:49:58 +00:00
'title' => __ ( 'Email heading' , 'woocommerce' ),
'type' => 'text' ,
'description' => sprintf ( __ ( 'Defaults to <code>%s</code>' , 'woocommerce' ), $this -> heading ),
'placeholder' => '' ,
'default' => ''
2012-09-17 00:53:17 +00:00
),
'email_type' => array (
2014-08-31 05:49:58 +00:00
'title' => __ ( 'Email type' , 'woocommerce' ),
'type' => 'select' ,
'description' => __ ( 'Choose which format of email to send.' , 'woocommerce' ),
'default' => 'html' ,
2015-01-12 17:45:47 +00:00
'class' => 'email_type wc-enhanced-select' ,
2014-08-31 05:49:58 +00:00
'options' => array (
'plain' => __ ( 'Plain text' , 'woocommerce' ),
'html' => __ ( 'HTML' , 'woocommerce' ),
'multipart' => __ ( 'Multipart' , 'woocommerce' ),
2012-09-17 00:53:17 +00:00
)
)
);
2014-08-31 05:49:58 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* Admin Panel Options Processing
* - Saves the options to the DB
*
* @ since 1.0 . 0
2014-09-07 23:37:55 +00:00
* @ return boolean | null
2012-09-17 00:53:17 +00:00
*/
2014-08-31 05:49:58 +00:00
public function process_admin_options () {
2012-11-27 16:22:47 +00:00
2014-08-31 05:49:58 +00:00
// Save regular options
parent :: process_admin_options ();
2012-11-27 16:22:47 +00:00
2014-08-31 05:49:58 +00:00
// Save templates
2012-09-17 00:53:17 +00:00
if ( ! empty ( $_POST [ 'template_html_code' ] ) && ! empty ( $this -> template_html ) ) {
2012-11-27 16:22:47 +00:00
2014-08-31 05:49:58 +00:00
$saved = false ;
$file = get_stylesheet_directory () . '/woocommerce/' . $this -> template_html ;
$code = stripslashes ( $_POST [ 'template_html_code' ] );
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( is_writeable ( $file ) ) {
2014-08-31 05:49:58 +00:00
2012-09-17 00:53:17 +00:00
$f = fopen ( $file , 'w+' );
2014-08-31 05:49:58 +00:00
2012-09-17 00:53:17 +00:00
if ( $f !== FALSE ) {
2014-08-31 05:49:58 +00:00
fwrite ( $f , $code );
fclose ( $f );
$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 ) {
2014-08-31 05:49:58 +00:00
$redirect = add_query_arg ( 'wc_error' , urlencode ( __ ( 'Could not write to template file.' , 'woocommerce' ) ) );
wp_redirect ( $redirect );
exit ;
}
2012-09-17 00:53:17 +00:00
}
2014-08-31 05:49:58 +00:00
2012-09-17 00:53:17 +00:00
if ( ! empty ( $_POST [ 'template_plain_code' ] ) && ! empty ( $this -> template_plain ) ) {
2012-11-27 16:22:47 +00:00
2014-08-31 05:49:58 +00:00
$saved = false ;
$file = get_stylesheet_directory () . '/woocommerce/' . $this -> template_plain ;
$code = stripslashes ( $_POST [ 'template_plain_code' ] );
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( is_writeable ( $file ) ) {
2014-08-31 05:49:58 +00:00
2012-09-17 00:53:17 +00:00
$f = fopen ( $file , 'w+' );
2014-08-31 05:49:58 +00:00
2012-09-17 00:53:17 +00:00
if ( $f !== FALSE ) {
2014-08-31 05:49:58 +00:00
fwrite ( $f , $code );
fclose ( $f );
$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 ) {
2014-08-31 05:49:58 +00:00
$redirect = add_query_arg ( 'wc_error' , __ ( 'Could not write to template file.' , 'woocommerce' ) );
wp_redirect ( $redirect );
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
2012-09-17 00:53:17 +00:00
/**
2014-12-03 03:30:28 +00:00
* Get template .
2012-09-17 00:53:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ param string $type
2012-09-17 00:53:17 +00:00
*
2014-12-03 03:30:28 +00:00
* @ return string
2012-09-17 00:53:17 +00:00
*/
2014-12-03 03:30:28 +00:00
public function get_template ( $type ) {
$type = esc_attr ( basename ( $type ) );
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
if ( 'template_html' == $type ) {
return $this -> template_html ;
} else if ( 'template_plain' == $type ) {
return $this -> template_plain ;
}
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
return '' ;
}
2014-08-31 05:49:58 +00:00
2014-12-03 03:30:28 +00:00
/**
* Get the template file in the current theme .
*
* @ param string $template
*
* @ return string
*/
public function get_theme_template_file ( $template ) {
return get_stylesheet_directory () . '/' . apply_filters ( 'woocommerce_template_directory' , 'woocommerce' , $template ) . '/' . $template ;
}
/**
* Move template action .
*
* @ param string $template_type
*/
protected function move_template_action ( $template_type ) {
if ( $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 ) ) {
2014-08-31 05:49:58 +00:00
2014-12-03 03:30:28 +00:00
$theme_file = $this -> get_theme_template_file ( $template );
2013-10-29 18:16:12 +00:00
2014-12-03 03:30:28 +00:00
if ( wp_mkdir_p ( dirname ( $theme_file ) ) && ! file_exists ( $theme_file ) ) {
2014-11-25 14:59:29 +00:00
2014-12-03 03:30:28 +00:00
// Locate template file
$core_file = $this -> template_base . $template ;
$template_file = apply_filters ( 'woocommerce_locate_core_template' , $core_file , $template , $this -> template_base );
2014-11-25 14:59:29 +00:00
2014-12-03 03:30:28 +00:00
// Copy template file
copy ( $template_file , $theme_file );
/**
* woocommerce_copy_email_template action hook
*
* @ param string $template_type The copied template type
* @ param string $email The email object
*/
do_action ( 'woocommerce_copy_email_template' , $template_type , $this );
echo '<div class="updated fade"><p>' . __ ( 'Template file copied to theme.' , 'woocommerce' ) . '</p></div>' ;
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 .
*
* @ param string $template_type
*/
protected function delete_template_action ( $template_type ) {
if ( $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 ) ) {
2014-08-31 05:49:58 +00:00
2014-12-03 03:30:28 +00:00
$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 ) ) {
unlink ( $theme_file );
2014-11-25 14:59:29 +00:00
2014-12-03 03:30:28 +00:00
/**
* woocommerce_delete_email_template action hook
*
* @ param string $template The deleted template type
* @ param string $email The email object
*/
do_action ( 'woocommerce_delete_email_template' , $template_type , $this );
echo '<div class="updated fade"><p>' . __ ( 'Template file deleted from theme.' , 'woocommerce' ) . '</p></div>' ;
2012-09-17 00:53:17 +00:00
}
}
2014-12-03 03:30:28 +00:00
}
}
/**
* Admin actions .
*/
protected function admin_actions () {
// Handle any actions
if ( ! empty ( $this -> template_html ) || ! empty ( $this -> template_plain ) ) {
if ( ! empty ( $_GET [ 'move_template' ] ) ) {
$this -> move_template_action ( $_GET [ 'move_template' ] );
}
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
if ( ! empty ( $_GET [ 'delete_template' ] ) ) {
$this -> delete_template_action ( $_GET [ 'delete_template' ] );
}
2012-09-17 00:53:17 +00:00
}
2014-12-03 03:30:28 +00:00
}
/**
* Admin Options
*
* Setup the gateway settings screen .
* Override this in your gateway .
*
* @ since 1.0 . 0
*/
public function admin_options () {
// Do admin acations.
$this -> admin_actions ();
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
?>
< h3 >< ? php echo ( ! empty ( $this -> title ) ) ? $this -> title : __ ( 'Settings' , 'woocommerce' ) ; ?> </h3>
< ? php echo ( ! empty ( $this -> description ) ) ? wpautop ( $this -> description ) : '' ; ?>
2014-11-25 14:59:29 +00:00
< ? php
2014-12-03 03:30:28 +00:00
/**
* woocommerce_email_settings_before action hook
*
* @ 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
2014-12-03 03:30:28 +00:00
/**
* woocommerce_email_settings_after action hook
*
* @ param string $email The email object
*/
do_action ( 'woocommerce_email_settings_after' , $this );
2014-11-25 14:59:29 +00:00
?>
2012-09-17 00:53:17 +00:00
< ? php if ( ! empty ( $this -> template_html ) || ! empty ( $this -> template_plain ) ) { ?>
< div id = " template " >
< ? php
2012-11-27 16:22:47 +00:00
$templates = array (
2014-12-03 03:30:28 +00:00
'template_html' => __ ( 'HTML template' , 'woocommerce' ),
'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 ;
$template_file = apply_filters ( 'woocommerce_locate_core_template' , $core_file , $template , $this -> template_base );
$template_dir = apply_filters ( 'woocommerce_template_directory' , 'woocommerce' , $template );
2012-09-17 00:53:17 +00:00
?>
2014-12-03 03:30:28 +00:00
< div class = " template <?php echo $template_type ; ?> " >
2012-11-27 16:22:47 +00:00
2012-10-17 12:46:35 +00:00
< h4 >< ? php echo wp_kses_post ( $title ); ?> </h4>
2012-11-27 16:22:47 +00:00
2013-11-24 10:24:12 +00:00
< ? php if ( file_exists ( $local_file ) ) { ?>
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
< p >
< a href = " # " class = " button toggle_editor " ></ a >
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
< ? php if ( is_writable ( $local_file ) ) : ?>
2015-01-29 14:33:06 +00:00
< a href = " <?php echo esc_url( remove_query_arg( array( 'move_template', 'saved' ), add_query_arg( 'delete_template', $template_type ) ) ); ?> " class = " delete_template button " >< ? php _e ( 'Delete template file' , 'woocommerce' ); ?> </a>
2012-09-17 00:53:17 +00:00
< ? php endif ; ?>
2014-12-03 03:30:28 +00:00
< ? php printf ( __ ( 'This template has been overridden by your theme and can be found in: <code>%s</code>.' , 'woocommerce' ), 'yourtheme/' . $template_dir . '/' . $template ); ?>
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 " >
2014-12-03 03:30:28 +00:00
< textarea class = " code " cols = " 25 " rows = " 20 " < ? php if ( ! is_writable ( $local_file ) ) : ?> readonly="readonly" disabled="disabled"<?php else : ?>data-name="<?php echo $template_type . '_code'; ?>"<?php endif; ?>><?php echo file_get_contents( $local_file ); ?></textarea>
2012-09-17 00:53:17 +00:00
</ div >
2012-11-27 16:22:47 +00:00
2013-11-24 10:24:12 +00:00
< ? php } elseif ( file_exists ( $template_file ) ) { ?>
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
< p >
< a href = " # " class = " button toggle_editor " ></ a >
2012-11-27 16:22:47 +00:00
2014-12-03 03:30:28 +00:00
< ? php if ( ( is_dir ( get_stylesheet_directory () . '/' . $template_dir . '/emails/' ) && is_writable ( get_stylesheet_directory () . '/' . $template_dir . '/emails/' ) ) || is_writable ( get_stylesheet_directory () ) ) { ?>
2015-01-29 14:33:06 +00:00
< a href = " <?php echo esc_url( remove_query_arg( array( 'delete_template', 'saved' ), add_query_arg( 'move_template', $template_type ) ) ); ?> " class = " button " >< ? php _e ( 'Copy file to theme' , 'woocommerce' ); ?> </a>
2013-11-24 10:24:12 +00:00
< ? php } ?>
2012-09-17 00:53:17 +00:00
2014-12-03 03:30:28 +00:00
< ? php printf ( __ ( 'To override and edit this email template copy <code>%s</code> to your theme folder: <code>%s</code>.' , 'woocommerce' ), plugin_basename ( $template_file ) , 'yourtheme/' . $template_dir . '/' . $template ); ?>
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 " >
2013-10-26 10:32:38 +00:00
< textarea class = " code " readonly = " readonly " disabled = " disabled " cols = " 25 " rows = " 20 " >< ? php echo file_get_contents ( $template_file ); ?> </textarea>
2012-09-17 00:53:17 +00:00
</ div >
2012-11-27 16:22:47 +00:00
2013-11-24 10:24:12 +00:00
< ? php } else { ?>
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
< p >< ? php _e ( 'File was not found.' , 'woocommerce' ); ?> </p>
2012-11-27 16:22:47 +00:00
2013-11-24 10:24:12 +00:00
< ? php } ?>
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
</ div >
< ? php
endforeach ;
?>
</ div >
< ? php
2014-12-03 03:30:28 +00:00
wc_enqueue_js ( "
2014-08-31 05:49:58 +00:00
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
2012-09-17 00:53:17 +00:00
jQuery ( '.template_plain, .template_html' ) . show ();
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( val != 'multipart' && val != 'html' )
jQuery ( '.template_html' ) . hide ();
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( val != 'multipart' && val != 'plain' )
jQuery ( '.template_plain' ) . hide ();
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
2012-09-17 00:53:17 +00:00
jQuery ( 'a.toggle_editor' ) . text ( view ) . toggle ( function () {
jQuery ( this ) . text ( hide ) . closest ( '.template' ) . find ( '.editor' ) . slideToggle ();
return false ;
}, function () {
jQuery ( this ) . text ( view ) . closest ( '.template' ) . find ( '.editor' ) . slideToggle ();
return false ;
} );
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
jQuery ( 'a.delete_template' ) . click ( function (){
2013-05-06 13:17:04 +00:00
var answer = confirm ( '" . esc_js( __( ' Are you sure you want to delete this template file ? ', ' woocommerce ' ) ) . "' );
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( answer )
return true ;
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
2012-09-17 00:53:17 +00:00
jQuery ( '.editor textarea' ) . change ( function (){
var name = jQuery ( this ) . attr ( 'data-name' );
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( name )
jQuery ( this ) . attr ( 'name' , name );
});
2014-12-03 03:30:28 +00:00
" );
2012-09-17 00:53:17 +00:00
}
}
2013-02-21 00:39:38 +00:00
}