2012-02-25 05:12:43 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Customize Setting Class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Customize
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
2012-02-27 20:46:52 +01:00
|
|
|
|
2012-02-25 05:12:43 +01:00
|
|
|
class WP_Customize_Setting {
|
2012-03-21 23:55:43 +01:00
|
|
|
public $manager;
|
2012-02-25 05:12:43 +01:00
|
|
|
public $id;
|
|
|
|
public $priority = 10;
|
|
|
|
public $section = '';
|
|
|
|
public $label = '';
|
|
|
|
public $control = 'text';
|
|
|
|
public $type = 'theme_mod';
|
|
|
|
public $choices = array();
|
|
|
|
public $capability = 'edit_theme_options';
|
|
|
|
public $theme_supports = '';
|
|
|
|
public $default = '';
|
|
|
|
public $sanitize_callback = '';
|
2012-03-22 08:17:26 +01:00
|
|
|
public $visibility;
|
2012-02-25 05:12:43 +01:00
|
|
|
|
|
|
|
protected $id_data = array();
|
|
|
|
private $_post_value; // Cached, sanitized $_POST value.
|
|
|
|
|
|
|
|
// Prefix for $_POST values to prevent naming conflicts.
|
|
|
|
const name_prefix = 'customize_';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param string $id An specific ID of the setting. Can be a
|
|
|
|
* theme mod or option name.
|
|
|
|
* @param array $args Setting arguments.
|
|
|
|
*/
|
2012-03-21 23:55:43 +01:00
|
|
|
function __construct( $manager, $id, $args = array() ) {
|
2012-02-25 05:12:43 +01:00
|
|
|
$keys = array_keys( get_class_vars( __CLASS__ ) );
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
if ( isset( $args[ $key ] ) )
|
|
|
|
$this->$key = $args[ $key ];
|
|
|
|
}
|
|
|
|
|
2012-03-21 23:55:43 +01:00
|
|
|
$this->manager = $manager;
|
2012-02-25 05:12:43 +01:00
|
|
|
$this->id = $id;
|
|
|
|
|
|
|
|
// Parse the ID for array keys.
|
|
|
|
$this->id_data[ 'keys' ] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
|
|
|
|
$this->id_data[ 'base' ] = array_shift( $this->id_data[ 'keys' ] );
|
|
|
|
|
|
|
|
// Rebuild the ID.
|
|
|
|
$this->id = $this->id_data[ 'base' ];
|
|
|
|
if ( ! empty( $this->id_data[ 'keys' ] ) )
|
|
|
|
$this->id .= '[' . implode( '][', $this->id_data[ 'keys' ] ) . ']';
|
|
|
|
|
|
|
|
if ( $this->sanitize_callback != '' )
|
|
|
|
add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback );
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue setting related scripts/styles.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
|
|
|
public function enqueue() {
|
|
|
|
switch( $this->control ) {
|
|
|
|
case 'color':
|
|
|
|
wp_enqueue_script( 'farbtastic' );
|
|
|
|
wp_enqueue_style( 'farbtastic' );
|
|
|
|
break;
|
2012-03-15 05:14:05 +01:00
|
|
|
case 'upload':
|
|
|
|
wp_enqueue_script( 'wp-plupload' );
|
|
|
|
break;
|
2012-02-25 05:12:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle previewing the setting.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
|
|
|
public function preview() {
|
|
|
|
switch( $this->type ) {
|
|
|
|
case 'theme_mod' :
|
|
|
|
add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
|
|
|
|
break;
|
|
|
|
case 'option' :
|
|
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
|
|
add_filter( 'pre_option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
|
|
|
|
else
|
|
|
|
add_filter( 'option_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
do_action( 'customize_preview_' . $this->id );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function to filter the theme mods and options.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
2012-02-27 20:46:52 +01:00
|
|
|
*
|
2012-02-25 05:12:43 +01:00
|
|
|
* @param mixed Old value.
|
|
|
|
* @return mixed New or old value.
|
|
|
|
*/
|
|
|
|
public function _preview_filter( $original ) {
|
|
|
|
return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of the parameter for a specific theme.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @return bool False if cap check fails or value isn't set.
|
|
|
|
*/
|
|
|
|
public final function save() {
|
|
|
|
$value = $this->post_value();
|
|
|
|
|
|
|
|
if ( ! $this->check_capabilities() || ! isset( $value ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
do_action( 'customize_save_' . $this->id_data[ 'base' ] );
|
|
|
|
|
|
|
|
$this->update( $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches, validates, and sanitizes the $_POST value.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $default mixed A default value which is used as a fallback. Default is null.
|
|
|
|
* @return mixed Either the default value on failure or sanitized value.
|
|
|
|
*/
|
|
|
|
public final function post_value( $default = null ) {
|
|
|
|
if ( isset( $this->_post_value ) )
|
|
|
|
return $this->_post_value;
|
|
|
|
|
|
|
|
$base = self::name_prefix . $this->id_data[ 'base' ];
|
|
|
|
|
|
|
|
if ( ! isset( $_POST[ $base ] ) )
|
|
|
|
return $default;
|
|
|
|
|
|
|
|
$result = $this->multidimensional_get( $_POST[ $base ], $this->id_data[ 'keys' ] );
|
|
|
|
if ( ! isset( $result ) )
|
|
|
|
return $default;
|
|
|
|
|
|
|
|
$result = $this->sanitize( $result );
|
|
|
|
if ( isset( $result ) )
|
|
|
|
return $this->_post_value = $result;
|
|
|
|
else
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize an input.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $value mixed The value to sanitize.
|
|
|
|
* @return mixed Null if an input isn't valid, otherwise the sanitized value.
|
|
|
|
*/
|
|
|
|
public function sanitize( $value ) {
|
2012-02-28 22:21:16 +01:00
|
|
|
$value = stripslashes_deep( $value );
|
2012-02-25 05:12:43 +01:00
|
|
|
return apply_filters( "customize_sanitize_{$this->id}", $value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value of the parameter for a specific theme.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $value mixed The value to update.
|
|
|
|
* @return mixed The result of saving the value.
|
|
|
|
*/
|
|
|
|
protected function update( $value ) {
|
|
|
|
switch( $this->type ) {
|
|
|
|
case 'theme_mod' :
|
|
|
|
return $this->_update_theme_mod( $value );
|
|
|
|
break;
|
|
|
|
case 'option' :
|
|
|
|
return $this->_update_option( $value );
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
return do_action( 'customize_update_' . $this->type, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the theme mod from the value of the parameter.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $value mixed The value to update.
|
|
|
|
* @return mixed The result of saving the value.
|
|
|
|
*/
|
|
|
|
protected function _update_theme_mod( $value ) {
|
|
|
|
// Handle non-array theme mod.
|
|
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
|
|
return set_theme_mod( $this->id_data[ 'base' ], $value );
|
|
|
|
|
|
|
|
// Handle array-based theme mod.
|
|
|
|
$mods = get_theme_mod( $this->id_data[ 'base' ] );
|
|
|
|
$mods = $this->multidimensional_replace( $mods, $this->id_data[ 'keys' ], $value );
|
|
|
|
if ( isset( $mods ) )
|
|
|
|
return set_theme_mod( $this->id_data[ 'base' ], $mods );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the theme mod from the value of the parameter.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $value mixed The value to update.
|
|
|
|
* @return mixed The result of saving the value.
|
|
|
|
*/
|
|
|
|
protected function _update_option( $value ) {
|
|
|
|
// Handle non-array option.
|
|
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
|
|
return update_option( $this->id_data[ 'base' ], $value );
|
|
|
|
|
|
|
|
// Handle array-based options.
|
|
|
|
$options = get_option( $this->id_data[ 'base' ] );
|
|
|
|
$options = $this->multidimensional_replace( $options, $this->id_data[ 'keys' ], $value );
|
|
|
|
if ( isset( $options ) )
|
|
|
|
return update_option( $this->id_data[ 'base' ], $options );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the value of the parameter for a specific theme.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @return mixed The requested value.
|
|
|
|
*/
|
|
|
|
public function value() {
|
|
|
|
switch( $this->type ) {
|
|
|
|
case 'theme_mod' :
|
|
|
|
$function = 'get_theme_mod';
|
|
|
|
break;
|
|
|
|
case 'option' :
|
|
|
|
$function = 'get_option';
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
return apply_filters( 'customize_value_' . $this->id_data[ 'base' ], $this->default );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle non-array value
|
|
|
|
if ( empty( $this->id_data[ 'keys' ] ) )
|
|
|
|
return $function( $this->id_data[ 'base' ], $this->default );
|
|
|
|
|
|
|
|
// Handle array-based value
|
|
|
|
$values = $function( $this->id_data[ 'base' ] );
|
|
|
|
return $this->multidimensional_get( $values, $this->id_data[ 'keys' ], $this->default );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the theme supports the setting and check user capabilities.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @return bool False if theme doesn't support the setting or user can't change setting, otherwise true.
|
|
|
|
*/
|
|
|
|
public final function check_capabilities() {
|
2012-03-21 05:59:57 +01:00
|
|
|
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
|
2012-02-25 05:12:43 +01:00
|
|
|
return false;
|
|
|
|
|
2012-03-21 05:59:57 +01:00
|
|
|
if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) )
|
2012-02-25 05:12:43 +01:00
|
|
|
return false;
|
|
|
|
|
2012-03-21 23:55:43 +01:00
|
|
|
$section = $this->manager->get_section( $this->section );
|
2012-02-25 05:12:43 +01:00
|
|
|
if ( isset( $section ) && ! $section->check_capabilities() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-21 23:55:43 +01:00
|
|
|
* Check capabiliites and render the control.
|
2012-02-25 05:12:43 +01:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
2012-03-21 23:55:43 +01:00
|
|
|
public final function maybe_render() {
|
2012-02-25 05:12:43 +01:00
|
|
|
if ( ! $this->check_capabilities() )
|
|
|
|
return;
|
|
|
|
|
2012-03-21 23:55:43 +01:00
|
|
|
do_action( 'customize_render_setting', $this );
|
|
|
|
do_action( 'customize_render_setting_' . $this->id, $this );
|
2012-02-25 05:12:43 +01:00
|
|
|
|
|
|
|
$this->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-22 08:17:26 +01:00
|
|
|
* Render the control. Renders the control wrapper, then calls $this->render_content().
|
2012-02-25 05:12:43 +01:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
|
|
|
protected function render() {
|
2012-03-22 08:17:26 +01:00
|
|
|
|
|
|
|
$id = 'customize-control-' . $this->id;
|
|
|
|
$class = 'customize-control customize-control-' . $this->control;
|
|
|
|
|
|
|
|
$style = '';
|
|
|
|
if ( $this->visibility ) {
|
2012-03-22 09:07:44 +01:00
|
|
|
if ( is_string( $this->visibility ) ) {
|
|
|
|
$visibility_id = $this->visibility;
|
|
|
|
$visibility_value = true;
|
|
|
|
} else {
|
|
|
|
$visibility_id = $this->visibility[0];
|
|
|
|
$visibility_value = $this->visibility[1];
|
|
|
|
}
|
|
|
|
$visibility_setting = $this->manager->get_setting( $visibility_id );
|
2012-03-22 08:17:26 +01:00
|
|
|
|
|
|
|
if ( $visibility_setting && $visibility_value != $visibility_setting->value() )
|
|
|
|
$style = 'style="display:none;"';
|
|
|
|
}
|
|
|
|
|
|
|
|
?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>" <?php echo $style; ?>>
|
|
|
|
<?php $this->render_content(); ?>
|
|
|
|
</li><?php
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the control's content.
|
|
|
|
*
|
|
|
|
* Allows the content to be overriden without having to rewrite the wrapper.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*/
|
|
|
|
protected function render_content() {
|
2012-02-25 05:12:43 +01:00
|
|
|
switch( $this->control ) {
|
|
|
|
case 'text':
|
|
|
|
?>
|
2012-03-15 06:30:11 +01:00
|
|
|
<label>
|
|
|
|
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
|
2012-02-25 05:12:43 +01:00
|
|
|
<input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->name(); ?> />
|
|
|
|
</label>
|
|
|
|
<?php
|
|
|
|
break;
|
|
|
|
case 'color':
|
|
|
|
?>
|
2012-03-06 03:03:50 +01:00
|
|
|
<label>
|
2012-03-15 06:30:11 +01:00
|
|
|
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
|
2012-03-06 03:03:50 +01:00
|
|
|
<div class="color-picker">
|
2012-03-06 23:48:07 +01:00
|
|
|
<input type="hidden" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->name(); ?> />
|
2012-03-06 03:03:50 +01:00
|
|
|
<a href="#"></a>
|
|
|
|
<div class="color-picker-controls">
|
|
|
|
<div class="farbtastic-placeholder"></div>
|
2012-03-07 00:55:16 +01:00
|
|
|
<div class="color-picker-details">
|
|
|
|
<div class="color-picker-hex">
|
|
|
|
<span>#</span>
|
|
|
|
<input type="text" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2012-03-06 03:03:50 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2012-02-25 05:12:43 +01:00
|
|
|
</label>
|
|
|
|
<?php
|
|
|
|
break;
|
|
|
|
case 'checkbox':
|
|
|
|
?>
|
|
|
|
<label>
|
2012-03-15 06:30:11 +01:00
|
|
|
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
|
|
|
|
<input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->name(); checked( $this->value() ); ?> class="customize-control-content" />
|
2012-02-25 05:12:43 +01:00
|
|
|
</label>
|
|
|
|
<?php
|
|
|
|
break;
|
|
|
|
case 'radio':
|
|
|
|
if ( empty( $this->choices ) )
|
|
|
|
return;
|
|
|
|
|
2012-03-15 06:30:11 +01:00
|
|
|
?>
|
|
|
|
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
|
|
|
|
<?php
|
2012-02-25 05:12:43 +01:00
|
|
|
foreach ( $this->choices as $value => $label ) :
|
|
|
|
?>
|
|
|
|
<label>
|
|
|
|
<input type="radio" value="<?php echo esc_attr( $value ); ?>" <?php $this->name(); checked( $this->value(), $value ); ?> />
|
|
|
|
<?php echo esc_html( $label ); ?><br/>
|
|
|
|
</label>
|
|
|
|
<?php
|
|
|
|
endforeach;
|
|
|
|
break;
|
|
|
|
case 'select':
|
|
|
|
if ( empty( $this->choices ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
?>
|
2012-03-15 06:30:11 +01:00
|
|
|
<label>
|
|
|
|
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
|
|
|
|
<select <?php $this->name(); ?> class="customize-control-content">
|
|
|
|
<?php
|
|
|
|
foreach ( $this->choices as $value => $label )
|
|
|
|
echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
|
|
|
|
?>
|
|
|
|
</select>
|
|
|
|
</label>
|
2012-02-25 05:12:43 +01:00
|
|
|
<?php
|
|
|
|
break;
|
2012-03-15 05:14:05 +01:00
|
|
|
case 'upload':
|
2012-03-22 08:30:44 +01:00
|
|
|
$value = $this->value();
|
|
|
|
$style = empty( $value ) ? 'style="display:none;"' : '';
|
2012-03-15 05:14:05 +01:00
|
|
|
?>
|
2012-03-15 06:30:11 +01:00
|
|
|
<label>
|
|
|
|
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
|
|
|
|
<div>
|
|
|
|
<input type="hidden" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->name(); ?> />
|
|
|
|
<a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
|
2012-03-22 08:30:44 +01:00
|
|
|
<a href="#" class="remove" <?php echo $style; ?>><?php _e( 'Remove' ); ?></a>
|
2012-03-15 06:30:11 +01:00
|
|
|
</div>
|
2012-03-15 05:14:05 +01:00
|
|
|
</label>
|
|
|
|
<?php
|
|
|
|
break;
|
2012-03-22 05:14:26 +01:00
|
|
|
case 'dropdown-pages':
|
|
|
|
printf(
|
|
|
|
'<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
|
|
|
|
$this->label,
|
|
|
|
wp_dropdown_pages(
|
|
|
|
array(
|
|
|
|
'name' => $this->get_name(),
|
|
|
|
'echo' => 0,
|
|
|
|
'show_option_none' => __( '— Select —' ),
|
|
|
|
'option_none_value' => '0',
|
|
|
|
'selected' => get_option( $this->id )
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
2012-02-25 05:12:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-21 23:55:43 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the name attribute for an input.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @return string The name.
|
|
|
|
*/
|
|
|
|
public final function get_name() {
|
|
|
|
return self::name_prefix . esc_attr( $this->id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echo the HTML name attribute for an input.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @return string The HTML name attribute.
|
|
|
|
*/
|
|
|
|
public final function name() {
|
|
|
|
echo 'name="' . $this->get_name() . '"';
|
|
|
|
}
|
|
|
|
|
2012-02-25 05:12:43 +01:00
|
|
|
/**
|
|
|
|
* Multidimensional helper function.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
|
|
|
* @param bool $create Default is false.
|
|
|
|
* @return null|array
|
|
|
|
*/
|
|
|
|
final protected function multidimensional( $root, $keys, $create = false ) {
|
|
|
|
if ( $create && empty( $root ) )
|
|
|
|
$root = array();
|
|
|
|
|
|
|
|
if ( ! isset( $root ) || empty( $keys ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
$last = array_pop( $keys );
|
|
|
|
$node = &$root;
|
|
|
|
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
if ( $create && ! isset( $node[ $key ] ) )
|
|
|
|
$node[ $key ] = array();
|
|
|
|
|
|
|
|
if ( ! is_array( $node ) || ! isset( $node[ $key ] ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
$node = &$node[ $key ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $create && ! isset( $node[ $last ] ) )
|
|
|
|
$node[ $last ] = array();
|
|
|
|
|
|
|
|
if ( ! isset( $node[ $last ] ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'root' => &$root,
|
|
|
|
'node' => &$node,
|
|
|
|
'key' => $last,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will attempt to replace a specific value in a multidimensional array.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
|
|
|
* @param mixed $value The value to update.
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
final protected function multidimensional_replace( $root, $keys, $value ) {
|
|
|
|
if ( ! isset( $value ) )
|
|
|
|
return $root;
|
|
|
|
elseif ( empty( $keys ) ) // If there are no keys, we're replacing the root.
|
|
|
|
return $value;
|
|
|
|
|
2012-03-07 17:32:11 +01:00
|
|
|
$result = $this->multidimensional( $root, $keys, true );
|
2012-02-25 05:12:43 +01:00
|
|
|
|
|
|
|
if ( isset( $result ) )
|
|
|
|
$result['node'][ $result['key'] ] = $value;
|
|
|
|
|
|
|
|
return $root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will attempt to fetch a specific value from a multidimensional array.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
|
|
|
* @param $default A default value which is used as a fallback. Default is null.
|
|
|
|
* @return mixed The requested value or the default value.
|
|
|
|
*/
|
|
|
|
final protected function multidimensional_get( $root, $keys, $default = null ) {
|
|
|
|
if ( empty( $keys ) ) // If there are no keys, test the root.
|
|
|
|
return isset( $root ) ? $root : $default;
|
|
|
|
|
|
|
|
$result = $this->multidimensional( $root, $keys );
|
|
|
|
return isset( $result ) ? $result['node'][ $result['key'] ] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Will attempt to check if a specific value in a multidimensional array is set.
|
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
*
|
|
|
|
* @param $root
|
|
|
|
* @param $keys
|
|
|
|
* @return bool True if value is set, false if not.
|
|
|
|
*/
|
|
|
|
final protected function multidimensional_isset( $root, $keys ) {
|
|
|
|
$result = $this->multidimensional_get( $root, $keys );
|
|
|
|
return isset( $result );
|
|
|
|
}
|
|
|
|
}
|