Skip to content

Commit

Permalink
Add constant notice; clear settings that are constants from saving; r…
Browse files Browse the repository at this point in the history
…efactor radio button render; fix CSS
  • Loading branch information
joshcanhelp committed Jul 31, 2018
1 parent 9cb72e6 commit 2f6afe9
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 93 deletions.
2 changes: 1 addition & 1 deletion assets/css/initial-setup.css

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion assets/css/initial-setup/main.styl
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@
margin:0;

[disabled] {
opacity: 0.5;
font-size: 0.9em;
margin: 0.3em;
margin-top: 0.3em;
}
}

Expand Down
2 changes: 1 addition & 1 deletion assets/css/initial-setup/partials/_colors.styl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tipBG = #E4E6E8;
aText=rgb(0,148,193);

text = rgba(0,0,0,.86);
softText = rgba(0,0,0,0.36);
softText = rgba(0,0,0,0.5);
formFieldsText = rgba(0,0,0,.86)

buttonPrimaryText = #FFFFFF;
Expand Down
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ test:
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs,$HOME/.composer/vendor/wimg/php-compatibility
override:
- phpcs --standard=$HOME/wp-auth0/phpcs-compat-ruleset.xml $HOME/wp-auth0 -pn -d memory_limit=-1
- phpcs --standard=$HOME/wp-auth0/phpcs-test-ruleset.xml $HOME/wp-auth0/tests -pn -d memory_limit=-1
- phpcbf --standard=$HOME/wp-auth0/phpcs-ruleset.xml $HOME/wp-auth0 -p -d memory_limit=-1
- phpcbf --standard=$HOME/wp-auth0/phpcs-test-ruleset.xml $HOME/wp-auth0 -p -d memory_limit=-1
- phpcs --standard=$HOME/wp-auth0/phpcs-test-ruleset.xml $HOME/wp-auth0/tests -pn -d memory_limit=-1
- phpcbf --standard=$HOME/wp-auth0/phpcs-test-ruleset.xml $HOME/wp-auth0/tests -p -d memory_limit=-1
- |
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
bash bin/install-wp-tests.sh wordpress_test ubuntu '' 127.0.0.1 3.8
Expand Down
3 changes: 3 additions & 0 deletions lib/WP_Auth0_Options_Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ public function set( $key, $value, $should_update = true ) {
* @return bool
*/
public function update_all() {
foreach ( $this->get_all_constant_keys() as $key ) {
unset( $this->_opts[ $key ] );
}
return update_option( $this->_options_name, $this->_opts );
}

Expand Down
23 changes: 20 additions & 3 deletions lib/admin/WP_Auth0_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,29 @@ public function init_admin() {
);
}

