| 1 |
<?php |
| 2 |
/** |
| 3 |
* Overrides Core's wp-includes/rest-api.php and registers the new endpoint for WP 6.0. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Add the post type's `icon`(menu_icon) in the response. |
| 10 |
* When we backport this change we will need to add the |
| 11 |
* `icon` to WP_REST_Post_Types_Controller schema. |
| 12 |
* |
| 13 |
* @param WP_REST_Response $response The response object. |
| 14 |
* @param WP_Post_Type $post_type The original post type object. |
| 15 |
*/ |
| 16 |
function gutenberg_update_post_types_rest_response( $response, $post_type ) { |
| 17 |
$response->data['icon'] = $post_type->menu_icon; |
| 18 |
return $response; |
| 19 |
} |
| 20 |
add_filter( 'rest_prepare_post_type', 'gutenberg_update_post_types_rest_response', 10, 2 ); |
| 21 |
|
| 22 |
/** |
| 23 |
* Exposes the site logo URL through the WordPress REST API. |
| 24 |
* |
| 25 |
* This is used for fetching this information when user has no rights |
| 26 |
* to update settings. |
| 27 |
* |
| 28 |
* Note: Backports into wp-includes/rest-api/class-wp-rest-server.php file. |
| 29 |
* |
| 30 |
* @param WP_REST_Response $response REST API response. |
| 31 |
* @return WP_REST_Response $response REST API response. |
| 32 |
*/ |
| 33 |
function gutenberg_add_site_icon_url_to_index( WP_REST_Response $response ) { |
| 34 |
$response->data['site_icon_url'] = get_site_icon_url(); |
| 35 |
|
| 36 |
return $response; |
| 37 |
} |
| 38 |
add_action( 'rest_index', 'gutenberg_add_site_icon_url_to_index' ); |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns the has_archive post type field. |
| 42 |
* |
| 43 |
* @param array $type The response data. |
| 44 |
* @param string $field_name The field name. The function handles field has_archive. |
| 45 |
*/ |
| 46 |
function gutenberg_get_post_type_has_archive_field( $type, $field_name ) { |
| 47 |
if ( ! empty( $type ) && ! empty( $type['slug'] ) && 'has_archive' === $field_name ) { |
| 48 |
$post_type_object = get_post_type_object( $type['slug'] ); |
| 49 |
return $post_type_object->has_archive; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Registers the has_archive post type REST API field. |
| 55 |
*/ |
| 56 |
function gutenberg_register_has_archive_on_post_types_endpoint() { |
| 57 |
register_rest_field( |
| 58 |
'type', |
| 59 |
'has_archive', |
| 60 |
array( |
| 61 |
'get_callback' => 'gutenberg_get_post_type_has_archive_field', |
| 62 |
'schema' => array( |
| 63 |
'description' => __( 'If the value is a string, the value will be used as the archive slug. If the value is false the post type has no archive.', 'gutenberg' ), |
| 64 |
'type' => array( 'string', 'boolean' ), |
| 65 |
'context' => array( 'view', 'edit' ), |
| 66 |
), |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
add_action( 'rest_api_init', 'gutenberg_register_has_archive_on_post_types_endpoint' ); |
| 71 |
|