PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/emails/class-wc-email.php

https://github.com/CammoKing/woocommerce
PHP | 604 lines | 312 code | 111 blank | 181 comment | 58 complexity | 48e89c95390c6568e9a538648ff6b0a1 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Email Class
  4. *
  5. * WooCommerce Email Class which is extended by specific email template classes to add emails to WooCommerce
  6. *
  7. * @class WC_Email
  8. * @version 1.7.0
  9. * @package WooCommerce/Classes/Emails
  10. * @author WooThemes
  11. * @extends WC_Settings_API
  12. */
  13. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  14. class WC_Email extends WC_Settings_API {
  15. /** @var string Payment method ID. */
  16. var $id;
  17. /** @var string Payment method title. */
  18. var $title;
  19. /** @var bool True if the method is enabled. */
  20. var $enabled;
  21. /** @var string Description for the gateway. */
  22. var $description;
  23. /** @var string plain text template path */
  24. var $template_plain;
  25. /** @var string html template path */
  26. var $template_html;
  27. /** @var string template path */
  28. var $template_base;
  29. /** @var string recipients for the email */
  30. var $recipient;
  31. /** @var string heading for the email content */
  32. var $heading;
  33. /** @var string subject for the email */
  34. var $subject;
  35. /** @var object this email is for, for example a customer, product, or email */
  36. var $object;
  37. /** @var array strings to find in subjects/headings */
  38. var $find;
  39. /** @var array strings to replace in subjects/headings */
  40. var $replace;
  41. /** @var string For multipart emails */
  42. var $mime_boundary;
  43. /** @var string For multipart emails */
  44. var $mime_boundary_header;
  45. /** @var bool true when email is being sent */
  46. var $sending;
  47. /**
  48. * Constructor
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. function __construct() {
  54. global $woocommerce;
  55. // Init settings
  56. $this->init_form_fields();
  57. $this->init_settings();
  58. // Save settings hook
  59. add_action( 'woocommerce_update_options_email_' . $this->id, array( &$this, 'process_admin_options' ) );
  60. // Settings
  61. $this->template_base = $woocommerce->plugin_path() . '/templates/';
  62. $this->heading = empty( $this->settings['heading'] ) ? $this->heading : $this->settings['heading'];
  63. $this->subject = empty( $this->settings['subject'] ) ? $this->subject : $this->settings['subject'];
  64. $this->email_type = $this->settings['email_type'];
  65. $this->enabled = $this->settings['enabled'];
  66. // Find/replace
  67. $this->find = array( '{blogname}' );
  68. $this->replace = array( $this->get_blogname() );
  69. // For multipart messages
  70. add_filter( 'phpmailer_init', array( &$this, 'handle_multipart' ) );
  71. }
  72. /**
  73. * handle_multipart function.
  74. *
  75. * @access public
  76. * @param mixed $mailer
  77. * @return void
  78. */
  79. function handle_multipart( $mailer ) {
  80. if ( $this->sending && $this->get_email_type() == 'multipart' ) {
  81. $mailer->AltBody = wordwrap( html_entity_decode( strip_tags( $this->get_content_plain() ) ), 70 );
  82. $this->sending = false;
  83. }
  84. return $mailer;
  85. }
  86. /**
  87. * format_string function.
  88. *
  89. * @access public
  90. * @param mixed $string
  91. * @return string
  92. */
  93. function format_string( $string ) {
  94. return str_replace( $this->find, $this->replace, $string );
  95. }
  96. /**
  97. * get_subject function.
  98. *
  99. * @access public
  100. * @return string
  101. */
  102. function get_subject() {
  103. return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->subject ), $this->object );
  104. }
  105. /**
  106. * get_heading function.
  107. *
  108. * @access public
  109. * @return string
  110. */
  111. function get_heading() {
  112. return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->heading ), $this->object );
  113. }
  114. /**
  115. * get_recipient function.
  116. *
  117. * @access public
  118. * @return string
  119. */
  120. function get_recipient() {
  121. return apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object );
  122. }
  123. /**
  124. * get_headers function.
  125. *
  126. * @access public
  127. * @return string
  128. */
  129. function get_headers() {
  130. return apply_filters( 'woocommerce_email_headers', "Content-Type: " . $this->get_content_type() . "\r\n", $this->id, $this->object );
  131. }
  132. /**
  133. * get_attachments function.
  134. *
  135. * @access public
  136. * @return string
  137. */
  138. function get_attachments() {
  139. return apply_filters( 'woocommerce_email_attachments', '', $this->id, $this->object );
  140. }
  141. /**
  142. * get_type function.
  143. *
  144. * @access public
  145. * @return string
  146. */
  147. function get_email_type() {
  148. return $this->email_type ? $this->email_type : 'plain';
  149. }
  150. /**
  151. * get_content_type function.
  152. *
  153. * @access public
  154. * @return void
  155. */
  156. function get_content_type() {
  157. switch ( $this->get_email_type() ) {
  158. case "html" :
  159. return 'text/html';
  160. case "multipart" :
  161. return 'multipart/alternative';
  162. default :
  163. return 'text/plain';
  164. }
  165. }
  166. /**
  167. * Checks if this email is enabled and will be sent.
  168. *
  169. * @access public
  170. * @return bool
  171. */
  172. function is_enabled() {
  173. if ( $this->enabled == "yes" )
  174. return true;
  175. }
  176. /**
  177. * get_blogname function.
  178. *
  179. * @access public
  180. * @return void
  181. */
  182. function get_blogname() {
  183. return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  184. }
  185. /**
  186. * get_content function.
  187. *
  188. * @access public
  189. * @return string
  190. */
  191. function get_content() {
  192. $this->sending = true;
  193. if ( $this->get_email_type() == 'plain' ) {
  194. $email_content = html_entity_decode( strip_tags( $this->get_content_plain() ) );
  195. } else {
  196. $email_content = $this->style_inline( $this->get_content_html() );
  197. }
  198. return $email_content;
  199. }
  200. /**
  201. * Apply inline styles to dynamic content.
  202. *
  203. * @access public
  204. * @param mixed $content
  205. * @return void
  206. */
  207. function style_inline( $content ) {
  208. if ( ! class_exists( 'DOMDocument' ) )
  209. return $content;
  210. $dom = new DOMDocument();
  211. @$dom->loadHTML( $content );
  212. $nodes = $dom->getElementsByTagName('img');
  213. foreach( $nodes as $node )
  214. if ( ! $node->hasAttribute( 'style' ) )
  215. $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;" );
  216. $nodes_h1 = $dom->getElementsByTagName('h1');
  217. $nodes_h2 = $dom->getElementsByTagName('h2');
  218. $nodes_h3 = $dom->getElementsByTagName('h3');
  219. foreach( $nodes_h1 as $node )
  220. if ( ! $node->hasAttribute( 'style' ) )
  221. $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%;" );
  222. foreach( $nodes_h2 as $node )
  223. if ( ! $node->hasAttribute( 'style' ) )
  224. $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%;" );
  225. foreach( $nodes_h3 as $node )
  226. if ( ! $node->hasAttribute( 'style' ) )
  227. $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%;" );
  228. $nodes = $dom->getElementsByTagName('a');
  229. foreach( $nodes as $node )
  230. if ( ! $node->hasAttribute( 'style' ) )
  231. $node->setAttribute( "style", "color: " . get_option( 'woocommerce_email_text_color' ) . "; font-weight:normal; text-decoration:underline;" );
  232. $content = $dom->saveHTML();
  233. return $content;
  234. }
  235. /**
  236. * get_content_plain function.
  237. *
  238. * @access public
  239. * @return void
  240. */
  241. function get_content_plain() {}
  242. /**
  243. * get_content_html function.
  244. *
  245. * @access public
  246. * @return void
  247. */
  248. function get_content_html() {}
  249. /**
  250. * Get from name for email.
  251. *
  252. * @access public
  253. * @return string
  254. */
  255. function get_from_name() {
  256. return esc_html( get_option( 'woocommerce_email_from_name' ) );
  257. }
  258. /**
  259. * Get from email address.
  260. *
  261. * @access public
  262. * @return string
  263. */
  264. function get_from_address() {
  265. return sanitize_email( get_option( 'woocommerce_email_from_address' ) );
  266. }
  267. /**
  268. * Send the email.
  269. *
  270. * @access public
  271. * @param mixed $to
  272. * @param mixed $subject
  273. * @param mixed $message
  274. * @param string $headers
  275. * @param string $attachments
  276. * @return void
  277. */
  278. function send( $to, $subject, $message, $headers, $attachments ) {
  279. add_filter( 'wp_mail_from', array( &$this, 'get_from_address' ) );
  280. add_filter( 'wp_mail_from_name', array( &$this, 'get_from_name' ) );
  281. add_filter( 'wp_mail_content_type', array( &$this, 'get_content_type' ) );
  282. wp_mail( $to, $subject, $message, $headers, $attachments );
  283. remove_filter( 'wp_mail_from', array( &$this, 'get_from_address' ) );
  284. remove_filter( 'wp_mail_from_name', array( &$this, 'get_from_name' ) );
  285. remove_filter( 'wp_mail_content_type', array( &$this, 'get_content_type' ) );
  286. }
  287. /**
  288. * Initialise Settings Form Fields - these are generic email options most will use.
  289. *
  290. * @access public
  291. * @return void
  292. */
  293. function init_form_fields() {
  294. $this->form_fields = array(
  295. 'enabled' => array(
  296. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  297. 'type' => 'checkbox',
  298. 'label' => __( 'Enable this email notification', 'woocommerce' ),
  299. 'default' => 'yes'
  300. ),
  301. 'subject' => array(
  302. 'title' => __( 'Email subject', 'woocommerce' ),
  303. 'type' => 'text',
  304. 'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->subject ),
  305. 'placeholder' => '',
  306. 'default' => ''
  307. ),
  308. 'heading' => array(
  309. 'title' => __( 'Email heading', 'woocommerce' ),
  310. 'type' => 'text',
  311. 'description' => sprintf( __( 'Defaults to <code>%s</code>', 'woocommerce' ), $this->heading ),
  312. 'placeholder' => '',
  313. 'default' => ''
  314. ),
  315. 'email_type' => array(
  316. 'title' => __( 'Email type', 'woocommerce' ),
  317. 'type' => 'select',
  318. 'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
  319. 'default' => 'html',
  320. 'class' => 'email_type',
  321. 'options' => array(
  322. 'plain' => __( 'Plain text', 'woocommerce' ),
  323. 'html' => __( 'HTML', 'woocommerce' ),
  324. 'multipart' => __( 'Multipart', 'woocommerce' ),
  325. )
  326. )
  327. );
  328. }
  329. /**
  330. * Admin Panel Options Processing
  331. * - Saves the options to the DB
  332. *
  333. * @since 1.0.0
  334. * @access public
  335. * @return bool
  336. */
  337. public function process_admin_options() {
  338. // Save regular options
  339. parent::process_admin_options();
  340. // Save templates
  341. if ( ! empty( $_POST['template_html_code'] ) && ! empty( $this->template_html ) ) {
  342. $saved = false;
  343. $file = get_stylesheet_directory() . '/woocommerce/' . $this->template_html;
  344. $code = stripslashes( $_POST['template_html_code'] );
  345. if ( is_writeable( $file ) ) {
  346. $f = fopen( $file, 'w+' );
  347. if ( $f !== FALSE ) {
  348. fwrite( $f, $code );
  349. fclose( $f );
  350. $saved = true;
  351. }
  352. }
  353. if ( ! $saved ) {
  354. $redirect = add_query_arg( 'wc_error', urlencode( __( 'Could not write to template file.', 'woocommerce' ) ) );
  355. wp_redirect( $redirect );
  356. exit;
  357. }
  358. }
  359. if ( ! empty( $_POST['template_plain_code'] ) && ! empty( $this->template_plain ) ) {
  360. $saved = false;
  361. $file = get_stylesheet_directory() . '/woocommerce/' . $this->template_plain;
  362. $code = stripslashes( $_POST['template_plain_code'] );
  363. if ( is_writeable( $file ) ) {
  364. $f = fopen( $file, 'w+' );
  365. if ( $f !== FALSE ) {
  366. fwrite( $f, $code );
  367. fclose( $f );
  368. $saved = true;
  369. }
  370. }
  371. if ( ! $saved ) {
  372. $redirect = add_query_arg( 'wc_error', __( 'Could not write to template file.', 'woocommerce' ) );
  373. wp_redirect( $redirect );
  374. exit;
  375. }
  376. }
  377. }
  378. /**
  379. * Admin Options
  380. *
  381. * Setup the gateway settings screen.
  382. * Override this in your gateway.
  383. *
  384. * @since 1.0.0
  385. * @access public
  386. * @return void
  387. */
  388. function admin_options() {
  389. global $woocommerce;
  390. // Handle any actions
  391. if ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) ) {
  392. if ( ! empty( $_GET['move_template'] ) && ( $template = esc_attr( basename( $_GET['move_template'] ) ) ) ) {
  393. if ( ! empty( $this->$template ) ) {
  394. if ( wp_mkdir_p( dirname( get_stylesheet_directory() . '/woocommerce/' . $this->$template ) ) && ! file_exists( get_stylesheet_directory() . '/woocommerce/' . $this->$template ) ) {
  395. copy( $this->template_base . $this->$template, get_stylesheet_directory() . '/woocommerce/' . $this->$template );
  396. echo '<div class="updated fade"><p>' . __( 'Template file copied to theme.', 'woocommerce' ) . '</p></div>';
  397. }
  398. }
  399. }
  400. if ( ! empty( $_GET['delete_template'] ) && ( $template = esc_attr( basename( $_GET['delete_template'] ) ) ) ) {
  401. if ( ! empty( $this->$template ) ) {
  402. if ( file_exists( get_stylesheet_directory() . '/woocommerce/' . $this->$template ) ) {
  403. unlink( get_stylesheet_directory() . '/woocommerce/' . $this->$template );
  404. echo '<div class="updated fade"><p>' . __( 'Template file deleted from theme.', 'woocommerce' ) . '</p></div>';
  405. }
  406. }
  407. }
  408. }
  409. ?>
  410. <h3><?php echo ( ! empty( $this->title ) ) ? $this->title : __( 'Settings','woocommerce' ) ; ?></h3>
  411. <?php echo ( ! empty( $this->description ) ) ? wpautop( $this->description ) : ''; ?>
  412. <table class="form-table">
  413. <?php $this->generate_settings_html(); ?>
  414. </table>
  415. <?php if ( ! empty( $this->template_html ) || ! empty( $this->template_plain ) ) { ?>
  416. <div id="template">
  417. <?php
  418. $templates = array(
  419. 'template_html' => __( 'HTML template', 'woocommerce' ),
  420. 'template_plain' => __( 'Plain text template', 'woocommerce' )
  421. );
  422. foreach ( $templates as $template => $title ) :
  423. if ( empty( $this->$template ) )
  424. continue;
  425. $local_file = get_stylesheet_directory() . '/woocommerce/' . $this->$template;
  426. $core_file = $this->template_base . $this->$template;
  427. ?>
  428. <div class="template <?php echo $template; ?>">
  429. <h4><?php echo wp_kses_post( $title ); ?></h4>
  430. <?php if ( file_exists( $local_file ) ) : ?>
  431. <p>
  432. <a href="#" class="button toggle_editor"></a>
  433. <?php if ( is_writable( $local_file ) ) : ?>
  434. <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>
  435. <?php endif; ?>
  436. <?php printf( __( 'This template has been overridden by your theme and can be found in: <code>%s</code>.', 'woocommerce' ), 'yourtheme/woocommerce/' . $this->$template ); ?>
  437. </p>
  438. <div class="editor" style="display:none">
  439. <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>
  440. </div>
  441. <?php elseif ( file_exists( $core_file ) ) : ?>
  442. <p>
  443. <a href="#" class="button toggle_editor"></a>
  444. <?php if ( ( is_dir( get_stylesheet_directory() . '/woocommerce/emails/' ) && is_writable( get_stylesheet_directory() . '/woocommerce/emails/' ) ) || is_writable( get_stylesheet_directory() ) ) : ?>
  445. <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>
  446. <?php endif; ?>
  447. <?php printf( __( 'To override and edit this email template copy <code>%s</code> to your theme folder: <code>%s</code>.', 'woocommerce' ), 'woocommerce/templates/' . $this->$template, 'yourtheme/woocommerce/' . $this->$template ); ?>
  448. </p>
  449. <div class="editor" style="display:none">
  450. <textarea class="code" readonly="readonly" disabled="disabled" cols="25" rows="20"><?php echo file_get_contents( $core_file ); ?></textarea>
  451. </div>
  452. <?php else : ?>
  453. <p><?php _e( 'File was not found.', 'woocommerce' ); ?></p>
  454. <?php endif; ?>
  455. </div>
  456. <?php
  457. endforeach;
  458. ?>
  459. </div>
  460. <?php
  461. $woocommerce->add_inline_js("
  462. jQuery('select.email_type').change(function(){
  463. var val = jQuery( this ).val();
  464. jQuery('.template_plain, .template_html').show();
  465. if ( val != 'multipart' && val != 'html' )
  466. jQuery('.template_html').hide();
  467. if ( val != 'multipart' && val != 'plain' )
  468. jQuery('.template_plain').hide();
  469. }).change();
  470. var view = '" . __( 'View template', 'woocommerce' ) . "';
  471. var hide = '" . __( 'Hide template', 'woocommerce' ) . "';
  472. jQuery('a.toggle_editor').text( view ).toggle( function() {
  473. jQuery( this ).text( hide ).closest('.template').find('.editor').slideToggle();
  474. return false;
  475. }, function() {
  476. jQuery( this ).text( view ).closest('.template').find('.editor').slideToggle();
  477. return false;
  478. } );
  479. jQuery('a.delete_template').click(function(){
  480. var answer = confirm('" . __( 'Are you sure you want to delete this template file?', 'woocommerce' ) . "');
  481. if (answer)
  482. return true;
  483. return false;
  484. });
  485. jQuery('.editor textarea').change(function(){
  486. var name = jQuery(this).attr( 'data-name' );
  487. if ( name )
  488. jQuery(this).attr( 'name', name );
  489. });
  490. ");
  491. }
  492. }
  493. }