Skip to content

Commit

Permalink
Centralize logic for whether an option is disabled in WPSEO_Option an…
Browse files Browse the repository at this point in the history
…d thus get rid of additional overhead in Yoast_Form, by relying on the relevant WPSEO_Option instance.
  • Loading branch information
Felix Arntz committed Sep 18, 2018
1 parent d06a621 commit 3841d9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 66 deletions.
73 changes: 11 additions & 62 deletions admin/class-yoast-form.php
Expand Up @@ -31,22 +31,12 @@ class Yoast_Form {
public $options;

/**
* Option name for additional higher level options, if present.
* Option instance.
*
* This can be the name of a set of network options in a multisite for example.
*
* @var string
*/
protected $override_option_name = '';

/**
* Additional higher level options, if present.
*
* This can be the the network options in a multisite for example.
*
* @var array
* @since 8.4
* @var WPSEO_Option
*/
protected $override_options = array();
protected $option_instance;

/**
* Get the singleton instance of this class
Expand Down Expand Up @@ -103,10 +93,6 @@ public function admin_header( $form = true, $option = 'wpseo', $contains_files =
else {
$action_url = admin_url( 'options.php' );
$hidden_fields_cb = 'settings_fields';
$override_option = WPSEO_Options::get_instance()->get_option_instance( $option )->get_override_option_name();
if ( $override_option ) {
$this->set_override_option( $override_option );
}
}

echo '<form action="' . esc_url( $action_url ) . '" method="post" id="wpseo-conf"' . $enctype . ' accept-charset="' . esc_attr( get_bloginfo( 'charset' ) ) . '">';
Expand All @@ -123,8 +109,9 @@ public function admin_header( $form = true, $option = 'wpseo', $contains_files =
* @param string $option_name Option key.
*/
public function set_option( $option_name ) {
$this->option_name = $option_name;
$this->options = $this->get_option();
$this->option_instance = WPSEO_Options::get_option_instance( $option_name );
$this->option_name = $option_name;
$this->options = WPSEO_Options::get_option( $option_name );
}

/**
Expand All @@ -147,58 +134,20 @@ public function set_options_value( $key, $value, $overwrite = false ) {
*
* @since 1.2.4
* @since 2.0 Moved to this class.
* @deprecated 8.4
*
* @return array
*/
public function get_option() {
_deprecated_function( __METHOD__, 'WPSEO 8.4' );

if ( is_network_admin() ) {
return get_site_option( $this->option_name );
}

return get_option( $this->option_name );
}

/**
* Sets the higher level option used in output for form elements.
*
* @param string $option_name Option key.
*
* @return void
*/
public function set_override_option( $option_name ) {
$this->override_option_name = $option_name;
$this->override_options = $this->get_override_option();
}

/**
* Sets a value in the higher level options.
*
* @param string $key The key of the option to set.
* @param mixed $value The value to set the option to.
* @param bool $overwrite Whether to overwrite existing options. Default is false.
*
* @return void
*/
public function set_override_options_value( $key, $value, $overwrite = false ) {
if ( $overwrite || ! array_key_exists( $key, $this->override_options ) ) {
$this->override_options[ $key ] = $value;
}
}

/**
* Retrieves higher level options based on whether we're on multisite or not.
*
* @return array
*/
public function get_override_option() {
// There is no higher level than the network admin.
if ( is_network_admin() ) {
return array();
}

return get_site_option( $this->override_option_name );
}

/**
* Generates the footer for admin pages
*
Expand Down Expand Up @@ -729,7 +678,7 @@ public function show_hide_switch( $var, $label, $inverse_keys = false, $help = '
* @return bool True if control should be disabled, false otherwise.
*/
protected function is_control_disabled( $var ) {
return isset( $this->override_options[ 'allow_' . $var ] ) && ! $this->override_options[ 'allow_' . $var ];
return $this->option_instance->is_disabled( $var );
}

/**
Expand Down
18 changes: 14 additions & 4 deletions inc/options/class-wpseo-option.php
Expand Up @@ -468,12 +468,21 @@ public function validate( $option_value ) {
}

/**
* Retrieves the name of the override option, if set.
* Checks whether a specific option key is disabled.
*
* @return string|null Override option name, or null if not set.
* This is determined by whether an override option is available with a key that equals the given key prefixed
* with 'allow_'.
*
* @param string $key Option key.
* @return bool True if option key is disabled, false otherwise.
*/
public function get_override_option_name() {
return $this->override_option_name;
public function is_disabled( $key ) {
$override_option = $this->get_override_option();
if ( empty( $override_option ) ) {
return false;
}

return isset( $override_option[ 'allow_' . $key ] ) && ! $override_option[ 'allow_' . $key ];
}

/**
Expand Down Expand Up @@ -678,6 +687,7 @@ protected function prevent_disabled_options_update( $updated, $old ) {
return $updated;
}

// This loop could as well call `is_disabled( $key )` for each iteration, however this would be worse performance-wise.
foreach ( $old as $key => $value ) {
if ( isset( $override_option[ 'allow_' . $key ] ) && ! $override_option[ 'allow_' . $key ] ) {
$updated[ $key ] = $old[ $key ];
Expand Down

0 comments on commit 3841d9d

Please sign in to comment.