public function input_validator( $input ) {
/**
* Main validator for settings page inputs.
* Delegates validation to settings sections in self::init_admin().
*
* @param array $input - Incoming array of settings fields to validate.
*
* @return mixed
*/
public function input_validator( array $input ) {
$constant_keys = $this->a0_options->get_all_constant_keys();

$old_options = $this->a0_options->get_options();
// Look for and set constant overrides so validation is still possible.
foreach ( $constant_keys as $key ) {
$input[ $key ] = $this->a0_options->get_constant_val( $key );
}

foreach ( $this->sections as $name => $section ) {
$input = $section->input_validator( $input, $old_options );
$input = $section->input_validator( $input );
}

// Remove constant overrides so they are not saved to the database.
foreach ( $constant_keys as $key ) {
unset( $input[ $key ] );
}

return $input;
Expand Down
1 change: 1 addition & 0 deletions lib/admin/WP_Auth0_Admin_Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ public function render_migration_ws( $args = array() ) {
$this->get_dashboard_link()
);
$this->render_field_description( 'Security token:' );
$this->render_const_notice( 'migration_token' );
printf(
'<textarea class="code" rows="%d" disabled>%s</textarea>',
$this->_textarea_rows,
Expand Down
19 changes: 13 additions & 6 deletions lib/admin/WP_Auth0_Admin_Appearance.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,19 @@ public function render_custom_js( $args = array() ) {
* @see add_settings_field()
*/
public function render_username_style( $args = array() ) {
$opt_name = $args['opt_name'];
$id_attr = $args['label_for'];
$value = $this->options->get( $opt_name );
$this->render_radio_button( $id_attr . '_au', $opt_name, '', 'Auto', empty( $value ) );
$this->render_radio_button( $id_attr . '_em', $opt_name, 'email', '', 'email' === $value );
$this->render_radio_button( $id_attr . '_un', $opt_name, 'username', '', 'username' === $value );
$this->render_radio_buttons(
array(
array(
'label' => 'Auto',
'value' => '',
),
'email',
'username',
),
$args['label_for'],
$args['opt_name'],
$this->options->get( $args['opt_name'], '' )
);
$this->render_field_description(
__( 'To allow the user to use either email or username to login, leave this as "Auto." ', 'wp-auth0' ) .
__( 'Only database connections that require a username will allow username logins', 'wp-auth0' )
Expand Down
10 changes: 7 additions & 3 deletions lib/admin/WP_Auth0_Admin_Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,13 @@ public function render_client_secret_b64_encoded( $args = array() ) {
* @see add_settings_field()
*/
public function render_client_signing_algorithm( $args = array() ) {
$value = $this->options->get( $args['opt_name'], WP_Auth0_Api_Client::DEFAULT_CLIENT_ALG );
$this->render_radio_button( $args['label_for'] . '_hs', $args['opt_name'], 'HS256', '', 'HS256' === $value );
$this->render_radio_button( $args['label_for'] . '_rs', $args['opt_name'], 'RS256', '', 'RS256' === $value );
$curr_value = $this->options->get( $args['opt_name'] ) ?: WP_Auth0_Api_Client::DEFAULT_CLIENT_ALG;
$this->render_radio_buttons(
array( 'HS256', 'RS256' ),
$args['label_for'],
$args['opt_name'],
$curr_value
);
$this->render_field_description(
__( 'This value can be found the Application settings in the ' ) .
$this->get_dashboard_link( 'applications' ) .
Expand Down
23 changes: 15 additions & 8 deletions lib/admin/WP_Auth0_Admin_Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,21 @@ public function init() {
* @see add_settings_field()
*/
public function render_password_policy( $args = array() ) {
$opt_name = $args['opt_name'];
$id_attr = $args['label_for'];
$curr_val = $this->options->get( $opt_name );

$this->render_radio_button( $id_attr . '_none', $opt_name, '', 'None', empty( $curr_val ) );
foreach ( array( 'low', 'fair', 'good', 'excellent' ) as $val ) {
$this->render_radio_button( $id_attr . '_' . $val, $opt_name, $val, '', $val === $curr_val );
}
$this->render_radio_buttons(
array(
array(
'label' => 'None',
'value' => '',
),
'low',
'fair',
'good',
'excellent',
),
$args['label_for'],
$args['opt_name'],
$this->options->get( $args['opt_name'], 'fair' )
);
$this->render_field_description(
__( 'Password security policy for the database connection used by this application. ', 'wp-auth0' ) .
__( 'Changing the policy here will change it for all other applications using this database. ', 'wp-auth0' ) .
Expand Down
78 changes: 57 additions & 21 deletions lib/admin/WP_Auth0_Admin_Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,18 @@ protected function rule_validation( $old_options, $input, $key, $rule_name, $rul
*/
protected function render_switch( $id, $input_name, $expand_id = '' ) {
$value = $this->options->get( $input_name );
if ( $field_is_const = $this->options->has_constant_val( $input_name ) ) {
$this->render_const_notice( $input_name );
}
printf(
'<div class="a0-switch"><input type="checkbox" name="%s[%s]" id="%s" data-expand="%s" value="1"%s>
'<div class="a0-switch"><input type="checkbox" name="%s[%s]" id="%s" data-expand="%s" value="1" %s %s>
<label for="%s"></label></div>',
esc_attr( $this->_option_name ),
esc_attr( $input_name ),
esc_attr( $id ),
! empty( $expand_id ) ? esc_attr( $expand_id ) : '',
checked( empty( $value ), false, false ),
$field_is_const ? 'disabled' : '',
esc_attr( $id )
);
}
Expand All @@ -147,20 +151,25 @@ protected function render_switch( $id, $input_name, $expand_id = '' ) {
*/
protected function render_text_field( $id, $input_name, $type = 'text', $placeholder = '', $style = '' ) {
$value = $this->options->get( $input_name );

// Secure fields are not output by default; validation keeps last value if a new one is not entered
if ( 'password' === $type ) {
$placeholder = ! empty( $value ) ? 'Not visible' : '';
$value = '';
}
if ( $field_is_const = $this->options->has_constant_val( $input_name ) ) {
$this->render_const_notice( $input_name );
}
printf(
'<input type="%s" name="%s[%s]" id="%s" value="%s" placeholder="%s" style="%s">',
'<input type="%s" name="%s[%s]" id="%s" value="%s" placeholder="%s" style="%s" %s>',
esc_attr( $type ),
esc_attr( $this->_option_name ),
esc_attr( $input_name ),
esc_attr( $id ),
esc_attr( $value ),
$placeholder ? esc_attr( $placeholder ) : '',
$style ? esc_attr( $style ) : ''
$style ? esc_attr( $style ) : '',
$field_is_const ? 'disabled' : ''
);
}

Expand All @@ -172,36 +181,48 @@ protected function render_text_field( $id, $input_name, $type = 'text', $placeho
*/
protected function render_textarea_field( $id, $input_name ) {
$value = $this->options->get( $input_name );
if ( $field_is_const = $this->options->has_constant_val( $input_name ) ) {
$this->render_const_notice( $input_name );
}
printf(
'<textarea name="%s[%s]" id="%s" rows="%d" class="code">%s</textarea>',
'<textarea name="%s[%s]" id="%s" rows="%d" class="code" %s>%s</textarea>',
esc_attr( $this->_option_name ),
esc_attr( $input_name ),
esc_attr( $id ),
$this->_textarea_rows,
$field_is_const ? 'disabled' : '',
esc_textarea( $value )
);
}

/**
* Output a radio button
* Output one or many radio buttons associated to the same option key.
*
* @param string $id - input id attribute
* @param string $input_name - input name attribute
* @param string|integer|float $value - input value attribute
* @param string $label - input label text
* @param bool $selected - is it active?
* @param array $buttons - Array of buttons to output; items can be strings or arrays with "label" and "value" keys.
* @param string $id - Input ID attribute.
* @param string $input_name - Option name saved to the options array.
* @param int|float|string $curr_value - Current option value.
*/
protected function render_radio_button( $id, $input_name, $value, $label = '', $selected = false ) {
printf(
'<label for="%s"><input type="radio" name="%s[%s]" id="%s" value="%s" %s>&nbsp;%s</label>',
esc_attr( $id ),
esc_attr( $this->_option_name ),
esc_attr( $input_name ),
esc_attr( $id ),
esc_attr( $value ),
checked( $selected, true, false ),
sanitize_text_field( ! empty( $label ) ? $label : ucfirst( $value ) )
);
protected function render_radio_buttons( array $buttons, $id, $input_name, $curr_value ) {
if ( $field_is_const = $this->options->has_constant_val( $input_name ) ) {
$this->render_const_notice( $input_name );
}
foreach ( $buttons as $index => $button ) {
$id_attr = $id . '_' . $index;
$label = is_array( $button ) ? $button['label'] : ucfirst( $button );
$value = is_array( $button ) ? $button['value'] : $button;
printf(
'<label for="%s"><input type="radio" name="%s[%s]" id="%s" value="%s" %s %s>&nbsp;%s</label>',
esc_attr( $id_attr ),
esc_attr( $this->_option_name ),
esc_attr( $input_name ),
esc_attr( $id_attr ),
esc_attr( $value ),
checked( $value === $curr_value, true, false ),
$field_is_const ? 'disabled' : '',
sanitize_text_field( $label )
);
}
}

/**
Expand All @@ -213,6 +234,21 @@ protected function render_field_description( $text ) {
printf( '<div class="subelement"><span class="description">%s.</span></div>', $text );
}

/**
* Check if the setting is provided by a constant and indicate.
*
* @param string $input_name - Input name for the field, used as option key.
*/
protected function render_const_notice( $input_name ) {
if ( $this->options->has_constant_val( $input_name ) ) {
printf(
'<p><span class="description">%s <code>%s</code></span></p>',
__( 'Value is set in the constant ', 'wp-auth0' ),
$this->options->get_constant_name( $input_name )
);
}
}

/**
* Output translated dashboard HTML link
*
Expand Down
16 changes: 9 additions & 7 deletions phpcs-test-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
<ruleset name="WP-Auth0" namespace="WPAuth0\CS\Standard">
<description>A custom coding standard for WP-Auth0 tests</description>

<!-- Tests only -->
<include-pattern>/tests/*</include-pattern>

<config name="testVersion" value="5.6-"/>
<config name="showProgress" value="1"/>
<config name="extensions" value="php"/>

<arg name="colors"/>

Expand All @@ -17,8 +13,14 @@
<rule ref="WordPress-Docs"/>
<rule ref="WordPress-Core">
<exclude name="WordPress.Files.FileName"/>
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase"/>
<exclude name="WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid"/>
<exclude name="Squiz.Commenting.FileComment.Missing"/>
<exclude name="WordPress.NamingConventions.ValidFunctionName"/>
</rule>
<rule ref="WordPress.VIP.DirectDatabaseQuery"/>
<rule ref="WordPress.VIP.SlowDBQuery"/>
<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array" value="wp-auth0" />
</properties>
</rule>

</ruleset>
2 changes: 1 addition & 1 deletion templates/back-to-auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
}
</style>
<div id="extra-options">
<a href="?"><?php printf( _e( '← Back to %s login', 'wp-auth0' ), $title ); ?></a>
<a href="?"><?php printf( __( '← Back to %s login', 'wp-auth0' ), $title ); ?></a>
</div>
10 changes: 5 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* PHPUnit bootstrap file
*
* @package Auth0
* @package WP-Auth0
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );
Expand Down Expand Up @@ -31,10 +31,10 @@ function _manually_load_plugin() {
require $_tests_dir . '/includes/bootstrap.php';

spl_autoload_register(
function( $className ) {
$fileName = stream_resolve_include_path( 'traits/' . $className . '.php' );
if ( false !== $fileName ) {
include $fileName;
function( $class_name ) {
$file_name = stream_resolve_include_path( 'traits/' . $class_name . '.php' );
if ( false !== $file_name ) {
include $file_name;
}
}
);
Loading

0 comments on commit 2f6afe9

Please sign in to comment.