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 payment failure classes #3197

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
105 changes: 9 additions & 96 deletions classes/class.pmproemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -946,62 +946,14 @@ function sendBillingFailureEmail( $user = NULL, $order = NULL ) {
global $current_user;
if(!$user)
$user = $current_user;

if(!$user || !$order)
return false;

//get Level from constructor
$membership_level = new PMPro_Membership_Level( $order->membership_id );

// Try to get the subscription ID.
$subscription = $order->get_subscription();
$subscription_id = ! empty( $subscription ) ? $subscription->get_id() : null;

$this->email = $user->user_email;
$this->subject = sprintf( __("Membership payment for level %s failed at %s", "paid-memberships-pro"),
$membership_level->name, 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' ),
'siteemail' => get_option( 'pmpro_from_email' ),
'membership_id' => $membership_level->id,
'membership_level_name' => $membership_level->name,
'display_name' => $user->display_name,
'user_email' => $user->user_email,
'billing_name' => $order->billing->name,
'billing_street' => $order->billing->street,
'billing_street2' => $order->billing->street2,
'billing_city' => $order->billing->city,
'billing_state' => $order->billing->state,
'billing_zip' => $order->billing->zip,
'billing_country' => $order->billing->country,
'billing_phone' => $order->billing->phone,
'cardtype' => $order->cardtype,
'accountnumber' => hideCardNumber($order->accountnumber),
'expirationmonth' => $order->expirationmonth,
'expirationyear' => $order->expirationyear,
'login_link' => pmpro_login_url( pmpro_url( 'billing', empty( $subscription_id ) ? '' : '?subscription_id=' . $subscription_id ) ),
'login_url' => pmpro_login_url( pmpro_url( 'billing', empty( $subscription_id ) ? '' : '?subscription_id=' . $subscription_id ) ),
'levels_url' => pmpro_url( 'levels' )
);
$this->data["billing_address"] = pmpro_formatAddress($order->billing->name,
$order->billing->street,
$order->billing->street2,
$order->billing->city,
$order->billing->state,
$order->billing->zip,
$order->billing->country,
$order->billing->phone);

$this->template = apply_filters("pmpro_email_template", "billing_failure", $this);
$email = new PMPro_Email_Template_Billing_Failure( $user, $order );
$email->send();
}

return $this->sendEmail();
}

/**
* Send the admin an email when their recurring payment has failed.
*
Expand All @@ -1011,52 +963,13 @@ function sendBillingFailureEmail( $user = NULL, $order = NULL ) {
function sendBillingFailureAdminEmail($email, $order = NULL) {
if(!$order)
return false;

$user = get_userdata($order->user_id);
$membership_level = new PMPro_Membership_Level( $order->membership_id );

$this->email = $email;
$this->subject = sprintf(__("Membership payment failed For %s at %s", "paid-memberships-pro"), $user->display_name, get_option("blogname"));

$this->data = array(
'subject' => $this->subject,
'header_name' => $this->get_admin_name( $email ),
'name' => 'Admin',
'user_login' => $user->user_login,
'sitename' => get_option( 'blogname' ),
'siteemail' => get_option( 'pmpro_from_email' ),
'membership_id' => $membership_level->id,
'membership_level_name' => $membership_level->name,
'display_name' => $user->display_name,
'user_email' => $user->user_email,
'billing_name' => $order->billing->name,
'billing_street' => $order->billing->street,
'billing_street2' => $order->billing->street2,
'billing_city' => $order->billing->city,
'billing_state' => $order->billing->state,
'billing_zip' => $order->billing->zip,
'billing_country' => $order->billing->country,
'billing_phone' => $order->billing->phone,
'cardtype' => $order->cardtype,
'accountnumber' => hideCardNumber($order->accountnumber),
'expirationmonth' => $order->expirationmonth,
'expirationyear' => $order->expirationyear,
'login_link' => pmpro_login_url( get_edit_user_link( $user->ID ) ),
'login_url' => pmpro_login_url( get_edit_user_link( $user->ID ) ),
'levels_url' => pmpro_url( 'levels' ),
);
$this->data["billing_address"] = pmpro_formatAddress($order->billing->name,
$order->billing->street,
$order->billing->street2,
$order->billing->city,
$order->billing->state,
$order->billing->zip,
$order->billing->country,
$order->billing->phone);

$this->template = apply_filters("pmpro_email_template", "billing_failure_admin", $this);
$user = get_userdata( $order->user_id );
if(!$user)
return false;

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
<?php

class PMPro_Email_Template_Billing_Failure_Admin extends PMPro_Email_Template {

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

/**
* The {@link MemberOrder} object of the order that was updated.
*
* @var MemberOrder
*/
protected $order;

