PluginProbe
Sight – Professional Image Gallery and Portfolio / 1.1.0
Sight – Professional Image Gallery and Portfolio v1.1.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
sight / core / block-renderer-controller.php

block-renderer-controller.php in Sight – Professional Image Gallery and Portfolio 1.1.0, at core/block-renderer-controller.php

186 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block Renderer REST API.
4 *
5 * @package Sight
6 */
7
8 /**
9 * Controller which provides REST endpoint for rendering a block.
10 *
11 * @since 5.0.0
12 *
13 * @see WP_REST_Controller
14 */
15 class Sight_Rest_Block_Renderer_Controller extends WP_REST_Controller {
16
17 /**
18 * Constructs the controller.
19 *
20 * @since 5.0.0
21 */
22 public function __construct() {
23 $this->namespace = 'wp/v2';
24 $this->rest_base = 'sight-renderer';
25 }
26
27 /**
28 * Registers the necessary REST API routes, one for each dynamic block.
29 *
30 * @since 5.0.0
31 */
32 public function register_routes() {
33 $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
34
35 foreach ( $block_types as $block_type ) {
36 if ( ! $block_type->is_dynamic() ) {
37 continue;
38 }
39
40 register_rest_route(
41 $this->namespace,
42 '/' . $this->rest_base . '/(?P<name>' . $block_type->name . ')',
43 array(
44 'args' => array(
45 'name' => array(
46 'description' => __( 'Unique registered name for the block.' ),
47 'type' => 'string',
48 ),
49 ),
50 array(
51 'methods' => WP_REST_Server::EDITABLE,
52 'callback' => array( $this, 'get_item' ),
53 'permission_callback' => array( $this, 'get_item_permissions_check' ),
54 'args' => array(
55 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
56 'attributes' => array(
57 /* translators: %s is the name of the block */
58 'description' => sprintf( __( 'Attributes for %s block' ), $block_type->name ),
59 'type' => 'object',
60 'additionalProperties' => false,
61 'properties' => $block_type->get_attributes(),
62 'default' => array(),
63 ),
64 'post_id' => array(
65 'description' => __( 'ID of the post context.' ),
66 'type' => 'integer',
67 ),
68 ),
69 ),
70 'schema' => array( $this, 'get_public_item_schema' ),
71 )
72 );
73 }
74 }
75
76 /**
77 * Checks if a given request has access to read blocks.
78 *
79 * @since 5.0.0
80 *
81 * @param WP_REST_Request $request Request.
82 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
83 */
84 public function get_item_permissions_check( $request ) {
85 global $post;
86
87 $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
88
89 if ( 0 < $post_id ) {
90 $post = get_post( $post_id );
91
92 if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
93 return new WP_Error(
94 'block_cannot_read',
95 __( 'Sorry, you are not allowed to read blocks of this post.' ),
96 array(
97 'status' => rest_authorization_required_code(),
98 )
99 );
100 }
101 } else {
102 if ( ! current_user_can( 'edit_posts' ) ) {
103 return new WP_Error(
104 'block_cannot_read',
105 __( 'Sorry, you are not allowed to read blocks as this user.' ),
106 array(
107 'status' => rest_authorization_required_code(),
108 )
109 );
110 }
111 }
112
113 return true;
114 }
115
116 /**
117 * Returns block output from block's registered render_callback.
118 *
119 * @since 5.0.0
120 *
121 * @param WP_REST_Request $request Full details about the request.
122 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
123 */
124 public function get_item( $request ) {
125 global $post;
126
127 $post_id = isset( $request['post_id'] ) ? intval( $request['post_id'] ) : 0;
128
129 if ( 0 < $post_id ) {
130 $post = get_post( $post_id );
131
132 // Set up postdata since this will be needed if post_id was set.
133 setup_postdata( $post );
134 }
135 $registry = WP_Block_Type_Registry::get_instance();
136 $block = $registry->get_registered( $request['name'] );
137
138 if ( null === $block ) {
139 return new WP_Error(
140 'block_invalid',
141 __( 'Invalid block.' ),
142 array(
143 'status' => 404,
144 )
145 );
146 }
147
148 $data = array(
149 'rendered' => $block->render( $request->get_param( 'attributes' ) ),
150 );
151 return rest_ensure_response( $data );
152 }
153
154 /**
155 * Retrieves block's output schema, conforming to JSON Schema.
156 *
157 * @since 5.0.0
158 *
159 * @return array Item schema data.
160 */
161 public function get_item_schema() {
162 return array(
163 '$schema' => 'http://json-schema.org/schema#',
164 'title' => 'rendered-block',
165 'type' => 'object',
166 'properties' => array(
167 'rendered' => array(
168 'description' => __( 'The rendered block.' ),
169 'type' => 'string',
170 'required' => true,
171 'context' => array( 'edit' ),
172 ),
173 ),
174 );
175 }
176 }
177
178 add_action(
179 'rest_api_init', function() {
180 // Block Renderer.
181 $controller = new Sight_Rest_Block_Renderer_Controller();
182
183 $controller->register_routes();
184 }, 99
185 );
186