Changeset 50993 for trunk/src/wp-includes/widgets.php
- Timestamp:
- 05/25/2021 07:51:14 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/widgets.php
r50961 r50993 358 358 * @since 5.3.0 Formalized the existing and already documented `...$params` parameter 359 359 * by adding it to the function signature. 360 * @since 5.8.0 Added show_instance_in_rest option. 360 361 * 361 362 * @global array $wp_registered_widgets Uses stored registered widgets. … … 370 371 * Optional. An array of supplementary widget options for the instance. 371 372 * 372 * @type string $classname Class name for the widget's HTML container. Default is a shortened 373 * version of the output callback name. 374 * @type string $description Widget description for display in the widget administration 375 * panel and/or theme. 373 * @type string $classname Class name for the widget's HTML container. Default is a shortened 374 * version of the output callback name. 375 * @type string $description Widget description for display in the widget administration 376 * panel and/or theme. 377 * @type bool $show_instance_in_rest Whether to show the widget's instance settings in the REST API. 378 * Only available for WP_Widget based widgets. 376 379 * } 377 380 * @param mixed ...$params Optional additional parameters to pass to the callback function when it's called. … … 1797 1800 register_widget( 'WP_Widget_Custom_HTML' ); 1798 1801 1802 register_widget( 'WP_Widget_Block' ); 1803 1799 1804 /** 1800 1805 * Fires after all default WordPress widgets have been registered. … … 1804 1809 do_action( 'widgets_init' ); 1805 1810 } 1811 1812 /** 1813 * Converts a widget ID into its id_base and number components. 1814 * 1815 * @since 5.8.0 1816 * 1817 * @param string $id Widget ID. 1818 * @return array Array containing a widget's id_base and number components. 1819 */ 1820 function wp_parse_widget_id( $id ) { 1821 $parsed = array(); 1822 1823 if ( preg_match( '/^(.+)-(\d+)$/', $id, $matches ) ) { 1824 $parsed['id_base'] = $matches[1]; 1825 $parsed['number'] = (int) $matches[2]; 1826 } else { 1827 // Likely an old single widget. 1828 $parsed['id_base'] = $id; 1829 } 1830 1831 return $parsed; 1832 } 1833 1834 /** 1835 * Finds the sidebar that a given widget belongs to. 1836 * 1837 * @since 5.8.0 1838 * 1839 * @param string $widget_id The widget id to look for. 1840 * @return string|null The found sidebar's id, or null if it was not found. 1841 */ 1842 function wp_find_widgets_sidebar( $widget_id ) { 1843 foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) { 1844 foreach ( $widget_ids as $maybe_widget_id ) { 1845 if ( $maybe_widget_id === $widget_id ) { 1846 return (string) $sidebar_id; 1847 } 1848 } 1849 } 1850 1851 return null; 1852 } 1853 1854 /** 1855 * Assigns a widget to the given sidebar. 1856 * 1857 * @since 5.8.0 1858 * 1859 * @param string $widget_id The widget id to assign. 1860 * @param string $sidebar_id The sidebar id to assign to. If empty, the widget won't be added to any sidebar. 1861 */ 1862 function wp_assign_widget_to_sidebar( $widget_id, $sidebar_id ) { 1863 $sidebars = wp_get_sidebars_widgets(); 1864 1865 foreach ( $sidebars as $maybe_sidebar_id => $widgets ) { 1866 foreach ( $widgets as $i => $maybe_widget_id ) { 1867 if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) { 1868 unset( $sidebars[ $maybe_sidebar_id ][ $i ] ); 1869 // We could technically break 2 here, but continue looping in case the id is duplicated. 1870 continue 2; 1871 } 1872 } 1873 } 1874 1875 if ( $sidebar_id ) { 1876 $sidebars[ $sidebar_id ][] = $widget_id; 1877 } 1878 1879 wp_set_sidebars_widgets( $sidebars ); 1880 } 1881 1882 /** 1883 * Calls the render callback of a widget and returns the output. 1884 * 1885 * @since 5.8.0 1886 * 1887 * @param string $widget_id Widget ID. 1888 * @param string $sidebar_id Sidebar ID. 1889 * @return string 1890 */ 1891 function wp_render_widget( $widget_id, $sidebar_id ) { 1892 global $wp_registered_widgets, $wp_registered_sidebars; 1893 1894 if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) { 1895 return ''; 1896 } 1897 1898 if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) { 1899 $sidebar = $wp_registered_sidebars[ $sidebar_id ]; 1900 } elseif ( 'wp_inactive_widgets' === $sidebar_id ) { 1901 $sidebar = array(); 1902 } else { 1903 return ''; 1904 } 1905 1906 $params = array_merge( 1907 array( 1908 array_merge( 1909 $sidebar, 1910 array( 1911 'widget_id' => $widget_id, 1912 'widget_name' => $wp_registered_widgets[ $widget_id ]['name'], 1913 ) 1914 ), 1915 ), 1916 (array) $wp_registered_widgets[ $widget_id ]['params'] 1917 ); 1918 1919 // Substitute HTML `id` and `class` attributes into `before_widget`. 1920 $classname_ = ''; 1921 foreach ( (array) $wp_registered_widgets[ $widget_id ]['classname'] as $cn ) { 1922 if ( is_string( $cn ) ) { 1923 $classname_ .= '_' . $cn; 1924 } elseif ( is_object( $cn ) ) { 1925 $classname_ .= '_' . get_class( $cn ); 1926 } 1927 } 1928 $classname_ = ltrim( $classname_, '_' ); 1929 $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ ); 1930 1931 /** This filter is documented in wp-includes/widgets.php */ 1932 $params = apply_filters( 'dynamic_sidebar_params', $params ); 1933 1934 $callback = $wp_registered_widgets[ $widget_id ]['callback']; 1935 1936 ob_start(); 1937 1938 /** This filter is documented in wp-includes/widgets.php */ 1939 do_action( 'dynamic_sidebar', $wp_registered_widgets[ $widget_id ] ); 1940 1941 if ( is_callable( $callback ) ) { 1942 call_user_func_array( $callback, $params ); 1943 } 1944 1945 return ob_get_clean(); 1946 } 1947 1948 /** 1949 * Calls the control callback of a widget and returns the output. 1950 * 1951 * @since 5.8.0 1952 * 1953 * @param string $id Widget ID. 1954 * @return string|null 1955 */ 1956 function wp_render_widget_control( $id ) { 1957 global $wp_registered_widget_controls; 1958 1959 if ( ! isset( $wp_registered_widget_controls[ $id ]['callback'] ) ) { 1960 return null; 1961 } 1962 1963 $callback = $wp_registered_widget_controls[ $id ]['callback']; 1964 $params = $wp_registered_widget_controls[ $id ]['params']; 1965 1966 ob_start(); 1967 1968 if ( is_callable( $callback ) ) { 1969 call_user_func_array( $callback, $params ); 1970 } 1971 1972 return ob_get_clean(); 1973 }
Note: See TracChangeset
for help on using the changeset viewer.