Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Created November 19, 2020 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronalfy/bb48fd2228315d7e6470f8ff15760f97 to your computer and use it in GitHub Desktop.
Save ronalfy/bb48fd2228315d7e6470f8ff15760f97 to your computer and use it in GitHub Desktop.
PMPro - ACF Pro - Restrict Domains Level with Register Helper
<?php
/**
* Requires ACF Pro. Registers an options page and restricts by level and domain. Add approved domains via Register helper (can be optional)
*
* !important
* Change 12 to level you'd like to have the domain field on.
* Change 13 to level ID you'd like to restrict.
*
* 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' );
// Register helper field for a domain.
function my_pmpro_acf_domain_field() {
// Don't break if Register Helper is not loaded.
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
pmprorh_add_checkout_box( 'approved_domain', 'Approved Domain' ); // order parameter defaults to one more than the last checkout box
// Define the fields.
$fields = array();
$fields[] = new PMProRH_Field(
'approved_domain', // input name, will also be used as meta key.
'text', // type of field.
array(
'label' => 'Domain', // label.
'size' => 40, // Size of the input.
'class' => 'approved-domain', // Class for styling.
'profile' => 'admin', // Admin can only view.
'required' => true, // This field is required.
'levels' => array( 12 ), // Only show on level 12.
)
);
// Add the fields into a new checkout_boxes are of the checkout page.
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'approved_domain', // location on checkout page
$field // PMProRH_Field object
);
}
}
add_action( 'init', 'my_pmpro_acf_domain_field' );
/**
* Check level 12 for a domain field.
*/
function my_pmpro_registration_domain_check( $maybe_continue ) {
global $pmpro_level, $pmpro_msg, $pmpro_msgt;
if ( 12 != $pmpro_level->id ) {
return $maybe_continue;
}
$domain = isset( $_REQUEST['approved_domain'] ) ? sanitize_text_field( $_REQUEST['approved_domain'] ) : false;
// Groovy Regex for checking valid domains.
preg_match( '/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/', $domain, $matches );
if ( ! $matches ) {
$pmpro_msg = __( 'The domain name you entered is invalid.', 'paid-memberships-pro' );
$pmpro_msgt = 'pmpro_error';
return false;
}
return $maybe_continue;
}
add_filter( 'pmpro_registration_checks', 'my_pmpro_registration_domain_check', 10, 1 );
/**
*
*/
function my_pmpro_add_domain_to_settings( $user_id, $order_object ) {
global $pmpro_level;
if ( 12 != $pmpro_level->id || ! isset( $_REQUEST['approved_domain'] ) || ! function_exists( 'get_field' ) ) {
return $maybe_continue;
}
$domain = $_REQUEST['approved_domain'];
if ( function_exists( 'get_field' ) ) {
$levels_domains = get_field( 'levels_domains', 'option' );
foreach ( $levels_domains as &$level_domain ) {
if ( 13 != $level_domain['level_input'] ) { // change me to level ID to restrict domains for.
continue;
}
$allowed_domains = isset( $level_domain['allowed_domains'] ) ? $level_domain['allowed_domains'] : array();
$allowed_domains[] = array( 'domain' => $domain );
$level_domain = array( 'allowed_domains' => $allowed_domains, 'level_input' => 13 ); // change me to level ID to restrict.
}
update_field( 'levels_domains', $levels_domains, 'option' );
}
}
add_action( 'pmpro_after_checkout', 'my_pmpro_add_domain_to_settings', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment