Last active
June 23, 2018 15:16
-
-
Save wpmudev-sls/4779e0ae29e07f89dd8ad62345368229 to your computer and use it in GitHub Desktop.
[Membership] - Resend welcome e-mail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Membership] - Resend welcome e-mail | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Allows to resend the Welcome email | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Resend_Welcome_Email' ) ) { | |
class WPMUDEV_MS_Resend_Welcome_Email { | |
private static $_instance = null; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_MS_Resend_Welcome_Email(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'wp_ajax_wpmudev_ms_resend_welcome_email', array( $this, 'resend_welcome_email' ), 20 ); | |
add_action( 'admin_footer', array( $this, 'admin_footer' ), 20 ); | |
add_filter( 'ms_view_member_editor_fields_edit', array( $this, 'filter_member_fields' ), 20 ); | |
add_action( 'show_user_profile', array( $this, 'additional_profile_fields' ) ); | |
add_action( 'edit_user_profile', array( $this, 'additional_profile_fields' ) ); | |
} | |
public function resend_welcome_email() { | |
check_ajax_referer( 'wpmudev_ms_resend_welcome_email', 'security' ); | |
$return = array( | |
'success' => false, | |
); | |
$user_id = isset( $_POST['user_id'] ) ? (int) $_POST['user_id'] : null; | |
if( ! is_null( $user_id ) ){ | |
// Since we will update user pass, WP will attempt to send a notification about pass change to user. Lets not send | |
add_filter( 'send_password_change_email', '__return_false', 50 ); | |
$member = MS_Factory::load( 'MS_Model_Member', $user_id ); | |
$comm = MS_Model_Communication::get_communication( MS_Model_Communication::COMM_TYPE_SIGNUP ); | |
$comm_vars = $comm->get_comm_vars( null, $member ); | |
$user_password = wp_generate_password(); | |
$comm_vars['%password%'] = $user_password; | |
if ( ! wp_update_user( array( 'ID' => $user_id, 'user_pass' => $user_password ) ) ) { | |
wp_send_json( $return ); | |
} | |
$headers = array(); | |
$message = str_replace( | |
array_keys( $comm_vars ), | |
array_values( $comm_vars ), | |
stripslashes( $comm->message ) | |
); | |
$subject = str_replace( | |
array_keys( $comm_vars ), | |
array_values( $comm_vars ), | |
stripslashes( $comm->subject ) | |
); | |
$subject = strip_tags( $subject ); | |
$content_type = $comm->get_mail_content_type(); | |
$headers[] = sprintf( | |
'Content-Type: %s; charset="UTF-8"', | |
$content_type | |
); | |
// Pre-process the message according to content-type. | |
if ( 'text/html' == $content_type ) { | |
$message = wpautop( $message ); | |
$message = make_clickable( $message ); | |
} else { | |
$message = preg_replace( | |
'/\<a .*?href=["\'](.*?)["\'].*?\>(.*?)\<\/a\>/is', | |
'$2 [$1]', | |
$message | |
); | |
$message = strip_tags( $message ); | |
} | |
$sent = wp_mail( $member->email, $subject, $message, $headers ); | |
if ( $sent ) { | |
$return = array( | |
'success' => true, | |
); | |
} | |
} | |
wp_send_json($return); | |
} | |
public function admin_footer() { | |
$screen = get_current_screen(); | |
if( ! isset( $screen->base ) || | |
( 'membership-2_page_membership2-add-member' != $screen->base && 'user-edit' != $screen->base ) | |
) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(function(d,$){ | |
$(d).ready(function(){ | |
$('#resend_welcome_email').on('click', function(e){ | |
let me = $(this), | |
user_id = me.data('member'), | |
conf = confirm("Are you sure you want to resend the Weclome email?\n\nThis will create a new password for the user. The new password will be included in the email if it contains `%password%` in it's content"); | |
e.preventDefault(); | |
if ( ! conf ) { | |
return; | |
} | |
let data = { | |
action: 'wpmudev_ms_resend_welcome_email', | |
security: '<?php echo wp_create_nonce( "wpmudev_ms_resend_welcome_email" ); ?>', | |
user_id: user_id | |
}; | |
$.post(ajaxurl, data, function(response) { | |
if( response.success ){ | |
alert( 'Welcome message has been sent! A new Password has been created for user' ); | |
} | |
else{ | |
alert( 'Error in request' ); | |
} | |
}); | |
}); | |
}); | |
})(document,jQuery); | |
</script> | |
<?php | |
} | |
public function additional_profile_fields( $user ) { | |
?> | |
<h3>Resend Welcome Email</h3> | |
<a id="resend_welcome_email" title="Resend Welcome email" class="wpmui-link button wpmui-field-input ms=welcome-email-resend" href="" target="_self" data-member="417"><i class="dashicons dashicons-email"></i>Resend Welcome email</a> | |
<?php | |
} | |
public function filter_member_fields( $fields ) { | |
foreach ( $fields as $field_key => $field_items ) { | |
// We are targeting the subscription fields not member fields | |
if( isset( $field_items['username'] ) ) { | |
continue; | |
} | |
$member_id = (int) $_GET['user_id']; | |
$new_field_item = array( | |
'id' => 'resend_welcome_email', | |
'type' => 'html_link', | |
'value' => '<i class="dashicons dashicons-email"></i>Resend Welcome email', | |
'class' => 'button wpmui-field-input ms=welcome-email-resend', | |
'config' => array( | |
'data-member' => $member_id | |
) | |
); | |
$fields[$field_key][] = $new_field_item; | |
} | |
return $fields; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_ms_resend_welcome_email' ) ) { | |
function wpmudev_ms_resend_welcome_email() { | |
return WPMUDEV_MS_Resend_Welcome_Email::get_instance(); | |
} | |
add_action( 'plugins_loaded', 'wpmudev_ms_resend_welcome_email', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds a "Resend Welcome Email" in the Edit Member page and in Edit User.
It regenerates user password
It includes the password in the email if the email contains
%password%
Requires the Additional Automated Messages addon