Skip to content

Commit

Permalink
move field_num into options page
Browse files Browse the repository at this point in the history
  • Loading branch information
bdolor committed May 16, 2018
1 parent 0711ef6 commit c5ed8b2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 64 deletions.
90 changes: 52 additions & 38 deletions includes/class-validate-by-domain-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,66 @@ function plugin_settings_init() {
'sanitize_callback' => [ $this, 'sanitize_input' ],
];

$page = $options = 'validate_by_domain';

register_setting(
'validate_by_domain',
'validate_by_domain_settings',
$options,
$options . '_settings',
$args
);

add_settings_section(
'options_section',
__( '', 'WordPress' ),
$options . '_section',
__( 'Validate by Domain Settings', 'validate-by-domain' ),
'',
'validate_by_domain'
$page
);

add_settings_field(
'validate_enable',
__( 'Enable', 'WordPress' ),
__( 'Enable', 'validate-by-domain' ),
[ $this, 'enable_render' ],
'validate_by_domain',
'options_section'
$page,
$options . '_section'
);

add_settings_field(
'field_num',
__( 'Field Number', 'validate-by-domain' ),
[ $this, 'field_render' ],
$page,
$options . '_section'
);

add_settings_field(
'validate_role',
__( 'Role', 'WordPress' ),
__( 'Role', 'validate-by-domain' ),
[ $this, 'role_render' ],
'validate_by_domain',
'options_section'
$page,
$options . '_section'
);

add_settings_field(
'validate_whitelist',
__( 'Whitelist', 'WordPress' ),
__( 'Whitelist', 'validate-by-domain' ),
[ $this, 'whitelist_render' ],
'validate_by_domain',
'options_section'
$page,
$options . '_section'
);

}

function field_render() {
$options = get_option( 'validate_by_domain_settings' );

// add default
if ( ! isset( $options['field_num'] ) ) {
$options['field_num'] = 0;
}

echo "<input type='text' name='validate_by_domain_settings[field_num]' value='{$options['field_num']}'>";
}

