wp/wp-includes/widgets.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   218  * If theme support for 'widgets' has not yet been added when this function is
   218  * If theme support for 'widgets' has not yet been added when this function is
   219  * called, it will be automatically enabled through the use of add_theme_support()
   219  * called, it will be automatically enabled through the use of add_theme_support()
   220  *
   220  *
   221  * @since 2.2.0
   221  * @since 2.2.0
   222  * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments.
   222  * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments.
       
   223  * @since 5.9.0 Added the `show_in_rest` argument.
   223  *
   224  *
   224  * @global array $wp_registered_sidebars Registered sidebars.
   225  * @global array $wp_registered_sidebars Registered sidebars.
   225  *
   226  *
   226  * @param array|string $args {
   227  * @param array|string $args {
   227  *     Optional. Array or string of arguments for the sidebar being registered.
   228  *     Optional. Array or string of arguments for the sidebar being registered.
   248  *                                  Outputs after the {@see 'dynamic_sidebar_before'} action.
   249  *                                  Outputs after the {@see 'dynamic_sidebar_before'} action.
   249  *                                  Default empty string.
   250  *                                  Default empty string.
   250  *     @type string $after_sidebar  HTML content to append to the sidebar when displayed.
   251  *     @type string $after_sidebar  HTML content to append to the sidebar when displayed.
   251  *                                  Outputs before the {@see 'dynamic_sidebar_after'} action.
   252  *                                  Outputs before the {@see 'dynamic_sidebar_after'} action.
   252  *                                  Default empty string.
   253  *                                  Default empty string.
       
   254  *     @type bool $show_in_rest     Whether to show this sidebar publicly in the REST API.
       
   255  *                                  Defaults to only showing the sidebar to administrator users.
   253  * }
   256  * }
   254  * @return string Sidebar ID added to $wp_registered_sidebars global.
   257  * @return string Sidebar ID added to $wp_registered_sidebars global.
   255  */
   258  */
   256 function register_sidebar( $args = array() ) {
   259 function register_sidebar( $args = array() ) {
   257 	global $wp_registered_sidebars;
   260 	global $wp_registered_sidebars;
   270 		'after_widget'   => "</li>\n",
   273 		'after_widget'   => "</li>\n",
   271 		'before_title'   => '<h2 class="widgettitle">',
   274 		'before_title'   => '<h2 class="widgettitle">',
   272 		'after_title'    => "</h2>\n",
   275 		'after_title'    => "</h2>\n",
   273 		'before_sidebar' => '',
   276 		'before_sidebar' => '',
   274 		'after_sidebar'  => '',
   277 		'after_sidebar'  => '',
       
   278 		'show_in_rest'   => false,
   275 	);
   279 	);
   276 
   280 
   277 	/**
   281 	/**
   278 	 * Filters the sidebar default arguments.
   282 	 * Filters the sidebar default arguments.
   279 	 *
   283 	 *
   483 	/**
   487 	/**
   484 	 * Fires just before a widget is removed from a sidebar.
   488 	 * Fires just before a widget is removed from a sidebar.
   485 	 *
   489 	 *
   486 	 * @since 3.0.0
   490 	 * @since 3.0.0
   487 	 *
   491 	 *
   488 	 * @param int $id The widget ID.
   492 	 * @param int|string $id The widget ID.
   489 	 */
   493 	 */
   490 	do_action( 'wp_unregister_sidebar_widget', $id );
   494 	do_action( 'wp_unregister_sidebar_widget', $id );
   491 
   495 
   492 	wp_register_sidebar_widget( $id, '', '' );
   496 	wp_register_sidebar_widget( $id, '', '' );
   493 	wp_unregister_widget_control( $id );
   497 	wp_unregister_widget_control( $id );
   513  *
   517  *
   514  *     @type int        $height  Never used. Default 200.
   518  *     @type int        $height  Never used. Default 200.
   515  *     @type int        $width   Width of the fully expanded control form (but try hard to use the default width).
   519  *     @type int        $width   Width of the fully expanded control form (but try hard to use the default width).
   516  *                               Default 250.
   520  *                               Default 250.
   517  *     @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the
   521  *     @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the
   518  *                               text widget. The widget id will end up looking like `{$id_base}-{$unique_number}`.
   522  *                               text widget. The widget ID will end up looking like `{$id_base}-{$unique_number}`.
   519  * }
   523  * }
   520  * @param mixed      ...$params        Optional additional parameters to pass to the callback function when it's called.
   524  * @param mixed      ...$params        Optional additional parameters to pass to the callback function when it's called.
   521  */
   525  */
   522 function wp_register_widget_control( $id, $name, $control_callback, $options = array(), ...$params ) {
   526 function wp_register_widget_control( $id, $name, $control_callback, $options = array(), ...$params ) {
   523 	global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
   527 	global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks;
  1034 	 */
  1038 	 */
  1035 	return apply_filters( 'sidebars_widgets', $sidebars_widgets );
  1039 	return apply_filters( 'sidebars_widgets', $sidebars_widgets );
  1036 }
  1040 }
  1037 
  1041 
  1038 /**
  1042 /**
       
  1043  * Retrieves the registered sidebar with the given ID.
       
  1044  *
       
  1045  * @since 5.9.0
       
  1046  *
       
  1047  * @global array $wp_registered_sidebars The registered sidebars.
       
  1048  *
       
  1049  * @param string $id The sidebar ID.
       
  1050  * @return array|null The discovered sidebar, or null if it is not registered.
       
  1051  */
       
  1052 function wp_get_sidebar( $id ) {
       
  1053 	global $wp_registered_sidebars;
       
  1054 
       
  1055 	foreach ( (array) $wp_registered_sidebars as $sidebar ) {
       
  1056 		if ( $sidebar['id'] === $id ) {
       
  1057 			return $sidebar;
       
  1058 		}
       
  1059 	}
       
  1060 
       
  1061 	if ( 'wp_inactive_widgets' === $id ) {
       
  1062 		return array(
       
  1063 			'id'   => 'wp_inactive_widgets',
       
  1064 			'name' => __( 'Inactive widgets' ),
       
  1065 		);
       
  1066 	}
       
  1067 
       
  1068 	return null;
       
  1069 }
       
  1070 
       
  1071 /**
  1039  * Set the sidebar widget option to update sidebars.
  1072  * Set the sidebar widget option to update sidebars.
  1040  *
  1073  *
  1041  * @since 2.2.0
  1074  * @since 2.2.0
  1042  * @access private
  1075  * @access private
  1043  *
  1076  *
  1253 
  1286 
  1254 	retrieve_widgets( true );
  1287 	retrieve_widgets( true );
  1255 }
  1288 }
  1256 
  1289 
  1257 /**
  1290 /**
  1258  * Look for "lost" widgets, this has to run at least on each theme change.
  1291  * Validates and remaps any "orphaned" widgets to wp_inactive_widgets sidebar,
       
  1292  * and saves the widget settings. This has to run at least on each theme change.
       
  1293  *
       
  1294  * For example, let's say theme A has a "footer" sidebar, and theme B doesn't have one.
       
  1295  * After switching from theme A to theme B, all the widgets previously assigned
       
  1296  * to the footer would be inaccessible. This function detects this scenario, and
       
  1297  * moves all the widgets previously assigned to the footer under wp_inactive_widgets.
       
  1298  *
       
  1299  * Despite the word "retrieve" in the name, this function actually updates the database
       
  1300  * and the global `$sidebars_widgets`. For that reason it should not be run on front end,
       
  1301  * unless the `$theme_changed` value is 'customize' (to bypass the database write).
  1259  *
  1302  *
  1260  * @since 2.8.0
  1303  * @since 2.8.0
  1261  *
  1304  *
  1262  * @global array $wp_registered_sidebars Registered sidebars.
  1305  * @global array $wp_registered_sidebars Registered sidebars.
  1263  * @global array $sidebars_widgets
  1306  * @global array $sidebars_widgets
  1308 		}
  1351 		}
  1309 	}
  1352 	}
  1310 	$sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] );
  1353 	$sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] );
  1311 
  1354 
  1312 	if ( 'customize' !== $theme_changed ) {
  1355 	if ( 'customize' !== $theme_changed ) {
       
  1356 		// Update the widgets settings in the database.
  1313 		wp_set_sidebars_widgets( $sidebars_widgets );
  1357 		wp_set_sidebars_widgets( $sidebars_widgets );
  1314 	}
  1358 	}
  1315 
  1359 
  1316 	return $sidebars_widgets;
  1360 	return $sidebars_widgets;
  1317 }
  1361 }
  1726 	$url          = esc_url_raw( strip_tags( $widget_rss['url'] ) );
  1770 	$url          = esc_url_raw( strip_tags( $widget_rss['url'] ) );
  1727 	$title        = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
  1771 	$title        = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : '';
  1728 	$show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
  1772 	$show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0;
  1729 	$show_author  = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
  1773 	$show_author  = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0;
  1730 	$show_date    = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
  1774 	$show_date    = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0;
       
  1775 	$error        = false;
       
  1776 	$link         = '';
  1731 
  1777 
  1732 	if ( $check_feed ) {
  1778 	if ( $check_feed ) {
  1733 		$rss   = fetch_feed( $url );
  1779 		$rss = fetch_feed( $url );
  1734 		$error = false;
  1780 
  1735 		$link  = '';
       
  1736 		if ( is_wp_error( $rss ) ) {
  1781 		if ( is_wp_error( $rss ) ) {
  1737 			$error = $rss->get_error_message();
  1782 			$error = $rss->get_error_message();
  1738 		} else {
  1783 		} else {
  1739 			$link = esc_url( strip_tags( $rss->get_permalink() ) );
  1784 			$link = esc_url( strip_tags( $rss->get_permalink() ) );
  1740 			while ( stristr( $link, 'http' ) !== $link ) {
  1785 			while ( stristr( $link, 'http' ) !== $link ) {
  1826  * unless a theme has removed support for widgets-block-editor or a plugin has
  1871  * unless a theme has removed support for widgets-block-editor or a plugin has
  1827  * filtered the return value of this function.
  1872  * filtered the return value of this function.
  1828  *
  1873  *
  1829  * @since 5.8.0
  1874  * @since 5.8.0
  1830  *
  1875  *
  1831  * @return boolean Whether or not to use the block editor to manage widgets.
  1876  * @return bool Whether to use the block editor to manage widgets.
  1832  */
  1877  */
  1833 function wp_use_widgets_block_editor() {
  1878 function wp_use_widgets_block_editor() {
  1834 	/**
  1879 	/**
  1835 	 * Filters whether or not to use the block editor to manage widgets.
  1880 	 * Filters whether to use the block editor to manage widgets.
  1836 	 *
  1881 	 *
  1837 	 * @since 5.8.0
  1882 	 * @since 5.8.0
  1838 	 *
  1883 	 *
  1839 	 * @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets.
  1884 	 * @param bool $use_widgets_block_editor Whether to use the block editor to manage widgets.
  1840 	 */
  1885 	 */
  1841 	return apply_filters(
  1886 	return apply_filters(
  1842 		'use_widgets_block_editor',
  1887 		'use_widgets_block_editor',
  1843 		get_theme_support( 'widgets-block-editor' )
  1888 		get_theme_support( 'widgets-block-editor' )
  1844 	);
  1889 	);
  1869 /**
  1914 /**
  1870  * Finds the sidebar that a given widget belongs to.
  1915  * Finds the sidebar that a given widget belongs to.
  1871  *
  1916  *
  1872  * @since 5.8.0
  1917  * @since 5.8.0
  1873  *
  1918  *
  1874  * @param string $widget_id The widget id to look for.
  1919  * @param string $widget_id The widget ID to look for.
  1875  * @return string|null The found sidebar's id, or null if it was not found.
  1920  * @return string|null The found sidebar's ID, or null if it was not found.
  1876  */
  1921  */
  1877 function wp_find_widgets_sidebar( $widget_id ) {
  1922 function wp_find_widgets_sidebar( $widget_id ) {
  1878 	foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
  1923 	foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) {
  1879 		foreach ( $widget_ids as $maybe_widget_id ) {
  1924 		foreach ( $widget_ids as $maybe_widget_id ) {
  1880 			if ( $maybe_widget_id === $widget_id ) {
  1925 			if ( $maybe_widget_id === $widget_id ) {
  1889 /**
  1934 /**
  1890  * Assigns a widget to the given sidebar.
  1935  * Assigns a widget to the given sidebar.
  1891  *
  1936  *
  1892  * @since 5.8.0
  1937  * @since 5.8.0
  1893  *
  1938  *
  1894  * @param string $widget_id  The widget id to assign.
  1939  * @param string $widget_id  The widget ID to assign.
  1895  * @param string $sidebar_id The sidebar id to assign to. If empty, the widget won't be added to any sidebar.
  1940  * @param string $sidebar_id The sidebar ID to assign to. If empty, the widget won't be added to any sidebar.
  1896  */
  1941  */
  1897 function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
  1942 function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
  1898 	$sidebars = wp_get_sidebars_widgets();
  1943 	$sidebars = wp_get_sidebars_widgets();
  1899 
  1944 
  1900 	foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
  1945 	foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
  1901 		foreach ( $widgets as $i => $maybe_widget_id ) {
  1946 		foreach ( $widgets as $i => $maybe_widget_id ) {
  1902 			if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
  1947 			if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
  1903 				unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
  1948 				unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
  1904 				// We could technically break 2 here, but continue looping in case the id is duplicated.
  1949 				// We could technically break 2 here, but continue looping in case the ID is duplicated.
  1905 				continue 2;
  1950 				continue 2;
  1906 			}
  1951 			}
  1907 		}
  1952 		}
  1908 	}
  1953 	}
  1909 
  1954 
  2033 		$wp_scripts->query( 'wp-customize-widgets', 'enqueued' )
  2078 		$wp_scripts->query( 'wp-customize-widgets', 'enqueued' )
  2034 	) {
  2079 	) {
  2035 		if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) {
  2080 		if ( $wp_scripts->query( 'wp-editor', 'enqueued' ) ) {
  2036 			_doing_it_wrong(
  2081 			_doing_it_wrong(
  2037 				'wp_enqueue_script()',
  2082 				'wp_enqueue_script()',
  2038 				'"wp-editor" script should not be enqueued together with the new widgets editor (wp-edit-widgets or wp-customize-widgets).',
  2083 				sprintf(
       
  2084 					/* translators: 1: 'wp-editor', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
       
  2085 					__( '"%1$s" script should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
       
  2086 					'wp-editor',
       
  2087 					'wp-edit-widgets',
       
  2088 					'wp-customize-widgets'
       
  2089 				),
  2039 				'5.8.0'
  2090 				'5.8.0'
  2040 			);
  2091 			);
  2041 		}
  2092 		}
  2042 		if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) {
  2093 		if ( $wp_styles->query( 'wp-edit-post', 'enqueued' ) ) {
  2043 			_doing_it_wrong(
  2094 			_doing_it_wrong(
  2044 				'wp_enqueue_style()',
  2095 				'wp_enqueue_style()',
  2045 				'"wp-edit-post" style should not be enqueued together with the new widgets editor (wp-edit-widgets or wp-customize-widgets).',
  2096 				sprintf(
       
  2097 					/* translators: 1: 'wp-edit-post', 2: 'wp-edit-widgets', 3: 'wp-customize-widgets'. */
       
  2098 					__( '"%1$s" style should not be enqueued together with the new widgets editor (%2$s or %3$s).' ),
       
  2099 					'wp-edit-post',
       
  2100 					'wp-edit-widgets',
       
  2101 					'wp-customize-widgets'
       
  2102 				),
  2046 				'5.8.0'
  2103 				'5.8.0'
  2047 			);
  2104 			);
  2048 		}
  2105 		}
  2049 	}
  2106 	}
  2050 }
  2107 }