From a8d45b1824f98253eb012b006d605d4a5d5bd851 Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Wed, 1 Nov 2017 13:08:28 +0200 Subject: [PATCH] - fixed email notifications for extensions; --- includes/class-functions.php | 2 +- includes/core/class-mail.php | 499 ++++++++++++++++++----------------- 2 files changed, 253 insertions(+), 248 deletions(-) diff --git a/includes/class-functions.php b/includes/class-functions.php index c3362c4d..77a58143 100644 --- a/includes/class-functions.php +++ b/includes/class-functions.php @@ -192,7 +192,7 @@ if ( ! class_exists( 'UM_Functions' ) ) { * * @access public * @param string $template_name - * @param string $path (default: '') + * @param string $basename (default: '') * @param array $t_args (default: array()) * @param bool $echo * diff --git a/includes/core/class-mail.php b/includes/core/class-mail.php index 25342733..f33bbf5a 100644 --- a/includes/core/class-mail.php +++ b/includes/core/class-mail.php @@ -5,295 +5,300 @@ namespace um\core; if ( ! defined( 'ABSPATH' ) ) exit; if ( ! class_exists( 'Mail' ) ) { - class Mail { + class Mail { - var $email_templates = array(); + var $email_templates = array(); + var $path_by_slug = array(); - function __construct() { + function __construct() { - $this->path_by_slug = apply_filters( 'um_email_templates_path_by_slug', array() ); + //mandrill compatibility + add_filter( 'mandrill_nl2br', array( &$this, 'mandrill_nl2br' ) ); + add_action( 'plugins_loaded', array( &$this, 'init_paths' ) ); - //mandrill compatibility - add_filter( 'mandrill_nl2br', array( &$this, 'mandrill_nl2br' ) ); + } - } + function init_paths() { + $this->path_by_slug = apply_filters( 'um_email_templates_path_by_slug', $this->path_by_slug ); + } - /** - * Mandrill compatibility - * - * @param $nl2br - * @param string $message - * @return bool - */ - function mandrill_nl2br( $nl2br, $message = '' ) { - // text emails - if ( ! um_get_option( 'email_html' ) ) { - $nl2br = true; - } + /** + * Mandrill compatibility + * + * @param $nl2br + * @param string $message + * @return bool + */ + function mandrill_nl2br( $nl2br, $message = '' ) { - return $nl2br; + // text emails + if ( ! um_get_option( 'email_html' ) ) { + $nl2br = true; + } - } + return $nl2br; + } - /** - * Send Email function - * - * @param string $email - * @param null $template - * @param array $args - */ - function send( $email, $template, $args = array() ) { - if ( ! is_email( $email ) ) return; - if ( um_get_option( $template . '_on' ) != 1 ) return; + /** + * Send Email function + * + * @param string $email + * @param null $template + * @param array $args + */ + function send( $email, $template, $args = array() ) { - $this->attachments = null; - $this->headers = 'From: '. um_get_option('mail_from') .' <'. um_get_option('mail_from_addr') .'>' . "\r\n"; + if ( ! is_email( $email ) ) return; + if ( um_get_option( $template . '_on' ) != 1 ) return; - $this->subject = um_convert_tags( um_get_option( $template . '_sub' ), $args ); + $this->attachments = null; + $this->headers = 'From: '. um_get_option('mail_from') .' <'. um_get_option('mail_from_addr') .'>' . "\r\n"; - $this->message = $this->prepare_template( $template, $args ); + $this->subject = um_convert_tags( um_get_option( $template . '_sub' ), $args ); - add_filter( 'wp_mail_content_type', array( &$this, 'set_content_type' ) ); - // Send mail - wp_mail( $email, $this->subject, $this->message, $this->headers, $this->attachments ); - remove_filter( 'wp_mail_content_type', array( &$this, 'set_content_type' ) ); - } + $this->message = $this->prepare_template( $template, $args ); + add_filter( 'wp_mail_content_type', array( &$this, 'set_content_type' ) ); + // Send mail + wp_mail( $email, $this->subject, $this->message, $this->headers, $this->attachments ); + remove_filter( 'wp_mail_content_type', array( &$this, 'set_content_type' ) ); + } - /** - * @param $slug - * @param $args - * @return bool|string - */ - function get_email_template( $slug, $args = array() ) { - $located = $this->locate_template( $slug ); + /** + * @param $slug + * @param $args + * @return bool|string + */ + function get_email_template( $slug, $args = array() ) { - $located = apply_filters( 'um_email_template_path', $located, $slug, $args ); + $located = $this->locate_template( $slug ); - if ( ! file_exists( $located ) ) { - _doing_it_wrong( __FUNCTION__, sprintf( '%s does not exist.', $located ), '2.1' ); - return false; - } + $located = apply_filters( 'um_email_template_path', $located, $slug, $args ); - ob_start(); + if ( ! file_exists( $located ) ) { + _doing_it_wrong( __FUNCTION__, sprintf( '%s does not exist.', $located ), '2.1' ); + return false; + } - do_action( 'um_before_email_template_part', $slug, $located, $args ); + ob_start(); - include( $located ); + do_action( 'um_before_email_template_part', $slug, $located, $args ); - do_action( 'um_after_email_template_part', $slug, $located, $args ); + include( $located ); - return ob_get_clean(); - } + do_action( 'um_after_email_template_part', $slug, $located, $args ); + return ob_get_clean(); + } - /** - * Prepare email template to send - * - * @param $slug - * @param $args - * @return mixed|string - */ - function prepare_template( $slug, $args = array() ) { - ob_start(); - if ( um_get_option( 'email_html' ) ) { ?> + /** + * Prepare email template to send + * + * @param $slug + * @param $args + * @return mixed|string + */ + function prepare_template( $slug, $args = array() ) { + ob_start(); - - + if ( um_get_option( 'email_html' ) ) { ?> - > + + - get_email_template( $slug, $args ); ?> + > - - + get_email_template( $slug, $args ); ?> - + - echo $this->get_email_template( $slug, $args ); + get_email_template( $slug, $args ); - $message = ob_get_clean(); + } - // Convert tags in email template - return um_convert_tags( $message, $args ); - } + $message = ob_get_clean(); + // Convert tags in email template + return um_convert_tags( $message, $args ); + } - /** - * Locate a template and return the path for inclusion. - * - * @access public - * @param string $template_name - * @return string - */ - function locate_template( $template_name ) { - //WPML compatibility and multilingual email templates - $lang = get_locale(); - $arr_english_lang = array( 'en', 'en_US', 'en_NZ', 'en_ZA', 'en_AU', 'en_GB' ); - if ( in_array( $lang, $arr_english_lang ) || strpos( $lang , 'en_' ) > -1 || empty( $lang ) || $lang == 0 ) { - $lang = ''; - } else { - $lang .= '/'; - } + /** + * Locate a template and return the path for inclusion. + * + * @access public + * @param string $template_name + * @return string + */ + function locate_template( $template_name ) { + //WPML compatibility and multilingual email templates + $lang = get_locale(); + $arr_english_lang = array( 'en', 'en_US', 'en_NZ', 'en_ZA', 'en_AU', 'en_GB' ); - // check if there is template at theme folder - $template = locate_template( array( - trailingslashit( 'ultimate-member/email' ) . $lang . $template_name . '.php' - ) ); + if ( in_array( $lang, $arr_english_lang ) || strpos( $lang , 'en_' ) > -1 || empty( $lang ) || $lang == 0 ) { + $lang = ''; + } else { + $lang .= '/'; + } - //if there isn't template at theme folder get template file from plugin dir - if ( ! $template ) { - $path = ! empty( $this->path_by_slug[ $template_name ] ) ? $this->path_by_slug[ $template_name ] : um_path . 'templates/email'; - $template = trailingslashit( $path ) . $lang . $template_name . '.php'; - } - - // Return what we found. - return apply_filters( 'um_locate_email_template', $template, $template_name ); - } - - - /** - * Locate a template and return the path for inclusion. - * - * @access public - * @param string $template_name - * @param bool $html - * @return string - */ - function template_in_theme( $template_name, $html = false ) { - //WPML compatibility and multilingual email templates - $lang = get_locale(); - $arr_english_lang = array( 'en', 'en_US', 'en_NZ', 'en_ZA', 'en_AU', 'en_GB' ); - - if ( in_array( $lang, $arr_english_lang ) || strpos( $lang , 'en_' ) > -1 || empty( $lang ) || $lang == 0 ) { - $lang = ''; - } else { - $lang .= '/'; - } - - $ext = ! $html ? '.php' : '.html'; - - // check if there is template at theme folder - $template = locate_template( array( - trailingslashit( 'ultimate-member/email' ) . $lang . $template_name . $ext - ) ); - - // Return what we found. - return ! $template ? false : true; - } - - - /** - * Method returns expected path for template - * - * @access public - * @param string $location - * @param string $template_name - * @param bool $html - * @return string - */ - function get_template_file( $location, $template_name, $html = false ) { - $template_path = ''; - - $ext = ! $html ? '.php' : '.html'; - - switch( $location ) { - case 'theme': - $template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' ) . $template_name . $ext; - break; - case 'plugin': - $path = ! empty( $this->path_by_slug[ $template_name ] ) ? $this->path_by_slug[ $template_name ] : um_path . 'templates/email'; - $template_path = trailingslashit( $path ) . $template_name . $ext; - break; - } - - //return apply_filters( 'wpc_client_template_location', $template_path, $location, $template_name, $path ); - return $template_path; - } - - - /** - * Set email content type - * - * - * @param $content_type - * @return string - */ - function set_content_type( $content_type ) { - - if ( um_get_option( 'email_html' ) ) { - return 'text/html'; - } else { - return 'text/plain'; - } - - } - - - /** - * Ajax copy template to the theme - * - * @param bool $template - * @return bool - */ - function copy_email_template( $template = false ) { - - $in_theme = $this->template_in_theme( $template ); - if ( $in_theme ) { - return false; - } - - $plugin_template_path = $this->get_template_file( 'plugin', $template ); - $theme_template_path = $this->get_template_file( 'theme', $template ); - - $temp_path = str_replace( trailingslashit( get_stylesheet_directory() ), '', $theme_template_path ); - $temp_path = str_replace( '/', DIRECTORY_SEPARATOR, $temp_path ); - $folders = explode( DIRECTORY_SEPARATOR, $temp_path ); - $folders = array_splice( $folders, 0, count( $folders ) - 1 ); - $cur_folder = ''; - $theme_dir = trailingslashit( get_stylesheet_directory() ); - - foreach ( $folders as $folder ) { - $prev_dir = $cur_folder; - $cur_folder .= $folder . DIRECTORY_SEPARATOR; - if ( ! is_dir( $theme_dir . $cur_folder ) && wp_is_writable( $theme_dir . $prev_dir ) ) { - mkdir( $theme_dir . $cur_folder, 0777 ); - } - } - - if ( copy( $plugin_template_path, $theme_template_path ) ) { - return true; - } else { - return false; - } - } - - - function delete_email_template() { - $template = $_POST['email_key']; - - $in_theme = $this->template_in_theme( $template ); - if ( ! $in_theme ) { - wp_send_json_error( new \WP_Error( 'template_in_theme', __( 'Template does not exists in theme', 'ultimate-member' ) ) ); - } - - $theme_template_path = $this->get_template_file( 'theme', $template ); - - if ( unlink( $theme_template_path ) ) { - wp_send_json_success(); - } else { - wp_send_json_error( new \WP_Error( 'template_not_exists', __( 'Can not remove template from theme', 'ultimate-member' ) ) ); - } - } - - } + // check if there is template at theme folder + $template = locate_template( array( + trailingslashit( 'ultimate-member/email' ) . $lang . $template_name . '.php' + ) ); + + //if there isn't template at theme folder get template file from plugin dir + if ( ! $template ) { + $path = ! empty( $this->path_by_slug[ $template_name ] ) ? $this->path_by_slug[ $template_name ] : um_path . 'templates/email'; + $template = trailingslashit( $path ) . $lang . $template_name . '.php'; + } + + // Return what we found. + return apply_filters( 'um_locate_email_template', $template, $template_name ); + } + + + /** + * Locate a template and return the path for inclusion. + * + * @access public + * @param string $template_name + * @param bool $html + * @return string + */ + function template_in_theme( $template_name, $html = false ) { + //WPML compatibility and multilingual email templates + $lang = get_locale(); + $arr_english_lang = array( 'en', 'en_US', 'en_NZ', 'en_ZA', 'en_AU', 'en_GB' ); + + if ( in_array( $lang, $arr_english_lang ) || strpos( $lang , 'en_' ) > -1 || empty( $lang ) || $lang == 0 ) { + $lang = ''; + } else { + $lang .= '/'; + } + + $ext = ! $html ? '.php' : '.html'; + + // check if there is template at theme folder + $template = locate_template( array( + trailingslashit( 'ultimate-member/email' ) . $lang . $template_name . $ext + ) ); + + // Return what we found. + return ! $template ? false : true; + } + + + /** + * Method returns expected path for template + * + * @access public + * @param string $location + * @param string $template_name + * @param bool $html + * @return string + */ + function get_template_file( $location, $template_name, $html = false ) { + $template_path = ''; + + $ext = ! $html ? '.php' : '.html'; + + switch( $location ) { + case 'theme': + $template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' ) . $template_name . $ext; + break; + case 'plugin': + $path = ! empty( $this->path_by_slug[ $template_name ] ) ? $this->path_by_slug[ $template_name ] : um_path . 'templates/email'; + $template_path = trailingslashit( $path ) . $template_name . $ext; + break; + } + + //return apply_filters( 'wpc_client_template_location', $template_path, $location, $template_name, $path ); + return $template_path; + } + + + /** + * Set email content type + * + * + * @param $content_type + * @return string + */ + function set_content_type( $content_type ) { + + if ( um_get_option( 'email_html' ) ) { + return 'text/html'; + } else { + return 'text/plain'; + } + + } + + + /** + * Ajax copy template to the theme + * + * @param bool $template + * @return bool + */ + function copy_email_template( $template = false ) { + + $in_theme = $this->template_in_theme( $template ); + if ( $in_theme ) { + return false; + } + + $plugin_template_path = $this->get_template_file( 'plugin', $template ); + $theme_template_path = $this->get_template_file( 'theme', $template ); + + $temp_path = str_replace( trailingslashit( get_stylesheet_directory() ), '', $theme_template_path ); + $temp_path = str_replace( '/', DIRECTORY_SEPARATOR, $temp_path ); + $folders = explode( DIRECTORY_SEPARATOR, $temp_path ); + $folders = array_splice( $folders, 0, count( $folders ) - 1 ); + $cur_folder = ''; + $theme_dir = trailingslashit( get_stylesheet_directory() ); + + foreach ( $folders as $folder ) { + $prev_dir = $cur_folder; + $cur_folder .= $folder . DIRECTORY_SEPARATOR; + if ( ! is_dir( $theme_dir . $cur_folder ) && wp_is_writable( $theme_dir . $prev_dir ) ) { + mkdir( $theme_dir . $cur_folder, 0777 ); + } + } + + if ( copy( $plugin_template_path, $theme_template_path ) ) { + return true; + } else { + return false; + } + } + + + function delete_email_template() { + $template = $_POST['email_key']; + + $in_theme = $this->template_in_theme( $template ); + if ( ! $in_theme ) { + wp_send_json_error( new \WP_Error( 'template_in_theme', __( 'Template does not exists in theme', 'ultimate-member' ) ) ); + } + + $theme_template_path = $this->get_template_file( 'theme', $template ); + + if ( unlink( $theme_template_path ) ) { + wp_send_json_success(); + } else { + wp_send_json_error( new \WP_Error( 'template_not_exists', __( 'Can not remove template from theme', 'ultimate-member' ) ) ); + } + } + + } } \ No newline at end of file