Forked from JarrydLong/mypmpro-restrict-user-emails-partially.php
Last active
June 28, 2022 17:42
-
-
Save kimwhite/dcbee33061df3429fff529ad1304e57a to your computer and use it in GitHub Desktop.
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 //do not copy | |
/** | |
* This recipe allows you to restrict partial user email addresses during checkout. | |
* You will be able to specify that all @paidmembershipspro.com email addresses are allowed to sign up, without | |
* having to enter each user's full email address. | |
* | |
* 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 mypmpro_remove_actions() { | |
remove_filter( "pmpro_registration_checks", "pmprorh_pmpro_registration_checks" ); | |
} | |
add_action( 'init', 'mypmpro_remove_actions' ); | |
/** | |
* Registration checks the restricted email domain | |
*/ | |
function mypmprorh_pmpro_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( sanitize_email( $_REQUEST['bemail'] ) ); | |
} | |
$haystack = explode( "\n", $restrict_emails ); | |
array_walk( $haystack, function( &$val ) { | |
$val = trim($val); | |
return $val; | |
}); | |
$okay = false; | |
foreach( $haystack as $hay ) { | |
if ( strpos( $needle, $hay ) !== false ) { | |
//Needle found in haystack - do nothing | |
$okay = true; | |
} | |
} | |
if( ! $okay ) { | |
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"; | |
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", "mypmprorh_pmpro_registration_checks" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment