mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 14:20:15 +00:00
Widgets: Rename and soft deprecate retrieve_widgets().
The original name `retrieve_widgets()` was unclear as it suggested it was a getter, i.e. getting the widgets. This function does more than get: finds orphaned widgets, assigns them to the inactive sidebar, and updates the database. The new name is `sync_registered_widgets()` which better represents what happens when this function is invoked. The original `retrieve_widgets()` function is soft deprecated to avoid unnecessary code churn downstream for developers that support more than the latest version of WordPress. Follow-up to [18630]. Props zieladam, timothyblynjacobs, andraganescu, hellofromTonya. See #53811. git-svn-id: https://develop.svn.wordpress.org/trunk@51705 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1773,7 +1773,7 @@ function upgrade_330() {
|
||||
|
||||
// Intentional fall-through to upgrade to the next version.
|
||||
case 2:
|
||||
$sidebars_widgets = retrieve_widgets();
|
||||
$sidebars_widgets = sync_registered_widgets();
|
||||
$sidebars_widgets['array_version'] = 3;
|
||||
update_option( 'sidebars_widgets', $sidebars_widgets );
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ register_sidebar(
|
||||
)
|
||||
);
|
||||
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
// We're saving a widget without JS.
|
||||
if ( isset( $_POST['savewidget'] ) || isset( $_POST['removewidget'] ) ) {
|
||||
|
||||
@@ -275,9 +275,9 @@ final class WP_Customize_Widgets {
|
||||
add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) );
|
||||
$this->manager->set_post_value( 'old_sidebars_widgets_data', $this->old_sidebars_widgets ); // Override any value cached in changeset.
|
||||
|
||||
// retrieve_widgets() looks at the global $sidebars_widgets.
|
||||
// sync_registered_widgets() looks at the global $sidebars_widgets.
|
||||
$sidebars_widgets = $this->old_sidebars_widgets;
|
||||
$sidebars_widgets = retrieve_widgets( 'customize' );
|
||||
$sidebars_widgets = sync_registered_widgets( 'customize' );
|
||||
add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 );
|
||||
// Reset global cache var used by wp_get_sidebars_widgets().
|
||||
unset( $GLOBALS['_wp_sidebars_widgets'] );
|
||||
@@ -287,7 +287,7 @@ final class WP_Customize_Widgets {
|
||||
* Filters old_sidebars_widgets_data Customizer setting.
|
||||
*
|
||||
* When switching themes, filter the Customizer setting old_sidebars_widgets_data
|
||||
* to supply initial $sidebars_widgets before they were overridden by retrieve_widgets().
|
||||
* to supply initial $sidebars_widgets before they were overridden by sync_registered_widgets().
|
||||
* The value for old_sidebars_widgets_data gets set in the old theme's sidebars_widgets
|
||||
* theme_mod.
|
||||
*
|
||||
@@ -305,7 +305,7 @@ final class WP_Customize_Widgets {
|
||||
/**
|
||||
* Filters sidebars_widgets option for theme switch.
|
||||
*
|
||||
* When switching themes, the retrieve_widgets() function is run when the Customizer initializes,
|
||||
* When switching themes, the sync_registered_widgets() function is run when the Customizer initializes,
|
||||
* and then the new sidebars_widgets here get supplied as the default value for the sidebars_widgets
|
||||
* option.
|
||||
*
|
||||
|
||||
@@ -98,7 +98,7 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller {
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
$data = array();
|
||||
foreach ( wp_get_sidebars_widgets() as $id => $widgets ) {
|
||||
@@ -137,7 +137,7 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller {
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
$sidebar = $this->get_sidebar( $request['id'] );
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
$prepared = array();
|
||||
|
||||
@@ -151,7 +151,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
$widget_id = $request['id'];
|
||||
$sidebar_id = wp_find_widgets_sidebar( $widget_id );
|
||||
@@ -237,7 +237,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
|
||||
global $wp_widget_factory;
|
||||
|
||||
/*
|
||||
* retrieve_widgets() contains logic to move "hidden" or "lost" widgets to the
|
||||
* sync_registered_widgets() contains logic to move "hidden" or "lost" widgets to the
|
||||
* wp_inactive_widgets sidebar based on the contents of the $sidebars_widgets global.
|
||||
*
|
||||
* When batch requests are processed, this global is not properly updated by previous
|
||||
@@ -248,7 +248,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
|
||||
*/
|
||||
wp_get_sidebars_widgets();
|
||||
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
$widget_id = $request['id'];
|
||||
$sidebar_id = wp_find_widgets_sidebar( $widget_id );
|
||||
@@ -313,7 +313,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
|
||||
global $wp_widget_factory, $wp_registered_widget_updates;
|
||||
|
||||
/*
|
||||
* retrieve_widgets() contains logic to move "hidden" or "lost" widgets to the
|
||||
* sync_registered_widgets() contains logic to move "hidden" or "lost" widgets to the
|
||||
* wp_inactive_widgets sidebar based on the contents of the $sidebars_widgets global.
|
||||
*
|
||||
* When batch requests are processed, this global is not properly updated by previous
|
||||
@@ -324,7 +324,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
|
||||
*/
|
||||
wp_get_sidebars_widgets();
|
||||
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
$widget_id = $request['id'];
|
||||
$sidebar_id = wp_find_widgets_sidebar( $widget_id );
|
||||
|
||||
@@ -805,7 +805,7 @@ function switch_theme( $stylesheet ) {
|
||||
add_option( "theme_mods_$stylesheet", $default_theme_mods );
|
||||
} else {
|
||||
/*
|
||||
* Since retrieve_widgets() is called when initializing a theme in the Customizer,
|
||||
* Since sync_registered_widgets() is called when initializing a theme in the Customizer,
|
||||
* we need to remove the theme mods to avoid overwriting changes made via
|
||||
* the Customizer when accessing wp-admin/widgets.php.
|
||||
*/
|
||||
|
||||
@@ -1251,13 +1251,30 @@ function _wp_sidebars_changed() {
|
||||
$sidebars_widgets = wp_get_sidebars_widgets();
|
||||
}
|
||||
|
||||
retrieve_widgets( true );
|
||||
sync_registered_widgets( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for "lost" widgets, this has to run at least on each theme change.
|
||||
* Do not use, deprecated.
|
||||
*
|
||||
* Use sync_registered_widgets() instead.
|
||||
*
|
||||
* @since 2.8.0
|
||||
* @deprecated 5.8.1 Use sync_registered_widgets()
|
||||
* @see sync_registered_widgets()
|
||||
*
|
||||
* @param string|bool $theme_changed
|
||||
* @return array
|
||||
*/
|
||||
function retrieve_widgets( $theme_changed = false ) {
|
||||
return sync_registered_widgets( $theme_changed );
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for "lost" widgets and Updates widgets-to-sidebars allocation.
|
||||
* This has to run at least on each theme change.
|
||||
*
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @global array $wp_registered_sidebars Registered sidebars.
|
||||
* @global array $sidebars_widgets
|
||||
@@ -1267,7 +1284,7 @@ function _wp_sidebars_changed() {
|
||||
* of 'customize' defers updates for the Customizer.
|
||||
* @return array Updated sidebars widgets.
|
||||
*/
|
||||
function retrieve_widgets( $theme_changed = false ) {
|
||||
function sync_registered_widgets( $theme_changed = false ) {
|
||||
global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
|
||||
|
||||
$registered_sidebars_keys = array_keys( $wp_registered_sidebars );
|
||||
|
||||
@@ -843,9 +843,9 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
/**
|
||||
* Tests for when 'sidebars_widgets' theme mod is populated.
|
||||
*
|
||||
* @covers ::retrieve_widgets
|
||||
* @covers ::sync_registered_widgets
|
||||
*/
|
||||
function test_retrieve_widgets_with_theme_mod() {
|
||||
function test_sync_registered_widgets_with_theme_mod() {
|
||||
global $sidebars_widgets, $_wp_sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
@@ -873,7 +873,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
|
||||
$result = retrieve_widgets( true );
|
||||
$result = sync_registered_widgets( true );
|
||||
|
||||
$this->assertIsArray( $result );
|
||||
$this->assertSame( $result, $sidebars_widgets );
|
||||
@@ -906,9 +906,9 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
/**
|
||||
* Tests for when sidebars widgets matches registered sidebars.
|
||||
*
|
||||
* @covers ::retrieve_widgets
|
||||
* @covers ::sync_registered_widgets
|
||||
*/
|
||||
function test_retrieve_widgets_with_sidebars_widgets_matching_registered_sidebars() {
|
||||
function test_sync_registered_widgets_with_sidebars_widgets_matching_registered_sidebars() {
|
||||
global $sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
@@ -921,7 +921,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
'wp_inactive_widgets' => array(),
|
||||
);
|
||||
|
||||
$result = retrieve_widgets( true );
|
||||
$result = sync_registered_widgets( true );
|
||||
|
||||
// $sidebars_widgets matches registered sidebars.
|
||||
$this->assertIsArray( $result );
|
||||
@@ -944,9 +944,9 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
/**
|
||||
* Tests for when sidebars widgets doesn't match registered sidebars.
|
||||
*
|
||||
* @covers ::retrieve_widgets
|
||||
* @covers ::sync_registered_widgets
|
||||
*/
|
||||
function test_retrieve_widgets_with_sidebars_widgets_not_matching_registered_sidebars() {
|
||||
function test_sync_registered_widgets_with_sidebars_widgets_not_matching_registered_sidebars() {
|
||||
global $sidebars_widgets, $_wp_sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
@@ -960,7 +960,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Theme changed.
|
||||
$result = retrieve_widgets( true );
|
||||
$result = sync_registered_widgets( true );
|
||||
|
||||
$_wp_sidebars_widgets = array();
|
||||
$this->assertIsArray( $result );
|
||||
@@ -1002,7 +1002,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Theme did not change.
|
||||
$result = retrieve_widgets();
|
||||
$result = sync_registered_widgets();
|
||||
|
||||
$_wp_sidebars_widgets = array();
|
||||
$this->assertIsArray( $result );
|
||||
@@ -1036,9 +1036,9 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
/**
|
||||
* Tests for Customizer mode.
|
||||
*
|
||||
* @covers ::retrieve_widgets
|
||||
* @covers ::sync_registered_widgets
|
||||
*/
|
||||
function test_retrieve_widgets_for_customizer() {
|
||||
function test_sync_registered_widgets_for_customizer() {
|
||||
global $sidebars_widgets, $_wp_sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
@@ -1053,7 +1053,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
);
|
||||
set_theme_mod( 'sidebars_widgets', $old_sidebars_widgets );
|
||||
|
||||
$result = retrieve_widgets( 'customize' );
|
||||
$result = sync_registered_widgets( 'customize' );
|
||||
|
||||
$_wp_sidebars_widgets = array();
|
||||
$this->assertIsArray( $result );
|
||||
@@ -1082,7 +1082,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
$this->assertNotEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
|
||||
}
|
||||
|
||||
function test_retrieve_widgets_with_single_widget() {
|
||||
function test_sync_registered_widgets_with_single_widget() {
|
||||
global $sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
@@ -1099,7 +1099,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
);
|
||||
|
||||
// Theme changed.
|
||||
$result = retrieve_widgets( true );
|
||||
$result = sync_registered_widgets( true );
|
||||
|
||||
$this->assertContains( 'single', $result['wp_inactive_widgets'] );
|
||||
}
|
||||
@@ -1107,9 +1107,9 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
/**
|
||||
* Tests for orphaned widgets being moved into inactive widgets.
|
||||
*
|
||||
* @covers ::retrieve_widgets
|
||||
* @covers ::sync_registered_widgets
|
||||
*/
|
||||
function test_retrieve_widgets_move_orphaned_widgets_to_inactive() {
|
||||
function test_sync_registered_widgets_move_orphaned_widgets_to_inactive() {
|
||||
global $sidebars_widgets;
|
||||
|
||||
wp_widgets_init();
|
||||
@@ -1122,7 +1122,7 @@ class Tests_Widgets extends WP_UnitTestCase {
|
||||
'orphaned_widgets_1' => array( 'calendar-1' ),
|
||||
);
|
||||
|
||||
retrieve_widgets();
|
||||
sync_registered_widgets();
|
||||
|
||||
$this->assertIsArray( $sidebars_widgets );
|
||||
|
||||
|
||||
Reference in New Issue
Block a user