Advertisement
Guest User

Untitled

a guest
Nov 14th, 2015
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.27 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: WooCommerce Email Customizer
  4. * Description: WooCommerce Email Customizer plugin allows you to fully customize the styling, colors, logo and text in the emails sent from your WooCommerce store.
  5. * Author: cxThemes
  6. * Author URI: http://codecanyon.net/user/cxThemes
  7. * Plugin URI: http://codecanyon.net/item/email-customizer-for-woocommerce/8654473
  8. * Version: 2.31
  9. * Text Domain: email-control
  10. * Domain Path: /languages/
  11. *
  12. * License: GNU General Public License v3.0
  13. * License URI: http://www.gnu.org/licenses/gpl-3.0.html
  14. *
  15. * @author cxThemes
  16. * @category WooCommerce, WordPress
  17. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
  18. */
  19.  
  20. if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  21.  
  22. /**
  23. * Define Constants
  24. */
  25. define( 'WC_EMAIL_CONTROL_VERSION', '2.31' );
  26. define( 'WC_EMAIL_CONTROL_REQUIRED_WOOCOMMERCE_VERSION', 2.2 );
  27. define( 'WC_EMAIL_CONTROL_DIR' , plugin_dir_path( __FILE__ ) );
  28. define( 'WC_EMAIL_CONTROL_URI' , plugin_dir_url( __FILE__ ) );
  29.  
  30. /**
  31. * Update Check
  32. */
  33. require 'plugin-updates/plugin-update-checker.php';
  34. $wc_email_control_update = new PluginUpdateChecker(
  35. 'http://cxthemes.com/plugins/woocommerce-email-control/email-control.json',
  36. __FILE__,
  37. 'email-control'
  38. );
  39.  
  40. /**
  41. * Check if WooCommerce is active, and is required WooCommerce version.
  42. */
  43. if ( ! WC_Email_Control::is_woocommerce_active() || version_compare( get_option( 'woocommerce_version' ), WC_EMAIL_CONTROL_REQUIRED_WOOCOMMERCE_VERSION, '<' ) ){
  44. add_action( 'admin_notices', array( 'WC_Email_Control', 'woocommerce_inactive_notice' ) );
  45. return;
  46. }
  47.  
  48. /**
  49. * Includes
  50. */
  51. include_once( 'includes/ec-woo-back-compat-functions.php' );
  52. include_once( 'includes/ec-functions.php' );
  53. include_once( 'includes/class-ec-settings.php' );
  54. include_once( 'ec-template-deluxe.php' ); // Template
  55. include_once( 'ec-template-supreme.php' ); // Template
  56.  
  57. /**
  58. * Instantiate plugin.
  59. */
  60. $wc_email_control = WC_Email_Control::get_instance();
  61.  
  62. /**
  63. * Main Class.
  64. */
  65. class WC_Email_Control {
  66.  
  67. private $id = 'woocommerce_email_control';
  68.  
  69. private static $instance;
  70.  
  71. /**
  72. * Get Instance creates a singleton class that's cached to stop duplicate instances
  73. */
  74. public static function get_instance() {
  75. if ( !self::$instance ) {
  76. self::$instance = new self();
  77. self::$instance->init();
  78. }
  79. return self::$instance;
  80. }
  81.  
  82. /**
  83. * Construct empty on purpose
  84. */
  85. private function __construct() {}
  86.  
  87. /**
  88. * Init behaves like, and replaces, construct
  89. */
  90. public function init() {
  91.  
  92. // Translations
  93. add_action( 'init', array( $this, 'load_translation' ) );
  94.  
  95. // Enqueue Scripts/Styles - in head of admin page
  96. add_action( 'admin_enqueue_scripts', array( $this, 'ec_head_scripts' ) );
  97.  
  98. // Enqueue Scripts/Styles - in head of email template page
  99. add_action( 'ec_render_template_head_scripts', array( $this, 'ec_head_scripts' ), 102 );
  100.  
  101. // Add menu item
  102. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  103.  
  104. // Ajax saving of options
  105. add_action( 'wp_ajax_save_meta', array( $this, 'save_meta' ) );
  106. add_action( 'wp_ajax_nopriv_save_meta', array( $this, 'nopriv_save_meta' ) );
  107.  
  108. // Ajax saving of options new
  109. add_action( 'wp_ajax_save_option', array( $this, 'save_option' ) );
  110. add_action( 'wp_ajax_nopriv_save_option', array( $this, 'nopriv_save_option' ) );
  111.  
  112. // Ajax send email
  113. add_action( 'wp_ajax_ec_send_email', array( $this, 'send_email' ) );
  114. add_action( 'wp_ajax_nopriv_ec_send_email', array( $this, 'nopriv_send_email' ) );
  115.  
  116. // Ajax saving of all edit settings
  117. add_action( 'wp_ajax_save_edit_email', array( $this, 'save_edit_email' ) );
  118. add_action( 'wp_ajax_nopriv_save_edit_email', array( $this, 'nopriv_save_edit_email' ) );
  119.  
  120. // WooCommerce order page meta boxe
  121. add_action( 'add_meta_boxes', array( $this, 'order_page_meta_box' ), 35 );
  122.  
  123. // Register email templates.
  124. do_action( 'register_email_template' );
  125.  
  126. // Check Templates
  127. add_filter( 'wc_get_template', array( $this, 'ec_get_template' ), 10, 5 ); // > WC 2.2 (added this filter recently so can't use until more regular support)
  128.  
  129. //Email Customizer - Admin and Template pages only
  130. if ( isset($_REQUEST["page"]) && $_REQUEST["page"] == $this->id ) {
  131.  
  132. // Remove all notifications
  133. remove_all_actions( 'admin_notices' );
  134.  
  135. // Remove admin bar
  136. require_once( 'includes/toolbar-removal/wp-toolbar-removal.php');
  137.  
  138. if ( !isset( $_REQUEST["ec_render_email"] ) ) {
  139.  
  140. //Email Customizer - Admin Page only
  141. add_action( 'in_admin_header', array( $this, 'ec_render_admin_page' ) );
  142. }
  143. else {
  144.  
  145. //Email Customizer - Template page only
  146. add_filter( 'wp_print_scripts', array( $this, 'deregister_all_scripts' ), 101 );
  147. add_action( 'wp_print_scripts', array( $this, 'ec_head_scripts' ), 102 );
  148. add_action( 'admin_init', array( $this, 'ec_render_template_page' ) );
  149. }
  150. }
  151.  
  152. // Admin boody class for when in popup from eg WC order page
  153. add_filter( 'admin_body_class', array( $this, 'admin_body_classes' ) );
  154.  
  155. // Add Button in WooCommerce->Settings->Email
  156. add_action( 'woocommerce_settings_tabs_email', array( $this, 'woocommerce_settings_button' ) );
  157.  
  158. // Setup global template args.
  159. add_action( 'woocommerce_before_template_part', array( $this, 'ec_before_template_setup_args_global' ) , 10, 4 );
  160. add_action( 'woocommerce_after_template_part', array( $this, 'ec_after_template_setup_args_global' ) , 10, 4 );
  161.  
  162. // Setup options filtering.
  163. add_action( 'woocommerce_before_template_part', array( $this, 'ec_before_template_filter_options' ) , 10, 4 );
  164.  
  165. // Modify email headers.
  166. add_action( 'woocommerce_email_headers', array( $this, 'ec_email_headers' ) );
  167.  
  168. // Other simpler WooCommerce emails - Content.
  169. add_filter( 'woocommerce_email_content_low_stock', array( $this, 'woocommerce_simple_email_content' ), 10, 2 );
  170. add_filter( 'woocommerce_email_content_no_stock', array( $this, 'woocommerce_simple_email_content' ), 10, 2 );
  171. add_filter( 'woocommerce_email_content_backorder', array( $this, 'woocommerce_simple_email_content' ), 10, 2 );
  172. // Other simpler WooCommerce emails - Headers.
  173. add_filter( 'woocommerce_email_headers', array( $this, 'woocommerce_simple_email_headers' ), 10, 2 );
  174. add_filter( 'woocommerce_email_headers', array( $this, 'woocommerce_simple_email_headers' ), 10, 2 );
  175. add_filter( 'woocommerce_email_headers', array( $this, 'woocommerce_simple_email_headers' ), 10, 2 );
  176. }
  177.  
  178. /**
  179. * Body classes on admin page when in popup - e.g. wc order page
  180. *
  181. * @date 20-08-2014
  182. * @since 1.0
  183. *
  184. * @param string $classes CSS class names passed by the wp filter
  185. * @return string Concatenated string of css class names
  186. */
  187. function admin_body_classes($classes) {
  188.  
  189. if ( isset($_REQUEST["ec_in_popup"]) )
  190. $classes .= "pe-in-popup ";
  191.  
  192. return $classes;
  193. }
  194.  
  195. /**
  196. * Dergister all scripts & styles
  197. *
  198. * Deregister all scripts so the email template preview is
  199. * css clean and free of other plugins js bugs
  200. *
  201. * @date 20-08-2014
  202. * @since 1.0
  203. */
  204. function deregister_all_scripts() {
  205.  
  206. global $wp_scripts, $wp_styles;
  207.  
  208. // Dequeue All Scripts
  209. if (false != $wp_scripts->queue) {
  210. foreach($wp_scripts->queue as $script) {
  211. $wp_scripts->dequeue( $script );
  212.  
  213. // if (isset($wp_scripts->registered[$script])) {
  214. // $wp_scripts->registered[$script]->deps = array();
  215. // }
  216. }
  217. }
  218.  
  219. // Dequeue All Styles
  220. if (false != $wp_styles->queue) {
  221. foreach($wp_styles->queue as $script) {
  222. $wp_styles->dequeue( $script );
  223.  
  224. // if (isset($wp_styles->registered[$script])) {
  225. // $wp_styles->registered[$script]->deps = array();
  226. // }
  227. }
  228. }
  229. }
  230.  
  231. /**
  232. * Enqueue CSS and Scripts
  233. *
  234. * @date 20-08-2014
  235. * @since 1.0
  236. */
  237. public function ec_head_scripts() {
  238.  
  239. global $woocommerce, $wp_scripts, $current_screen;
  240.  
  241. // All Pages
  242. wp_register_style( 'woocommerce_admin', $woocommerce->plugin_url() . '/assets/css/admin.css' );
  243. wp_enqueue_style( 'woocommerce_admin' );
  244.  
  245. wp_enqueue_script( 'woocommerce_admin' );
  246.  
  247. wp_register_script( 'jquery-tiptip', $woocommerce->plugin_url() . '/assets/js/jquery-tiptip/jquery.tipTip.js', array('jquery') );
  248. wp_enqueue_script( 'jquery-tiptip' );
  249.  
  250. // Email Customizer - Admin page only
  251. if (
  252. ( isset($_REQUEST["page"]) && $_REQUEST["page"] == $this->id)
  253. ||
  254. ( isset($_REQUEST["page"]) && $_REQUEST["page"] == "wc-settings")
  255. ||
  256. ( isset($_REQUEST["ec_render_email"]) )
  257. ||
  258. ( isset($current_screen->id) && $current_screen->id == "shop_order")
  259. ) {
  260.  
  261.  
  262. // For image uplaoder on settings page_link
  263. wp_enqueue_media();
  264.  
  265.  
  266. // Magnificent Popup
  267. wp_register_script( 'magnificent-popup', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/js/magnificent-popup/magnificent.js', basename( __FILE__ ) ), array('jquery'), WC_EMAIL_CONTROL_VERSION );
  268. wp_enqueue_script( 'magnificent-popup' );
  269. wp_register_style( 'magnificent-popup', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/js/magnificent-popup/magnificent.css', basename( __FILE__ ) ), '', WC_EMAIL_CONTROL_VERSION, 'screen' );
  270. wp_enqueue_style( 'magnificent-popup' );
  271.  
  272. // Notification Systsem
  273. wp_register_style( 'cx-notification', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/css/notification.css', basename( __FILE__ ) ), '', WC_EMAIL_CONTROL_VERSION, 'screen' );
  274. wp_enqueue_style( 'cx-notification' );
  275.  
  276. // Font Awesome
  277. wp_register_style( 'fontawesome', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/css/fontawesome/css/font-awesome.min.css', basename( __FILE__ ) ), '', WC_EMAIL_CONTROL_VERSION, 'screen' );
  278. wp_enqueue_style( 'fontawesome' );
  279.  
  280. // Email Customizer Custom Scripts
  281. wp_register_style( 'email-control', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/css/email-control-back-end.css', basename( __FILE__ ) ), '', WC_EMAIL_CONTROL_VERSION, 'screen' );
  282. wp_enqueue_style( 'email-control' );
  283. wp_register_script( 'email-control', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/js/email-control-back-end.js', basename( __FILE__ ) ), array('jquery', 'jquery-tiptip', 'iris'), WC_EMAIL_CONTROL_VERSION );
  284. wp_enqueue_script( 'email-control' );
  285. wp_localize_script('email-control', 'woocommerce_email_control', array(
  286. 'home_url' => get_home_url(),
  287. 'admin_url' => admin_url(),
  288. 'ajaxurl' => admin_url('admin-ajax.php')
  289. ));
  290.  
  291. // Open Sans - incase it has not yet
  292. wp_enqueue_style( 'open-sans' );
  293.  
  294. }
  295.  
  296. wp_enqueue_style( 'open-sans' );
  297.  
  298. // Email Customizer - Template page only
  299. if ( ( isset($_REQUEST["page"]) && $_REQUEST["page"] == $this->id ) && isset( $_REQUEST["ec_render_email"] ) ) {
  300.  
  301. // Load jQuery
  302. wp_enqueue_script( 'jquery' );
  303.  
  304. // Load Dashicons
  305. wp_enqueue_style( 'dashicons' );
  306.  
  307. // Email Customizer Custom Scripts
  308. wp_register_style( 'email-control', plugins_url( basename( plugin_dir_path( __FILE__ ) ) . '/assets/css/email-control-back-end.css', basename( __FILE__ ) ), '', WC_EMAIL_CONTROL_VERSION, 'screen' );
  309. wp_enqueue_style( 'email-control' );
  310.  
  311. }
  312. }
  313.  
  314. /**
  315. * Localization
  316. *
  317. * @date 20-08-2014
  318. * @since 1.0
  319. */
  320. public static function load_translation() {
  321.  
  322. // Domain ID - used in eg __( 'Text', 'email-control' )
  323. $domain = 'email-control';
  324.  
  325. // get the languages locale eg 'en_US'
  326. $locale = apply_filters( 'plugin_locale', get_locale(), $domain );
  327.  
  328. // Look for languages here: wp-content/languages/pluginname/pluginname-en_US.mo
  329. //load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' ); // Removed this location.
  330.  
  331. // Look for languages here: wp-content/languages/pluginname/pluginname-en_US.mo
  332. load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $domain . '-' . $locale . '.mo' );
  333.  
  334. // Look for languages here: wp-content/languages/pluginname-en_US.mo
  335. load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '-' . $locale . '.mo' );
  336.  
  337. // Look for languages here: wp-content/plugins/pluginname/languages/pluginname-en_US.mo
  338. load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  339. }
  340.  
  341. /*
  342. * Save option
  343. *
  344. * @date 20-08-2014
  345. * @since 1.0
  346. */
  347. function save_meta() {
  348.  
  349. /*
  350. if ( !wp_verify_nonce( $_REQUEST['nonce'], "save_meta_nonce")) {
  351. exit("No naughty business please");
  352. }
  353. */
  354.  
  355. global $current_user;
  356. get_currentuserinfo();
  357.  
  358. $field_name = (isset($_REQUEST["field_name"])) ? $_REQUEST["field_name"] : "" ;
  359. $field_value = (isset($_REQUEST["field_value"])) ? $_REQUEST["field_value"] : "" ;
  360.  
  361. if ( strpos($field_name, "userspecifc") ) {
  362. //Save the option specific to the current user
  363. update_user_meta( $current_user->ID, $field_name, $field_value );
  364. }
  365. else {
  366. //Save the option to the global options
  367. update_option( $field_name, $field_value );
  368. }
  369.  
  370. die();
  371. }
  372.  
  373. function nopriv_save_meta() {
  374. _e('You must be logged in','email-control');
  375. die();
  376. }
  377.  
  378. /*
  379. * Save option new
  380. *
  381. * @date 20-08-2014
  382. * @since 1.0
  383. */
  384. function save_option() {
  385. /*
  386. if ( !wp_verify_nonce( $_REQUEST['nonce'], "save_option_nonce")) {
  387. exit("No naughty business please");
  388. }
  389. */
  390.  
  391. $field_name = (isset($_REQUEST["field_name"])) ? $_REQUEST["field_name"] : "" ;
  392. $field_value = (isset($_REQUEST["field_value"])) ? $_REQUEST["field_value"] : "" ;
  393.  
  394. update_option( $field_name, $field_value );
  395.  
  396. die();
  397. }
  398.  
  399. function nopriv_save_option() {
  400. _e('You must be logged in', 'email-control');
  401. die();
  402. }
  403.  
  404. /*
  405. * Ajax send email
  406. *
  407. * @date 20-08-2014
  408. * @since 1.0
  409. */
  410. public function send_email() {
  411.  
  412. global $order, $woocommerce;
  413.  
  414. $email_type = $_REQUEST['ec_email_type'];
  415. $email_order = $_REQUEST['ec_email_order'];
  416. //$email_addresses = $_REQUEST['ec_email_addresses'];
  417. $email_template_id = $_REQUEST['ec_email_template'];
  418.  
  419. // Handle button actions
  420. if ( !empty( $_REQUEST['ec_email_type'] ) ) {
  421.  
  422. // Load mailer
  423. $mailer = $woocommerce->mailer();
  424. $mails = $mailer->get_emails();
  425.  
  426. // Ensure gateways are loaded in case they need to insert data into the emails
  427. $woocommerce->payment_gateways();
  428. $woocommerce->shipping();
  429.  
  430. $email_to_send = wc_clean( $_REQUEST['ec_email_type'] );
  431.  
  432. if ( !empty( $mails ) ) {
  433. foreach ( $mails as $mail ) {
  434. if ( $mail->id == $email_to_send ) {
  435.  
  436. // Old method - used our own Sedning function
  437. //$this->trigger_send_email( $order->id, $mail, $email_addresses);
  438.  
  439. // New method - filters the recicpeint address and used the respective mails own sending function to send
  440. add_filter( 'woocommerce_email_recipient_' . $mail->id, array( $this, 'woocommerce_email_recipient' ) );
  441. $mail->trigger( $email_order );
  442. }
  443. }
  444. }
  445.  
  446. }
  447.  
  448. die();
  449. }
  450.  
  451. function nopriv_send_email() {
  452. _e('You must be logged in', 'email-control');
  453. die();
  454. }
  455.  
  456. /*
  457. * Filter used to modify the receivers email address so tester can specify their own.
  458. */
  459. function woocommerce_email_recipient () {
  460. if ( isset( $_REQUEST['ec_email_addresses'] ) ) {
  461. return $_REQUEST['ec_email_addresses'];
  462. }
  463. }
  464.  
  465. /*
  466. * Ajax send email
  467. *
  468. * @date 20-08-2014
  469. * @since 1.0
  470. */
  471. function trigger_send_email( $order, $mail, $email_addresses) {
  472.  
  473. $email_addresses = str_replace( " ", "", $email_addresses );
  474. $email_addresses = explode( ",", $email_addresses );
  475.  
  476. $mail->object = new WC_Order($order);
  477.  
  478.  
  479. $mail->find[] = '{order_date}';
  480. $mail->replace[] = date_i18n( wc_date_format(), strtotime( $mail->object->order_date ) );
  481.  
  482. $mail->find[] = '{order_number}';
  483. $mail->replace[] = $mail->object->get_order_number();
  484.  
  485.  
  486. $mail->recipient = $mail->object->billing_email;
  487.  
  488. $email_subject = $mail->get_subject();
  489. $email_content = $mail->get_content();
  490. $email_headers = $mail->get_headers();
  491. $email_attachments = $mail->get_attachments();
  492.  
  493.  
  494. foreach ($email_addresses as $email_address) {
  495. echo $mail->send( $email_address, $email_subject, $email_content, $email_headers, $email_attachments );
  496. }
  497.  
  498. die();
  499. }
  500.  
  501. /*
  502. * Save all edit template options.
  503. *
  504. * @date 20-08-2014
  505. * @since 1.0
  506. */
  507. public function save_edit_email() {
  508.  
  509. $email_type = ($_REQUEST['ec_email_type']) ? $_REQUEST['ec_email_type'] : false ;
  510. $email_id = ($_REQUEST['ec_email_id']) ? $_REQUEST['ec_email_id'] : false ;
  511.  
  512. $settings = ec_get_settings($email_id);
  513.  
  514. EC_Settings::save_fields( $settings );
  515.  
  516. die();
  517. }
  518.  
  519. function nopriv_save_edit_email() {
  520. _e('You must be logged in', 'email-control');
  521. die();
  522. }
  523.  
  524. /**
  525. * WC order page meta box
  526. */
  527. public function order_page_meta_box() {
  528.  
  529. add_meta_box(
  530. 'woocommerce-order-actions-new',
  531. __( 'Email Customizer', 'email-control' ),
  532. array($this, 'order_meta_box'),
  533. 'shop_order',
  534. 'side',
  535. 'high'
  536. );
  537. }
  538.  
  539. /**
  540. * WC order page meta box
  541. *
  542. * @date 20-08-2014
  543. * @since 1.0
  544. *
  545. * @param object $post The order post
  546. */
  547. public function order_meta_box( $post ) {
  548. global $woocommerce, $theorder, $wpdb;
  549.  
  550. if ( !is_object( $theorder ) )
  551. $theorder = new WC_Order( $post->ID );
  552.  
  553. $order = $theorder;
  554. ?>
  555.  
  556. <div class="ec_order_page_ui">
  557.  
  558. <div class="ec_actions_dropdown help_tip_new" data-tip="<?php _e( 'Choose which email to preview or send.', 'email-control' ); ?>" >
  559.  
  560. <?php do_action( 'woocommerce_order_actions_start', $post->ID ); ?>
  561.  
  562. <select name="ec_order_action" id="ec_order_action">
  563. <option value=""><?php _e( 'Emails', 'email-control' ); ?></option>
  564.  
  565. <?php
  566. // Load mailer
  567. if ( class_exists('WC') ) {
  568. $mailer = WC()->mailer();
  569. $mails = $mailer->get_emails();
  570.  
  571. // Ensure gateways are loaded in case they need to insert data into the emails
  572. WC()->payment_gateways();
  573. WC()->shipping();
  574.  
  575. }
  576. else{
  577. $mailer = $woocommerce->mailer();
  578. $mails = $mailer->get_emails();
  579.  
  580. // Ensure gateways are loaded in case they need to insert data into the emails
  581. $woocommerce->payment_gateways();
  582. $woocommerce->shipping();
  583. }
  584.  
  585. if ( !empty( $mails ) ) {
  586. foreach ( $mails as $mail ) {
  587. ?>
  588. <option value="send_email_<?php echo esc_attr( $mail->id ) ?>">
  589. <?php echo esc_html( $mail->title ) ?>
  590. </option>
  591. <?php
  592. }
  593. }
  594. ?>
  595. </select>
  596.  
  597. </div>
  598. <div class="ec_actions_buttons">
  599.  
  600. <!-- Buttons Row -->
  601. <a class="button help_tip_new" id="preview-email-button" data-tip="<?php _e( "Preview the email selected above.", 'email-control' ); ?>" target="_blank" ><?php _e( 'Preview Email', 'email-control' ); ?></a>
  602. <a class="button help_tip_new" id="send-email" data-tip="<?php _e( "Send the email selected above to this customer's billing address email. Will default to 'New Order' email if nothing is selected.", 'email-control' ); ?>" target="_blank" ><?php _e( 'Send Email', 'email-control' ); ?></a>
  603. <!-- /Buttons Row -->
  604.  
  605. </div>
  606.  
  607. </div>
  608.  
  609. <?php
  610. }
  611.  
  612. /**
  613. * Render admin page.
  614. *
  615. * @date 20-08-2014
  616. * @since 1.0
  617. */
  618. public function ec_render_admin_page() {
  619.  
  620. require_once( 'pages/ec-admin-page.php');
  621. }
  622.  
  623. /**
  624. * Render template page.
  625. *
  626. * @date 20-08-2014
  627. * @since 1.0
  628. */
  629. public function ec_render_template_page() {
  630.  
  631. require_once( 'pages/ec-template-page.php');
  632. }
  633.  
  634. /**
  635. * Add a submenu item to the WooCommerce menu
  636. *
  637. * @date 20-08-2014
  638. * @since 1.0
  639. */
  640. public function admin_menu() {
  641.  
  642. /*
  643. global $submenu;
  644. $submenu['woocommerce'][500] = array(
  645. __( 'Email Customizer', 'email-control' ),
  646. 'manage_woocommerce',
  647. admin_url( "?ec_show_interface=true" )
  648. );
  649. */
  650.  
  651. add_submenu_page(
  652. 'woocommerce',
  653. __('Email Customizer', 'email-control'),
  654. __('Email Customizer', 'email-control'),
  655. 'manage_woocommerce',
  656. $this->id,
  657. array( $this, 'ec_render_admin_page' )
  658. );
  659. }
  660.  
  661. /**
  662. * Add info and button to WooCommerce->settings->email page
  663. *
  664. * @date 20-08-2014
  665. * @since 1.0
  666. */
  667. function woocommerce_settings_button($data) {
  668.  
  669. global $woocommerce, $wp_scripts, $current_screen;
  670.  
  671. $ec_url = "";
  672. $ec_url .= admin_url();
  673. $ec_url .= "admin.php";
  674. $ec_url .= "?";
  675. $ec_url .= "page=woocommerce_email_control";
  676.  
  677. if ( isset($_REQUEST["section"]) ) {
  678.  
  679. if ( class_exists('WC') ) {
  680. $mailer = WC()->mailer();
  681. $mails = $mailer->get_emails();
  682. }
  683. else{
  684. $mailer = $woocommerce->mailer();
  685. $mails = $mailer->get_emails();
  686. }
  687.  
  688. if ( !empty($mails) ) {
  689. foreach ( $mails as $mail ) {
  690. $template = str_replace("wc_email_", "", $_REQUEST["section"] );
  691. if ( $mail->id == $template ) {
  692. $ec_url .= "&ec_email_type=" . $template;
  693. }
  694. }
  695. }
  696. }
  697.  
  698. ?>
  699. <div class="pe-wc-settings-holder">
  700.  
  701. <?php if ( isset($_REQUEST["section"]) && $_REQUEST["section"] != "" ) { ?>
  702.  
  703. <!-- Inner Tabs -->
  704. <h4>Email Customizer</h4>
  705. <p>
  706. <a class="button ec" href="<?php echo $ec_url ?>" target="preview_email">Preview Email</a>
  707. <?php _e("Preview and test emails as they will appear in mail clients when received.", "email-control") ?>
  708. </p>
  709.  
  710. <?php } else { ?>
  711.  
  712. <!-- First Tab -->
  713. <h3>Email Customizer</h3>
  714. <p>
  715. <a class="button ec" href="<?php echo $ec_url ?>" target="preview_email">Preview Email</a>
  716. <?php _e("Preview and test emails as they will appear in mail clients when received.", "email-control") ?>
  717. </p>
  718.  
  719. <?php } ?>
  720.  
  721. </div>
  722. <?php
  723. }
  724.  
  725. /**
  726. * Check for and return our template.
  727. *
  728. * WC 2.2 and above - added this filter recently so can't use until more regular support
  729. *
  730. * @date 20-08-2014
  731. * @since 1.0
  732. *
  733. * @return string Template file location
  734. */
  735. function ec_get_template( $located, $template_name, $args, $template_path, $default_path ) {
  736.  
  737. // echo "<br>";
  738. // echo "get_template";
  739. // echo "<br>";
  740. // echo "<br>";
  741. // echo 'located--------------------: ' . $located;
  742. // echo "<br>";
  743. // echo 'template_name----------: ' . $template_name;
  744. // echo "<br>";
  745. // echo "args------------------------: ";
  746. // print_r( $args );
  747. // echo "<br>";
  748. // echo 'template_path-----------: ' . $template_path;
  749. // echo "<br>";
  750. // echo 'default_path-------------: ' . $default_path;
  751. // echo "<br><br><br><br>";
  752.  
  753. if ( FALSE !== strrpos( $template_name, 'emails' ) ) {
  754.  
  755. global $ec_email_templates, $woocommerce;
  756.  
  757. // Use the selected template from database
  758. $ec_template_selected = get_option("ec_template");
  759.  
  760. // Overide selected template with that passed by preview
  761. if ( isset( $_REQUEST["ec_email_template_preview"] ) ) $ec_template_selected = $_REQUEST["ec_email_template_preview"];
  762.  
  763. $this_template = false;
  764.  
  765. if ( is_array( $ec_email_templates ) && isset( $ec_template_selected ) && $ec_template_selected !== false ) {
  766. if ( array_key_exists( $ec_template_selected, $ec_email_templates ) ) {
  767.  
  768. // It's one of our templates so do custom filters
  769. add_filter( 'woocommerce_email_custom_details_header', '__return_empty_string' ); // Remove the header of the customer details.
  770.  
  771. $this_plugin_path = trailingslashit( $ec_email_templates[$ec_template_selected]["template_folder_path"] );
  772. $this_template_path = trailingslashit( "templates" );
  773. $this_template_name = str_replace(".php", "-". str_replace ( "_", "-", $ec_template_selected) .".php", $template_name);
  774.  
  775. // First check in all the normal overide places. In this case it will be just the theme folder.
  776. // template name emails/admin-new-order.php becomes emails/admin-new-order-inlinesky.php
  777. // for this test.
  778. $wc_template_path = $woocommerce->template_path();
  779. $this_template = locate_template( array(
  780. trailingslashit( $wc_template_path ) . $this_template_name,
  781. $this_template_name
  782. ));
  783.  
  784. // else check if we cover this template in the plugin template folder.
  785. // again we are looking for this file emails/admin-new-order-inlinesky.php
  786. if ( !$this_template ) {
  787. $this_template = trailingslashit( $this_plugin_path ) . trailingslashit( $this_template_path ) . $this_template_name;
  788. if ( !file_exists($this_template) ) $this_template = false;
  789. }
  790. //else also check the path if the template was registered with the path to the woocommerce template folder
  791. if ( !$this_template ) {
  792. $this_template = trailingslashit( $this_plugin_path ) . $this_template_name;
  793. if ( !file_exists($this_template) ) $this_template = false;
  794. }
  795. // else set this template to what it was passed to this - $located
  796. if ( !$this_template ) {
  797. $this_template = $located;
  798. }
  799.  
  800. //set the located as $this_template.
  801. $located = $this_template;
  802. }
  803. }
  804. }
  805.  
  806. // echo "<br>";
  807. // echo "get_template";
  808. // echo "<br>";
  809. // echo "<br>";
  810. // echo 'located--------------------: ' . $located;
  811. // echo "<br>";
  812. // echo 'template_name----------: ' . $template_name;
  813. // echo "<br>";
  814. // echo "args------------------------: ";
  815. // print_r( $args );
  816. // echo "<br>";
  817. // echo 'template_path-----------: ' . $template_path;
  818. // echo "<br>";
  819. // echo 'default_path-------------: ' . $default_path;
  820. // echo "<br><br><br><br>";
  821.  
  822. return $located;
  823. }
  824.  
  825. /**
  826. * Modify options before template
  827. *
  828. * Only if one of the email templates are run, so as not to waste processing time,
  829. * and check if REQUEST fields are being posted and rather use those.
  830. *
  831. * @date 20-08-2014
  832. * @since 1.0
  833. */
  834. function ec_before_template_filter_options( $template_name, $template_path, $located, $args ) {
  835.  
  836. if ( FALSE !== strrpos( $template_name, 'email' ) ) {
  837.  
  838. // Get active templates.
  839. $ec_template_selected = false;
  840. if ( get_option( "ec_template" ) ) {
  841. $ec_template_selected = get_option( "ec_template" );
  842. }
  843. if ( isset( $_REQUEST['ec_email_template'] ) ) {
  844. $ec_template_selected = $_REQUEST['ec_email_template'];
  845. }
  846.  
  847. // Modify if theres preview fields.
  848. $settings = ec_get_settings( $ec_template_selected );
  849.  
  850. if ( $settings ) {
  851. foreach ( $settings as $setting_key => $setting_value ) {
  852.  
  853. $field_id = $setting_value["id"];
  854. $field_type = $setting_value["type"];
  855.  
  856. add_filter( "default_option_{$field_id}", array('EC_Settings', 'ec_default_option') );
  857. add_filter( "option_{$field_id}", create_function( '$field_value', 'return EC_Settings::ec_render_option("'.$field_id.'", $field_value ); ' ) );
  858. }
  859. }
  860.  
  861. // Only do this once, the first time an email template is called.
  862. remove_filter( 'woocommerce_before_template_part', array( $this, 'ec_before_template_filter_options' ) );
  863. }
  864. }
  865.  
  866. /**
  867. * Push the args into a global.
  868. *
  869. * To be used in the shortcodes. Has to be done this way while we are
  870. * not getting passed $args due to not being able to use WC new filter
  871. * in wc_get_template as it was only released late - around 2.2
  872. *
  873. * @date 20-08-2014
  874. * @since 2.12
  875. */
  876. function ec_before_template_setup_args_global( $template_name, $template_path, $located, $args ) {
  877.  
  878. // Only do this for email templates.
  879. if ( FALSE !== strrpos( $template_name, 'email' ) ) {
  880.  
  881. global $ec_template_args;
  882.  
  883. if ( NULL == $ec_template_args ) {
  884.  
  885. $ec_template_args = array_merge( $args, array( 'ec_template_name' => $template_name ) );
  886.  
  887. // Debugging
  888. //echo 'start:&nbsp;' . $template_name;
  889. }
  890. }
  891. }
  892.  
  893. /**
  894. * Remove the args from global on last call to the template function.
  895. *
  896. * @date 09-02-2015
  897. * @since 2.17
  898. */
  899. function ec_after_template_setup_args_global( $template_name, $template_path, $located, $args ) {
  900.  
  901. // Only do this for email templates.
  902. if ( FALSE !== strrpos( $template_name, 'email' ) ) {
  903.  
  904. global $ec_template_args;
  905.  
  906. if ( isset( $ec_template_args['ec_template_name'] ) && $template_name == $ec_template_args['ec_template_name'] ) {
  907.  
  908. $ec_template_args = NULL;
  909.  
  910. // Debugging
  911. //echo 'end:&nbsp;' . $template_name;
  912. }
  913. }
  914. }
  915.  
  916. /**
  917. * Force UTF-8 to email headers
  918. *
  919. * @date 09-02-2015
  920. * @since 2.17
  921. */
  922. function ec_email_headers( $headers ) {
  923.  
  924. //$headers = str_replace( "\r\n", '; charset=ISO-8859-2' . "\r\n" , $headers );
  925. $headers = str_replace( "\r\n", '; charset=UTF-8' . "\r\n" , $headers );
  926. return $headers;
  927. }
  928.  
  929. /**
  930. * Format the other simpler WooCommerce emails - Content.
  931. */
  932. function woocommerce_simple_email_content( $message ) {
  933.  
  934. ob_start();
  935. wc_get_template('emails/email-header.php' );
  936. echo $message;
  937. wc_get_template('emails/email-footer.php' );
  938. return ob_get_clean();
  939. }
  940. /**
  941. * Format the other simpler WooCommerce emails - Headers.
  942. */
  943. function woocommerce_simple_email_headers() {
  944.  
  945. return "Content-Type: text/html; charset=UTF-8\r\n";
  946. }
  947.  
  948. /**
  949. * Is WooCommerce active.
  950. */
  951. public static function is_woocommerce_active() {
  952.  
  953. $active_plugins = (array) get_option( 'active_plugins', array() );
  954.  
  955. if ( is_multisite() )
  956. $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
  957.  
  958. return in_array( 'woocommerce/woocommerce.php', $active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins );
  959. }
  960.  
  961. /**
  962. * Display Notifications on specific criteria.
  963. *
  964. * @since 2.14
  965. */
  966. public static function woocommerce_inactive_notice() {
  967. if ( current_user_can( 'activate_plugins' ) ) :
  968. if ( !class_exists( 'WooCommerce' ) ) :
  969. ?>
  970. <div id="message" class="error">
  971. <p>
  972. <?php
  973. printf(
  974. __( '%sEmail Customizer for WooCommerce needs WooCommerce%s %sWooCommerce%s must be active for Email Customizer to work. Please install & activate WooCommerce.', 'email-control' ),
  975. '<strong>',
  976. '</strong><br>',
  977. '<a href="http://wordpress.org/extend/plugins/woocommerce/" target="_blank" >',
  978. '</a>'
  979. );
  980. ?>
  981. </p>
  982. </div>
  983. <?php
  984. elseif ( version_compare( get_option( 'woocommerce_db_version' ), WC_EMAIL_CONTROL_REQUIRED_WOOCOMMERCE_VERSION, '<' ) ) :
  985. ?>
  986. <div id="message" class="error">
  987. <!--<p style="float: right; color: #9A9A9A; font-size: 13px; font-style: italic;">For more information <a href="http://cxthemes.com/plugins/update-notice.html" target="_blank" style="color: inheret;">click here</a></p>-->
  988. <p>
  989. <?php
  990. printf(
  991. __( '%sEmail Customizer for WooCommerce is inactive%s This version of Email Customizer requires WooCommerce %s or newer. For more information about our WooCommerce version support %sclick here%s.', 'email-control' ),
  992. '<strong>',
  993. '</strong><br>',
  994. WC_EMAIL_CONTROL_REQUIRED_WOOCOMMERCE_VERSION,
  995. '<a href="https://helpcx.zendesk.com/hc/en-us/articles/202241041/" target="_blank" style="color: inheret;" >',
  996. '</a>'
  997. );
  998. ?>
  999. </p>
  1000. <div style="clear:both;"></div>
  1001. </div>
  1002. <?php
  1003. endif;
  1004. endif;
  1005. }
  1006.  
  1007. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement