Created
March 22, 2023 14:54
-
-
Save dparker1005/7c2e90ed4d29f6923734bad6e53a2856 to your computer and use it in GitHub Desktop.
Only allow specific email addresses and usernames to check out using PMPro. Forked from Register Helper.
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 | |
/* | |
* Plugin Name: PMPro Allowlist | |
* Plugin URI: https://www.paidmembershipspro.com | |
* Description: Only allow specific email addresses and usernames to check out using PMPro. Forked from Register Helper. | |
* Version: 1.0 | |
* Author: Paid Memberships Pro | |
* Author URI: https://www.paidmembershipspro.com | |
*/ | |
/** | |
* Add settings to edit membership level page. | |
*/ | |
function pmpro_allowlist_after_other_settings() | |
{ | |
$level = $_REQUEST['edit']; | |
$restrict_emails = pmpro_getOption("level_" . $level . "_restrict_emails"); | |
?> | |
<h3 class="topborder">Restrict by Email</h3> | |
<p>To restrict signups to specific email addresses, enter those email addresses below, one per line. If blank, signups will not be restricted.</p> | |
<textarea rows="10" cols="80" name="restrict_emails" id="restrict_emails"><?php echo str_replace("\"", """, stripslashes($restrict_emails))?></textarea> | |
<?php | |
$restrict_usernames = pmpro_getOption("level_" . $level . "_restrict_usernames"); | |
?> | |
<h3 class="topborder">Restrict by Username</h3> | |
<p>To restrict signups to specific users or usernames, enter those usernames below, one per line. If blank, signups will not be restricted.</p> | |
<textarea rows="10" cols="80" name="restrict_usernames" id="restrict_usernames"><?php echo str_replace("\"", """, stripslashes($restrict_usernames))?></textarea> | |
<?php | |
} | |
add_action("pmpro_membership_level_after_other_settings", "pmpro_allowlist_after_other_settings"); | |
/** | |
* Save membership level settings. | |
*/ | |
function pmpro_allowlist_save_membership_level($saveid) | |
{ | |
$restrict_emails = $_REQUEST['restrict_emails']; | |
pmpro_setOption("level_" . $saveid . "_restrict_emails", $restrict_emails); | |
$restrict_emails = $_REQUEST['restrict_usernames']; | |
pmpro_setOption("level_" . $saveid . "_restrict_usernames", $restrict_emails); | |
} | |
add_action("pmpro_save_membership_level", "pmpro_allowlist_save_membership_level"); | |
/** | |
* Enforce allowlist at checkout. | |
*/ | |
function pmpro_allowlist_registration_checks($okay) | |
{ | |
global $current_user; | |
//only check if we're okay so far and there is an email to check | |
if($okay && (!empty($_REQUEST['bemail']) || !empty($current_user->user_email))) | |
{ | |
//are we restricting emails for this level | |
global $pmpro_level; | |
$restrict_emails = pmpro_getOption("level_" . $pmpro_level->id . "_restrict_emails"); | |
if(!empty($restrict_emails)) | |
{ | |
$restrict_emails = strtolower(str_replace(array(";", ",", " "), "\n", $restrict_emails)); | |
if(!empty($current_user->user_email)) | |
$needle = strtolower($current_user->user_email); | |
else | |
$needle = strtolower($_REQUEST['bemail']); | |
$haystack = explode("\n", $restrict_emails); | |
array_walk( $haystack, function( &$val ) { | |
$val = trim($val); | |
return $val; | |
}); | |
if(!in_array($needle, $haystack)) | |
{ | |
global $pmpro_msg, $pmpro_msgt; | |
$pmpro_msg = "This membership level is restricted to certain users only. Make sure you've entered your email address correctly."; | |
$pmpro_msgt = "pmpro_error"; | |
$okay = false; | |
//no further checks here | |
return $okay; | |
} | |
} | |
//are we restricting user names for this level | |
$restrict_usernames = pmpro_getOption("level_" . $pmpro_level->id . "_restrict_usernames"); | |
if(!empty($restrict_usernames)) | |
{ | |
$restrict_usernames = strtolower(str_replace(array(";", ",", " "), "\n", $restrict_usernames)); | |
if(!empty($current_user->user_login)) | |
$needle = strtolower($current_user->user_login); | |
else | |
$needle = strtolower($_REQUEST['username']); | |
$haystack = explode("\n", $restrict_usernames); | |
array_walk( $haystack, function( &$val ) { | |
$val = trim($val); | |
return $val; | |
}); | |
if(!in_array($needle, $haystack)) | |
{ | |
global $pmpro_msg, $pmpro_msgt; | |
$pmpro_msg = "This membership level is restricted to certain users only. Make sure you are logged into your existing account and using the proper username."; | |
$pmpro_msgt = "pmpro_error"; | |
$okay = false; | |
} | |
} | |
} | |
return $okay; | |
} | |
add_filter("pmpro_registration_checks", "pmpro_allowlist_registration_checks"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment