84 * |
92 * |
85 * @param WP_REST_Request $request Full details about the request. |
93 * @param WP_REST_Request $request Full details about the request. |
86 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
94 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
87 */ |
95 */ |
88 public function get_items_permissions_check( $request ) { |
96 public function get_items_permissions_check( $request ) { |
89 return $this->do_permissions_check(); |
97 $this->retrieve_widgets(); |
90 } |
|
91 |
|
92 /** |
|
93 * Retrieves the list of sidebars (active or inactive). |
|
94 * |
|
95 * @since 5.8.0 |
|
96 * |
|
97 * @param WP_REST_Request $request Full details about the request. |
|
98 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
99 */ |
|
100 public function get_items( $request ) { |
|
101 retrieve_widgets(); |
|
102 |
|
103 $data = array(); |
|
104 foreach ( wp_get_sidebars_widgets() as $id => $widgets ) { |
98 foreach ( wp_get_sidebars_widgets() as $id => $widgets ) { |
105 $sidebar = $this->get_sidebar( $id ); |
99 $sidebar = $this->get_sidebar( $id ); |
106 |
100 |
107 if ( ! $sidebar ) { |
101 if ( ! $sidebar ) { |
108 continue; |
102 continue; |
109 } |
103 } |
110 |
104 |
|
105 if ( $this->check_read_permission( $sidebar ) ) { |
|
106 return true; |
|
107 } |
|
108 } |
|
109 |
|
110 return $this->do_permissions_check(); |
|
111 } |
|
112 |
|
113 /** |
|
114 * Retrieves the list of sidebars (active or inactive). |
|
115 * |
|
116 * @since 5.8.0 |
|
117 * |
|
118 * @param WP_REST_Request $request Full details about the request. |
|
119 * @return WP_REST_Response Response object on success. |
|
120 */ |
|
121 public function get_items( $request ) { |
|
122 $this->retrieve_widgets(); |
|
123 |
|
124 $data = array(); |
|
125 $permissions_check = $this->do_permissions_check(); |
|
126 |
|
127 foreach ( wp_get_sidebars_widgets() as $id => $widgets ) { |
|
128 $sidebar = $this->get_sidebar( $id ); |
|
129 |
|
130 if ( ! $sidebar ) { |
|
131 continue; |
|
132 } |
|
133 |
|
134 if ( is_wp_error( $permissions_check ) && ! $this->check_read_permission( $sidebar ) ) { |
|
135 continue; |
|
136 } |
|
137 |
111 $data[] = $this->prepare_response_for_collection( |
138 $data[] = $this->prepare_response_for_collection( |
112 $this->prepare_item_for_response( $sidebar, $request ) |
139 $this->prepare_item_for_response( $sidebar, $request ) |
113 ); |
140 ); |
114 } |
141 } |
115 |
142 |
123 * |
150 * |
124 * @param WP_REST_Request $request Full details about the request. |
151 * @param WP_REST_Request $request Full details about the request. |
125 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
152 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
126 */ |
153 */ |
127 public function get_item_permissions_check( $request ) { |
154 public function get_item_permissions_check( $request ) { |
|
155 $this->retrieve_widgets(); |
|
156 |
|
157 $sidebar = $this->get_sidebar( $request['id'] ); |
|
158 if ( $sidebar && $this->check_read_permission( $sidebar ) ) { |
|
159 return true; |
|
160 } |
|
161 |
128 return $this->do_permissions_check(); |
162 return $this->do_permissions_check(); |
129 } |
163 } |
130 |
164 |
131 /** |
165 /** |
|
166 * Checks if a sidebar can be read publicly. |
|
167 * |
|
168 * @since 5.9.0 |
|
169 * |
|
170 * @param array $sidebar The registered sidebar configuration. |
|
171 * @return bool Whether the side can be read. |
|
172 */ |
|
173 protected function check_read_permission( $sidebar ) { |
|
174 return ! empty( $sidebar['show_in_rest'] ); |
|
175 } |
|
176 |
|
177 /** |
132 * Retrieves one sidebar from the collection. |
178 * Retrieves one sidebar from the collection. |
133 * |
179 * |
134 * @since 5.8.0 |
180 * @since 5.8.0 |
135 * |
181 * |
136 * @param WP_REST_Request $request Full details about the request. |
182 * @param WP_REST_Request $request Full details about the request. |
137 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
183 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
138 */ |
184 */ |
139 public function get_item( $request ) { |
185 public function get_item( $request ) { |
140 retrieve_widgets(); |
186 $this->retrieve_widgets(); |
141 |
187 |
142 $sidebar = $this->get_sidebar( $request['id'] ); |
188 $sidebar = $this->get_sidebar( $request['id'] ); |
143 |
|
144 if ( ! $sidebar ) { |
189 if ( ! $sidebar ) { |
145 return new WP_Error( 'rest_sidebar_not_found', __( 'No sidebar exists with that id.' ), array( 'status' => 404 ) ); |
190 return new WP_Error( 'rest_sidebar_not_found', __( 'No sidebar exists with that id.' ), array( 'status' => 404 ) ); |
146 } |
191 } |
147 |
192 |
148 return $this->prepare_item_for_response( $sidebar, $request ); |
193 return $this->prepare_item_for_response( $sidebar, $request ); |
172 if ( isset( $request['widgets'] ) ) { |
217 if ( isset( $request['widgets'] ) ) { |
173 $sidebars = wp_get_sidebars_widgets(); |
218 $sidebars = wp_get_sidebars_widgets(); |
174 |
219 |
175 foreach ( $sidebars as $sidebar_id => $widgets ) { |
220 foreach ( $sidebars as $sidebar_id => $widgets ) { |
176 foreach ( $widgets as $i => $widget_id ) { |
221 foreach ( $widgets as $i => $widget_id ) { |
177 // This automatically removes the passed widget ids from any other sidebars in use. |
222 // This automatically removes the passed widget IDs from any other sidebars in use. |
178 if ( $sidebar_id !== $request['id'] && in_array( $widget_id, $request['widgets'], true ) ) { |
223 if ( $sidebar_id !== $request['id'] && in_array( $widget_id, $request['widgets'], true ) ) { |
179 unset( $sidebars[ $sidebar_id ][ $i ] ); |
224 unset( $sidebars[ $sidebar_id ][ $i ] ); |
180 } |
225 } |
181 |
226 |
182 // This automatically removes omitted widget ids to the inactive sidebar. |
227 // This automatically removes omitted widget IDs to the inactive sidebar. |
183 if ( $sidebar_id === $request['id'] && ! in_array( $widget_id, $request['widgets'], true ) ) { |
228 if ( $sidebar_id === $request['id'] && ! in_array( $widget_id, $request['widgets'], true ) ) { |
184 $sidebars['wp_inactive_widgets'][] = $widget_id; |
229 $sidebars['wp_inactive_widgets'][] = $widget_id; |
185 } |
230 } |
186 } |
231 } |
187 } |
232 } |
232 /** |
277 /** |
233 * Retrieves the registered sidebar with the given id. |
278 * Retrieves the registered sidebar with the given id. |
234 * |
279 * |
235 * @since 5.8.0 |
280 * @since 5.8.0 |
236 * |
281 * |
237 * @global array $wp_registered_sidebars The registered sidebars. |
|
238 * |
|
239 * @param string|int $id ID of the sidebar. |
282 * @param string|int $id ID of the sidebar. |
240 * @return array|null The discovered sidebar, or null if it is not registered. |
283 * @return array|null The discovered sidebar, or null if it is not registered. |
241 */ |
284 */ |
242 protected function get_sidebar( $id ) { |
285 protected function get_sidebar( $id ) { |
243 global $wp_registered_sidebars; |
286 return wp_get_sidebar( $id ); |
244 |
287 } |
245 foreach ( (array) $wp_registered_sidebars as $sidebar ) { |
288 |
246 if ( $sidebar['id'] === $id ) { |
289 /** |
247 return $sidebar; |
290 * Looks for "lost" widgets once per request. |
248 } |
291 * |
249 } |
292 * @since 5.9.0 |
250 |
293 * |
251 if ( 'wp_inactive_widgets' === $id ) { |
294 * @see retrieve_widgets() |
252 return array( |
295 */ |
253 'id' => 'wp_inactive_widgets', |
296 protected function retrieve_widgets() { |
254 'name' => __( 'Inactive widgets' ), |
297 if ( ! $this->widgets_retrieved ) { |
255 ); |
298 retrieve_widgets(); |
256 } |
299 $this->widgets_retrieved = true; |
257 |
300 } |
258 return null; |
|
259 } |
301 } |
260 |
302 |
261 /** |
303 /** |
262 * Prepares a single sidebar output for response. |
304 * Prepares a single sidebar output for response. |
263 * |
305 * |
264 * @since 5.8.0 |
306 * @since 5.8.0 |
|
307 * @since 5.9.0 Renamed `$raw_sidebar` to `$item` to match parent class for PHP 8 named parameter support. |
265 * |
308 * |
266 * @global array $wp_registered_sidebars The registered sidebars. |
309 * @global array $wp_registered_sidebars The registered sidebars. |
267 * @global array $wp_registered_widgets The registered widgets. |
310 * @global array $wp_registered_widgets The registered widgets. |
268 * |
311 * |
269 * @param array $raw_sidebar Sidebar instance. |
312 * @param array $item Sidebar instance. |
270 * @param WP_REST_Request $request Full details about the request. |
313 * @param WP_REST_Request $request Full details about the request. |
271 * @return WP_REST_Response Prepared response object. |
314 * @return WP_REST_Response Prepared response object. |
272 */ |
315 */ |
273 public function prepare_item_for_response( $raw_sidebar, $request ) { |
316 public function prepare_item_for_response( $item, $request ) { |
274 global $wp_registered_sidebars, $wp_registered_widgets; |
317 global $wp_registered_sidebars, $wp_registered_widgets; |
275 |
318 |
276 $id = $raw_sidebar['id']; |
319 // Restores the more descriptive, specific name for use within this method. |
277 $sidebar = array( 'id' => $id ); |
320 $raw_sidebar = $item; |
|
321 $id = $raw_sidebar['id']; |
|
322 $sidebar = array( 'id' => $id ); |
278 |
323 |
279 if ( isset( $wp_registered_sidebars[ $id ] ) ) { |
324 if ( isset( $wp_registered_sidebars[ $id ] ) ) { |
280 $registered_sidebar = $wp_registered_sidebars[ $id ]; |
325 $registered_sidebar = $wp_registered_sidebars[ $id ]; |
281 |
326 |
282 $sidebar['status'] = 'active'; |
327 $sidebar['status'] = 'active'; |