/**
* Constructor.
*
* @since TBD
*
* @param WP_User $user The user object of the user to send the email to.
* @param MemberOrder $order The order object that is associated to the member.
*/
public function __construct( WP_User $user, MemberOrder $order ) {
$this->user = $user;
$this->order = $order;
}

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

/**
* 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 __( 'Payment Failure (admin)', 'paid-memberships-pro' );
}

/**
* Get the email template description.
*
* @since TBD
*
* @return string The email template description.
*/
public static function get_template_description() {
return __( 'This email is sent to the site admin when a member\'s payment fails.', 'paid-memberships-pro' );
}

/**
* Get the email subject.
*
* @since TBD
*
* @return string The email subject.
*/
public static function get_default_subject() {
return __( "Membership payment failed for !!display_name!! at !!sitename!!", 'paid-memberships-pro' );
}

/**
* Get the email body.
*
* @since TBD
*
* @return string The email body.
*/
public static function get_default_body() {
return __( '<p>The subscription payment for !!user_login!! for level !!membership_level_name!! at !!sitename!! has failed.</p>
<p>Account: !!display_name!! (!!user_email!!)</p>
<p>Membership Level: !!membership_level_name!!</p>

<p>Log in to your WordPress admin 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 get_bloginfo( 'admin_email' );
}

/**
* Get the name of the email recipient.
*
* @since TBD
*
* @return string The name of the email recipient.
*/
public function get_recipient_name() {
//get user by email
$user = get_user_by( 'email', $this->get_recipient_email() );
return $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' ),
'!!user_login!!' => __( 'The username of the user.', 'paid-memberships-pro' ),
'!!user_email!!' => __( 'The email address of the user billing failed', 'paid-memberships-pro' ),
'!!display_name!!' => __( 'The display name of the user billing failed', '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' ),
'!!billing_name!!' => __( 'Billing Info Name', 'paid-memberships-pro' ),
'!!billing_street!!' => __( 'Billing Info Street', 'paid-memberships-pro' ),
'!!billing_street2!!' => __( 'Billing Info Street 2', 'paid-memberships-pro' ),
'!!billing_city!!' => __( 'Billing Info City', 'paid-memberships-pro' ),
'!!billing_state!!' => __( 'Billing Info State', 'paid-memberships-pro' ),
'!!billing_zip!!' => __( 'Billing Info Zip', 'paid-memberships-pro' ),
'!!billing_country!!' => __( 'Billing Info Country', 'paid-memberships-pro' ),
'!!billing_phone!!' => __( 'Billing Info Phone', 'paid-memberships-pro' ),
'!!billing_address!!' => __( 'Billing Info Complete Address', 'paid-memberships-pro' ),
'!!cardtype!!' => __( 'Credit Card Type', 'paid-memberships-pro' ),
'!!accountnumber!!' => __( 'Credit Card Number (last 4 digits)', 'paid-memberships-pro' ),
'!!expirationmonth!!' => __( 'Credit Card Expiration Month (mm format)', 'paid-memberships-pro' ),
'!!expirationyear!!' => __( 'Credit Card Expiration Year (yyyy format)', '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() {
$order = $this->order;
$user = $this->user;
$membership_level = pmpro_getLevel( $order->membership_id );
return array(
'subject' => $this->get_default_subject(),
'header_name' => $user->display_name,
'name' => $user->display_name,
'user_login' => $user->user_login,
'membership_id' => $membership_level->id,
'membership_level_name' => $membership_level->name,
'display_name' => $user->display_name,
'user_email' => $user->user_email,
'billing_name' => $order->billing->name,
'billing_street' => $order->billing->street,
'billing_street2' => $order->billing->street2,
'billing_city' => $order->billing->city,
'billing_state' => $order->billing->state,
'billing_zip' => $order->billing->zip,
'billing_country' => $order->billing->country,
'billing_phone' => $order->billing->phone,
'billing_address'=> pmpro_formatAddress( $order->billing->name,
$order->billing->street,
$order->billing->street2,
$order->billing->city,
$order->billing->state,
$order->billing->zip,
$order->billing->country,
$order->billing->phone ),
'cardtype' => $order->cardtype,
'accountnumber' => hideCardNumber( $order->accountnumber ),
'expirationmonth' => $order->expirationmonth,
'expirationyear' => $order->expirationyear,
);
}
}

/**
* 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_billing_failure_admin( $email_templates ) {
$email_templates['billing_failure_admin'] = 'PMPro_Email_Template_Billing_Failure_Admin';
return $email_templates;
}
add_filter( 'pmpro_email_templates', 'pmpro_email_templates_billing_failure_admin' );


Loading