Last active
February 27, 2021 11:44
-
-
Save ipokkel/25bf7dabdbe7ededeb861de1c54325c4 to your computer and use it in GitHub Desktop.
Customize the PMPro Sponsored Levels' email by placing the sponsor code and link details at the bottom of the email.
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 | |
/** | |
* This recipe places the sponsor message at the bottom of the confirmation mail. | |
* | |
* 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_pmproet_pmprosm_custom_filters() { | |
global $pmprosm_sponsored_account_levels; | |
// Is Sponsored Members active and the array available? | |
if ( ! function_exists( 'pmprosm_pmpro_email_body' ) || empty( $pmprosm_sponsored_account_levels ) || ! is_array( $pmprosm_sponsored_account_levels ) ) { | |
return; | |
} | |
if ( function_exists( 'my_pmprosm_email_msg_bottom' ) ) { | |
remove_filter( 'pmpro_email_body', 'pmprosm_pmpro_email_body', 10, 2 ); | |
if ( has_filter( 'pmpro_email_filter', 'pmproet_email_filter' ) && function_exists( 'my_pmproet_email_filter' ) ) { | |
remove_filter( 'pmpro_email_filter', 'pmproet_email_filter', 10, 1 ); | |
add_filter( 'pmpro_email_filter', 'my_pmproet_email_filter', 10, 1 ); | |
add_filter( 'pmproet_email_body', 'my_pmprosm_email_msg_bottom', 10, 2 ); | |
} else { | |
add_filter( 'pmpro_email_body', 'my_pmprosm_email_msg_bottom', 10, 2 ); | |
} | |
} | |
} | |
add_action( 'init', 'my_pmpro_pmproet_pmprosm_custom_filters' ); | |
function my_pmprosm_email_msg_bottom( $body, $pmpro_email ) { | |
global $wpdb, $pmprosm_sponsored_account_levels; | |
// Is Sponsored Members active and the array available? | |
if ( ! function_exists( 'pmprosm_pmpro_email_body' ) || empty( $pmprosm_sponsored_account_levels ) || ! is_array( $pmprosm_sponsored_account_levels ) ) { | |
return $body; | |
} | |
//only checkout emails, not admins | |
if ( strpos( $pmpro_email->template, 'checkout' ) !== false && strpos( $pmpro_email->template, 'admin' ) === false && strpos( $pmpro_email->template, 'debug' ) === false ) { | |
$user_id = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '" . esc_sql( $pmpro_email->data['user_email'] ) . "' LIMIT 1" ); | |
$level_id = $pmpro_email->data['membership_id']; | |
$code_id = pmprosm_getCodeByUserID( $user_id ); | |
if ( ! empty( $user_id ) && ! empty( $code_id ) && pmprosm_isMainLevel( $level_id ) ) { | |
// Get code. | |
$code = pmprosm_getDiscountCodeByCodeID( $code_id ); | |
// Get sponsored levels. | |
$pmprosm_values = pmprosm_getValuesByMainLevel( $level_id ); | |
if ( ! is_array( $pmprosm_values['sponsored_level_id'] ) ) { | |
$sponsored_level_ids = array( $pmprosm_values['sponsored_level_id'] ); | |
} else { | |
$sponsored_level_ids = $pmprosm_values['sponsored_level_id']; | |
} | |
// No sponsored levels to use codes for. | |
if ( empty( $sponsored_level_ids ) || empty( $sponsored_level_ids[0] ) ) { | |
return $body; | |
} | |
if ( isset( $pmprosm_values['add_created_accounts_to_confirmation_email'] ) && $pmprosm_values['add_created_accounts_to_confirmation_email'] === true ) { | |
$children = pmprosm_getChildren( $user_id ); | |
if ( ! empty( $children ) ) { | |
$message = '<p>' . esc_html__( 'Accounts created at checkout:', 'pmpro-sponsored-members' ) . '<br />'; | |
$message .= '<ul>'; | |
foreach ( $children as $child_id ) { | |
$child = get_userdata( $child_id ); | |
$message .= '<li>' . esc_html( $child->display_name ) . ' ( ' . $child->user_email . ' ) </li>'; | |
} | |
$message .= '</ul>'; | |
$body .= $message; | |
} | |
} | |
// Check if we should update confirmation email. | |
if ( isset( $pmprosm_values['hide_display_discount_code'] ) && $pmprosm_values['hide_display_discount_code'] === true ) { | |
return $body; | |
} | |
// Check if we should update confirmation email. | |
if ( isset( $pmprosm_values['add_code_to_confirmation_email'] ) && $pmprosm_values['add_code_to_confirmation_email'] === false ) { | |
return $body; | |
} | |
// Figure out urls for code. | |
$code_urls = array(); | |
$pmpro_levels = pmpro_getAllLevels( true, true ); | |
foreach ( $sponsored_level_ids as $sponsored_level_id ) { | |
$level_name = $pmpro_levels[ $sponsored_level_id ]->name; | |
$code_urls[] = array( | |
'name' => $level_name, | |
'url' => pmpro_url( 'checkout', '?level=' . $sponsored_level_id . '&discount_code=' . $code->code ), | |
); | |
} | |
// Build message. | |
$message = '<p>' . sprintf( esc_html__( 'Give this code to your sponsored members to use at checkout: %s', 'pmpro-sponsored-members' ), $code->code ) . '<br />'; | |
if ( count( $code_urls ) > 1 ) { | |
$message .= esc_html__( 'Or provide one of these direct links to register:', 'pmpro-sponsored-members' ) . '</p>'; | |
} else { | |
$message .= esc_html__( 'Or provide this direct link to register:', 'pmpro-sponsored-members' ) . '</p>'; | |
} | |
$message .= '<ul>'; | |
foreach ( $code_urls as $code_url ) { | |
$message .= '<li>' . esc_html( $code_url['name'] ) . ': <strong>' . esc_url( $code_url['url'] ) . '</strong></li>'; | |
} | |
$message .= '</ul>'; | |
$body .= $message; | |
} | |
} | |
return $body; | |
} | |
add_filter( 'my_pmproet_email_body', 'my_pmprosm_email_msg_bottom', 10, 2 ); | |
function my_pmproet_email_filter( $email ) { | |
global $pmproet_email_defaults; | |
//is this email disabled or is it not in the templates array? | |
if ( pmpro_getOption( 'email_' . $email->template . '_disabled' ) == 'true' ) { | |
return false; | |
} | |
//leave the email alone if it's not in the list of templates | |
if ( empty( $pmproet_email_defaults[ $email->template ] ) ) { | |
return $email; | |
} | |
$et_subject = pmpro_getOption( 'email_' . $email->template . '_subject' ); | |
$et_header = pmpro_getOption( 'email_header_body' ); | |
$et_body = pmpro_getOption( 'email_' . $email->template . '_body' ); | |
$et_footer = pmpro_getOption( 'email_footer_body' ); | |
if ( ! empty( $et_subject ) ) { | |
$email->subject = $et_subject; | |
} | |
//is header disabled? | |
if ( pmpro_getOption( 'email_header_disabled' ) != 'true' ) { | |
if ( ! empty( $et_header ) ) { | |
$temp_content = $et_header; | |
} else { | |
$temp_content = pmproet_getTemplateBody( 'header' ); | |
} | |
} else { | |
$temp_content = ''; | |
} | |
if ( ! empty( $et_body ) ) { | |
$temp_content .= $et_body; | |
$temp_content = apply_filters( 'my_pmproet_email_body', $temp_content, $email ); | |
} else { | |
$temp_content .= pmproet_getTemplateBody( $email->template ); | |
$temp_content = apply_filters( 'my_pmproet_email_body', $temp_content, $email ); | |
} | |
//is footer disabled? | |
if ( pmpro_getOption( 'email_footer_disabled' ) != 'true' ) { | |
if ( ! empty( $et_footer ) ) { | |
$temp_content .= $et_footer; | |
} else { | |
$temp_content .= pmproet_getTemplateBody( 'footer' ); | |
} | |
} | |
$email->body = $temp_content; | |
// Temporary workaround for avoiding double period when using !!membership_change!! | |
$email->body = str_replace( '!!membership_change!!.', '!!membership_change!!', $email->body ); | |
//replace data | |
foreach ( $email->data as $key => $value ) { | |
$email->body = str_replace( '!!' . $key . '!!', $value, $email->body ); | |
$email->subject = str_replace( '!!' . $key . '!!', $value, $email->subject ); | |
} | |
$email->subject = html_entity_decode( $email->subject, ENT_QUOTES ); | |
return $email; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment