Skip to content

Commit

Permalink
* Add Payment Email Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianoRicoTabo committed Dec 8, 2024
1 parent fb6611e commit e71bc27
Show file tree
Hide file tree
Showing 9 changed files with 814 additions and 184 deletions.
155 changes: 34 additions & 121 deletions classes/class.pmproemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,84 +1134,21 @@ function sendCreditCardExpiringEmail($user = NULL, $order = NULL) {
* @param object $user The WordPress user object.
* @param MemberOrder $order The order object that is associated to the member.
*/
function sendInvoiceEmail($user = NULL, $order = NULL)
{
function sendInvoiceEmail( $user = NULL, $order = NULL ) {
global $wpdb, $current_user;
if(!$user)
$user = $current_user;

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

//Bail if no membership level in the order
if ( empty( $order->membership_id ) ) {
return false;
}

$membership_level = pmpro_getSpecificMembershipLevelForUser($user->ID, $order->membership_id);

$this->email = $user->user_email;
$this->subject = sprintf(__("Order for %s membership", "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' ),
'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,
'order_id' => $order->code,
'order_total' => $order->get_formatted_total(),
'order_date' => date_i18n( get_option( 'date_format' ), $order->getTimestamp() ),
'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(),
'login_url' => pmpro_login_url(),
'order_link' => pmpro_login_url( pmpro_url( 'invoice', '?invoice=' . $order->code ) ),
'order_url' => pmpro_login_url( pmpro_url( 'invoice', '?invoice=' . $order->code ) ),
'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);

if($order->getDiscountCode()) {
if(!empty($order->discount_code->code))
$this->data["discount_code"] = "<p>" . __("Discount Code", 'paid-memberships-pro' ) . ": " . $order->discount_code->code . "</p>\n";
else
$this->data["discount_code"] = "<p>" . __("Discount Code", 'paid-memberships-pro' ) . ": " . $order->discount_code . "</p>\n";
} else {
$this->data["discount_code"] = "";
}

$enddate = $wpdb->get_var("SELECT UNIX_TIMESTAMP(CONVERT_TZ(enddate, '+00:00', @@global.time_zone)) FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user->ID . "' AND status = 'active' LIMIT 1");
if($enddate)
$this->data["membership_expiration"] = "<p>" . sprintf(__("This membership will expire on %s.", 'paid-memberships-pro' ), date_i18n(get_option('date_format'), $enddate)) . "</p>\n";
else
$this->data["membership_expiration"] = "";


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

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

/**
Expand Down Expand Up @@ -1440,93 +1377,69 @@ function sendBillableInvoiceEmail( $user = NULL, $order = NULL ) {
* @param string $order_url The link to the order that is generated by Stripe.
* @return void
*/
function sendPaymentActionRequiredEmail($user = NULL, $order = NULL, $order_url = NULL)
{
function sendPaymentActionRequiredEmail( $user = NULL, $order = NULL, $order_url = NULL ) {
global $current_user;
if(!$user)
$user = $current_user;

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

// if an order URL wasn't passed in, grab it from the order
if(empty($order_url) && isset($order->order_url))
if( empty( $order_url ) && isset( $order->order_url ) )
$order_url = $order->order_url;

// still no order URL? bail
if(empty($order_url))
if( empty( $order_url ) )
return false;

$this->email = $user->user_email;
$this->subject = sprintf(__("Payment action required for your %s membership", 'paid-memberships-pro' ), get_option("blogname"));

$this->template = "payment_action";

$this->template = apply_filters("pmpro_email_template", $this->template, $this);
$email = new PMPro_Email_Template_Payment_Action( $user, $order_url );
$email->send();

$this->data = array(
'subject' => $this->subject,
'header_name' => $user->display_name,
'name' => $user->display_name,
'display_name' => $user->display_name,
'user_login' => $user->user_login,
'sitename' => get_option( 'blogname' ),
'siteemail' => get_option( 'pmpro_from_email' ),
'order_link' => $order_url,
'order_url' => $order_url,
'levels_url' => pmpro_url( 'levels' )
);

return $this->sendEmail();
}

/**
* Send the Payment Action is required email to the admin when a member's payment requires the payment action intent. This is used for Stripe payments.
*
* @param object $user
* @param MemberOrder $order
* @param string $invoice_url The link to the invoice that is generated by Stripe.
* @param string $order_url The url to the order that is generated by Stripe.
* @return void
*/
function sendPaymentActionRequiredAdminEmail($user = NULL, $order = NULL, $invoice_url = NULL)
{
function sendPaymentActionRequiredAdminEmail($user = NULL, $order = NULL, $order_url = NULL) {
global $current_user;
if(!$user)
if( !$user )
$user = $current_user;

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

// if an invoice URL wasn't passed in, grab it from the order
if(empty($invoice_url) && isset($order->invoice_url))
$invoice_url = $order->invoice_url;
if( empty( $order_url ) && isset( $order->invoice_url ) )
$order_url = $order->invoice_url;

// still no invoice URL? bail
if(empty($invoice_url))
if( empty( $order_url ) )
return false;

$this->email = get_bloginfo("admin_email");
$this->subject = sprintf(__("Payment action required: membership for %s at %s", 'paid-memberships-pro' ), $user->user_login, get_option("blogname"));

$this->template = "payment_action_admin";

$this->template = apply_filters("pmpro_email_template", $this->template, $this);
$email = new PMPro_Email_Template_Payment_Action_Admin( $user, $order_url );
$email->send();
}

$this->data = array(
'subject' => $this->subject,
'header_name' => $this->get_admin_name( $this->email ),
'name' => $user->display_name,
'display_name' => $user->display_name,
'user_login' => $user->user_login,
'sitename' => get_option('blogname'),
'siteemail' => get_option('pmpro_from_email'),
'user_email' => $user->user_email,
'invoice_link' => $invoice_url,
'invoice_url' => $invoice_url,
'levels_url' => pmpro_url( 'levels' )
);

return $this->sendEmail();
/**
* Send the payment reminder email to a member.
*
* @param object $user The WordPress user object.
* @return void
* @since TBD
*/
function send_recurring_payment_reminder( $subscription_obj = NULL ) {
// Bail if we don't have a subscription object.
if ( ! $subscription_obj ) {
return false;
}
$email = new PMPro_Email_Template_Payment_Reminder( $subscription_obj );
$email->send();
}

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

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

/**
* The URL of the order.
*
*/
protected $order_url;

/**
* 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.
* @param string $order_url The URL of the order.
*/
public function __construct( WP_User $user, string $order_url ) {
$this->user = $user;
$this->order_url = $order_url;
}

/**
* Get the email template slug.
*
* @since TBD
*
* @return string The email template slug.
*/
public static function get_template_slug() {
return 'payment_action_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 Action Required (admin)', '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 site administrator when an attempted membership checkout requires additional customer authentication.', '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 __( "Payment action required: membership for !!user_login!! at !!sitename!!", '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>A payment at !!sitename!! for !!user_login!! requires additional customer authentication to complete.</p>
<p>Below is a copy of the email we sent to !!user_email!! to notify them that they need to complete their payment:</p>
<p>Customer authentication is required to finish setting up your subscription at !!sitename!!.</p>
<p>Please complete the verification steps issued by your payment provider at the following link:</p>
<p>!!order_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' ),
'!!display_name!!' => __( 'The display name of the user.', 'paid-memberships-pro' ),
'!!order_link!!' => __( 'The URL of the order.', 'paid-memberships-pro' ),
'!!order_url!!' => __( 'The URL of the order.', 'paid-memberships-pro' ),
'!!invoice_url!!' => __( 'The URL of the order. Legacy purpose', 'paid-memberships-pro' ),
'!!levels_url!!' => __( 'The URL of the membership levels page.', '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() {
$user = $this->user;
$order_url = $this->order_url;
return array(
"subject" => $this->subject,
"name" => $user->display_name,
"user_login" => $user->user_login,
"header_name" => $user->display_name,
"display_name" => $user->display_name,
"order_link" => $order_url,
"order_url" => $order_url,
"invoice_url" => $order_url, // Legacy purpose, remove in future version
"levels_url" => pmpro_url( 'levels' )
);
}
}

/**
* 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_payment_action_admin( $email_templates ) {
$email_templates['payment_action_admin'] = 'PMPro_Email_Template_Payment_Action_Admin';
return $email_templates;
}
add_filter( 'pmpro_email_templates', 'pmpro_email_templates_payment_action_admin' );

Loading

0 comments on commit e71bc27

Please sign in to comment.