| 1 |
<?php |
| 2 |
/** |
| 3 |
* Reusable blocks REST API: WP_REST_Blocks_Controller class |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
* @since 0.10.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Controller which provides a REST endpoint for Gutenberg to read, create, |
| 11 |
* edit and delete reusable blocks. Blocks are stored as posts with the wp_block |
| 12 |
* post type. |
| 13 |
* |
| 14 |
* @since 0.10.0 |
| 15 |
* |
| 16 |
* @see WP_REST_Controller |
| 17 |
*/ |
| 18 |
class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller { |
| 19 |
/** |
| 20 |
* Checks if a block can be read. |
| 21 |
* |
| 22 |
* @since 2.1.0 |
| 23 |
* |
| 24 |
* @param object $post Post object that backs the block. |
| 25 |
* @return bool Whether the block can be read. |
| 26 |
*/ |
| 27 |
public function check_read_permission( $post ) { |
| 28 |
// Ensure that the user is logged in and has the read_blocks capability. |
| 29 |
$post_type = get_post_type_object( $post->post_type ); |
| 30 |
if ( ! current_user_can( $post_type->cap->read_post, $post->ID ) ) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
return parent::check_read_permission( $post ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Handle a DELETE request. |
| 39 |
* |
| 40 |
* @since 1.10.0 |
| 41 |
* |
| 42 |
* @param WP_REST_Request $request Full details about the request. |
| 43 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 44 |
*/ |
| 45 |
public function delete_item( $request ) { |
| 46 |
// Always hard-delete a block. |
| 47 |
$request->set_param( 'force', true ); |
| 48 |
|
| 49 |
return parent::delete_item( $request ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Given an update or create request, build the post object that is saved to |
| 54 |
* the database. |
| 55 |
* |
| 56 |
* @since 1.10.0 |
| 57 |
* |
| 58 |
* @param WP_REST_Request $request Request object. |
| 59 |
* @return stdClass|WP_Error Post object or WP_Error. |
| 60 |
*/ |
| 61 |
public function prepare_item_for_database( $request ) { |
| 62 |
$prepared_post = parent::prepare_item_for_database( $request ); |
| 63 |
|
| 64 |
// Force blocks to always be published. |
| 65 |
$prepared_post->post_status = 'publish'; |
| 66 |
|
| 67 |
return $prepared_post; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Given a block from the database, build the array that is returned from an |
| 72 |
* API response. |
| 73 |
* |
| 74 |
* @since 1.10.0 |
| 75 |
* |
| 76 |
* @param WP_Post $post Post object that backs the block. |
| 77 |
* @param WP_REST_Request $request Request object. |
| 78 |
* @return WP_REST_Response Response object. |
| 79 |
*/ |
| 80 |
public function prepare_item_for_response( $post, $request ) { |
| 81 |
$data = array( |
| 82 |
'id' => $post->ID, |
| 83 |
'title' => $post->post_title, |
| 84 |
'content' => $post->post_content, |
| 85 |
); |
| 86 |
|
| 87 |
$response = rest_ensure_response( $data ); |
| 88 |
|
| 89 |
return apply_filters( "rest_prepare_{$this->post_type}", $response, $post, $request ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Builds the block's schema, conforming to JSON Schema. |
| 94 |
* |
| 95 |
* @since 1.10.0 |
| 96 |
* |
| 97 |
* @return array Item schema data. |
| 98 |
*/ |
| 99 |
public function get_item_schema() { |
| 100 |
return array( |
| 101 |
'$schema' => 'http://json-schema.org/schema#', |
| 102 |
'title' => $this->post_type, |
| 103 |
'type' => 'object', |
| 104 |
'properties' => array( |
| 105 |
'id' => array( |
| 106 |
'description' => __( 'Unique identifier for the block.', 'gutenberg' ), |
| 107 |
'type' => 'integer', |
| 108 |
'readonly' => true, |
| 109 |
), |
| 110 |
'title' => array( |
| 111 |
'description' => __( 'The block’s title.', 'gutenberg' ), |
| 112 |
'type' => 'string', |
| 113 |
'required' => true, |
| 114 |
), |
| 115 |
'content' => array( |
| 116 |
'description' => __( 'The block’s HTML content.', 'gutenberg' ), |
| 117 |
'type' => 'string', |
| 118 |
'required' => true, |
| 119 |
), |
| 120 |
), |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|