woocommerce/classes/abstracts/abstract-wc-email.php

665 lines
19 KiB
PHP
Raw Normal View History

<?php
2013-02-20 17:14:46 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
2013-02-20 17:14:46 +00:00
* Abstract Email Class
*
* WooCommerce Email Class which is extended by specific email template classes to add emails to WooCommerce
*
* @class WC_Email
2012-12-03 19:19:58 +00:00
* @version 2.0.0
2013-02-20 17:14:46 +00:00
* @package WooCommerce/Abstracts
* @author WooThemes
2013-02-20 17:14:46 +00:00
* @category Abstract Class
* @extends WC_Settings_API
*/
abstract class WC_Email extends WC_Settings_API {
/** @var string Payment method ID. */
var $id;
/** @var string Payment method title. */
var $title;
/** @var bool True if the method is enabled. */
var $enabled;
/** @var string Description for the gateway. */
var $description;
2012-11-27 16:22:47 +00:00
/** @var string plain text template path */
var $template_plain;
2012-11-27 16:22:47 +00:00
/** @var string html template path */
var $template_html;
2012-11-27 16:22:47 +00:00
/** @var string template path */
var $template_base;
2012-11-27 16:22:47 +00:00
/** @var string recipients for the email */
var $recipient;
2012-11-27 16:22:47 +00:00
/** @var string heading for the email content */
var $heading;
2012-11-27 16:22:47 +00:00
/** @var string subject for the email */
var $subject;
2012-11-27 16:22:47 +00:00
/** @var object this email is for, for example a customer, product, or email */
var $object;
2012-11-27 16:22:47 +00:00
/** @var array strings to find in subjects/headings */
var $find;
2012-11-27 16:22:47 +00:00
/** @var array strings to replace in subjects/headings */
var $replace;
2012-11-27 16:22:47 +00:00
/** @var string For multipart emails */
var $mime_boundary;
2012-11-27 16:22:47 +00:00
/** @var string For multipart emails */
var $mime_boundary_header;
2012-11-27 16:22:47 +00:00
/** @var bool true when email is being sent */
var $sending;
2012-12-13 09:52:20 +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
* @access public
* @see $replace
*/
var $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
'/&gt;/i', // Greater-than
'/&lt;/i', // Less-than
'/&(amp|#38);/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
'/&[^&;]+;/i', // Unknown/unhandled entities
'/[ ]{2,}/' // Runs of spaces, post-handling
);
/**
* List of pattern replacements corresponding to patterns searched.
*
* @var array $replace
* @access public
* @see $search
*/
var $plain_replace = array(
'', // Non-legal carriage return
' ', // Non-breaking space
'"', // Double quotes
"'", // Single quotes
'>',
'<',
'&',
'(c)',
'(tm)',
'(R)',
'--',
'-',
'*',
'£',
'EUR', // Euro sign. € ?
'', // Unknown/unhandled entities
' ' // Runs of spaces, post-handling
);
/**
* Constructor
*
* @access public
* @return void
*/
function __construct() {
global $woocommerce;
// Init settings
$this->init_form_fields();
$this->init_settings();
2012-11-27 16:22:47 +00:00
// Save settings hook
add_action( 'woocommerce_update_options_email_' . $this->id, array( $this, 'process_admin_options' ) );
// Settings
$this->template_base = $woocommerce->plugin_path() . '/templates/';
$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
// Find/replace
$this->find = array( '{blogname}' );
$this->replace = array( $this->get_blogname() );
2012-11-27 16:22:47 +00:00
// For multipart messages
add_filter( 'phpmailer_init', array( $this, 'handle_multipart' ) );
}
2012-11-27 16:22:47 +00:00
/**
* handle_multipart function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @param mixed $mailer
* @return void
*/
function handle_multipart( $mailer ) {
2012-11-27 16:22:47 +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 );
$this->sending = false;
}
2012-11-27 16:22:47 +00:00
return $mailer;
}
/**
* format_string function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @param mixed $string
* @return string
*/
2012-11-27 16:22:47 +00:00
function format_string( $string ) {
return str_replace( $this->find, $this->replace, $string );
}
/**
* get_subject function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return string
*/
function get_subject() {
return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->subject ), $this->object );
}
2012-11-27 16:22:47 +00:00
/**
* get_heading function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return string
*/
function get_heading() {
return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->heading ), $this->object );
}
2012-11-27 16:22:47 +00:00
/**
* get_recipient function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return string
*/
function get_recipient() {
return apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object );
}
2012-11-27 16:22:47 +00:00
/**
* get_headers function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return string
*/
function get_headers() {
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
/**
* get_attachments function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return string
*/
function get_attachments() {
return apply_filters( 'woocommerce_email_attachments', '', $this->id, $this->object );
}
2012-11-27 16:22:47 +00:00
/**
* get_type function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return string
*/
function get_email_type() {
return $this->email_type ? $this->email_type : 'plain';
}
2012-11-27 16:22:47 +00:00
/**
* get_content_type function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return void
*/
function get_content_type() {
switch ( $this->get_email_type() ) {
case "html" :
return 'text/html';
case "multipart" :
return 'multipart/alternative';
default :
return 'text/plain';
}
}
2012-11-27 16:22:47 +00:00
/**
* Checks if this email is enabled and will be sent.
*
* @access public
* @return bool
*/
function is_enabled() {
if ( $this->enabled == "yes" )
return true;
}
2012-11-27 16:22:47 +00:00
/**
* get_blogname function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return void
*/
function get_blogname() {
return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
}
/**
* get_content function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return string
*/
function get_content() {
2012-11-27 16:22:47 +00:00
$this->sending = true;
2012-11-27 16:22:47 +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() ) );
} else {
$email_content = $this->style_inline( $this->get_content_html() );
}
return $email_content;
}
2012-11-27 16:22:47 +00:00
/**
* Apply inline styles to dynamic content.
2012-11-27 16:22:47 +00:00
*
* @access public
* @param mixed $content
* @return void
*/
function style_inline( $content ) {
2012-11-27 16:22:47 +00:00
if ( ! class_exists( 'DOMDocument' ) )
return $content;
2012-11-27 16:22:47 +00:00
$dom = new DOMDocument();
@$dom->loadHTML( $content );
2012-11-27 16:22:47 +00:00
$nodes = $dom->getElementsByTagName('img');
2012-11-27 16:22:47 +00:00
foreach( $nodes as $node )
if ( ! $node->hasAttribute( 'style' ) )
$node->setAttribute( "style", "display:inline; border:none; font-size:14px; font-weight:bold; height:auto; line-height:100%; outline:none; text-decoration:none; text-transform:capitalize;" );
2012-11-27 16:22:47 +00:00
$nodes_h1 = $dom->getElementsByTagName('h1');
$nodes_h2 = $dom->getElementsByTagName('h2');
$nodes_h3 = $dom->getElementsByTagName('h3');
2012-11-27 16:22:47 +00:00
foreach( $nodes_h1 as $node )
if ( ! $node->hasAttribute( 'style' ) )
$node->setAttribute( "style", "color: " . get_option( 'woocommerce_email_text_color' ) . "; display:block; font-family:Arial; font-size:34px; font-weight:bold; margin-top: 10px; margin-right:0; margin-bottom:10px; margin-left:0; text-align:left; line-height: 150%;" );
2012-11-27 16:22:47 +00:00
foreach( $nodes_h2 as $node )
if ( ! $node->hasAttribute( 'style' ) )
$node->setAttribute( "style", "color: " . get_option( 'woocommerce_email_text_color' ) . "; display:block; font-family:Arial; font-size:30px; font-weight:bold; margin-top: 10px; margin-right:0; margin-bottom:10px; margin-left:0; text-align:left; line-height: 150%;" );
2012-11-27 16:22:47 +00:00
foreach( $nodes_h3 as $node )
if ( ! $node->hasAttribute( 'style' ) )
$node->setAttribute( "style", "color: " . get_option( 'woocommerce_email_text_color' ) . "; display:block; font-family:Arial; font-size:26px; font-weight:bold; margin-top: 10px; margin-right:0; margin-bottom:10px; margin-left:0; text-align:left; line-height: 150%;" );
2012-11-27 16:22:47 +00:00
$nodes = $dom->getElementsByTagName('a');
2012-11-27 16:22:47 +00:00
foreach( $nodes as $node )
if ( ! $node->hasAttribute( 'style' ) )
$node->setAttribute( "style", "color: " . get_option( 'woocommerce_email_text_color' ) . "; font-weight:normal; text-decoration:underline;" );
2012-11-27 16:22:47 +00:00
$content = $dom->saveHTML();
2012-11-27 16:22:47 +00:00
return $content;
}
2012-11-27 16:22:47 +00:00
/**
* get_content_plain function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return void
*/
function get_content_plain() {}
2012-11-27 16:22:47 +00:00
/**
* get_content_html function.
2012-11-27 16:22:47 +00:00
*
* @access public
* @return void
*/
function get_content_html() {}
/**
* Get from name for email.
*
* @access public
* @return string
*/
function get_from_name() {
2012-10-18 10:33:47 +00:00
return esc_html( get_option( 'woocommerce_email_from_name' ) );
}
/**
* Get from email address.
*
* @access public
* @return string
*/
function get_from_address() {
2012-10-18 10:33:47 +00:00
return sanitize_email( get_option( 'woocommerce_email_from_address' ) );
}
2012-11-27 16:22:47 +00:00
/**
* Send the email.
*
* @access public
* @param mixed $to
* @param mixed $subject
* @param mixed $message
* @param string $headers
* @param string $attachments
* @return void
*/
function send( $to, $subject, $message, $headers, $attachments ) {
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
wp_mail( $to, $subject, $message, $headers, $attachments );
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' ) );
}
2012-11-27 16:22:47 +00:00
/**
* Initialise Settings Form Fields - these are generic email options most will use.
*
* @access public
* @return void
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable this email notification', 'woocommerce' ),
'default' => 'yes'
),
'subject' => array(
'title' => __( 'Email subject', 'woocommerce' ),
'type' => 'text',
'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->subject ),
'placeholder' => '',
'default' => ''
),
'heading' => array(
'title' => __( 'Email heading', 'woocommerce' ),
'type' => 'text',
'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->heading ),
'placeholder' => '',
'default' => ''
),
'email_type' => array(
'title' => __( 'Email type', 'woocommerce' ),
'type' => 'select',
'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
'default' => 'html',
'class' => 'email_type',
'options' => array(
'plain' => __( 'Plain text', 'woocommerce' ),
'html' => __( 'HTML', 'woocommerce' ),
'multipart' => __( 'Multipart', 'woocommerce' ),
)
)
);
}
2012-11-27 16:22:47 +00:00
/**
* Admin Panel Options Processing
* - Saves the options to the DB
*
* @since 1.0.0
* @access public
* @return bool
*/
public function process_admin_options() {
2012-11-27 16:22:47 +00:00
// Save regular options
parent::process_admin_options();
2012-11-27 16:22:47 +00:00
// Save templates
if ( ! empty( $_POST['template_html_code'] ) && ! empty( $this->template_html ) ) {
2012-11-27 16:22:47 +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
if ( is_writeable( $file ) ) {
$f = fopen( $file, 'w+' );
if ( $f !== FALSE ) {
fwrite( $f, $code );
fclose( $f );
$saved = true;
}
}
2012-11-27 16:22:47 +00:00
if ( ! $saved ) {
$redirect = add_query_arg( 'wc_error', urlencode( __( 'Could not write to template file.', 'woocommerce' ) ) );
wp_redirect( $redirect );
exit;
2012-11-27 16:22:47 +00:00
}
}
if ( ! empty( $_POST['template_plain_code'] ) && ! empty( $this->template_plain ) ) {
2012-11-27 16:22:47 +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
if ( is_writeable( $file ) ) {
$f = fopen( $file, 'w+' );
if ( $f !== FALSE ) {
fwrite( $f, $code );
fclose( $f );
$saved = true;
}
}
2012-11-27 16:22:47 +00:00
if ( ! $saved ) {
$redirect = add_query_arg( 'wc_error', __( 'Could not write to template file.', 'woocommerce' ) );
wp_redirect( $redirect );
exit;
2012-11-27 16:22:47 +00:00
}
}
}
2012-11-27 16:22:47 +00:00
/**
* Admin Options
*
* Setup the gateway settings screen.
* Override this in your gateway.
*
* @since 1.0.0
* @access public
* @return void
*/
2012-11-27 16:22:47 +00:00
function admin_options() {
global $woocommerce;
2012-11-27 16:22:47 +00:00
// Handle any actions
if ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) ) {
2012-11-27 16:22:47 +00:00
if ( ! empty( $_GET['move_template'] ) && ( $template = esc_attr( basename( $_GET['move_template'] ) ) ) ) {
if ( ! empty( $this->$template ) ) {
if ( wp_mkdir_p( dirname( get_stylesheet_directory() . '/woocommerce/' . $this->$template ) ) && ! file_exists( get_stylesheet_directory() . '/woocommerce/' . $this->$template ) ) {
copy( $this->template_base . $this->$template, get_stylesheet_directory() . '/woocommerce/' . $this->$template );
echo '<div class="updated fade"><p>' . __( 'Template file copied to theme.', 'woocommerce' ) . '</p></div>';
}
}
}
2012-11-27 16:22:47 +00:00
if ( ! empty( $_GET['delete_template'] ) && ( $template = esc_attr( basename( $_GET['delete_template'] ) ) ) ) {
if ( ! empty( $this->$template ) ) {
if ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $this->$template ) ) {
unlink( get_stylesheet_directory() . '/woocommerce/' . $this->$template );
echo '<div class="updated fade"><p>' . __( 'Template file deleted from theme.', 'woocommerce' ) . '</p></div>';
}
}
}
2012-11-27 16:22:47 +00:00
}
2012-11-27 16:22:47 +00:00
?>
<h3><?php echo ( ! empty( $this->title ) ) ? $this->title : __( 'Settings','woocommerce' ) ; ?></h3>
<?php echo ( ! empty( $this->description ) ) ? wpautop( $this->description ) : ''; ?>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table>
2012-11-27 16:22:47 +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(
'template_html' => __( 'HTML template', 'woocommerce' ),
'template_plain' => __( 'Plain text template', 'woocommerce' )
);
foreach ( $templates as $template => $title ) :
if ( empty( $this->$template ) )
continue;
2012-11-27 16:22:47 +00:00
$local_file = get_stylesheet_directory() . '/woocommerce/' . $this->$template;
$core_file = $this->template_base . $this->$template;
?>
<div class="template <?php echo $template; ?>">
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
<?php if ( file_exists( $local_file ) ) : ?>
2012-11-27 16:22:47 +00:00
<p>
<a href="#" class="button toggle_editor"></a>
2012-11-27 16:22:47 +00:00
<?php if ( is_writable( $local_file ) ) : ?>
<a href="<?php echo remove_query_arg( array( 'move_template', 'saved' ), add_query_arg( 'delete_template', $template ) ); ?>" class="delete_template button"><?php _e( 'Delete template file', 'woocommerce' ); ?></a>
<?php endif; ?>
<?php printf( __( 'This template has been overridden by your theme and can be found in: <code>%s</code>.', 'woocommerce' ), 'yourtheme/woocommerce/' . $this->$template ); ?>
</p>
2012-11-27 16:22:47 +00:00
<div class="editor" style="display:none">
2012-11-27 16:22:47 +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 . '_code'; ?>"<?php endif; ?>><?php echo file_get_contents( $local_file ); ?></textarea>
2012-11-27 16:22:47 +00:00
</div>
2012-11-27 16:22:47 +00:00
<?php elseif ( file_exists( $core_file ) ) : ?>
2012-11-27 16:22:47 +00:00
<p>
<a href="#" class="button toggle_editor"></a>
2012-11-27 16:22:47 +00:00
<?php if ( ( is_dir( get_stylesheet_directory() . '/woocommerce/emails/' ) && is_writable( get_stylesheet_directory() . '/woocommerce/emails/' ) ) || is_writable( get_stylesheet_directory() ) ) : ?>
<a href="<?php echo remove_query_arg( array( 'delete_template', 'saved' ), add_query_arg( 'move_template', $template ) ); ?>" class="button"><?php _e( 'Copy file to theme', 'woocommerce' ); ?></a>
<?php endif; ?>
2013-02-21 00:59:21 +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( $core_file ) , 'yourtheme/woocommerce/' . $this->$template ); ?>
</p>
2012-11-27 16:22:47 +00:00
<div class="editor" style="display:none">
2012-11-27 16:22:47 +00:00
<textarea class="code" readonly="readonly" disabled="disabled" cols="25" rows="20"><?php echo file_get_contents( $core_file ); ?></textarea>
2012-11-27 16:22:47 +00:00
</div>
2012-11-27 16:22:47 +00:00
<?php else : ?>
2012-11-27 16:22:47 +00:00
<p><?php _e( 'File was not found.', 'woocommerce' ); ?></p>
2012-11-27 16:22:47 +00:00
<?php endif; ?>
2012-11-27 16:22:47 +00:00
</div>
<?php
endforeach;
?>
</div>
<?php
$woocommerce->add_inline_js("
jQuery('select.email_type').change(function(){
2012-11-27 16:22:47 +00:00
var val = jQuery( this ).val();
2012-11-27 16:22:47 +00:00
jQuery('.template_plain, .template_html').show();
2012-11-27 16:22:47 +00:00
if ( val != 'multipart' && val != 'html' )
jQuery('.template_html').hide();
2012-11-27 16:22:47 +00:00
if ( val != 'multipart' && val != 'plain' )
jQuery('.template_plain').hide();
2012-11-27 16:22:47 +00:00
}).change();
2012-11-27 16:22:47 +00:00
var view = '" . __( 'View template', 'woocommerce' ) . "';
var hide = '" . __( 'Hide template', 'woocommerce' ) . "';
2012-11-27 16:22:47 +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
jQuery('a.delete_template').click(function(){
2012-10-16 09:45:33 +00:00
var answer = confirm('" . __( 'Are you sure you want to delete this template file?', 'woocommerce' ) . "');
2012-11-27 16:22:47 +00:00
if (answer)
return true;
2012-11-27 16:22:47 +00:00
return false;
});
2012-11-27 16:22:47 +00:00
jQuery('.editor textarea').change(function(){
var name = jQuery(this).attr( 'data-name' );
2012-11-27 16:22:47 +00:00
if ( name )
jQuery(this).attr( 'name', name );
});
");
}
}
}