Skip to content

Commit

Permalink
Merge pull request #3221 from MaximilianoRicoTabo/enhancement/members…
Browse files Browse the repository at this point in the history
…hip-expiring

[ENHANCEMENT] * Add Membership expiring email class
  • Loading branch information
dparker1005 authored Dec 18, 2024
2 parents 8bdff4b + 24dc95a commit d0c9a62
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 39 deletions.
32 changes: 5 additions & 27 deletions classes/class.pmproemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,36 +916,14 @@ function sendMembershipExpiringEmail( $user = NULL, $membership_id = NULL )

if(!$user)
return false;

if ( empty( $membership_id ) ) {
$membership_level = pmpro_getMembershipLevelForUser($user->ID);
} else {
$membership_level = pmpro_getSpecificMembershipLevelForUser($user->ID, $membership_id);
$membership_level = pmpro_getMembershipLevelForUser( $user->ID );
$membership_id = $membership_level->id;
}

$this->email = $user->user_email;
$this->subject = sprintf(__("Your membership at %s will end soon", "paid-memberships-pro"), get_option("blogname"));

$this->data = array(
'subject' => $this->subject,
'header_name' => $user->display_name,
'name' => $user->display_name,
'user_login' => $user->user_login,
'sitename' => get_option('blogname'),
'membership_id' => $membership_level->id,
'membership_level_name' => $membership_level->name,
'siteemail' => get_option('pmpro_from_email'),
'login_link' => pmpro_login_url(),
'login_url' => pmpro_login_url(),
'enddate' => date_i18n(get_option('date_format'), $membership_level->enddate),
'display_name' => $user->display_name,
'user_email' => $user->user_email,
'levels_url' => pmpro_url( 'levels' )
);

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

return $this->sendEmail();
$email = new PMPro_Email_Template_Membership_Expiring( $user, $membership_id );
$email->send();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
class PMPro_Email_Template_Membership_Expiring 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_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_id ) {
$this->user = $user;
$this->membership_id = $membership_id;
}

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

/**
* 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 Expiring', '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 expiration date is approaching, at an interval based on the term of the membership.', '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!! will end soon", '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>Thank you for your membership to !!sitename!!. This is just a reminder that your membership will end on !!enddate!!.</p>
<p>Account: !!display_name!! (!!user_email!!)</p>
<p>Membership Level: !!membership_level_name!!</p>
<p>Log in to your membership 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' ),
'!!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' ),
'!!enddate!!' => __( 'The expiration date of the membership level.', '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",
$this->user->ID
)
);

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

// Get the membership level object.
if ( empty( $membership_id ) ) {
$membership_level = pmpro_getMembershipLevelForUser($this->user->ID);
} else {
$membership_level = pmpro_getSpecificMembershipLevelForUser($this->user->ID, $membership_id);
}

return array(
"subject" => $this->get_default_subject(),
"name" => $this->user->display_name,
"user_login" => $this->user->user_login,
"display_name" => $this->user->display_name,
"user_email" => $this->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' ) . ']',
"enddate" => date_i18n( get_option('date_format'), $membership_level->enddate ),
);
}
}

/**
* 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_expiring( $email_templates ) {
$email_templates['membership_expiring'] = 'PMPro_Email_Template_Membership_Expiring';
return $email_templates;
}
add_filter( 'pmpro_email_templates', 'pmpro_email_templates_membership_expiring' );
11 changes: 0 additions & 11 deletions includes/email-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,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_expiring' => array(
'subject' => __( "Your membership at !!sitename!! will end soon", 'paid-memberships-pro' ),
'description' => __('Membership Expiring', 'paid-memberships-pro'),
'body' => __( '<p>Thank you for your membership to !!sitename!!. This is just a reminder that your membership will end on !!enddate!!.</p>
<p>Account: !!display_name!! (!!user_email!!)</p>
<p>Membership Level: !!membership_level_name!!</p>
<p>Log in to your membership account here: !!login_url!!</p>', 'paid-memberships-pro' ),
'help_text' => __( 'This email is sent to the member when their expiration date is approaching, at an interval based on the term of the membership.', 'paid-memberships-pro' )
),
'refund' => array(
'subject' => __( 'Order #!!order_id!! at !!sitename!! has been REFUNDED', 'paid-memberships-pro' ),
'description' => __('Refund', '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 @@ -369,7 +369,7 @@ function pmpro_email_templates_send_test() {
break;
case 'membership_expiring';
$send_email = 'sendMembershipExpiringEmail';
$params = array($test_user);
$params = array( $test_user, $test_order->membership_id );
break;
case 'payment_action':
$send_email = 'sendPaymentActionRequiredEmail';
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-expiring.php' ); // expiring email template
require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-membership-expired.php' ); // change email template
require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-credit-card-expiring.php' ); // credit card expiring email template
require_once( PMPRO_DIR . '/classes/email-templates/class-pmpro-email-template-checkout-check.php' );
Expand Down

0 comments on commit d0c9a62

Please sign in to comment.