Created
November 12, 2020 17:36
-
-
Save ronalfy/22b75d458ad178440e74c2e6b3ee719c to your computer and use it in GitHub Desktop.
PMPro + ACF - Add Options Page and Restrict Levels by Domains
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 | |
/** | |
* Requires ACF Pro. Registers an options page and restricts by level and domain. | |
* | |
* 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/ | |
*/ | |
/** | |
* Register the Options page. | |
*/ | |
function pmpro_acf_domains_option_register() { | |
if ( function_exists( 'acf_add_options_page' ) ) { | |
acf_add_options_page( | |
array( | |
'page_title' => 'Allowed Domains', | |
'menu_title' => 'Allowed Domains', | |
'menu_slug' => 'pmpro-allowed-domains', | |
'capability' => 'manage_options', | |
'redirect' => false, | |
'parent_slug' => 'pmpro-dashboard', | |
) | |
); | |
} | |
} | |
add_action( 'init', 'pmpro_acf_domains_option_register' ); | |
/** | |
* Restrict Membership Signup by Email Domain & Level - Requires ACF Pro. | |
* Make sure to edit the $valid_domains array defined further below | |
* to include only the domains you'd like to allow. | |
* | |
* Add this code to a custom plugin. More info: https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_registration_checks_restrict_email_addresses( $value ) { | |
global $pmpro_level; | |
$protected_levels = array(); | |
if ( function_exists( 'get_field' ) ) { | |
$levels_domains = get_field( 'levels_domains', 'option' ); | |
foreach ( $levels_domains as $level_domain ) { | |
$level = absint( $level_domain['level_input'] ); | |
$domains = array(); | |
$level_domains_group = $level_domain['allowed_domains']; | |
foreach ( $level_domains_group as $key => $level_domain_group ) { | |
$domains[] = $level_domain_group['domain']; | |
} | |
$protected_levels[ $level ] = $domains; | |
} | |
} else { | |
return $value; | |
} | |
if ( ! array_key_exists( $pmpro_level->id, $protected_levels ) ) { | |
return $value; | |
} | |
$email = $_REQUEST['bemail']; | |
if ( ! my_checkForValidDomain( $email, $protected_levels[ $pmpro_level->id ] ) ) { | |
global $pmpro_msg, $pmpro_msgt; | |
$pmpro_msg = 'Please enter a valid email address'; | |
$pmpro_msgt = 'pmpro_error'; | |
$value = false; | |
} | |
return $value; | |
} | |
add_filter( 'pmpro_registration_checks', 'my_pmpro_registration_checks_restrict_email_addresses', 10, 1 ); | |
// Taken from: http://www.bitrepository.com/how-to-extract-domain-name-from-an-e-mail-address-string.html | |
function my_getDomainFromEmail( $email ) { | |
// Get the data after the @ sign | |
$domain = substr( strrchr( $email, '@' ), 1 ); | |
return $domain; | |
} | |
/** | |
* Check for valid email domains. | |
*/ | |
function my_checkForValidDomain( $email, $valid_domains ) { | |
$domain = my_getDomainFromEmail( $email ); | |
if ( ! is_array( $valid_domains ) ) { | |
return true; | |
} | |
foreach ( $valid_domains as $valid_domain ) { | |
$components = explode( '.', $valid_domain ); | |
$domain_to_check = explode( '.', $domain ); | |
if ( $components[0] == '*' && ( count( $domain_to_check ) > 2 ) ) { | |
if ( $components[1] == $domain_to_check[1] && $components[2] == $domain_to_check[2] ) { | |
return true; | |
} | |
} else { | |
if ( ! ( strpos( $valid_domain, $domain ) === false ) ) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
/** | |
* Add the field to the admin panel. | |
*/ | |
function pmpro_add_acf_levels_group() { | |
if ( function_exists( 'acf_add_local_field_group' ) ) : | |
acf_add_local_field_group( | |
array( | |
'key' => 'group_5fad64690295c', | |
'title' => 'Levels and Domains', | |
'fields' => array( | |
array( | |
'key' => 'field_5fad646f81527', | |
'label' => 'Levels and Allowed Domains', | |
'name' => 'levels_domains', | |
'type' => 'repeater', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'collapsed' => '', | |
'min' => 0, | |
'max' => 0, | |
'layout' => 'row', | |
'button_label' => 'Add domain', | |
'sub_fields' => array( | |
array( | |
'key' => 'field_5fad648781528', | |
'label' => 'Allowed Domains', | |
'name' => 'allowed_domains', | |
'type' => 'repeater', | |
'instructions' => '', | |
'required' => 1, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'collapsed' => '', | |
'min' => 0, | |
'max' => 0, | |
'layout' => 'table', | |
'button_label' => '', | |
'sub_fields' => array( | |
array( | |
'key' => 'field_5fad6ad543417', | |
'label' => 'Domain Extension', | |
'name' => 'domain', | |
'type' => 'text', | |
'instructions' => 'domain.com', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => 'domain.com', | |
'prepend' => '', | |
'append' => '', | |
'maxlength' => '', | |
), | |
), | |
), | |
array( | |
'key' => 'field_5fad67a743415', | |
'label' => 'Level Input', | |
'name' => 'level_input', | |
'type' => 'number', | |
'instructions' => '', | |
'required' => 1, | |
'conditional_logic' => 0, | |
'wrapper' => array( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => 10, | |
'prepend' => '', | |
'append' => '', | |
'min' => '', | |
'max' => '', | |
'step' => '', | |
), | |
), | |
), | |
), | |
'location' => array( | |
array( | |
array( | |
'param' => 'options_page', | |
'operator' => '==', | |
'value' => 'pmpro-allowed-domains', | |
), | |
), | |
), | |
'menu_order' => 0, | |
'position' => 'normal', | |
'style' => 'default', | |
'label_placement' => 'top', | |
'instruction_placement' => 'label', | |
'hide_on_screen' => '', | |
'active' => true, | |
'description' => '', | |
) | |
); | |
endif; | |
} | |
add_action( 'init', 'pmpro_add_acf_levels_group' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment