Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*add expired membership email class #3220

Open
wants to merge 1 commit into
base: enhancement/email-template-abstraction
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 3 additions & 45 deletions classes/class.pmproemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ function sendTrialEndingEmail( $user = NULL, $membership_id = NULL ) {
* @since 3.1
*/
function sendMembershipExpiredEmail( $user = NULL, $membership_id = NULL ) {
global $current_user, $wpdb;
global $current_user;
if( !$user ) {
$user = $current_user;
}
Expand All @@ -1287,51 +1287,9 @@ function sendMembershipExpiredEmail( $user = NULL, $membership_id = NULL ) {
return false;
}

// If we don't have a level ID, query the user's most recently expired level from the database.
if ( empty( $membership_id ) ) {
$membership_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT membership_id FROM $wpdb->pmpro_memberships_users
WHERE user_id = %d
AND status = 'expired'
ORDER BY enddate DESC
LIMIT 1",
$user->ID
)
);

// If we still don't have a level ID, bail.
if ( empty( $membership_id ) ) {
$membership_id = 0;
}
}

// Get the membership level object.
$membership_level = pmpro_getLevel( $membership_id );

$this->email = $user->user_email;
$this->subject = sprintf( __("Your membership at %s has ended", "paid-memberships-pro"), get_option( "blogname" ) );
$email = new PMPro_Email_Template_Membership_Expired( $user, $membership_id );

$this->data = array(
"subject" => $this->subject,
"name" => $user->display_name,
"user_login" => $user->user_login,
"header_name" => $user->display_name,
"sitename" => get_option("blogname"),
"siteemail" => get_option("pmpro_from_email"),
"login_link" => pmpro_login_url(),
"login_url" => pmpro_login_url(),
"display_name" => $user->display_name,
"user_email" => $user->user_email,
"levels_link" => pmpro_url("levels"),
"levels_url" => pmpro_url("levels"),
"membership_id" => $membership_id,
"membership_level_name" => ( ! empty( $membership_level ) && ! empty( $membership_level->name ) ) ? $membership_level->name : '[' . esc_html( 'deleted', 'paid-memberships-pro' ) . ']',
);

$this->template = apply_filters("pmpro_email_template", "membership_expired", $this);

return $this->sendEmail();
$email->send();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
class PMPro_Email_Template_Membership_Expired extends PMPro_Email_Template {

/**
* The user object of the user to send the email to.
*
* @var WP_User
*/
protected $user;

/**
* The membership level that expired.
*
* @var int
*/
protected $membership_level_id;

/**
* Constructor.
*
* @since TBD
*
* @param WP_User $user The user object of the user to send the email to.
* @param int $membership_id The membership level id of the membership level that expired.
*/
public function __construct( WP_User $user, int $membership_level_id ) {
$this->user = $user;
$this->membership_level_id = $membership_level_id;
}

/**
* Get the email template slug.
*
* @since TBD
*
* @return string The email template slug.
*/
public static function get_template_slug() {
return 'membership_expired';
}

/**
* Get the "nice name" of the email template.
*
* @since TBD
*
* @return string The "nice name" of the email template.
*/
public static function get_template_name() {
return __( 'Membership Expired', 'paid-memberships-pro' );
}

/**
* Get "help text" to display to the admin when editing the email template.
*
* @since TBD
*
* @return string The "help text" to display to the admin when editing the email template.
*/
public static function get_template_description() {
return __( 'This email is sent to the member when their membership expires.', 'paid-memberships-pro' );
}

/**
* Get the default subject for the email.
*
* @since TBD
*
* @return string The default subject for the email.
*/
public static function get_default_subject() {
return __( 'Your membership at !!sitename!! has ended', 'paid-memberships-pro' );
}

/**
* Get the default body content for the email.
*
* @since TBD
*
* @return string The default body content for the email.
*/
public static function get_default_body() {
return __( '<p>Your membership at !!sitename!! has ended.</p>

<p>Thank you for your support.</p>

<p>View our current membership offerings here: !!levels_url!!</p>

<p>Log in to manage your account here: !!login_url!!</p>', 'paid-memberships-pro' );
}

/**
* Get the email address to send the email to.
*
* @since TBD
*
* @return string The email address to send the email to.
*/
public function get_recipient_email() {
return $this->user->user_email;
}

/**
* Get the name of the email recipient.
*
* @since TBD
*
* @return string The name of the email recipient.
*/
public function get_recipient_name() {
return $this->user->display_name;
}


/**
* Get the email template variables for the email paired with a description of the variable.
*
* @since TBD
*
* @return array The email template variables for the email (key => value pairs).
*/
public static function get_email_template_variables_with_description() {
return array(
'!!subject!!' => __( 'The default subject for the email. This will be removed in a future version.', 'paid-memberships-pro' ),
'!!header_name!!' => __( 'The name of the email recipient.', 'paid-memberships-pro' ),
'!!name!!' => __( 'The display name of the user.', 'paid-memberships-pro' ),
'!!membership_id!!' => __( 'The ID of the membership level.', 'paid-memberships-pro' ),
'!!membership_level_name!!' => __( 'The name of the membership level.', 'paid-memberships-pro' ),
'!!user_login!!' => __( 'The username of the user.', 'paid-memberships-pro' ),
'!!user_email!!' => __( 'The email address of the user.', 'paid-memberships-pro' ),
'!!display_name!!' => __( 'The display name of the user.', 'paid-memberships-pro' ),
'!!levels_link!!' => __( 'The URL of the page where users can view available membership levels.', 'paid-memberships-pro' ),
);
}

/**
* Get the email template variables for the email.
*
* @since TBD
*
* @return array The email template variables for the email (key => value pairs).
*/
public function get_email_template_variables() {
global $wpdb;
// If we don't have a level ID, query the user's most recently expired level from the database.
if ( empty( $this->$membership_id ) ) {
$membership_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT membership_id FROM $wpdb->pmpro_memberships_users
WHERE user_id = %d
AND status = 'expired'
ORDER BY enddate DESC
LIMIT 1",
$user->ID
)
);

// If we still don't have a level ID, bail.
if ( empty( $membership_id ) ) {
$membership_id = 0;
}
}

// Get the membership level object.
$membership_level = pmpro_getLevel( $membership_id );

return array(
"subject" => $this->subject,
"name" => $user->display_name,
"user_login" => $user->user_login,
"header_name" => $user->display_name,
"display_name" => $user->display_name,
"user_email" => $user->user_email,
"levels_link" => pmpro_url("levels"),
"membership_id" => ( ! empty( $membership_level ) && ! empty( $membership_level->id ) ) ? $membership_level->id : 0,
"membership_level_name" => ( ! empty( $membership_level ) && ! empty( $membership_level->name ) ) ? $membership_level->name : '[' . esc_html( 'deleted', 'paid-memberships-pro' ) . ']',
);
}
}

