| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API: Gutenberg icon collections controller. |
| 4 |
* |
| 5 |
* Changes to this class should be synced to the corresponding class |
| 6 |
* in WordPress core: src/wp-includes/rest-api/endpoints/class-wp-rest-icon-collections-controller.php. |
| 7 |
* |
| 8 |
* @package gutenberg |
| 9 |
* @subpackage REST_API |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Gutenberg Icon Collections REST API Controller. |
| 14 |
* |
| 15 |
* @since 7.2.0 |
| 16 |
*/ |
| 17 |
class WP_REST_Icon_Collections_Controller_Gutenberg extends WP_REST_Icon_Collections_Controller { |
| 18 |
|
| 19 |
/** |
| 20 |
* Retrieves all public icon collections. |
| 21 |
* |
| 22 |
* @param WP_REST_Request $request Full details about the request. |
| 23 |
* @return WP_REST_Response Response object. |
| 24 |
*/ |
| 25 |
public function get_items( $request ) { |
| 26 |
$response = array(); |
| 27 |
$collections = WP_Icon_Collections_Registry::get_instance()->get_all_registered(); |
| 28 |
foreach ( $collections as $collection ) { |
| 29 |
if ( ! $collection['public'] ) { |
| 30 |
continue; |
| 31 |
} |
| 32 |
$prepared_collection = $this->prepare_item_for_response( $collection, $request ); |
| 33 |
$response[] = $this->prepare_response_for_collection( $prepared_collection ); |
| 34 |
} |
| 35 |
return rest_ensure_response( $response ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Retrieves a specific public icon collection. |
| 40 |
* |
| 41 |
* @param WP_REST_Request $request Full details about the request. |
| 42 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 43 |
*/ |
| 44 |
public function get_item( $request ) { |
| 45 |
$collection = WP_Icon_Collections_Registry::get_instance()->get_registered( $request['slug'] ); |
| 46 |
|
| 47 |
if ( null !== $collection && ! $collection['public'] ) { |
| 48 |
return new WP_Error( |
| 49 |
'rest_icon_collection_not_found', |
| 50 |
sprintf( |
| 51 |
/* translators: %s: Icon collection slug. */ |
| 52 |
__( 'Icon collection not found: "%s".', 'gutenberg' ), |
| 53 |
$request['slug'] |
| 54 |
), |
| 55 |
array( 'status' => 404 ) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
return parent::get_item( $request ); |
| 60 |
} |
| 61 |
} |
| 62 |
|