- Timestamp:
- 11/05/2021 02:14:07 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php
r51791 r52016 16 16 */ 17 17 class WP_REST_Widgets_Controller extends WP_REST_Controller { 18 19 /** 20 * Tracks whether {@see retrieve_widgets()} has been called in the current request. 21 * 22 * @since 5.9.0 23 * @var bool 24 */ 25 protected $widgets_retrieved = false; 18 26 19 27 /** … … 98 106 */ 99 107 public function get_items_permissions_check( $request ) { 108 $this->retrieve_widgets(); 109 if ( isset( $request['sidebar'] ) && $this->check_read_sidebar_permission( $request['sidebar'] ) ) { 110 return true; 111 } 112 113 foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) { 114 if ( $this->check_read_sidebar_permission( $sidebar_id ) ) { 115 return true; 116 } 117 } 118 100 119 return $this->permissions_check( $request ); 101 120 } … … 110 129 */ 111 130 public function get_items( $request ) { 112 retrieve_widgets(); 113 114 $prepared = array(); 131 $this->retrieve_widgets(); 132 133 $prepared = array(); 134 $permissions_check = $this->permissions_check( $request ); 115 135 116 136 foreach ( wp_get_sidebars_widgets() as $sidebar_id => $widget_ids ) { … … 119 139 } 120 140 141 if ( is_wp_error( $permissions_check ) && ! $this->check_read_sidebar_permission( $sidebar_id ) ) { 142 continue; 143 } 144 121 145 foreach ( $widget_ids as $widget_id ) { 122 146 $response = $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' ), $request ); … … 140 164 */ 141 165 public function get_item_permissions_check( $request ) { 166 $this->retrieve_widgets(); 167 168 $widget_id = $request['id']; 169 $sidebar_id = wp_find_widgets_sidebar( $widget_id ); 170 171 if ( $sidebar_id && $this->check_read_sidebar_permission( $sidebar_id ) ) { 172 return true; 173 } 174 142 175 return $this->permissions_check( $request ); 143 176 } 144 177 145 178 /** 179 * Checks if a sidebar can be read publicly. 180 * 181 * @since 5.9.0 182 * 183 * @param string $sidebar_id The sidebar id. 184 * @return bool Whether the sidebar can be read. 185 */ 186 protected function check_read_sidebar_permission( $sidebar_id ) { 187 $sidebar = wp_get_sidebar( $sidebar_id ); 188 189 return ! empty( $sidebar['show_in_rest'] ); 190 } 191 192 /** 146 193 * Gets an individual widget. 147 194 * … … 152 199 */ 153 200 public function get_item( $request ) { 154 retrieve_widgets();201 $this->retrieve_widgets(); 155 202 156 203 $widget_id = $request['id']; … … 248 295 */ 249 296 wp_get_sidebars_widgets(); 250 251 retrieve_widgets(); 297 $this->retrieve_widgets(); 252 298 253 299 $widget_id = $request['id']; … … 324 370 */ 325 371 wp_get_sidebars_widgets(); 326 327 retrieve_widgets(); 372 $this->retrieve_widgets(); 328 373 329 374 $widget_id = $request['id']; … … 438 483 439 484 return true; 485 } 486 487 /** 488 * Looks for "lost" widgets once per request. 489 * 490 * @since 5.9.0 491 * 492 * @see retrieve_widgets() 493 */ 494 protected function retrieve_widgets() { 495 if ( ! $this->widgets_retrieved ) { 496 retrieve_widgets(); 497 $this->widgets_retrieved = true; 498 } 440 499 } 441 500 … … 768 827 'description' => __( 'Instance settings of the widget, if supported.' ), 769 828 'type' => 'object', 770 'context' => array( ' view', 'edit', 'embed' ),829 'context' => array( 'edit' ), 771 830 'default' => null, 772 831 'properties' => array( … … 774 833 'description' => __( 'Base64 encoded representation of the instance settings.' ), 775 834 'type' => 'string', 776 'context' => array( ' view', 'edit', 'embed' ),835 'context' => array( 'edit' ), 777 836 ), 778 837 'hash' => array( 779 838 'description' => __( 'Cryptographic hash of the instance settings.' ), 780 839 'type' => 'string', 781 'context' => array( ' view', 'edit', 'embed' ),840 'context' => array( 'edit' ), 782 841 ), 783 842 'raw' => array( 784 843 'description' => __( 'Unencoded instance settings, if supported.' ), 785 844 'type' => 'object', 786 'context' => array( ' view', 'edit', 'embed' ),845 'context' => array( 'edit' ), 787 846 ), 788 847 ),
Note: See TracChangeset
for help on using the changeset viewer.