Forked from ipokkel/date-format-for-pmpro-email-variables.php
Last active
August 23, 2022 15:14
-
-
Save michaelbeil/0e0b6a4b8178fe35d8da6f5aca0d05fd to your computer and use it in GitHub Desktop.
Set a custom date format for the PMPro email date related variables that overrides the site's default date format.
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 | |
/** | |
* Set a custom date format for some PMPro date email data variables. | |
* | |
* Set your custom date format in the $date_format variable. | |
* See https://www.php.net/manual/en/datetime.format.php for more information. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_email_data_date_format( $data, $email ) { | |
global $wpdb; | |
// Set your preferred date format. | |
$my_date_format = 'd-m-Y'; | |
// Let's get the user | |
$user = get_user_by( 'email', $email->email ) ? get_user_by( 'email', $email->email ) : $user = get_user_by( 'login', $data['user_login'] ); | |
// Bail if we don't have a user. | |
if ( empty( $user ) ) { | |
return $data; | |
} | |
// Get the user's membership level. | |
$level = pmpro_getMembershipLevelForUser( $user->ID ); | |
// Set custom date format for !!startdate!! variable. | |
if ( ! empty( $level->startdate ) ) { | |
$data['startdate'] = date_i18n( $my_date_format, $level->startdate ); | |
} else { | |
$data['startdate'] = ''; | |
} | |
// Set custom date format for !!enddate!! variable. | |
if ( ! empty( $level->enddate ) ) { | |
$data['enddate'] = date_i18n( $my_date_format, $level->enddate ); | |
} else { | |
$data['enddate'] = ''; | |
} | |
// Set custom date format for !!membership_expiration!! variable. | |
if ( ! empty( $level->enddate ) ) { | |
$data['membership_expiration'] = '<p>' . sprintf( __( 'This membership will expire on %s.', 'paid-memberships-pro' ), date_i18n( $my_date_format, $level->enddate ) ) . "</p>\n"; | |
} else { | |
/* If this is a recurring payment membership let's show them the next payment date. */ | |
// We want to ping the gateways when we check for the next payment date. | |
add_filter( 'pmpro_next_payment', array( 'PMProGateway_paypalexpress', 'pmpro_next_payment' ), 10, 3 ); | |
add_filter( 'pmpro_next_payment', array( 'PMProGateway_stripe', 'pmpro_next_payment' ), 10, 3 ); | |
$next_payment = pmpro_next_payment( $user->ID ); | |
if ( ! empty( $next_payment ) ) { | |
$data['membership_expiration'] = '<p>' . sprintf( __( 'Your next payment will be on %s.', 'paid-memberships-pro' ), date_i18n( $my_date_format, $next_payment ) ) . "</p>\n"; | |
} | |
} | |
// Set date format for !!invoice_date!! variable. | |
$invoice = new MemberOrder(); | |
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->pmpro_membership_orders WHERE user_id = %d ORDER BY timestamp DESC LIMIT 1", $user->ID ) ); | |
$invoice->getMemberOrderByID( $order_id ); | |
if ( isset( $data['invoice_date'] ) && ! empty( $data['invoice_date'] ) ) { | |
$data['invoice_date'] = date_i18n( $my_date_format, $invoice->timestamp ); | |
} | |
return $data; | |
} | |
add_filter( 'pmpro_email_data', 'my_pmpro_email_data_date_format', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment