| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* Interact with the list of available block editor extensions (blocks, plugins) |
| 4 |
* made available by the Jetpack plugin. |
| 5 |
* |
| 6 |
* @package automattic/jetpack |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit( 0 ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Gutenberg: List Available Gutenberg Extensions (Blocks and Plugins) |
| 15 |
* |
| 16 |
* [ |
| 17 |
* { # Availability Object. See schema for more detail. |
| 18 |
* available: (boolean) Whether the extension is available |
| 19 |
* unavailable_reason: (string) Reason for the extension not being available |
| 20 |
* }, |
| 21 |
* ... |
| 22 |
* ] |
| 23 |
* |
| 24 |
* @since 6.9 |
| 25 |
*/ |
| 26 |
class WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions extends WP_REST_Controller { |
| 27 |
/** |
| 28 |
* Constructor. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->namespace = 'wpcom/v2'; |
| 32 |
$this->rest_base = 'gutenberg'; |
| 33 |
$this->wpcom_is_site_specific_endpoint = true; |
| 34 |
|
| 35 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register the endpoint route. |
| 40 |
*/ |
| 41 |
public function register_routes() { |
| 42 |
register_rest_route( |
| 43 |
$this->namespace, |
| 44 |
$this->rest_base . '/available-extensions', |
| 45 |
array( |
| 46 |
array( |
| 47 |
'methods' => WP_REST_Server::READABLE, |
| 48 |
'callback' => array( 'Jetpack_Gutenberg', 'get_availability' ), |
| 49 |
'permission_callback' => array( $this, 'get_items_permission_check' ), |
| 50 |
), |
| 51 |
'schema' => array( $this, 'get_item_schema' ), |
| 52 |
) |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Return the available Gutenberg extensions schema |
| 58 |
* |
| 59 |
* @return array Available Gutenberg extensions schema |
| 60 |
*/ |
| 61 |
public function get_public_item_schema() { |
| 62 |
$schema = array( |
| 63 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 64 |
'title' => 'gutenberg-available-extensions', |
| 65 |
'type' => 'object', |
| 66 |
'properties' => array( |
| 67 |
'available' => array( |
| 68 |
'description' => __( 'Whether the extension is available', 'jetpack' ), |
| 69 |
'type' => 'boolean', |
| 70 |
), |
| 71 |
'unavailable_reason' => array( |
| 72 |
'description' => __( 'Reason for the extension not being available', 'jetpack' ), |
| 73 |
'type' => 'string', |
| 74 |
), |
| 75 |
), |
| 76 |
); |
| 77 |
|
| 78 |
return $this->add_additional_fields_schema( $schema ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Ensure the user has proper permissions |
| 83 |
* |
| 84 |
* @return boolean |
| 85 |
*/ |
| 86 |
public function get_items_permission_check() { |
| 87 |
return current_user_can( 'edit_posts' ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Gutenberg_Available_Extensions' ); |
| 92 |
|