-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathStories_Base_Controller.php
More file actions
356 lines (314 loc) · 9.42 KB
/
Stories_Base_Controller.php
File metadata and controls
356 lines (314 loc) · 9.42 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* Class Stories_Base_Controller
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2020 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types = 1);
namespace Google\Web_Stories\REST_API;
use Google\Web_Stories\Decoder;
use Google\Web_Stories\Services;
use stdClass;
use WP_Error;
use WP_Post;
use WP_REST_Posts_Controller;
use WP_REST_Request;
use WP_REST_Response;
/**
* Stories_Base_Controller class.
*
* @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
*
* Override the WP_REST_Posts_Controller class to add `post_content_filtered` to REST request.
*
* @phpstan-type Link array{
* href?: string,
* embeddable?: bool,
* taxonomy?: string
* }
* @phpstan-type Links array<string, Link|Link[]>
* @phpstan-type SchemaEntry array{
* description: string,
* type: string,
* context: string[],
* default?: mixed,
* }
* @phpstan-type Schema array{
* properties: array{
* content?: SchemaEntry,
* story_data?: SchemaEntry
* }
* }
* @phpstan-type RegisteredMetadata array{
* type: string,
* description: string,
* single: bool,
* sanitize_callback?: callable,
* auth_callback: callable,
* show_in_rest: bool|array{schema: array<string, mixed>},
* default?: mixed
* }
*/
class Stories_Base_Controller extends WP_REST_Posts_Controller {
/**
* Decoder instance.
*
* @var Decoder Decoder instance.
*/
private Decoder $decoder;
/**
* Constructor.
*
* Override the namespace.
*
* @since 1.0.0
*
* @param string $post_type Post type.
*/
public function __construct( $post_type ) {
parent::__construct( $post_type );
$injector = Services::get_injector();
/**
* Decoder instance.
*
* @var Decoder $decoder Decoder instance.
*/
$decoder = $injector->make( Decoder::class );
$this->decoder = $decoder;
}
/**
* Prepares a single template output for response.
*
* Adds post_content_filtered field to output.
*
* @since 1.0.0
*
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response Response object.
*
* @phpstan-param WP_REST_Request<covariant array{context: string}> $request
*/
public function prepare_item_for_response( $post, $request ): WP_REST_Response {
$response = parent::prepare_item_for_response( $post, $request );
$fields = $this->get_fields_for_response( $request );
/**
* Schema.
*
* @phpstan-var Schema $schema
*/
$schema = $this->get_item_schema();
/**
* Response data.
*
* @var array<string,mixed> $data
*/
$data = $response->get_data();
if ( ! empty( $schema['properties']['story_data'] ) && rest_is_field_included( 'story_data', $fields ) ) {
$post_story_data = json_decode( $post->post_content_filtered, true );
$data['story_data'] = post_password_required( $post ) ? (object) [] : rest_sanitize_value_from_schema( $post_story_data, $schema['properties']['story_data'] );
}
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->filter_response_by_context( $data, $context );
$links = $response->get_links();
// Wrap the data in a response object.
$response = new WP_REST_Response( $data );
foreach ( $links as $rel => $rel_links ) {
foreach ( $rel_links as $link ) {
// @phpstan-ignore method.internal (false positive)
$response->add_link( $rel, $link['href'], $link['attributes'] );
}
}
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
return apply_filters( "rest_prepare_{$this->post_type}", $response, $post, $request );
}
/**
* Retrieves the story's schema, conforming to JSON Schema.
*
* @since 1.0.0
*
* @return array Item schema data.
*
* @phpstan-return Schema
*/
public function get_item_schema(): array {
if ( $this->schema ) {
/**
* Schema.
*
* @phpstan-var Schema $schema
*/
$schema = $this->add_additional_fields_schema( $this->schema );
return $schema;
}
$schema = parent::get_item_schema();
$schema['properties']['story_data'] = [
'description' => __( 'Story data', 'web-stories' ),
'type' => 'object',
'context' => [ 'view', 'edit' ],
'default' => [],
];
$schema['properties']['original_id'] = [
'description' => __( 'Unique identifier for original story id.', 'web-stories' ),
'type' => 'integer',
'context' => [ 'view', 'edit', 'embed' ],
];
$this->schema = $schema;
/**
* Schema.
*
* @phpstan-var Schema $schema
*/
$schema = $this->add_additional_fields_schema( $this->schema );
return $schema;
}
/**
* Prepares a single story for create or update. Add post_content_filtered field to save/insert.
*
* @since 1.0.0
*
* @param WP_REST_Request $request Request object.
* @return stdClass|WP_Error Post object or WP_Error.
*/
protected function prepare_item_for_database( $request ) {
$prepared_post = parent::prepare_item_for_database( $request );
if ( is_wp_error( $prepared_post ) ) {
return $prepared_post;
}
/**
* Schema.
*
* @phpstan-var Schema $schema
*/
$schema = $this->get_item_schema();
// Post content.
if ( ! empty( $schema['properties']['content'] ) ) {
// Ensure that content and story_data are updated together.
// Exception: new auto-draft created from a template.
if (
(
( ! empty( $request['story_data'] ) && empty( $request['content'] ) ) ||
( ! empty( $request['content'] ) && empty( $request['story_data'] ) )
) && ( 'auto-draft' !== $prepared_post->post_status )
) {
return new \WP_Error(
'rest_empty_content',
\sprintf(
/* translators: 1: content, 2: story_data */
__( '%1$s and %2$s should always be updated together.', 'web-stories' ),
'content',
'story_data'
),
[ 'status' => 412 ]
);
}
if ( isset( $request['content'] ) ) {
$prepared_post->post_content = $this->decoder->base64_decode( $prepared_post->post_content );
}
}
// If the request is updating the content as well, let's make sure the JSON representation of the story is saved, too.
if ( ! empty( $schema['properties']['story_data'] ) && isset( $request['story_data'] ) ) {
$prepared_post->post_content_filtered = wp_json_encode( $request['story_data'] );
}
return $prepared_post;
}
/**
* Get registered post meta.
*
* @since 1.23.0
*
* @param WP_Post $original_post Post Object.
* @return array<string, mixed> $meta
*/
protected function get_registered_meta( WP_Post $original_post ): array {
$meta_keys = get_registered_meta_keys( 'post', $this->post_type );
$meta = [];
/**
* Meta key settings.
*
* @var array $settings
* @phpstan-var RegisteredMetadata $settings
*/
foreach ( $meta_keys as $key => $settings ) {
if ( $settings['show_in_rest'] ) {
$meta[ $key ] = get_post_meta( $original_post->ID, $key, $settings['single'] );
}
}
return $meta;
}
/**
* Prepares links for the request.
*
* Ensures that {@see Stories_Users_Controller} is used for author embeds.
*
* @since 1.10.0
*
* @param WP_Post $post Post object.
* @return array Links for the given post.
*
* @phpstan-return Links
*/
protected function prepare_links( $post ): array {
$links = parent::prepare_links( $post );
if ( ! empty( $post->post_author ) && post_type_supports( $post->post_type, 'author' ) ) {
$links['author'] = [
'href' => rest_url( \sprintf( '%s/%s/%s', $this->namespace, 'users', $post->post_author ) ),
'embeddable' => true,
];
}
// If we have a featured media, add that.
$featured_media = get_post_thumbnail_id( $post->ID );
if ( $featured_media ) {
$image_url = rest_url( \sprintf( '%s/%s/%s', $this->namespace, 'media', $featured_media ) );
$links['https://api.w.org/featuredmedia'] = [
'href' => $image_url,
'embeddable' => true,
];
}
if ( ! \in_array( $post->post_type, [ 'attachment', 'nav_menu_item', 'revision' ], true ) ) {
$attachments_url = rest_url( \sprintf( '%s/%s', $this->namespace, 'media' ) );
$attachments_url = add_query_arg( 'parent', $post->ID, $attachments_url );
$links['https://api.w.org/attachment'] = [
'href' => $attachments_url,
];
}
return $links;
}
/**
* Get the link relations available for the post and current user.
*
* @since 1.10.0
*
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return string[] List of link relations.
*/
protected function get_available_actions( $post, $request ): array {
$rels = parent::get_available_actions( $post, $request );
if ( $this->check_delete_permission( $post ) ) {
$rels[] = 'https://api.w.org/action-delete';
}
if ( $this->check_update_permission( $post ) ) {
$rels[] = 'https://api.w.org/action-edit';
}
return $rels;
}
}