Created
March 24, 2017 00:19
-
-
Save pingram3541/dc5095242b9d587fb2a837e87329ade8 to your computer and use it in GitHub Desktop.
Paid Memberships Pro - Disable Billing Email Templates based on User Setting
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 | |
/********************************************* | |
* ADDON - Add user meta to profile pages | |
* Adds option to disable PMPro invoice/billing emails | |
* | |
* OVERRIDE - Disable PMPro billing emails per user | |
* Disable billing emails if user meta gfc_disable_billing_notice = Yes | |
********************************************/ | |
add_action( 'show_user_profile', 'gfc_pmpro_membership_disable_billing' ); | |
add_action( 'edit_user_profile', 'gfc_pmpro_membership_disable_billing' ); | |
function gfc_pmpro_membership_disable_billing( $user ) { | |
//only give right users access | |
$membership_level_capability = apply_filters("pmpro_edit_member_capability", "manage_options"); | |
if ( current_user_can( $membership_level_capability ) ) { | |
?> | |
<h3>Disable Membership Billing Notifications</h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="gfc_disable_billing_notice">Disable?</label></th> | |
<td> | |
<select name="gfc_disable_billing_notice" id="gfc_disable_billing_notice" > | |
<option value="" <?php selected( '', get_the_author_meta( 'gfc_disable_billing_notice', $user->ID ) ); ?>>No</option> | |
<option value="Yes" <?php selected( 'Yes', get_the_author_meta( 'gfc_disable_billing_notice', $user->ID ) ); ?>>Yes</option> | |
</select> | |
<span class="description">Please choose Yes to disable all billing notices for this account</span> | |
</td> | |
</tr> | |
</table> | |
<?php } | |
} | |
//saving... | |
add_action( 'personal_options_update', 'gfc_save_pmpro_membership_billing_account' ); | |
add_action( 'edit_user_profile_update', 'gfc_save_pmpro_membership_billing_account' ); | |
function gfc_save_pmpro_membership_billing_account( $user_id ) { | |
//only give right users access | |
$membership_level_capability = apply_filters("pmpro_edit_member_capability", "manage_options"); | |
if ( !current_user_can( $membership_level_capability ) ) | |
return false; | |
update_usermeta( absint( $user_id ), 'gfc_disable_billing_notice', wp_kses_post( $_POST['gfc_disable_billing_notice'] ) ); | |
} | |
//Disable Billing Notice/Receipt Emails based on user profile setting | |
function gfc_pmpro_recipient_filter($recipient, $email) { | |
//disabled email templates - https://www.paidmembershipspro.com/documentation/member-communications/list-of-pmpro-email-templates/ | |
$disabled_templates = array( | |
'billing', | |
'billing_failure', | |
'billable_invoice', | |
'invoice', | |
); | |
//only if using one of those tmeplates | |
if( in_array( $email->template, $disabled_templates ) ) { | |
//check if user has billing notices disabled | |
$user = get_user_by( 'login', $email->data['user_login'] ); | |
$billing_disabled = get_user_meta( $user->ID, 'gfc_disable_billing_notice', true ); | |
if( $billing_disabled == 'Yes' ){ | |
$recipient = NULL; | |
} | |
} | |
return $recipient; | |
} | |
add_filter('pmpro_email_recipient', 'gfc_pmpro_recipient_filter'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment