Add support for `WP_Widget::get_settings()` returning `ArrayIterator`/`ArrayObject` instances.
Plugins can use `pre_option_widget_{$id_base}` filters to return `ArrayIterator`/`ArrayObject` instances instead of primitive arrays. This makes possible for widget instance data to be drawn from somewhere else than `wp_options`, such as a custom post type. Add unit tests for widgets. Fixes #32474. Built from https://develop.svn.wordpress.org/trunk@32602 git-svn-id: http://core.svn.wordpress.org/trunk@32572 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
0f5e6d2825
commit
7792b5c942
|
@ -4,7 +4,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '4.3-alpha-32601';
|
||||
$wp_version = '4.3-alpha-32602';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
|
@ -214,20 +214,24 @@ class WP_Widget {
|
|||
$settings = $this->get_settings();
|
||||
$empty = true;
|
||||
|
||||
if ( is_array($settings) ) {
|
||||
foreach ( array_keys($settings) as $number ) {
|
||||
if ( is_numeric($number) ) {
|
||||
$this->_set($number);
|
||||
$this->_register_one($number);
|
||||
// When $settings is an array-like object, get an intrinsic array for use with array_keys().
|
||||
if ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) {
|
||||
$settings = $settings->getArrayCopy();
|
||||
}
|
||||
|
||||
if ( is_array( $settings ) ) {
|
||||
foreach ( array_keys( $settings ) as $number ) {
|
||||
if ( is_numeric( $number ) ) {
|
||||
$this->_set( $number );
|
||||
$this->_register_one( $number );
|
||||
$empty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $empty ) {
|
||||
// If there are none, we register the widget's existence with a
|
||||
// generic template
|
||||
$this->_set(1);
|
||||
// If there are none, we register the widget's existence with a generic template.
|
||||
$this->_set( 1 );
|
||||
$this->_register_one();
|
||||
}
|
||||
}
|
||||
|
@ -294,15 +298,16 @@ class WP_Widget {
|
|||
* }
|
||||
*/
|
||||
public function display_callback( $args, $widget_args = 1 ) {
|
||||
if ( is_numeric($widget_args) )
|
||||
if ( is_numeric( $widget_args ) ) {
|
||||
$widget_args = array( 'number' => $widget_args );
|
||||
}
|
||||
|
||||
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
|
||||
$this->_set( $widget_args['number'] );
|
||||
$instance = $this->get_settings();
|
||||
$instances = $this->get_settings();
|
||||
|
||||
if ( array_key_exists( $this->number, $instance ) ) {
|
||||
$instance = $instance[$this->number];
|
||||
if ( isset( $instances[ $this->number ] ) ) {
|
||||
$instance = $instances[ $this->number ];
|
||||
|
||||
/**
|
||||
* Filter the settings for a particular widget instance.
|
||||
|
@ -424,6 +429,7 @@ class WP_Widget {
|
|||
* @access public
|
||||
*
|
||||
* @param int|array $widget_args Widget instance number or array of widget arguments.
|
||||
* @return string|null
|
||||
*/
|
||||
public function form_callback( $widget_args = 1 ) {
|
||||
if ( is_numeric($widget_args) )
|
||||
|
@ -516,20 +522,22 @@ class WP_Widget {
|
|||
*/
|
||||
public function get_settings() {
|
||||
|
||||
$settings = get_option($this->option_name);
|
||||
$settings = get_option( $this->option_name );
|
||||
|
||||
if ( false === $settings && isset($this->alt_option_name) )
|
||||
$settings = get_option($this->alt_option_name);
|
||||
|
||||
if ( !is_array($settings) )
|
||||
$settings = array();
|
||||
|
||||
if ( !empty($settings) && !array_key_exists('_multiwidget', $settings) ) {
|
||||
// old format, convert if single widget
|
||||
$settings = wp_convert_widget_settings($this->id_base, $this->option_name, $settings);
|
||||
if ( false === $settings && isset( $this->alt_option_name ) ) {
|
||||
$settings = get_option( $this->alt_option_name );
|
||||
}
|
||||
|
||||
unset($settings['_multiwidget'], $settings['__i__']);
|
||||
if ( ! is_array( $settings ) && ! ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) ) {
|
||||
$settings = array();
|
||||
}
|
||||
|
||||
if ( ! empty( $settings ) && ! isset( $settings['_multiwidget'] ) ) {
|
||||
// Old format, convert if single widget.
|
||||
$settings = wp_convert_widget_settings( $this->id_base, $this->option_name, $settings );
|
||||
}
|
||||
|
||||
unset( $settings['_multiwidget'], $settings['__i__'] );
|
||||
return $settings;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue