class-wp-rest-blocks-controller.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Reusable blocks REST API: WP_REST_Blocks_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Controller which provides a REST endpoint for the editor to read, create,
  11. * edit and delete reusable blocks. Blocks are stored as posts with the wp_block
  12. * post type.
  13. *
  14. * @since 5.0.0
  15. *
  16. * @see WP_REST_Posts_Controller
  17. * @see WP_REST_Controller
  18. */
  19. class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller {
  20. /**
  21. * Checks if a block can be read.
  22. *
  23. * @since 5.0.0
  24. *
  25. * @param WP_Post $post Post object that backs the block.
  26. * @return bool Whether the block can be read.
  27. */
  28. public function check_read_permission( $post ) {
  29. // By default the read_post capability is mapped to edit_posts.
  30. if ( ! current_user_can( 'read_post', $post->ID ) ) {
  31. return false;
  32. }
  33. return parent::check_read_permission( $post );
  34. }
  35. /**
  36. * Filters a response based on the context defined in the schema.
  37. *
  38. * @since 5.0.0
  39. *
  40. * @param array $data Response data to filter.
  41. * @param string $context Context defined in the schema.
  42. * @return array Filtered response.
  43. */
  44. public function filter_response_by_context( $data, $context ) {
  45. $data = parent::filter_response_by_context( $data, $context );
  46. /*
  47. * Remove `title.rendered` and `content.rendered` from the response. It
  48. * doesn't make sense for a reusable block to have rendered content on its
  49. * own, since rendering a block requires it to be inside a post or a page.
  50. */
  51. unset( $data['title']['rendered'] );
  52. unset( $data['content']['rendered'] );
  53. return $data;
  54. }
  55. /**
  56. * Retrieves the block's schema, conforming to JSON Schema.
  57. *
  58. * @since 5.0.0
  59. *
  60. * @return array Item schema data.
  61. */
  62. public function get_item_schema() {
  63. // Do not cache this schema because all properties are derived from parent controller.
  64. $schema = parent::get_item_schema();
  65. /*
  66. * Allow all contexts to access `title.raw` and `content.raw`. Clients always
  67. * need the raw markup of a reusable block to do anything useful, e.g. parse
  68. * it or display it in an editor.
  69. */
  70. $schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' );
  71. $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
  72. /*
  73. * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t
  74. * make sense for a reusable block to have rendered content on its own, since
  75. * rendering a block requires it to be inside a post or a page.
  76. */
  77. unset( $schema['properties']['title']['properties']['rendered'] );
  78. unset( $schema['properties']['content']['properties']['rendered'] );
  79. return $schema;
  80. }
  81. }