| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Controller which provides REST endpoint for registered icons. |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! class_exists( 'WP_REST_Icons_Controller' ) ) { |
| 10 |
class WP_REST_Icons_Controller extends WP_REST_Controller { |
| 11 |
/** |
| 12 |
* Constructs the controller. |
| 13 |
*/ |
| 14 |
public function __construct() { |
| 15 |
$this->namespace = 'wp/v2'; |
| 16 |
$this->rest_base = 'icons'; |
| 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<name>[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?/[a-z0-9](?:[a-z0-9_-]*[a-z0-9])?)', |
| 40 |
array( |
| 41 |
'args' => array( |
| 42 |
'name' => array( |
| 43 |
'description' => __( 'Icon name.', '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 icons. |
| 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 icons.', '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. |
| 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 icons. |
| 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 |
$search = $request->get_param( 'search' ); |
| 111 |
$icons = WP_Icons_Registry::get_instance()->get_registered_icons( $search ); |
| 112 |
foreach ( $icons as $icon ) { |
| 113 |
$prepared_icon = $this->prepare_item_for_response( $icon, $request ); |
| 114 |
$response[] = $this->prepare_response_for_collection( $prepared_icon ); |
| 115 |
} |
| 116 |
return rest_ensure_response( $response ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Retrieves a specific icon. |
| 121 |
* |
| 122 |
* @param WP_REST_Request $request Full details about the request. |
| 123 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 124 |
*/ |
| 125 |
public function get_item( $request ) { |
| 126 |
$icon = $this->get_icon( $request['name'] ); |
| 127 |
if ( is_wp_error( $icon ) ) { |
| 128 |
return $icon; |
| 129 |
} |
| 130 |
|
| 131 |
$data = $this->prepare_item_for_response( $icon, $request ); |
| 132 |
return rest_ensure_response( $data ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Retrieves a specific icon from the registry. |
| 137 |
* |
| 138 |
* @param string $name Icon name. |
| 139 |
* @return array|WP_Error Icon data on success, or WP_Error object on failure. |
| 140 |
*/ |
| 141 |
public function get_icon( $name ) { |
| 142 |
$registry = WP_Icons_Registry::get_instance(); |
| 143 |
$icon = $registry->get_registered_icon( $name ); |
| 144 |
|
| 145 |
if ( null === $icon ) { |
| 146 |
return new WP_Error( |
| 147 |
'rest_icon_not_found', |
| 148 |
sprintf( |
| 149 |
// translators: %s is the name of any user-provided name |
| 150 |
__( 'Icon not found: "%s".', 'gutenberg' ), |
| 151 |
$name |
| 152 |
), |
| 153 |
array( 'status' => 404 ) |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
return $icon; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Prepare a raw icon before it gets output in a REST API response. |
| 162 |
* |
| 163 |
* @param array $item Raw icon as registered, before any changes. |
| 164 |
* @param WP_REST_Request $request Request object. |
| 165 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 166 |
*/ |
| 167 |
public function prepare_item_for_response( $item, $request ) { |
| 168 |
$fields = $this->get_fields_for_response( $request ); |
| 169 |
$keys = array( |
| 170 |
'name' => 'name', |
| 171 |
'label' => 'label', |
| 172 |
'content' => 'content', |
| 173 |
); |
| 174 |
$data = array(); |
| 175 |
foreach ( $keys as $item_key => $rest_key ) { |
| 176 |
if ( isset( $item[ $item_key ] ) && rest_is_field_included( $rest_key, $fields ) ) { |
| 177 |
$data[ $rest_key ] = $item[ $item_key ]; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
$context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 182 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
| 183 |
$data = $this->filter_response_by_context( $data, $context ); |
| 184 |
return rest_ensure_response( $data ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Retrieves the icon schema, conforming to JSON Schema. |
| 189 |
* |
| 190 |
* @return array Item schema data. |
| 191 |
*/ |
| 192 |
public function get_item_schema() { |
| 193 |
if ( $this->schema ) { |
| 194 |
return $this->add_additional_fields_schema( $this->schema ); |
| 195 |
} |
| 196 |
|
| 197 |
$schema = array( |
| 198 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 199 |
'title' => 'icon', |
| 200 |
'type' => 'object', |
| 201 |
'properties' => array( |
| 202 |
'name' => array( |
| 203 |
'description' => __( 'The icon name.', 'gutenberg' ), |
| 204 |
'type' => 'string', |
| 205 |
'readonly' => true, |
| 206 |
'context' => array( 'view', 'edit', 'embed' ), |
| 207 |
), |
| 208 |
'label' => array( |
| 209 |
'description' => __( 'The icon label.', 'gutenberg' ), |
| 210 |
'type' => 'string', |
| 211 |
'readonly' => true, |
| 212 |
'context' => array( 'view', 'edit', 'embed' ), |
| 213 |
), |
| 214 |
'content' => array( |
| 215 |
'description' => __( 'The icon content (SVG markup).', 'gutenberg' ), |
| 216 |
'type' => 'string', |
| 217 |
'readonly' => true, |
| 218 |
'context' => array( 'view', 'edit', 'embed' ), |
| 219 |
), |
| 220 |
), |
| 221 |
); |
| 222 |
|
| 223 |
$this->schema = $schema; |
| 224 |
|
| 225 |
return $this->add_additional_fields_schema( $this->schema ); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Retrieves the query params for the icons collection. |
| 230 |
* |
| 231 |
* @return array Collection parameters. |
| 232 |
*/ |
| 233 |
public function get_collection_params() { |
| 234 |
$query_params = parent::get_collection_params(); |
| 235 |
$query_params['context']['default'] = 'view'; |
| 236 |
return $query_params; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|