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:
Weston Ruter 2015-05-26 16:51:27 +00:00
parent 0f5e6d2825
commit 7792b5c942
2 changed files with 32 additions and 24 deletions

View File

@ -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.

View File

@ -214,6 +214,11 @@ class WP_Widget {
$settings = $this->get_settings();
$empty = true;
// 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 ) ) {
@ -225,8 +230,7 @@ class WP_Widget {
}
if ( $empty ) {
// If there are none, we register the widget's existence with a
// generic template
// 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) )
@ -518,14 +524,16 @@ class WP_Widget {
$settings = get_option( $this->option_name );
if ( false === $settings && isset($this->alt_option_name) )
if ( false === $settings && isset( $this->alt_option_name ) ) {
$settings = get_option( $this->alt_option_name );
}
if ( !is_array($settings) )
if ( ! is_array( $settings ) && ! ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) ) {
$settings = array();
}
if ( !empty($settings) && !array_key_exists('_multiwidget', $settings) ) {
// old format, convert if single widget
if ( ! empty( $settings ) && ! isset( $settings['_multiwidget'] ) ) {
// Old format, convert if single widget.
$settings = wp_convert_widget_settings( $this->id_base, $this->option_name, $settings );
}