forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wp-rest-shortcodes-controller.php
executable file
·189 lines (173 loc) · 5.66 KB
/
class-wp-rest-shortcodes-controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Shortcode Blocks REST API: WP_REST_Shortcodes_Controller class
*
* @package gutenberg
* @since 2.0.0
*/
/**
* Controller which provides a REST endpoint for Gutenberg to preview shortcode blocks.
*
* @since 2.0.0
*
* @see WP_REST_Controller
*/
class WP_REST_Shortcodes_Controller extends WP_REST_Controller {
/**
* Constructs the controller.
*
* @since 2.0.0
* @access public
*/
public function __construct() {
// @codingStandardsIgnoreLine - PHPCS mistakes $this->namespace for the namespace keyword
$this->namespace = 'gutenberg/v1';
$this->rest_base = 'shortcodes';
}
/**
* Registers the necessary REST API routes.
*
* @since 0.10.0
* @access public
*/
public function register_routes() {
// @codingStandardsIgnoreLine - PHPCS mistakes $this->namespace for the namespace keyword
$namespace = $this->namespace;
register_rest_route( $namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_shortcode_output' ),
'permission_callback' => array( $this, 'get_shortcode_output_permissions_check' ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
}
/**
* Checks if a given request has access to read shortcode blocks.
*
* @since 2.0.0
* @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_shortcode_output_permissions_check( $request ) {
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error( 'gutenberg_shortcode_block_cannot_read', __( 'Sorry, you are not allowed to read shortcode blocks as this user.', 'gutenberg' ), array(
'status' => rest_authorization_required_code(),
) );
}
return true;
}
/**
* Filters shortcode content through their hooks.
*
* @since 2.0.0
* @access public
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_shortcode_output( $request ) {
global $post;
global $wp_embed;
$head_scripts_styles = '';
$footer_scripts_styles = '';
$type = 'html';
$output = '';
$args = $request->get_params();
$post = isset( $args['postId'] ) ? get_post( $args['postId'] ) : null;
$shortcode = isset( $args['shortcode'] ) ? trim( $args['shortcode'] ) : '';
// Initialize $data.
$data = array(
'html' => $output,
'type' => $type,
'head_scripts_styles' => $head_scripts_styles,
'footer_scripts_styles' => $footer_scripts_styles,
);
if ( empty( $shortcode ) ) {
$data['html'] = __( 'Enter something to preview', 'gutenberg' );
return rest_ensure_response( $data );
}
if ( ! empty( $post ) ) {
setup_postdata( $post );
}
// Since the [embed] shortcode needs to be run earlier than other shortcodes.
if ( has_shortcode( $shortcode, 'embed' ) ) {
$output = $wp_embed->run_shortcode( $shortcode );
} else {
$output = do_shortcode( $shortcode );
}
if ( empty( $output ) ) {
$data['html'] = __( 'Sorry, couldn\'t render a preview', 'gutenberg' );
return rest_ensure_response( $data );
}
ob_start();
wp_head();
$head_scripts_styles = ob_get_clean();
ob_start();
wp_footer();
$footer_scripts_styles = ob_get_clean();
// Check if shortcode is returning a video. The video type will be used by the frontend to maintain 16:9 aspect ratio.
if ( has_shortcode( $shortcode, 'video' ) ) {
$type = 'video';
} elseif ( has_shortcode( $shortcode, 'embed' ) ) {
$embed_request = new WP_REST_Request( 'GET', '/oembed/1.0/proxy' );
$pattern = get_shortcode_regex();
if ( preg_match_all( '/' . $pattern . '/s', $shortcode, $matches ) ) {
$embed_request['url'] = $matches[5][0];
$embed_response = rest_do_request( $embed_request );
if ( $embed_response->is_error() ) {
$data['html'] = __( 'Sorry, couldn\'t render a preview', 'gutenberg' );
return rest_ensure_response( $data );
}
$embed_data = $embed_response->get_data();
}
$type = $embed_data->type;
}
$data = array(
'html' => $output,
'head_scripts_styles' => $head_scripts_styles,
'footer_scripts_styles' => $footer_scripts_styles,
'type' => $type,
);
return rest_ensure_response( $data );
}
/**
* Retrieves a shortcode block's schema, conforming to JSON Schema.
*
* @since 0.10.0
* @access public
*
* @return array Item schema data.
*/
public function get_item_schema() {
return array(
'$schema' => 'http://json-schema.org/schema#',
'title' => 'shortcode-block',
'type' => 'object',
'properties' => array(
'html' => array(
'description' => __( 'The block\'s content with shortcodes filtered through hooks.', 'gutenberg' ),
'type' => 'string',
'required' => true,
),
'type' => array(
'description' => __( 'The filtered content type - video or otherwise', 'gutenberg' ),
'type' => 'string',
'required' => true,
),
'head_scripts_styles' => array(
'description' => __( 'Links to style sheets and scripts to render the shortcode', 'gutenberg' ),
'type' => 'string',
'required' => true,
),
'footer_scripts_styles' => array(
'description' => __( 'Links to style sheets and scripts to render the shortcode', 'gutenberg' ),
'type' => 'string',
'required' => true,
),
),
);
}
}