/**
* Register the email template.
*
* @since TBD
*
* @param array $email_templates The email templates (template slug => email template class name)
* @return array The modified email templates array.
*/
function pmpro_email_templates_membership_expired( $email_templates ) {
$email_templates['membership_expired'] = 'PMPro_Email_Template_Membership_Expired';
return $email_templates;
}
add_filter( 'pmpro_email_templates', 'pmpro_email_templates_membership_expired' );
12 changes: 0 additions & 12 deletions includes/email-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,6 @@
<p>To view an online version of this order, click here: !!order_url!!</p>', 'paid-memberships-pro' ),
'help_text' => __( 'This email is sent to the member each time a new subscription payment is made.', 'paid-memberships-pro' )
),
'membership_expired' => array(
'subject' => __( "Your membership at !!sitename!! has ended", 'paid-memberships-pro' ),
'description' => __('Membership Expired', 'paid-memberships-pro'),
'body' => __( '<p>Your membership at !!sitename!! has ended.</p>

<p>Thank you for your support.</p>

<p>View our current membership offerings here: !!levels_url!!</p>

<p>Log in to manage your account here: !!login_url!!</p>', 'paid-memberships-pro' ),
'help_text' => __( 'This email is sent to the member when their membership expires.', 'paid-memberships-pro' )
),
'membership_expiring' => array(
'subject' => __( "Your membership at !!sitename!! will end soon", 'paid-memberships-pro' ),
'description' => __('Membership Expiring', 'paid-memberships-pro'),
Expand Down
2 changes: 1 addition & 1 deletion includes/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function pmpro_email_templates_send_test() {
break;
case 'membership_expired';
$send_email = 'sendMembershipExpiredEmail';
$params = array($test_user);
$params = array($test_user, $test_order->membership_id );
break;
case 'membership_expiring';
$send_email = 'sendMembershipExpiringEmail';
Expand Down
1 change: 1 addition & 0 deletions paid-memberships-pro.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-cancel-admin.php' ); // cancel email template
require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-admin-change.php' ); // change email template
require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-admin-change-admin.php' ); // change email template
require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-membership-expired.php' ); // change email template
require_once( PMPRO_DIR . '/includes/filters.php' ); // filters, hacks, etc, moved into the plugin
require_once( PMPRO_DIR . '/includes/reports.php' ); // load reports for admin (reports may also include tracking code, etc)

Expand Down