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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 object $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. // Ensure that the user is logged in and has the read_blocks capability.
  30. $post_type = get_post_type_object( $post->post_type );
  31. if ( ! current_user_can( $post_type->cap->read_post, $post->ID ) ) {
  32. return false;
  33. }
  34. return parent::check_read_permission( $post );
  35. }
  36. /**
  37. * Filters a response based on the context defined in the schema.
  38. *
  39. * @since 5.0.0
  40. *
  41. * @param array $data Response data to fiter.
  42. * @param string $context Context defined in the schema.
  43. * @return array Filtered response.
  44. */
  45. public function filter_response_by_context( $data, $context ) {
  46. $data = parent::filter_response_by_context( $data, $context );
  47. /*
  48. * Remove `title.rendered` and `content.rendered` from the response. It
  49. * doesn't make sense for a reusable block to have rendered content on its
  50. * own, since rendering a block requires it to be inside a post or a page.
  51. */
  52. unset( $data['title']['rendered'] );
  53. unset( $data['content']['rendered'] );
  54. return $data;
  55. }
  56. /**
  57. * Retrieves the block's schema, conforming to JSON Schema.
  58. *
  59. * @since 5.0.0
  60. *
  61. * @return array Item schema data.
  62. */
  63. public function get_item_schema() {
  64. // Do not cache this schema because all properties are derived from parent controller.
  65. $schema = parent::get_item_schema();
  66. /*
  67. * Allow all contexts to access `title.raw` and `content.raw`. Clients always
  68. * need the raw markup of a reusable block to do anything useful, e.g. parse
  69. * it or display it in an editor.
  70. */
  71. $schema['properties']['title']['properties']['raw']['context'] = array( 'view', 'edit' );
  72. $schema['properties']['content']['properties']['raw']['context'] = array( 'view', 'edit' );
  73. /*
  74. * Remove `title.rendered` and `content.rendered` from the schema. It doesn’t
  75. * make sense for a reusable block to have rendered content on its own, since
  76. * rendering a block requires it to be inside a post or a page.
  77. */
  78. unset( $schema['properties']['title']['properties']['rendered'] );
  79. unset( $schema['properties']['content']['properties']['rendered'] );
  80. return $schema;
  81. }
  82. }