diff -r 3d4e9c994f10 -r a86126ab1dd4 wp/wp-includes/widgets.php --- a/wp/wp-includes/widgets.php Tue Oct 22 16:11:46 2019 +0200 +++ b/wp/wp-includes/widgets.php Tue Dec 15 13:49:49 2020 +0100 @@ -10,8 +10,8 @@ * This functionality was found in a plugin before the WordPress 2.2 release, which * included it in the core from that point on. * - * @link https://codex.wordpress.org/Plugins/WordPress_Widgets WordPress Widgets - * @link https://codex.wordpress.org/Plugins/WordPress_Widgets_Api Widgets API + * @link https://wordpress.org/support/article/wordpress-widgets/ + * @link https://developer.wordpress.org/themes/functionality/widgets/ * * @package WordPress * @subpackage Widgets @@ -19,7 +19,7 @@ */ // -// Global Variables +// Global Variables. // /** @ignore */ @@ -42,7 +42,7 @@ $wp_registered_widgets = array(); /** - * Stores the registered widget control (options). + * Stores the registered widget controls (options). * * @global array $wp_registered_widget_controls * @since 2.2.0 @@ -91,7 +91,7 @@ ); // -// Template tags & API functions +// Template tags & API functions. // /** @@ -155,7 +155,7 @@ * Optional. Array or string of arguments for building a sidebar. * * @type string $id The base string of the unique identifier for each sidebar. If provided, and multiple - * sidebars are being defined, the id will have "-2" appended, and so on. + * sidebars are being defined, the ID will have "-2" appended, and so on. * Default 'sidebar-' followed by the number the sidebar creation is currently at. * @type string $name The name or title for the sidebars displayed in the admin dashboard. If registering * more than one sidebar, include '%d' in the string as a placeholder for the uniquely @@ -175,16 +175,21 @@ $_args = $args; if ( $number > 1 ) { - $_args['name'] = isset( $args['name'] ) ? sprintf( $args['name'], $i ) : sprintf( __( 'Sidebar %d' ), $i ); + if ( isset( $args['name'] ) ) { + $_args['name'] = sprintf( $args['name'], $i ); + } else { + /* translators: %d: Sidebar number. */ + $_args['name'] = sprintf( __( 'Sidebar %d' ), $i ); + } } else { $_args['name'] = isset( $args['name'] ) ? $args['name'] : __( 'Sidebar' ); } // Custom specified ID's are suffixed if they exist already. - // Automatically generated sidebar names need to be suffixed regardless starting at -0 + // Automatically generated sidebar names need to be suffixed regardless starting at -0. if ( isset( $args['id'] ) ) { $_args['id'] = $args['id']; - $n = 2; // Start at -2 for conflicting custom ID's + $n = 2; // Start at -2 for conflicting custom IDs. while ( is_registered_sidebar( $_args['id'] ) ) { $_args['id'] = $args['id'] . '-' . $n++; } @@ -215,7 +220,7 @@ * * @since 2.2.0 * - * @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID. + * @global array $wp_registered_sidebars Registered sidebars. * * @param array|string $args { * Optional. Array or string of arguments for the sidebar being registered. @@ -247,6 +252,7 @@ $id_is_empty = empty( $args['id'] ); $defaults = array( + /* translators: %d: Sidebar number. */ 'name' => sprintf( __( 'Sidebar %d' ), $i ), 'id' => "sidebar-$i", 'description' => '', @@ -257,11 +263,29 @@ 'after_title' => "\n", ); - $sidebar = wp_parse_args( $args, $defaults ); + /** + * Filters the sidebar default arguments. + * + * @since 5.3.0 + * + * @see register_sidebar() + * + * @param array $defaults The default sidebar arguments. + */ + $sidebar = wp_parse_args( $args, apply_filters( 'register_sidebar_defaults', $defaults ) ); if ( $id_is_empty ) { - /* translators: 1: the id argument, 2: sidebar name, 3: recommended id value */ - _doing_it_wrong( __FUNCTION__, sprintf( __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), 'id', $sidebar['name'], $sidebar['id'] ), '4.2.0' ); + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: 1: The 'id' argument, 2: Sidebar name, 3: Recommended 'id' value. */ + __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), + 'id', + $sidebar['name'], + $sidebar['id'] + ), + '4.2.0' + ); } $wp_registered_sidebars[ $sidebar['id'] ] = $sidebar; @@ -285,7 +309,7 @@ * * @since 2.2.0 * - * @global array $wp_registered_sidebars Removes the sidebar from this array by sidebar ID. + * @global array $wp_registered_sidebars Registered sidebars. * * @param string|int $sidebar_id The ID of the sidebar when it was registered. */ @@ -320,6 +344,8 @@ * parameter is an empty string. * * @since 2.2.0 + * @since 5.3.0 Formalized the existing and already documented `...$params` parameter + * by adding it to the function signature. * * @global array $wp_registered_widgets Uses stored registered widgets. * @global array $wp_registered_widget_controls Stores the registered widget controls (options). @@ -337,8 +363,9 @@ * @type string $description Widget description for display in the widget administration * panel and/or theme. * } + * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. */ -function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) { +function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array(), ...$params ) { global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; $id = strtolower( $id ); @@ -361,7 +388,7 @@ 'name' => $name, 'id' => $id, 'callback' => $output_callback, - 'params' => array_slice( func_get_args(), 4 ), + 'params' => $params, ); $widget = array_merge( $widget, $options ); @@ -456,18 +483,18 @@ * Registers widget control callback for customizing options. * * @since 2.2.0 - * - * @todo `$params` parameter? + * @since 5.3.0 Formalized the existing and already documented `...$params` parameter + * by adding it to the function signature. * * @global array $wp_registered_widget_controls * @global array $wp_registered_widget_updates * @global array $wp_registered_widgets * @global array $_wp_deprecated_widgets_callbacks * - * @param int|string $id Sidebar ID. - * @param string $name Sidebar display name. - * @param callable $control_callback Run when sidebar is displayed. - * @param array $options { + * @param int|string $id Sidebar ID. + * @param string $name Sidebar display name. + * @param callable $control_callback Run when sidebar is displayed. + * @param array $options { * Optional. Array or string of control options. Default empty array. * * @type int $height Never used. Default 200. @@ -476,8 +503,9 @@ * @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the * text widget. The widget id will end up looking like `{$id_base}-{$unique_number}`. * } + * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. */ -function wp_register_widget_control( $id, $name, $control_callback, $options = array() ) { +function wp_register_widget_control( $id, $name, $control_callback, $options = array(), ...$params ) { global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks; $id = strtolower( $id ); @@ -501,7 +529,7 @@ $defaults = array( 'width' => 250, 'height' => 200, - ); // height is never used + ); // Height is never used. $options = wp_parse_args( $options, $defaults ); $options['width'] = (int) $options['width']; $options['height'] = (int) $options['height']; @@ -510,7 +538,7 @@ 'name' => $name, 'id' => $id, 'callback' => $control_callback, - 'params' => array_slice( func_get_args(), 4 ), + 'params' => $params, ); $widget = array_merge( $widget, $options ); @@ -532,6 +560,8 @@ * Registers the update callback for a widget. * * @since 2.8.0 + * @since 5.3.0 Formalized the existing and already documented `...$params` parameter + * by adding it to the function signature. * * @global array $wp_registered_widget_updates * @@ -539,8 +569,9 @@ * @param callable $update_callback Update callback method for the widget. * @param array $options Optional. Widget control options. See wp_register_widget_control(). * Default empty array. + * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. */ -function _register_widget_update_callback( $id_base, $update_callback, $options = array() ) { +function _register_widget_update_callback( $id_base, $update_callback, $options = array(), ...$params ) { global $wp_registered_widget_updates; if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) { @@ -552,7 +583,7 @@ $widget = array( 'callback' => $update_callback, - 'params' => array_slice( func_get_args(), 3 ), + 'params' => $params, ); $widget = array_merge( $widget, $options ); @@ -563,6 +594,8 @@ * Registers the form callback for a widget. * * @since 2.8.0 + * @since 5.3.0 Formalized the existing and already documented `...$params` parameter + * by adding it to the function signature. * * @global array $wp_registered_widget_controls * @@ -571,8 +604,10 @@ * @param callable $form_callback Form callback. * @param array $options Optional. Widget control options. See wp_register_widget_control(). * Default empty array. + * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. */ -function _register_widget_form_callback( $id, $name, $form_callback, $options = array() ) { + +function _register_widget_form_callback( $id, $name, $form_callback, $options = array(), ...$params ) { global $wp_registered_widget_controls; $id = strtolower( $id ); @@ -598,7 +633,7 @@ 'name' => $name, 'id' => $id, 'callback' => $form_callback, - 'params' => array_slice( func_get_args(), 4 ), + 'params' => $params, ); $widget = array_merge( $widget, $options ); @@ -620,15 +655,15 @@ * Display dynamic sidebar. * * By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or - * 'name' parameter for its registered sidebars you can pass an id or name as the $index parameter. + * 'name' parameter for its registered sidebars you can pass an ID or name as the $index parameter. * Otherwise, you can pass in a numerical index to display the sidebar at that index. * * @since 2.2.0 * * @global array $wp_registered_sidebars Registered sidebars. - * @global array $wp_registered_widgets + * @global array $wp_registered_widgets Registered widgets. * - * @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar. + * @param int|string $index Optional. Index, name or ID of dynamic sidebar. Default 1. * @return bool True, if widget sidebar was found and called. False if not found or not called. */ function dynamic_sidebar( $index = 1 ) { @@ -639,7 +674,7 @@ } else { $index = sanitize_title( $index ); foreach ( (array) $wp_registered_sidebars as $key => $value ) { - if ( sanitize_title( $value['name'] ) == $index ) { + if ( sanitize_title( $value['name'] ) === $index ) { $index = $key; break; } @@ -691,7 +726,7 @@ (array) $wp_registered_widgets[ $id ]['params'] ); - // Substitute HTML id and class attributes into before_widget + // Substitute HTML `id` and `class` attributes into `before_widget`. $classname_ = ''; foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) { if ( is_string( $cn ) ) { @@ -752,16 +787,16 @@ * @param array $widget_id { * An associative array of widget arguments. * - * @type string $name Name of the widget. - * @type string $id Widget ID. - * @type array|callable $callback When the hook is fired on the front end, $callback is an array - * containing the widget object. Fired on the back end, $callback - * is 'wp_widget_control', see $_callback. - * @type array $params An associative array of multi-widget arguments. - * @type string $classname CSS class applied to the widget container. - * @type string $description The widget description. - * @type array $_callback When the hook is fired on the back end, $_callback is populated - * with an array containing the widget object, see $callback. + * @type string $name Name of the widget. + * @type string $id Widget ID. + * @type callable $callback When the hook is fired on the front end, $callback is an array + * containing the widget object. Fired on the back end, $callback + * is 'wp_widget_control', see $_callback. + * @type array $params An associative array of multi-widget arguments. + * @type string $classname CSS class applied to the widget container. + * @type string $description The widget description. + * @type array $_callback When the hook is fired on the back end, $_callback is populated + * with an array containing the widget object, see $callback. * } */ do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] ); @@ -822,10 +857,10 @@ * * @global array $wp_registered_widgets * - * @param string|false $callback Optional, Widget callback to check. Default false. - * @param int|false $widget_id Optional. Widget ID. Optional, but needed for checking. Default false. - * @param string|false $id_base Optional. The base ID of a widget created by extending WP_Widget. Default false. - * @param bool $skip_inactive Optional. Whether to check in 'wp_inactive_widgets'. Default true. + * @param callable|false $callback Optional, Widget callback to check. Default false. + * @param int|false $widget_id Optional. Widget ID. Optional, but needed for checking. Default false. + * @param string|false $id_base Optional. The base ID of a widget created by extending WP_Widget. Default false. + * @param bool $skip_inactive Optional. Whether to check in 'wp_inactive_widgets'. Default true. * @return string|false False if widget is not active or id of sidebar in which the widget is active. */ function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) { @@ -841,8 +876,8 @@ if ( is_array( $widgets ) ) { foreach ( $widgets as $widget ) { - if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] == $callback ) || ( $id_base && _get_widget_id_base( $widget ) == $id_base ) ) { - if ( ! $widget_id || $widget_id == $wp_registered_widgets[ $widget ]['id'] ) { + if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] === $callback ) || ( $id_base && _get_widget_id_base( $widget ) === $id_base ) ) { + if ( ! $widget_id || $widget_id === $wp_registered_widgets[ $widget ]['id'] ) { return $sidebar; } } @@ -862,14 +897,16 @@ * * @since 2.2.0 * - * @global array $wp_registered_widgets + * @global array $wp_registered_widgets Registered widgets. * @global array $wp_registered_sidebars Registered sidebars. * - * @return bool True, if using widgets. False, if not using widgets. + * @return bool True if using widgets, false otherwise. */ function is_dynamic_sidebar() { global $wp_registered_widgets, $wp_registered_sidebars; + $sidebars_widgets = get_option( 'sidebars_widgets' ); + foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) { if ( ! empty( $sidebars_widgets[ $index ] ) ) { foreach ( (array) $sidebars_widgets[ $index ] as $widget ) { @@ -879,11 +916,12 @@ } } } + return false; } /** - * Determines whether a sidebar is in use. + * Determines whether a sidebar contains widgets. * * For more information on this and similar theme functions, check out * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ @@ -892,7 +930,7 @@ * @since 2.8.0 * * @param string|int $index Sidebar name, id or number to check. - * @return bool true if the sidebar is in use, false otherwise. + * @return bool True if the sidebar has widgets, false otherwise. */ function is_active_sidebar( $index ) { $index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index ); @@ -912,7 +950,7 @@ } // -// Internal Functions +// Internal Functions. // /** @@ -931,7 +969,7 @@ * @return array Upgraded list of widgets to version 3 array format when called from the admin. */ function wp_get_sidebars_widgets( $deprecated = true ) { - if ( $deprecated !== true ) { + if ( true !== $deprecated ) { _deprecated_argument( __FUNCTION__, '2.8.1' ); } @@ -1008,25 +1046,27 @@ } /** - * Convert the widget settings from single to multi-widget format. + * Converts the widget settings from single to multi-widget format. * * @since 2.8.0 * * @global array $_wp_sidebars_widgets * - * @param string $base_name - * @param string $option_name - * @param array $settings - * @return array + * @param string $base_name Root ID for all widgets of this type. + * @param string $option_name Option name for this widget type. + * @param array $settings The array of widget instance settings. + * @return array The array of widget settings converted to multi-widget format. */ function wp_convert_widget_settings( $base_name, $option_name, $settings ) { // This test may need expanding. - $single = $changed = false; + $single = false; + $changed = false; + if ( empty( $settings ) ) { $single = true; } else { foreach ( array_keys( $settings ) as $number ) { - if ( 'number' == $number ) { + if ( 'number' === $number ) { continue; } if ( ! is_numeric( $number ) ) { @@ -1039,7 +1079,7 @@ if ( $single ) { $settings = array( 2 => $settings ); - // If loading from the front page, update sidebar in memory but don't save to options + // If loading from the front page, update sidebar in memory but don't save to options. if ( is_admin() ) { $sidebars_widgets = get_option( 'sidebars_widgets' ); } else { @@ -1052,7 +1092,7 @@ foreach ( (array) $sidebars_widgets as $index => $sidebar ) { if ( is_array( $sidebar ) ) { foreach ( $sidebar as $i => $name ) { - if ( $base_name == $name ) { + if ( $base_name === $name ) { $sidebars_widgets[ $index ][ $i ] = "$name-2"; $changed = true; break 2; @@ -1100,8 +1140,15 @@ global $wp_widget_factory; if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) { - /* translators: %s: register_widget() */ - _doing_it_wrong( __FUNCTION__, sprintf( __( 'Widgets need to be registered using %s, before they can be displayed.' ), 'register_widget()' ), '4.9.0' ); + _doing_it_wrong( + __FUNCTION__, + sprintf( + /* translators: %s: register_widget() */ + __( 'Widgets need to be registered using %s, before they can be displayed.' ), + 'register_widget()' + ), + '4.9.0' + ); return; } @@ -1121,6 +1168,13 @@ $instance = wp_parse_args( $instance ); + /** This filter is documented in wp-includes/class-wp-widget.php */ + $instance = apply_filters( 'widget_display_callback', $instance, $widget_obj, $args ); + + if ( false === $instance ) { + return; + } + /** * Fires before rendering the requested widget. * @@ -1173,7 +1227,7 @@ * * @global array $wp_registered_sidebars Registered sidebars. * @global array $sidebars_widgets - * @global array $wp_registered_widgets + * @global array $wp_registered_widgets Registered widgets. * * @param string|bool $theme_changed Whether the theme was changed as a boolean. A value * of 'customize' defers updates for the Customizer. @@ -1229,7 +1283,7 @@ } /** - * Compares a list of sidebars with their widgets against a whitelist. + * Compares a list of sidebars with their widgets against an allowed list. * * @since 4.9.0 * @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping. @@ -1326,11 +1380,11 @@ // Go back and check the next new sidebar. continue 3; } - } // endforeach ( $slug_group as $slug ) - } // endforeach ( $existing_sidebars_widgets as $sidebar => $widgets ) - } // endforeach foreach ( $wp_registered_sidebars as $new_sidebar => $args ) - } // endforeach ( $slug_group as $slug ) - } // endforeach ( $common_slug_groups as $slug_group ) + } // End foreach ( $slug_group as $slug ). + } // End foreach ( $existing_sidebars_widgets as $sidebar => $widgets ). + } // End foreach ( $wp_registered_sidebars as $new_sidebar => $args ). + } // End foreach ( $slug_group as $slug ). + } // End foreach ( $common_slug_groups as $slug_group ). } // Move any left over widgets to inactive sidebar. @@ -1390,11 +1444,11 @@ // ...otherwise remove it from the old sidebar and keep it in the new one. unset( $old_sidebars_widgets[ $old_sidebar ][ $key ] ); } - } // endif ( $active_key ) - } // endforeach ( $old_widgets as $key => $widget_id ) - } // endforeach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) - } // endforeach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) - } // endif ( ! empty( $old_sidebars_widgets ) ) + } // End if ( $active_key ). + } // End foreach ( $old_widgets as $key => $widget_id ). + } // End foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ). + } // End foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ). + } // End if ( ! empty( $old_sidebars_widgets ) ). // Restore widget settings from when theme was previously active. $new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets ); @@ -1404,22 +1458,22 @@ } /** - * Compares a list of sidebars with their widgets against a whitelist. + * Compares a list of sidebars with their widgets against an allowed list. * * @since 4.9.0 * - * @param array $sidebars_widgets List of sidebars and their widget instance IDs. - * @param array $whitelist Optional. List of widget IDs to compare against. Default: Registered widgets. - * @return array Sidebars with whitelisted widgets. + * @param array $sidebars_widgets List of sidebars and their widget instance IDs. + * @param array $allowed_widget_ids Optional. List of widget IDs to compare against. Default: Registered widgets. + * @return array Sidebars with allowed widgets. */ -function _wp_remove_unregistered_widgets( $sidebars_widgets, $whitelist = array() ) { - if ( empty( $whitelist ) ) { - $whitelist = array_keys( $GLOBALS['wp_registered_widgets'] ); +function _wp_remove_unregistered_widgets( $sidebars_widgets, $allowed_widget_ids = array() ) { + if ( empty( $allowed_widget_ids ) ) { + $allowed_widget_ids = array_keys( $GLOBALS['wp_registered_widgets'] ); } foreach ( $sidebars_widgets as $sidebar => $widgets ) { if ( is_array( $widgets ) ) { - $sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $whitelist ); + $sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $allowed_widget_ids ); } } @@ -1431,8 +1485,8 @@ * * @since 2.5.0 * - * @param string|array|object $rss RSS url. - * @param array $args Widget arguments. + * @param string|array|object $rss RSS url. + * @param array $args Widget arguments. */ function wp_widget_rss_output( $rss, $args = array() ) { if ( is_string( $rss ) ) { @@ -1477,7 +1531,7 @@ echo '