/**
* Render the options page enable field
*/
Expand Down Expand Up @@ -111,53 +132,48 @@ function whitelist_render() {

/**
* @param $input
* Sanitize the whitelist
*
* @return mixed|void
* @return array
*/
function sanitize_input( $input ) {
$domains = [];
$integers = [ 'validate_enable', 'field_num' ];

// Create our array for storing the sanitized options
$output = $domains = [];

// add all of our options to the output
if ( is_array( $input ) ) {
foreach ( $input as $key => $value ) {
$output[ $key ] = $value;
}
}

// Check if the current option has a value. If so, process it.
// strip tags, parse, make list unique, check for domain pattern, trim whitespace
if ( isset( $input['validate_whitelist'] ) ) {

// Strip all HTML and PHP tags
$output['validate_whitelist'] = strip_tags( stripslashes( $input['validate_whitelist'] ) );
$input['validate_whitelist'] = strip_tags( stripslashes( $input['validate_whitelist'] ) );

// Split the string by new lines, commas, single space, or multiple whitespace
$output['validate_whitelist'] = preg_split( "/(\r\n|\n|\r|,|[\s]|[\s][\s])/", $output['validate_whitelist'] );
$input['validate_whitelist'] = preg_split( "/(\r\n|\n|\r|,|[\s]|[\s][\s])/", $input['validate_whitelist'] );

// Make items unique
$output['validate_whitelist'] = array_unique( $output['validate_whitelist'] );
$input['validate_whitelist'] = array_unique( $input['validate_whitelist'] );

// make sure they have a valid domain pattern
// @see https://stackoverflow.com/questions/3026957/how-to-validate-a-domain-name-using-regex-php#16491074
foreach ( $output['validate_whitelist'] as $k => $domain ) {
foreach ( $input['validate_whitelist'] as $k => $domain ) {
$ok = filter_var( $domain, FILTER_VALIDATE_REGEXP, [ 'options' => [ 'regexp' => '/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/' ] ] );
if ( $ok ) {
$domains[] = $ok;
}
}

// Removes empty elements created by blank new lines, trim any whitespace before or after
$output['validate_whitelist'] = array_filter( array_map( 'trim', $domains ) );
$input['validate_whitelist'] = array_filter( array_map( 'trim', $domains ) );

// Let's send back string with one item per line
$output['validate_whitelist'] = implode( PHP_EOL, $output['validate_whitelist'] );
$input['validate_whitelist'] = implode( PHP_EOL, $input['validate_whitelist'] );

}

// integers
foreach ( $integers as $int ) {
$input[ $int ] = absint( $input[ $int ] );
}

// Return the array processing any additional functions filtered by this action
return apply_filters( 'sanitize_input', $output, $input );
return $input;

}

Expand All @@ -167,9 +183,7 @@ function sanitize_input( $input ) {
*/
function plugin_options_page() {
?>
<form action='options.php' method='post'>

<h2>Validate by Domain Options</h2>
<form id='vbd_settings' action='options.php' method='post'>

<?php
settings_fields( 'validate_by_domain' );
Expand Down
55 changes: 29 additions & 26 deletions public/class-validate-by-domain-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Validate_By_Domain_Public {
/**
* @var array list of problem domains
*/
private $spam_domains = array(
private $spam_domains = [
'marvsz.com',
'kellergy.com',
'pixymix.com',
Expand All @@ -49,16 +49,16 @@ class Validate_By_Domain_Public {
'ultramoonbear.com',
'islaby.com',
'360ezzz.com',
);
];

/**
* @var array list of top level domains associated with spam
*/
private $spam_tld = array(
private $spam_tld = [
'ru',
'pl',
'eu',
);
];

/**
* Initialize the class and set its properties.
Expand All @@ -76,19 +76,12 @@ public function __construct( $bc_validate, $version ) {
}

/**
* set different field values depending on prod or dev env
* todo: bring this into plugin options page, or make it more user friendly somehow
* set the value according to user prefs
*/
public function setFieldNum() {
$host = parse_url( network_site_url(), PHP_URL_HOST );

if ( 0 === strcmp( 'earlyyearsbc.ca', $host ) ) {
$field_val = '155';
} else {
$field_val = '3';
}

$this->field_val = $field_val;
$options = get_option( 'validate_by_domain_settings' );
$this->field_val = $options['field_num'];

}

Expand All @@ -110,7 +103,7 @@ public function enqueue_styles() {
* between the defined hooks and the functions defined in this
* class.
*/
// wp_enqueue_style( $this->bc_validate, plugin_dir_url( __FILE__ ) . 'css/bc-validate-public.css', array(), $this->version, 'all' );
// wp_enqueue_style( $this->bc_validate, plugin_dir_url( __FILE__ ) . 'css/bc-validate-public.css', array(), $this->version, 'all' );
}

/**
Expand All @@ -131,7 +124,7 @@ public function enqueue_scripts() {
* between the defined hooks and the functions defined in this
* class.
*/
// wp_enqueue_script( $this->bc_validate, plugin_dir_url( __FILE__ ) . 'js/plugin-name-public.js', array( 'jquery' ), $this->version, false );
// wp_enqueue_script( $this->bc_validate, plugin_dir_url( __FILE__ ) . 'js/plugin-name-public.js', array( 'jquery' ), $this->version, false );
}

/**
Expand All @@ -140,21 +133,29 @@ public function enqueue_scripts() {
*/
public function signupUserBC() {
global $bp;
$field_val = 'field_' . $this->field_val;
if ( isset( $_POST ) && ( 'request-details' != $bp->signup->step ) ) {
return;
}
$field_val = 'field_' . $this->field_val;
$options = get_option( 'validate_by_domain_settings' );

// Filter email addresses for Organizers, check for spam domains on Learners
if ( 0 === strcmp( $_POST[ $field_val ], 'Organizer' ) ) {
$domain = $this->parseEmail( $_POST['signup_email'] );
$valid = $this->isBCDomain( $domain );
// condition is that the user has enabled this feature
if ( 1 === $options['validate_enable'] ) {

$role = $options['validate_role'];

// Filter email addresses for Organizers, check for spam domains on Learners
if ( 0 === strcmp( $_POST[ $field_val ], 'Organizer' ) ) {
$domain = $this->parseEmail( $_POST['signup_email'] );
$valid = $this->isWhiteListedDomain( $domain );

if ( false == $valid ) {
$bp->signup->errors['signup_email'] = 'Please use an email address from an allowed agency or institution within British Columbia';
if ( false == $valid ) {
$bp->signup->errors['signup_email'] = 'Please use an email address from an authorized domain.';
}
}
}

// opinionated default blacklist
if ( 0 === strcmp( $_POST[ $field_val ], 'Learner' ) ) {
$domain = $this->parseEmail( $_POST['signup_email'] );
$spam = $this->isSpamDomain( $domain );
Expand Down Expand Up @@ -202,13 +203,14 @@ public function parseWhiteList() {


/**
* Compares the domain of the users email to a list of BC institution domains
* Compares the domain of the users email to a list of BC institution
* domains
*
* @param string $domain
*
* @return boolean
*/
private function isBCDomain( $domain ) {
private function isWhiteListedDomain( $domain ) {

if ( empty( $domain ) ) {
return false;
Expand Down Expand Up @@ -298,7 +300,8 @@ public function signupMetaBC( $usermeta ) {
}

/**
* At the moment after signup, during activation, update capabilities to contributor
* At the moment after signup, during activation, update capabilities to
* contributor
*
* @param $user_id
*
Expand Down

0 comments on commit c5ed8b2

Please sign in to comment.