Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge WP_Auth0_Options_Generic into WP_Auth0_Options #741

Merged
merged 2 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 232 additions & 65 deletions lib/WP_Auth0_Options.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,246 @@
<?php

class WP_Auth0_Options extends WP_Auth0_Options_Generic {
class WP_Auth0_Options {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything added to this class already existed in WP_Auth0_Options_Generic


/**
* Name used in options table option_name column.
*
* @var string
*/
protected $_options_name = 'wp_auth0_settings';

/**
* Current array of options stored in memory.
*
* @var null|array
*/
private $_opts = null;

/**
* Array of options overridden by constants.
*
* @var array
*/
protected $constant_opts = [];

/**
* @var WP_Auth0_Options
*/
protected static $_instance = null;
protected $_options_name = 'wp_auth0_settings';

/**
* WP_Auth0_Options constructor.
* Finds and stores all constant-defined settings values.
*/
public function __construct() {
$option_keys = $this->get_defaults( true );
foreach ( $option_keys as $key ) {
$setting_const = $this->get_constant_name( $key );
if ( defined( $setting_const ) ) {
$this->constant_opts[ $key ] = constant( $setting_const );
}
}
}

/**
* @return WP_Auth0_Options
*/
public static function Instance() {
if ( null === self::$_instance ) {
self::$_instance = new WP_Auth0_Options;
self::$_instance = new self;
}
return self::$_instance;
}

/**
* Takes an option key and creates the constant name to look for.
*
* @param string $key - Option key to transform.
*
* @return string
*/
public function get_constant_name( $key ) {
// NOTE: the add_filter call must load before WP_Auth0::init() so it cannot be used in a theme.
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
$constant_prefix = apply_filters( 'auth0_settings_constant_prefix', 'AUTH0_ENV_' );
return $constant_prefix . strtoupper( $key );
}

/**
* Does a certain option pull from a constant?
*
* @param string $key - Option key to check.
*
* @return boolean
*/
public function has_constant_val( $key ) {
return isset( $this->constant_opts[ $key ] );
}

/**
* Get the value of an overriding constant if one is set, return null if not.
*
* @param string $key - Option key to look for.
*
* @return string|null
*/
public function get_constant_val( $key ) {
return $this->has_constant_val( $key ) ? constant( $this->get_constant_name( $key ) ) : null;
}

/**
* Get all the keys for constant-overridden settings.
*
* @return array
*/
public function get_all_constant_keys() {
return array_keys( $this->constant_opts );
}

/**
* Get the option_name for the settings array.
*
* @return string
*/
public function get_options_name() {
return $this->_options_name;
}

/**
* Return options from memory, database, defaults, or constants.
*
* @return array
*/
public function get_options() {
if ( empty( $this->_opts ) ) {
$options = get_option( $this->_options_name, [] );
// Brand new install, no saved options so get all defaults.
if ( empty( $options ) || ! is_array( $options ) ) {
$options = $this->defaults();
}

// Check for constant overrides and replace.
if ( ! empty( $this->constant_opts ) ) {
$options = array_replace_recursive( $options, $this->constant_opts );
}
$this->_opts = $options;
}
return $this->_opts;
}

/**
* Return a filtered settings value or default.
*
* @param string $key - Settings key to get.
* @param mixed $default - Default value to return if not found.
*
* @return mixed
*
* @link https://auth0.com/docs/cms/wordpress/extending#wp_auth0_get_option
*/
public function get( $key, $default = null ) {
$options = $this->get_options();
$value = isset( $options[ $key ] ) ? $options[ $key ] : $default;
return apply_filters( 'wp_auth0_get_option', $value, $key );
}

/**
* Update a setting if not already stored in a constant.
* This method will fail silently if the option is already set in a constant.
*
* @param string $key - Option key name to update.
* @param mixed $value - Value to update with.
* @param bool $should_update - Flag to update DB options array with value stored in memory.
*
* @return bool
*/
public function set( $key, $value, $should_update = true ) {

// Cannot set a setting that is being overridden by a constant.
if ( $this->has_constant_val( $key ) ) {
return false;
}

$options = $this->get_options();
$options[ $key ] = $value;
$this->_opts = $options;

// No database update so process completed successfully.
if ( ! $should_update ) {
return true;
}

return $this->update_all();
}

/**
* Remove a setting from the options array in memory.
*
* @param string $key - Option key name to remove.
*/
public function remove( $key ) {

// Cannot remove a setting that is being overridden by a constant.
if ( $this->has_constant_val( $key ) ) {
return;
}

$options = $this->get_options();
unset( $options[ $key ] );
$this->_opts = $options;
}

/**
* Save the options array as it exists in memory.
*
* @return bool
*/
public function update_all() {
$options = $this->get_options();

foreach ( $this->get_all_constant_keys() as $key ) {
unset( $options[ $key ] );
}
return update_option( $this->_options_name, $options );
}

/**
* Save the options array for the first time.
*/
public function save() {
$this->get_options();
$this->update_all();
}

/**
* Delete the options array.
*
* @return bool
*/
public function delete() {
return delete_option( $this->_options_name );
}

/**
* Reset options to defaults.
*/
public function reset() {
$this->_opts = null;
$this->delete();
$this->save();
}

/**
* Return default options as key => value or just keys.
*
* @param bool $keys_only - Only return the array keys for the default options.
*
* @return array
*/
public function get_defaults( $keys_only = false ) {
$default_opts = $this->defaults();
return $keys_only ? array_keys( $default_opts ) : $default_opts;
}

public function is_wp_registration_enabled() {
return is_multisite() ? users_can_register_signup_filter() : get_site_option( 'users_can_register' );
}
Expand Down Expand Up @@ -188,17 +417,6 @@ public function strategy_skips_verified_email( $strategy ) {
return in_array( $strategy, $skip_strategies );
}

/**
* @deprecated - 3.11.0, use wp_auth0_can_show_wp_login_form() instead.
*
* @return bool
*
* @codeCoverageIgnore
*/
public function can_show_wp_login_form() {
return wp_auth0_can_show_wp_login_form();
}

/**
* Default settings when plugin is installed or reset
*
Expand Down Expand Up @@ -257,55 +475,4 @@ protected function defaults() {
'auth0_server_domain' => 'auth0.auth0.com',
];
}

/*
*
* DEPRECATED
*
*/

/**
* @deprecated - 3.8.0, not used and no replacement provided. Connection settings now live in top-level settings.
*
* @codeCoverageIgnore - Deprecated
*/
public function set_connection( $key, $value ) {
// phpcs:ignore
@trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );

$options = $this->get_options();
$options['connections'][ $key ] = $value;

$this->set( 'connections', $options['connections'] );
}

/**
* @deprecated - 3.8.0, not used and no replacement provided. Connection settings now live in top-level settings.
*
* @codeCoverageIgnore - Deprecated
*/
public function get_connection( $key, $default = null ) {
// phpcs:ignore
@trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );

$options = $this->get_options();

if ( ! isset( $options['connections'][ $key ] ) ) {
return apply_filters( 'wp_auth0_get_option', $default, $key );
}
return apply_filters( 'wp_auth0_get_option', $options['connections'][ $key ], $key );
}

/**
* @deprecated - 3.6.0, social connections are no longer set during initial setup so this data is no longer needed.
*
* @return array
*
* @codeCoverageIgnore - Deprecated
*/
public function get_enabled_connections() {
// phpcs:ignore
@trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );
return [ 'facebook', 'twitter', 'google-oauth2' ];
}
}
Loading