Forked from greathmaster/pmpro-sponsored-member-message-shortcode.php
Last active
March 30, 2021 20:06
-
-
Save dparker1005/ae78515819d64d998f28ff3886e8fdee to your computer and use it in GitHub Desktop.
Creates a PMPro email shortcode for the sponsored members message to use for greater control over location of message. Use !!sponsored_message!! in email checkout body to display. Removes the automatic insertion of message at the top of the email.
This file contains hidden or 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 | |
/* | |
* Creates a PMPro email shortcode for the sponsored members message to use for greater control over location of message. | |
* Use !!sponsored_message!! in email checkout body to display. | |
* Removes the automatic insertion of message at the top of the email. | |
*/ | |
function my_sponsored_email_shortcode($pmpro_email) | |
{ | |
global $wpdb, $pmprosm_sponsored_account_levels; | |
//only checkout emails, not admins | |
if(function_exists('pmprosm_getCodeByUserID') && strpos($pmpro_email->template, "checkout") !== false && strpos($pmpro_email->template, "admin") === false && strpos($pmpro_email->template, "debug") === false) | |
{ | |
//get the user_id from the email | |
$user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . $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; | |
//no uses for this code | |
if(empty($code->uses)) | |
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>" . __( "Accounts created at checkout:", "pmpro_sponsored_members" ) . "<br />"; | |
$message .= "<ul>"; | |
foreach ( $children as $child_id ) { | |
$child = get_userdata($child_id); | |
$message .= "<li>" . $child->display_name . " ( " . $child->user_email . " ) </li>"; | |
} | |
$message .= "</ul>"; | |
$body = $message . "<hr />" . $body; | |
} | |
} | |
//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(__("Give this code to your sponsored members to use at checkout: %s", "pmpro_sponsored_members"), $code->code) . "<br />"; | |
if(count($code_urls) > 1) | |
$message .= __("Or provide one of these direct links to register:", "pmpro_sponsored_members") . "</p>"; | |
else | |
$message .= __("Or provide this direct link to register:", "pmpro_sponsored_members") . "</p>"; | |
$message .= "<ul>"; | |
foreach($code_urls as $code_url) { | |
$message .= "<li>" . $code_url['name'] . ": <strong>" . $code_url['url'] . "</strong></li>"; | |
} | |
$message .= "</ul>"; | |
return $message; | |
} | |
} | |
return ''; | |
} | |
function my_pmpro_email_data($data, $email) | |
{ | |
$data['sponsored_message'] = my_sponsored_email_shortcode($email); | |
return $data; | |
} | |
add_filter("pmpro_email_data", "my_pmpro_email_data", 10, 2); | |
function my_init() | |
{ | |
remove_filter("pmpro_email_body", "pmprosm_pmpro_email_body", 10, 2); | |
} | |
add_action('init', 'my_init'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment