PluginProbe
Gutenberg / 10.3.2
Gutenberg v10.3.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / widgets-api.php

widgets-api.php in Gutenberg 10.3.2, at lib/widgets-api.php

208 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions for working with widgets in WordPress. These should ultimately live
4 * in wp-includes/widgets.php, wp-admin/includes/widgets.php, etc. when merged
5 * to Core.
6 *
7 * @package gutenberg
8 */
9
10 /**
11 * Assigns a widget to the given sidebar.
12 *
13 * Belongs in wp-includes/widgets.php when merged to Core.
14 *
15 * @since 10.2.0
16 *
17 * @param string $widget_id The widget id to assign.
18 * @param string $sidebar_id The sidebar id to assign to. If empty, the widget won't be added to any sidebar.
19 */
20 function gutenberg_assign_widget_to_sidebar( $widget_id, $sidebar_id ) {
21 $sidebars = wp_get_sidebars_widgets();
22
23 foreach ( $sidebars as $maybe_sidebar_id => $widgets ) {
24 foreach ( $widgets as $i => $maybe_widget_id ) {
25 if ( $widget_id === $maybe_widget_id && $sidebar_id !== $maybe_sidebar_id ) {
26 unset( $sidebars[ $maybe_sidebar_id ][ $i ] );
27 // We could technically break 2 here, but continue looping in case the id is duplicated.
28 continue 2;
29 }
30 }
31 }
32
33 if ( $sidebar_id ) {
34 $sidebars[ $sidebar_id ][] = $widget_id;
35 }
36
37 wp_set_sidebars_widgets( $sidebars );
38 }
39
40 /**
41 * Converts a widget ID into its id_base and number components.
42 *
43 * Belongs in wp-includes/widgets.php when merged to Core.
44 * WP_Customize_Widgets::parse_widget_id should then be deprecated.
45 *
46 * @since 10.2.0
47 *
48 * @param string $id Widget ID.
49 * @return array Array containing a widget's id_base and number components.
50 */
51 function gutenberg_parse_widget_id( $id ) {
52 $parsed = array();
53
54 if ( preg_match( '/^(.+)-(\d+)$/', $id, $matches ) ) {
55 $parsed['id_base'] = $matches[1];
56 $parsed['number'] = (int) $matches[2];
57 } else {
58 // Likely an old single widget.
59 $parsed['id_base'] = $id;
60 }
61
62 return $parsed;
63 }
64
65 /**
66 * Calls the render callback of a widget and returns the output.
67 *
68 * Belongs in wp-includes/widgets.php when merged to Core. Some of the code in
69 * dynamic_sidebar() and WP_Customize_Widgets should then be DRYed up.
70 *
71 * @since 10.2.0
72 *
73 * @param string $widget_id Widget ID.
74 * @param string $sidebar_id Sidebar ID.
75 * @return string
76 */
77 function gutenberg_render_widget( $widget_id, $sidebar_id ) {
78 global $wp_registered_widgets, $wp_registered_sidebars;
79
80 if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) {
81 return '';
82 }
83
84 if ( isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
85 $sidebar = $wp_registered_sidebars[ $sidebar_id ];
86 } elseif ( 'wp_inactive_widgets' === $sidebar_id ) {
87 $sidebar = array();
88 } else {
89 return '';
90 }
91
92 $params = array_merge(
93 array(
94 array_merge(
95 $sidebar,
96 array(
97 'widget_id' => $widget_id,
98 'widget_name' => $wp_registered_widgets[ $widget_id ]['name'],
99 )
100 ),
101 ),
102 (array) $wp_registered_widgets[ $widget_id ]['params']
103 );
104
105 // Substitute HTML `id` and `class` attributes into `before_widget`.
106 $classname_ = '';
107 foreach ( (array) $wp_registered_widgets[ $widget_id ]['classname'] as $cn ) {
108 if ( is_string( $cn ) ) {
109 $classname_ .= '_' . $cn;
110 } elseif ( is_object( $cn ) ) {
111 $classname_ .= '_' . get_class( $cn );
112 }
113 }
114 $classname_ = ltrim( $classname_, '_' );
115 $params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $widget_id, $classname_ );
116
117 /** This filter is documented in wp-includes/widgets.php */
118 $params = apply_filters( 'dynamic_sidebar_params', $params );
119
120 $callback = $wp_registered_widgets[ $widget_id ]['callback'];
121
122 ob_start();
123
124 /** This filter is documented in wp-includes/widgets.php */
125 do_action( 'dynamic_sidebar', $wp_registered_widgets[ $widget_id ] );
126
127 if ( is_callable( $callback ) ) {
128 call_user_func_array( $callback, $params );
129 }
130
131 return ob_get_clean();
132 }
133
134 /**
135 * Calls the control callback of a widget and returns the output.
136 *
137 * Belongs in wp-admin/includes/widgets.php when merged to Core. Some of the
138 * code in wp_widget_control() should then be DRYed up.
139 *
140 * @since 10.2.0
141 *
142 * @param string $id Widget ID.
143 * @return string|null
144 */
145 function gutenberg_render_widget_control( $id ) {
146 global $wp_registered_widget_controls;
147
148 if ( ! isset( $wp_registered_widget_controls[ $id ]['callback'] ) ) {
149 return null;
150 }
151
152 $callback = $wp_registered_widget_controls[ $id ]['callback'];
153 $params = $wp_registered_widget_controls[ $id ]['params'];
154
155 ob_start();
156
157 if ( is_callable( $callback ) ) {
158 call_user_func_array( $callback, $params );
159 }
160
161 return ob_get_clean();
162 }
163
164 /**
165 * Returns the instance settings of the given widget. Must be a widget that
166 * is registered using WP_Widget.
167 *
168 * Belongs in WP_Widget when merged to Core.
169 *
170 * @since 10.2.0
171 *
172 * @param string $id Widget ID.
173 * @return array|null
174 */
175 function gutenberg_get_widget_instance( $id ) {
176 $parsed_id = gutenberg_parse_widget_id( $id );
177 $widget_object = gutenberg_get_widget_object( $parsed_id['id_base'] );
178
179 if ( ! isset( $parsed_id['number'] ) || ! $widget_object ) {
180 return null;
181 }
182
183 $all_instances = $widget_object->get_settings();
184 return $all_instances[ $parsed_id['number'] ];
185 }
186
187 /**
188 * Returns the registered WP_Widget object for the given widget type.
189 *
190 * Belongs in WP_Widget_Factory when merged to Core.
191 *
192 * @since 10.2.0
193 *
194 * @param string $id_base Widget type ID.
195 * @return WP_Widget|null
196 */
197 function gutenberg_get_widget_object( $id_base ) {
198 global $wp_widget_factory;
199
200 foreach ( $wp_widget_factory->widgets as $widget_object ) {
201 if ( $widget_object->id_base === $id_base ) {
202 return $widget_object;
203 }
204 }
205
206 return null;
207 }
208