| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Controller which provides REST endpoint for registered icon collections. |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! class_exists( 'WP_REST_Icon_Collections_Controller' ) ) { |
| 10 |
class WP_REST_Icon_Collections_Controller extends WP_REST_Controller { |
| 11 |
/** |
| 12 |
* Constructs the controller. |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
$this->namespace = 'wp/v2'; |
| 16 |
$this->rest_base = 'icon-collections'; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Registers the routes for the objects of the controller. |
| 21 |
*/ |
| 22 |
public function register_routes() { |
| 23 |
register_rest_route( |
| 24 |
$this->namespace, |
| 25 |
'/' . $this->rest_base, |
| 26 |
array( |
| 27 |
array( |
| 28 |
'methods' => WP_REST_Server::READABLE, |
| 29 |
'callback' => array( $this, 'get_items' ), |
| 30 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 31 |
'args' => $this->get_collection_params(), |
| 32 |
), |
| 33 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 34 |
) |
| 35 |
); |
| 36 |
|
| 37 |
register_rest_route( |
| 38 |
$this->namespace, |
| 39 |
'/' . $this->rest_base . '/(?P<slug>[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)', |
| 40 |
array( |
| 41 |
'args' => array( |
| 42 |
'slug' => array( |
| 43 |
'description' => __( 'Icon collection slug.', 'gutenberg' ), |
| 44 |
'type' => 'string', |
| 45 |
), |
| 46 |
), |
| 47 |
array( |
| 48 |
'methods' => WP_REST_Server::READABLE, |
| 49 |
'callback' => array( $this, 'get_item' ), |
| 50 |
'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 51 |
'args' => array( |
| 52 |
'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
| 53 |
), |
| 54 |
), |
| 55 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Checks whether a given request has permission to read icon collections. |
| 62 |
* |
| 63 |
* @param WP_REST_Request $_request Full details about the request. |
| 64 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 65 |
*/ |
| 66 |
public function get_items_permissions_check( |
| 67 |
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 68 |
$request |
| 69 |
) { |
| 70 |
if ( current_user_can( 'edit_posts' ) ) { |
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) { |
| 75 |
if ( current_user_can( $post_type->cap->edit_posts ) ) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return new WP_Error( |
| 81 |
'rest_cannot_view', |
| 82 |
__( 'Sorry, you are not allowed to view the registered icon collections.', 'gutenberg' ), |
| 83 |
array( 'status' => rest_authorization_required_code() ) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Checks if a given request has access to read a specific icon collection. |
| 89 |
* |
| 90 |
* @param WP_REST_Request $request Full details about the request. |
| 91 |
* @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. |
| 92 |
*/ |
| 93 |
public function get_item_permissions_check( $request ) { |
| 94 |
$check = $this->get_items_permissions_check( $request ); |
| 95 |
if ( is_wp_error( $check ) ) { |
| 96 |
return $check; |
| 97 |
} |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Retrieves all icon collections. |
| 104 |
* |
| 105 |
* @param WP_REST_Request $request Full details about the request. |
| 106 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 107 |
*/ |
| 108 |
public function get_items( $request ) { |
| 109 |
$response = array(); |
| 110 |
$collections = WP_Icon_Collections_Registry::get_instance()->get_all_registered(); |
| 111 |
foreach ( $collections as $collection ) { |
| 112 |
$prepared_collection = $this->prepare_item_for_response( $collection, $request ); |
| 113 |
$response[] = $this->prepare_response_for_collection( $prepared_collection ); |
| 114 |
} |
| 115 |
return rest_ensure_response( $response ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Retrieves a specific icon collection. |
| 120 |
* |
| 121 |
* @param WP_REST_Request $request Full details about the request. |
| 122 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 123 |
*/ |
| 124 |
public function get_item( $request ) { |
| 125 |
$registry = WP_Icon_Collections_Registry::get_instance(); |
| 126 |
$collection = $registry->get_registered( $request['slug'] ); |
| 127 |
|
| 128 |
if ( null === $collection ) { |
| 129 |
return new WP_Error( |
| 130 |
'rest_icon_collection_not_found', |
| 131 |
sprintf( |
| 132 |
/* translators: %s: Icon collection slug. */ |
| 133 |
__( 'Icon collection not found: "%s".', 'gutenberg' ), |
| 134 |
$request['slug'] |
| 135 |
), |
| 136 |
array( 'status' => 404 ) |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
$data = $this->prepare_item_for_response( $collection, $request ); |
| 141 |
return rest_ensure_response( $data ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Prepare a raw icon collection before it gets output in a REST API response. |
| 146 |
* |
| 147 |
* @param array $item Raw icon collection as registered, before any changes. |
| 148 |
* @param WP_REST_Request $request Request object. |
| 149 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 150 |
*/ |
| 151 |
public function prepare_item_for_response( $item, $request ) { |
| 152 |
$fields = $this->get_fields_for_response( $request ); |
| 153 |
$keys = array( |
| 154 |
'slug' => 'slug', |
| 155 |
'label' => 'label', |
| 156 |
'description' => 'description', |
| 157 |
); |
| 158 |
$data = array(); |
| 159 |
foreach ( $keys as $item_key => $rest_key ) { |
| 160 |
if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) { |
| 161 |
$data[ $rest_key ] = $item[ $item_key ]; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 166 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
| 167 |
$data = $this->filter_response_by_context( $data, $context ); |
| 168 |
return rest_ensure_response( $data ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Retrieves the icon collection schema, conforming to JSON Schema. |
| 173 |
* |
| 174 |
* @return array Item schema data. |
| 175 |
*/ |
| 176 |
public function get_item_schema() { |
| 177 |
if ( $this->schema ) { |
| 178 |
return $this->add_additional_fields_schema( $this->schema ); |
| 179 |
} |
| 180 |
|
| 181 |
$schema = array( |
| 182 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 183 |
'title' => 'icon-collection', |
| 184 |
'type' => 'object', |
| 185 |
'properties' => array( |
| 186 |
'slug' => array( |
| 187 |
'description' => __( 'The icon collection slug.', 'gutenberg' ), |
| 188 |
'type' => 'string', |
| 189 |
'readonly' => true, |
| 190 |
'context' => array( 'view', 'edit', 'embed' ), |
| 191 |
), |
| 192 |
'label' => array( |
| 193 |
'description' => __( 'The icon collection label.', 'gutenberg' ), |
| 194 |
'type' => 'string', |
| 195 |
'readonly' => true, |
| 196 |
'context' => array( 'view', 'edit', 'embed' ), |
| 197 |
), |
| 198 |
'description' => array( |
| 199 |
'description' => __( 'The icon collection description.', 'gutenberg' ), |
| 200 |
'type' => 'string', |
| 201 |
'readonly' => true, |
| 202 |
'context' => array( 'view', 'edit', 'embed' ), |
| 203 |
), |
| 204 |
), |
| 205 |
); |
| 206 |
|
| 207 |
$this->schema = $schema; |
| 208 |
|
| 209 |
return $this->add_additional_fields_schema( $this->schema ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Retrieves the query params for the icon collections collection. |
| 214 |
* |
| 215 |
* @return array Collection parameters. |
| 216 |
*/ |
| 217 |
public function get_collection_params() { |
| 218 |
$query_params = parent::get_collection_params(); |
| 219 |
$query_params['context']['default'] = 'view'; |
| 220 |
return $query_params; |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|