-
Notifications
You must be signed in to change notification settings - Fork 96
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
Add constant settings support #509
Changes from all commits
054f77c
7ada019
6d1ca93
b30eb43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,220 @@ | ||
<?php | ||
/** | ||
* Contains WP_Auth0_Options_Generic. | ||
* | ||
* @package WP-Auth0 | ||
*/ | ||
|
||
/** | ||
* Class WP_Auth0_Options_Generic. | ||
*/ | ||
class WP_Auth0_Options_Generic { | ||
|
||
/** | ||
* Name used in options table option_name column. | ||
* | ||
* @var string | ||
*/ | ||
protected $_options_name = ''; | ||
private $_opt = null; | ||
|
||
/** | ||
* Current array of options stored in memory. | ||
* | ||
* @var null|array | ||
*/ | ||
private $_opts = null; | ||
|
||
/** | ||
* Array of options overridden by constants. | ||
* | ||
* @var array | ||
*/ | ||
protected $constant_opts = array(); | ||
|
||
/** | ||
* WP_Auth0_Options_Generic constructor. | ||
* Finds and stores all constant-defined settings values. | ||
*/ | ||
public function __construct() { | ||
$option_keys = $this->get_defaults( true ); | ||
foreach ( $option_keys as $key ) { | ||
if ( $this->has_constant_val( $key ) ) { | ||
$this->constant_opts[ $key ] = $this->get_constant_val( $key ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 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 ) { | ||
$constant_prefix = apply_filters( 'wp_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 ) { | ||
$setting_const = $this->get_constant_name( $key ); | ||
return defined( $setting_const ); | ||
} | ||
|
||
/** | ||
* 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->_opt ) ) { | ||
if ( empty( $this->_opts ) ) { | ||
$options = get_option( $this->_options_name, array() ); | ||
if ( empty( $options ) || ! is_array( $options ) ) { | ||
|
||
if ( ! is_array( $options ) ) { | ||
// Brand new install, no saved options so get all defaults. | ||
$options = $this->defaults(); | ||
} | ||
} else { | ||
|
||
$options = array_merge( $this->defaults(), $options ); | ||
// Make sure we have settings for everything we need. | ||
$options = array_merge( $this->defaults(), $options ); | ||
} | ||
|
||
$this->_opt = $options; | ||
// Check for constant overrides and replace. | ||
if ( ! empty( $this->constant_opts ) ) { | ||
$options = array_replace_recursive( $options, $this->constant_opts ); | ||
} | ||
$this->_opts = $options; | ||
} | ||
return $this->_opt; | ||
return $this->_opts; | ||
} | ||
|
||
/** | ||
* Return a settings value | ||
* Return a filtered settings value or default. | ||
* | ||
* @param string $key - settings key | ||
* @param null $default - what to return if a value is not set | ||
* @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 = $default; | ||
|
||
if ( isset( $options[ $key ] ) ) { | ||
$value = $options[ $key ]; | ||
} | ||
|
||
$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 ) { | ||
$options = $this->get_options(); | ||
$options = $this->get_options(); | ||
|
||
// Cannot set a setting that is being overridden by a constant. | ||
if ( $this->has_constant_val( $key ) ) { | ||
return false; | ||
} | ||
|
||
$options[ $key ] = $value; | ||
$this->_opt = $options; | ||
$this->_opts = $options; | ||
|
||
if ( $should_update ) { | ||
update_option( $this->_options_name, $options ); | ||
// No database update so process completed successfully. | ||
if ( ! $should_update ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this update something you'll normally run after setting each key-value? Think of the case of setting a bunch of variables. I guess only the last call to this method would have this value as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's run most of the time, yes.
Not excellent but we don't
BC there so I'll punt that to when we refactor this whole system. |
||
return true; | ||
} | ||
|
||
return $this->update_all(); | ||
} | ||
|
||
/** | ||
* Save the options array as it exists in memory. | ||
* | ||
* @return bool | ||
*/ | ||
public function update_all() { | ||
update_option( $this->_options_name, $this->_opt ); | ||
foreach ( $this->get_all_constant_keys() as $key ) { | ||
unset( $this->_opts[ $key ] ); | ||
} | ||
return update_option( $this->_options_name, $this->_opts ); | ||
} | ||
|
||
/** | ||
* Save the options array for the first time. | ||
*/ | ||
public function save() { | ||
$options = $this->get_options(); | ||
update_option( $this->_options_name, $options ); | ||
$this->get_options(); | ||
$this->update_all(); | ||
} | ||
|
||
/** | ||
* Delete the options array. | ||
* | ||
* @return bool | ||
*/ | ||
public function delete() { | ||
delete_option( $this->_options_name ); | ||
return delete_option( $this->_options_name ); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
* Default settings when plugin is installed or reset | ||
* | ||
* @return array | ||
*/ | ||
protected function defaults() { | ||
return array(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting