- fixed email templates for multisite;

- fixed upgrade script to 2.0.x version for multisites;
This commit is contained in:
nikitozzzzzzz
2018-06-07 01:01:42 +03:00
parent 1f2c65a658
commit d856188e60
7 changed files with 311 additions and 210 deletions
@@ -240,14 +240,6 @@ th.column-email.column-primary {
}
*/
.email_template_wrapper .reset_email_template {
display: none;
}
.email_template_wrapper.in_theme .reset_email_template {
display: block;
}
.um_setting_ajax_button_response.complete {
color:darkgreen;
font-style: italic;
@@ -1,42 +1,4 @@
jQuery( document ).ready( function() {
/**
* Email templates
*/
/*jQuery( 'body' ).on( 'click', '.copy_email_template', function() {
var obj = jQuery(this);
jQuery.ajax({
url: php_data.copy_email_template,
type: 'POST',
data: { email_key : obj.parents('.email_template_wrapper').data('key') },
success: function(data){
obj.parents('.email_template_wrapper').addClass('in_theme');
},
error: function(data){
alert('Something went wrong');
}
});
});*/
jQuery( 'body' ).on( 'click', '.reset_email_template', function() {
var obj = jQuery(this);
jQuery.ajax({
url: php_data.delete_email_template,
type: 'POST',
data: { email_key : obj.parents('.email_template_wrapper').data('key') },
success: function(data){
obj.parents('.email_template_wrapper').removeClass('in_theme');
},
error: function(data){
alert('Something went wrong');
}
});
});
/**
* Licenses
*/
@@ -156,7 +156,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Enqueue' ) ) {
wp_enqueue_script( 'um_admin_settings' );
$localize_data = array(
'delete_email_template' => UM()->get_ajax_route( 'um\core\Mail', 'delete_email_template' ),
'onbeforeunload_text' => __( 'Are sure, maybe some settings not saved', 'ultimate-member' ),
'texts' => array(
'remove' => __( 'Remove', 'ultimate-member' ),
@@ -1040,11 +1040,6 @@ if ( ! class_exists( 'um\admin\core\Admin_Forms' ) ) {
ob_start(); ?>
<div class="email_template_wrapper <?php echo $field_data['in_theme'] ? 'in_theme' : '' ?>" data-key="<?php echo $field_data['id'] ?>" style="position: relative;">
<!-- <input type="button" class="reset_email_template button" value="--><?php //_e( 'Reset Template to Default', 'ultimate-member' ) ?><!--" />-->
<!--<div class="copy_button_overlay">
<span><?php /*_e( 'Currently UM use default Email Template, you can edit this template after then you copy in to theme', 'ultimate-member' ) */?></span>
<input type="button" class="copy_email_template button" value="<?php /*_e( 'Copy Template to Theme', 'ultimate-member' ) */?>" />
</div>-->
<?php wp_editor( $value,
$id,
+4 -6
View File
@@ -1692,8 +1692,9 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
$email_key = empty( $_GET['email'] ) ? '' : urldecode( $_GET['email'] );
$emails = UM()->config()->email_notifications;
if ( empty( $email_key ) || empty( $emails[$email_key] ) )
if ( empty( $email_key ) || empty( $emails[ $email_key ] ) ) {
return $section;
}
$in_theme = UM()->mail()->template_in_theme( $email_key );
@@ -1722,7 +1723,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
array(
'id' => 'um_email_template',
'type' => 'hidden',
'value' => $email_key,
'value' => $email_key,
),
array(
'id' => $email_key . '_on',
@@ -2535,10 +2536,7 @@ Use Only Cookies: <?php echo ini_get( 'session.use_only_cookies' ) ?
$theme_template_path = UM()->mail()->get_template_file( 'theme', $template );
$in_theme = UM()->mail()->template_in_theme( $template );
if ( ! $in_theme ) {
UM()->mail()->copy_email_template( $template );
}
UM()->mail()->copy_email_template( $template );
$fp = fopen( $theme_template_path, "w" );
$result = fputs( $fp, $content );
@@ -1,53 +1,241 @@
<?php
/**
* Locate a template and return the path for inclusion.
*
* @access public
* @param string $template_name
* @param bool $html
* @return string
*/
function um_upgrade20beta1_template_in_theme( $template_name, $html = false ) {
$template_name_file = UM()->mail()->get_template_filename( $template_name );
$ext = ! $html ? '.php' : '.html';
$blog_id = '';
if ( ! $html ) {
$blog_id = UM()->mail()->get_blog_id();
}
// check if there is template at theme folder
$template = locate_template( array(
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name_file . $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 um_upgrade20beta1_get_template_file( $location, $template_name, $html = false ) {
$template_path = '';
$template_name_file = UM()->mail()->get_template_filename( $template_name );
$ext = ! $html ? '.php' : '.html';
switch( $location ) {
case 'theme':
$blog_id = '';
if ( ! $html ) {
$blog_id = UM()->mail()->get_blog_id();
}
$template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' . $blog_id ). $template_name_file . $ext;
break;
case 'plugin':
$path = ! empty( UM()->mail()->path_by_slug[ $template_name ] ) ? UM()->mail()->path_by_slug[ $template_name ] : um_path . 'templates/email';
$template_path = trailingslashit( $path ) . $template_name . $ext;
break;
}
return $template_path;
}
/**
* Ajax copy template to the theme
*
* @param string $template
* @return bool
*/
function um_upgrade20beta1_copy_email_template( $template ) {
$in_theme = um_upgrade20beta1_template_in_theme( $template );
if ( $in_theme ) {
return false;
}
$plugin_template_path = um_upgrade20beta1_get_template_file( 'plugin', $template );
$theme_template_path = um_upgrade20beta1_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 ( file_exists( $plugin_template_path ) && copy( $plugin_template_path, $theme_template_path ) ) {
return true;
} else {
return false;
}
}
/**
* Insert email template content to file
*
* @param string $path Filepath
* @param string $content Email template content
*/
function um_upgrade20beta1_insert_content( $path, $content ) {
$fp = @fopen( $path, "w" );
@fputs( $fp, $content );
@fclose( $fp );
}
/**
* Transferring email templates to new logic
*/
$templates_in_theme = 0;
function um_upgrade20beta1_email_templates_process() {
$templates_in_theme = 0;
$emails = UM()->config()->email_notifications;
foreach ( $emails as $email_key => $value ) {
$in_theme = um_upgrade20beta1_template_in_theme( $email_key, true );
$theme_template_path = um_upgrade20beta1_get_template_file( 'theme', $email_key );
if ( ! $in_theme ) {
//there isn't HTML email template's file in theme, get from option
//this value is correct for each multisite's subsites
$setting_value = UM()->options()->get( $email_key );
$html_email = UM()->options()->get( 'email_html' );
if ( $html_email ) {
if ( ! um_upgrade20beta1_copy_email_template( $email_key ) ) {
um_upgrade20beta1_insert_content( $theme_template_path, $setting_value );
} else {
$templates_in_theme++;
}
} else {
um_upgrade20beta1_insert_content( $theme_template_path, $setting_value );
}
} else {
//there is HTML email template in a theme's folder
$theme_template_path_html = um_upgrade20beta1_get_template_file( 'theme', $email_key, true );
$setting_value = preg_replace( '/<\/body>|<\/head>|<html>|<\/html>|<body.*?>|<head.*?>/' , '', file_get_contents( $theme_template_path_html ) );
if ( file_exists( $theme_template_path_html ) ) {
if ( copy( $theme_template_path_html, $theme_template_path ) ) {
um_upgrade20beta1_insert_content( $theme_template_path, $setting_value );
$templates_in_theme++;
}
}
}
}
$email_html = ( $templates_in_theme > 0 ) ? true : false;
UM()->options()->update( 'email_html', $email_html );
}
if ( is_multisite() ) {
$start_blog_id = get_current_blog_id();
$blog_ids = get_sites( array(
'fields' => 'ids',
) );
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
um_upgrade20beta1_email_templates_process();
}
restore_current_blog();
} else {
um_upgrade20beta1_email_templates_process();
}
/**
* Transferring email templates to new logic
*/
/*$templates_in_theme = 0;
$emails = UM()->config()->email_notifications;
foreach ( $emails as $email_key => $value ) {
$in_theme = UM()->mail()->template_in_theme( $email_key, true );
$theme_template_path = UM()->mail()->get_template_file( 'theme', $email_key );
$in_theme = um_upgrade20beta1_template_in_theme( $email_key, true );
$theme_template_path = um_upgrade20beta1_get_template_file( 'theme', $email_key );
if ( ! $in_theme ) {
//there isn't HTML email template's file in theme, get from option
//this value is correct for each multisite's subsites
$setting_value = UM()->options()->get( $email_key );
$html_email = UM()->options()->get( 'email_html' );
if ( $html_email ) {
if ( ! UM()->mail()->copy_email_template( $email_key ) ) {
$setting_value = UM()->options()->get( $email_key );
$fp = fopen( $theme_template_path, "w" );
$result = fputs( $fp, $setting_value );
fclose( $fp );
if ( ! um_upgrade20beta1_copy_email_template( $email_key ) ) {
um_upgrade20beta1_insert_content( $theme_template_path, $setting_value );
} else {
$templates_in_theme++;
}
} else {
$setting_value = UM()->options()->get( $email_key );
$fp = @fopen( $theme_template_path, "w" );
$result = @fputs( $fp, $setting_value );
fclose( $fp );
um_upgrade20beta1_insert_content( $theme_template_path, $setting_value );
}
} else {
$theme_template_path_html = UM()->mail()->get_template_file( 'theme', $email_key, true );
//there is HTML email template in a theme's folder
$theme_template_path_html = um_upgrade20beta1_get_template_file( 'theme', $email_key, true );
$setting_value = preg_replace( '/<\/body>|<\/head>|<html>|<\/html>|<body.*?>|<head.*?>/' , '', file_get_contents( $theme_template_path_html ) );
if ( file_exists( $theme_template_path_html ) ) {
if ( copy( $theme_template_path_html, $theme_template_path ) ) {
$fp = fopen( $theme_template_path, "w" );
$result = fputs( $fp, $setting_value );
fclose( $fp );
um_upgrade20beta1_insert_content( $theme_template_path, $setting_value );
$templates_in_theme++;
}
}
}
}
if ( $templates_in_theme > 0 ) {
UM()->options()->update( 'email_html', true );
} else {
UM()->options()->update( 'email_html', false );
}
$email_html = ( $templates_in_theme > 0 ) ? true : false;
UM()->options()->update( 'email_html', $email_html );*/
+97 -130
View File
@@ -36,6 +36,23 @@ if ( ! class_exists( 'um\core\Mail' ) ) {
}
/**
* Mandrill compatibility
*
* @param $nl2br
* @param string $message
* @return bool
*/
function mandrill_nl2br( $nl2br, $message = '' ) {
// text emails
if ( ! UM()->options()->get( 'email_html' ) ) {
$nl2br = true;
}
return $nl2br;
}
/**
* Init paths for email notifications
*/
@@ -66,72 +83,73 @@ if ( ! class_exists( 'um\core\Mail' ) ) {
/**
* Mandrill compatibility
* Check blog ID on multisite, return '' if single site
*
* @param $nl2br
* @param string $message
* @return bool
* @return string
*/
function mandrill_nl2br( $nl2br, $message = '' ) {
// text emails
if ( ! UM()->options()->get( 'email_html' ) ) {
$nl2br = true;
function get_blog_id() {
$blog_id = '';
if ( is_multisite() ) {
$blog_id = '/' . get_current_blog_id();
}
return $nl2br;
return $blog_id;
}
/**
* Send Email function
* Locate a template and return the path for inclusion.
*
* @param string $email
* @param null $template
* @param array $args
* @access public
* @param string $template_name
* @return string
*/
function send( $email, $template, $args = array() ) {
function locate_template( $template_name ) {
// check if there is template at theme folder
$blog_id = $this->get_blog_id();
if ( ! is_email( $email ) ) return;
if ( UM()->options()->get( $template . '_on' ) != 1 ) return;
//get template file from current blog ID folder
$template = locate_template( array(
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name . '.php'
) );
$this->attachments = null;
$this->headers = 'From: '. UM()->options()->get('mail_from') .' <'. UM()->options()->get('mail_from_addr') .'>' . "\r\n";
//if there isn't template at theme folder for current blog ID get template file from theme folder
if ( is_multisite() && ! $template ) {
$template = locate_template( array(
trailingslashit( 'ultimate-member/email' ) . $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 ) . $template_name . '.php';
}
// Return what we found.
/**
* UM hook
*
* @type filter
* @title um_email_send_subject
* @description Change email notification subject
* @title um_locate_email_template
* @description Change email notification template path
* @input_vars
* [{"var":"$subject","type":"string","desc":"Subject"},
* {"var":"$key","type":"string","desc":"Template Key"}]
* [{"var":"$template","type":"string","desc":"Template Path"},
* {"var":"$template_name","type":"string","desc":"Template Name"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_email_send_subject', 'function_name', 10, 2 ); ?>
* <?php add_filter( 'um_locate_email_template', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_email_send_subject', 'my_email_send_subject', 10, 2 );
* function my_email_send_subject( $subject, $key ) {
* add_filter( 'um_locate_email_template', 'my_locate_email_template', 10, 2 );
* function my_email_template_body_attrs( $template, $template_name ) {
* // your code here
* return $paths;
* return $template;
* }
* ?>
*/
$subject = apply_filters( 'um_email_send_subject', UM()->options()->get( $template . '_sub' ), $template );
$this->subject = um_convert_tags( $subject , $args );
$this->message = $this->prepare_template( $template, $args );
if ( UM()->options()->get( 'email_html' ) ) {
$this->headers .= "Content-Type: text/html\r\n";
} else {
$this->headers .= "Content-Type: text/plain\r\n";
}
// Send mail
wp_mail( $email, $this->subject, $this->message, $this->headers, $this->attachments );
return apply_filters( 'um_locate_email_template', $template, $template_name );
}
@@ -313,7 +331,7 @@ if ( ! class_exists( 'um\core\Mail' ) ) {
<body <?php echo $body_attrs ?>>
<?php echo $this->get_email_template( $slug, $args ); ?>
<?php echo $this->get_email_template( $slug, $args ); ?>
</body>
</html>
@@ -358,58 +376,55 @@ if ( ! class_exists( 'um\core\Mail' ) ) {
/**
* Locate a template and return the path for inclusion.
* Send Email function
*
* @access public
* @param string $template_name
* @return string
* @param string $email
* @param null $template
* @param array $args
*/
function locate_template( $template_name ) {
// check if there is template at theme folder
$blog_id = $this->get_blog_id();
function send( $email, $template, $args = array() ) {
//get template file from current blog ID folder
$template = locate_template( array(
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name . '.php'
) );
if ( ! is_email( $email ) ) return;
if ( UM()->options()->get( $template . '_on' ) != 1 ) return;
//if there isn't template at theme folder for current blog ID get template file from theme folder
if ( is_multisite() && ! $template ) {
$template = locate_template( array(
trailingslashit( 'ultimate-member/email' ) . $template_name . '.php'
) );
}
$this->attachments = null;
$this->headers = 'From: '. UM()->options()->get('mail_from') .' <'. UM()->options()->get('mail_from_addr') .'>' . "\r\n";
//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 ) . $template_name . '.php';
}
// Return what we found.
/**
* UM hook
*
* @type filter
* @title um_locate_email_template
* @description Change email notification template path
* @title um_email_send_subject
* @description Change email notification subject
* @input_vars
* [{"var":"$template","type":"string","desc":"Template Path"},
* {"var":"$template_name","type":"string","desc":"Template Name"}]
* [{"var":"$subject","type":"string","desc":"Subject"},
* {"var":"$key","type":"string","desc":"Template Key"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_locate_email_template', 'function_name', 10, 2 ); ?>
* <?php add_filter( 'um_email_send_subject', 'function_name', 10, 2 ); ?>
* @example
* <?php
* add_filter( 'um_locate_email_template', 'my_locate_email_template', 10, 2 );
* function my_email_template_body_attrs( $template, $template_name ) {
* add_filter( 'um_email_send_subject', 'my_email_send_subject', 10, 2 );
* function my_email_send_subject( $subject, $key ) {
* // your code here
* return $template;
* return $paths;
* }
* ?>
*/
return apply_filters( 'um_locate_email_template', $template, $template_name );
$subject = apply_filters( 'um_email_send_subject', UM()->options()->get( $template . '_sub' ), $template );
$this->subject = um_convert_tags( $subject , $args );
$this->message = $this->prepare_template( $template, $args );
if ( UM()->options()->get( 'email_html' ) ) {
$this->headers .= "Content-Type: text/html\r\n";
} else {
$this->headers .= "Content-Type: text/plain\r\n";
}
// Send mail
wp_mail( $email, $this->subject, $this->message, $this->headers, $this->attachments );
}
@@ -449,72 +464,47 @@ if ( ! class_exists( 'um\core\Mail' ) ) {
*
* @access public
* @param string $template_name
* @param bool $html
* @return string
*/
function template_in_theme( $template_name, $html = false ) {
function template_in_theme( $template_name ) {
$template_name_file = $this->get_template_filename( $template_name );
$ext = ! $html ? '.php' : '.html';
$blog_id = $this->get_blog_id();
// check if there is template at theme blog ID folder
$template = locate_template( array(
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name_file . $ext
trailingslashit( 'ultimate-member/email' . $blog_id ) . $template_name_file . '.php'
) );
//if there isn't template at theme folder for current blog ID get template file from theme folder
if ( is_multisite() && ! $template ) {
$template = locate_template( array(
trailingslashit( 'ultimate-member/email' ) . $template_name_file . $ext
) );
}
// Return what we found.
return ! $template ? false : true;
}
/**
* Check blog ID on multisite, return '' if single site
*
* @return string
*/
function get_blog_id() {
$blog_id = '';
if ( is_multisite() ) {
$blog_id = '/' . get_current_blog_id();
}
return $blog_id;
}
/**
* 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 ) {
function get_template_file( $location, $template_name ) {
$template_path = '';
$template_name_file = $this->get_template_filename( $template_name );
$ext = ! $html ? '.php' : '.html';
switch( $location ) {
case 'theme':
//save email template in blog ID folder if we use multisite
$blog_id = $this->get_blog_id();
$template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' . $blog_id ). $template_name_file . $ext;
$template_path = trailingslashit( get_stylesheet_directory() . '/ultimate-member/email' . $blog_id ). $template_name_file . '.php';
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;
$template_path = trailingslashit( $path ) . $template_name . '.php';
break;
}
@@ -525,10 +515,10 @@ if ( ! class_exists( 'um\core\Mail' ) ) {
/**
* Ajax copy template to the theme
*
* @param bool $template
* @param string $template
* @return bool
*/
function copy_email_template( $template = false ) {
function copy_email_template( $template ) {
$in_theme = $this->template_in_theme( $template );
if ( $in_theme ) {
@@ -559,28 +549,5 @@ if ( ! class_exists( 'um\core\Mail' ) ) {
return false;
}
}
/**
* Delete Email Notification Template
*
* @deprecated since 2.0.17
*/
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' ) ) );
}
}
}
}