PluginProbe
Assistant – Every Day Productivity Apps / 1.5.1
Assistant – Every Day Productivity Apps v1.5.1
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
← All changes | backend/src/Controllers/PostsController.php +453 -424 0.3.11.5.1 View file →
@@ -1,424 +1,453 @@
1 -<?php
2 -
3 -namespace FL\Assistant\Controllers;
4 -
5 -use FL\Assistant\Data\Repository\PostsRepository;
6 -use FL\Assistant\Data\Transformers\PostTransformer;
7 -use FL\Assistant\System\Integrations\BeaverBuilder;
8 -use FL\Assistant\System\Contracts\ControllerAbstract;
9 -use WP_REST_Request;
10 -use WP_REST_Server;
11 -
12 -/**
13 - * REST API logic for posts.
14 - */
15 -class PostsController extends ControllerAbstract {
16 -
17 - protected $posts;
18 - protected $transformer;
19 -
20 - public function __construct( PostsRepository $posts, PostTransformer $transformer ) {
21 - $this->posts = $posts;
22 - $this->transformer = $transformer;
23 - }
24 -
25 - /**
26 - * Register routes.
27 - */
28 - public function register_routes() {
29 - $this->route(
30 - '/posts', [
31 - [
32 - 'methods' => WP_REST_Server::READABLE,
33 - 'callback' => [ $this, 'posts' ],
34 - 'permission_callback' => function () {
35 - return current_user_can( 'edit_others_posts' );
36 - },
37 - ],
38 - [
39 - 'methods' => WP_REST_Server::CREATABLE,
40 - 'callback' => [ $this, 'create_post' ],
41 - 'permission_callback' => function () {
42 - return current_user_can( 'edit_others_posts' );
43 - },
44 - ],
45 - ]
46 - );
47 -
48 - $this->route(
49 - '/posts/hierarchical', [
50 - [
51 - 'methods' => WP_REST_Server::READABLE,
52 - 'callback' => [ $this, 'hierarchical_posts' ],
53 - 'permission_callback' => function () {
54 - return current_user_can( 'edit_others_posts' );
55 - },
56 - ],
57 - ]
58 - );
59 -
60 - $this->route(
61 - '/posts/count', [
62 - [
63 - 'methods' => WP_REST_Server::READABLE,
64 - 'callback' => [ $this, 'posts_count' ],
65 - 'permission_callback' => function () {
66 - return current_user_can( 'edit_others_posts' );
67 - },
68 - ],
69 - ]
70 - );
71 -
72 - $this->route(
73 - '/posts/(?P<id>\d+)', [
74 - [
75 - 'methods' => WP_REST_Server::READABLE,
76 - 'callback' => [ $this, 'post' ],
77 - 'args' => [
78 - 'id' => [
79 - 'required' => true,
80 - 'type' => 'number',
81 - ],
82 - ],
83 - 'permission_callback' => function () {
84 - return current_user_can( 'edit_others_posts' );
85 - },
86 - ],
87 - [
88 - 'methods' => WP_REST_Server::CREATABLE,
89 - 'callback' => [ $this, 'update_post' ],
90 - 'args' => [
91 - 'id' => [
92 - 'required' => true,
93 - 'type' => 'number',
94 - ],
95 - 'action' => [
96 - 'required' => true,
97 - 'type' => 'string',
98 - ],
99 - ],
100 - 'permission_callback' => function () {
101 - return current_user_can( 'edit_others_posts' );
102 - },
103 - ],
104 - [
105 - 'methods' => WP_REST_Server::DELETABLE,
106 - 'callback' => [ $this, 'delete_post' ],
107 - 'args' => [
108 - 'id' => [
109 - 'required' => true,
110 - 'type' => 'number',
111 - ],
112 - ],
113 - 'permission_callback' => function () {
114 - return current_user_can( 'edit_others_posts' );
115 - },
116 - ],
117 - ]
118 - );
119 -
120 - $this->route(
121 - '/posts/(?P<id>\d+)/clone', [
122 - [
123 - 'methods' => WP_REST_Server::CREATABLE,
124 - 'callback' => [ $this, 'clone_post' ],
125 - 'args' => [
126 - 'id' => [
127 - 'required' => true,
128 - 'type' => 'number',
129 - ],
130 - ],
131 - 'permission_callback' => function () {
132 - return current_user_can( 'edit_others_posts' );
133 - },
134 - ],
135 - ]
136 - );
137 - }
138 -
139 - /**
140 - * Returns an array of posts and related data.
141 - */
142 - public function posts( WP_REST_Request $request ) {
143 -
144 - $params = $request->get_params();
145 -
146 - $params['perm'] = 'editable';
147 -
148 - $pager = $this->posts->paginate( $params, $this->transformer );
149 -
150 - return rest_ensure_response( $pager->to_array() );
151 - }
152 -
153 - /**
154 - * Returns an array of posts and related data
155 - * with child posts contained in the parent
156 - * post's data array.
157 - */
158 - public function hierarchical_posts( $request ) {
159 - $response = [];
160 - $children = [];
161 - $params = $request->get_params();
162 - $posts = get_posts(
163 - array_merge(
164 - $params, [
165 - 'perm' => 'editable',
166 - ]
167 - )
168 - );
169 -
170 - foreach ( $posts as $post ) {
171 - if ( $post->post_parent ) {
172 - if ( ! isset( $children[ $post->post_parent ] ) ) {
173 - $children[ $post->post_parent ] = [];
174 - }
175 - $children[ $post->post_parent ][] = $post;
176 - }
177 - }
178 -
179 - foreach ( $posts as $post ) {
180 - if ( ! $post->post_parent ) {
181 - $parent = $this->transform( $post );
182 - $parent['children'] = $this->get_child_posts( $post, $children );
183 - $response[] = $parent;
184 - }
185 - }
186 -
187 - return rest_ensure_response( $response );
188 - }
189 -
190 - /**
191 - * Returns an array of response data for a single post.
192 - */
193 - public function transform( $post ) {
194 - return call_user_func( $this->transformer, $post );
195 - }
196 -
197 - /**
198 - * Returns an array of child posts for the given post.
199 - * A $children array must be passed to search for children.
200 - */
201 - public function get_child_posts( $post, $children ) {
202 - if ( isset( $children[ $post->ID ] ) ) {
203 - $post_children = $children[ $post->ID ];
204 - foreach ( $post_children as $i => $child ) {
205 - $post_children[ $i ] = $this->transform( $child );
206 - $post_children[ $i ]['children'] = $this->get_child_posts( $child, $children );
207 - }
208 -
209 - return $post_children;
210 - }
211 -
212 - return [];
213 - }
214 -
215 - /**
216 - * Returns an array of counts by post type.
217 - */
218 - public function posts_count( $request ) {
219 -
220 - $post_types = $this->posts->get_types();
221 - $response = [];
222 -
223 - foreach ( $post_types as $slug => $label ) {
224 - $counts = wp_count_posts( $slug );
225 - $counts->total = $counts->publish + $counts->draft + $counts->pending + $counts->private + $counts->future;
226 - $response[ $slug ] = $counts;
227 - }
228 -
229 - return rest_ensure_response( $response );
230 - }
231 -
232 - /**
233 - * Returns data for a single post.
234 - */
235 - public function post( $request ) {
236 - $id = $request->get_param( 'id' );
237 -
238 - if ( current_user_can( 'edit_post', $id ) ) {
239 - $post = get_post( $id );
240 -
241 - return $this->transform( $post );
242 - }
243 -
244 - return [];
245 - }
246 -
247 - /**
248 - * Creates a single post.
249 - */
250 - public function create_post( $request ) {
251 - $id = wp_insert_post( $request->get_params() );
252 -
253 - if ( ! $id || is_wp_error( $id ) ) {
254 - return [
255 - 'error' => true,
256 - ];
257 - }
258 -
259 - return $this->transform( get_post( $id ) );
260 - }
261 -
262 - /**
263 - * Clones a single post.
264 - */
265 - public function clone_post( $request ) {
266 - global $wpdb;
267 -
268 - $post_id = absint( $request->get_param( 'id' ) );
269 - $post = get_post( $post_id );
270 - $current_user = wp_get_current_user();
271 - $post_data = [
272 - 'comment_status' => $post->comment_status,
273 - 'ping_status' => $post->ping_status,
274 - 'post_author' => $current_user->ID,
275 - 'post_content' => $post->post_content,
276 - 'post_excerpt' => $post->post_excerpt,
277 - 'post_name' => $post->post_name . '-copy',
278 - 'post_parent' => $post->post_parent,
279 - 'post_password' => $post->post_password,
280 - 'post_status' => 'draft',
281 - /* translators: %s: post/page title */
282 - 'post_title' => sprintf( _x( '%s - Copy', '%s stands for post/page title.', 'fl-assistant' ), $post->post_title ),
283 - 'post_type' => $post->post_type,
284 - 'to_ping' => $post->to_ping,
285 - 'menu_order' => $post->menu_order,
286 - ];
287 -
288 - $new_post_id = wp_insert_post( $post_data );
289 - $post_meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $post_id ) );
290 - $taxonomies = get_object_taxonomies( $post->post_type );
291 -
292 - if ( count( $post_meta ) !== 0 ) {
293 - foreach ( $post_meta as $meta_info ) {
294 - $meta_key = $meta_info->meta_key;
295 - $meta_value = addslashes( $meta_info->meta_value );
296 - $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) values ({$new_post_id}, '{$meta_key}', '{$meta_value}')" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
297 - }
298 - }
299 -
300 - foreach ( $taxonomies as $taxonomy ) {
301 - $post_terms = wp_get_object_terms( $post_id, $taxonomy );
302 - for ( $i = 0; $i < count( $post_terms ); $i ++ ) {
303 - wp_set_object_terms( $new_post_id, $post_terms[ $i ]->slug, $taxonomy, true );
304 - }
305 - }
306 -
307 - return $this->transform( get_post( $new_post_id ) );
308 - }
309 -
310 - /**
311 - * Updates a single post based on the specified action.
312 - */
313 - public function update_post( $request ) {
314 - $id = $request->get_param( 'id' );
315 - $action = $request->get_param( 'action' );
316 -
317 - if ( ! current_user_can( 'edit_post', $id ) ) {
318 - return rest_ensure_response(
319 - [
320 - 'error' => true,
321 - ]
322 - );
323 - }
324 -
325 - switch ( $action ) {
326 - case 'data':
327 - $data = (array) $request->get_param( 'data' );
328 - if ( isset( $data['meta'] ) ) {
329 - $this->update_post_meta( $id, $data['meta'] );
330 - unset( $data['meta'] );
331 - }
332 - if ( isset( $data['terms'] ) ) {
333 - $this->update_post_terms( $id, $data['terms'] );
334 - unset( $data['terms'] );
335 - }
336 - if ( isset( $data['thumbnail'] ) ) {
337 - if ( '0' === $data['thumbnail'] ) {
338 - delete_post_meta( $id, '_thumbnail_id' );
339 - } else {
340 - set_post_thumbnail( $id, $data['thumbnail'] );
341 - }
342 -
343 - unset( $data['thumbnail'] );
344 - }
345 - wp_update_post(
346 - array_merge(
347 - $data, [
348 - 'ID' => $id,
349 - ]
350 - )
351 - );
352 - break;
353 - case 'meta':
354 - $data = (array) $request->get_param( 'data' );
355 - $this->update_post_meta( $id, $data );
356 - break;
357 - case 'terms':
358 - $data = (array) $request->get_param( 'data' );
359 - $this->update_post_terms( $id, $data );
360 - break;
361 - case 'trash':
362 - if ( ! EMPTY_TRASH_DAYS ) {
363 - wp_delete_post( $id );
364 - } else {
365 - wp_trash_post( $id );
366 - }
367 - break;
368 - case 'untrash':
369 - wp_untrash_post( $id );
370 - break;
371 -
372 - }
373 -
374 - $updated_post = get_post( $id );
375 -
376 - return rest_ensure_response(
377 - [
378 - 'success' => true,
379 - 'post' => $updated_post ? $this->transform( $updated_post ) : null,
380 - ]
381 - );
382 - }
383 -
384 - /**
385 - * Updates post meta values for a post.
386 - */
387 - public function update_post_meta( $id, $meta ) {
388 - foreach ( $meta as $key => $value ) {
389 - update_post_meta( $id, $key, $value );
390 - }
391 - }
392 -
393 - /**
394 - * Updates terms for a post.
395 - */
396 - public function update_post_terms( $id, $terms ) {
397 - foreach ( $terms as $taxonomy => $ids ) {
398 - wp_set_post_terms( $id, $ids, $taxonomy, false );
399 - }
400 - }
401 -
402 - /**
403 - * Deletes a single post based on the specified ID.
404 - */
405 - public function delete_post( $request ) {
406 - $id = $request->get_param( 'id' );
407 -
408 - if ( ! current_user_can( 'edit_post', $id ) ) {
409 - return rest_ensure_response(
410 - [
411 - 'error' => true,
412 - ]
413 - );
414 - }
415 -
416 - wp_delete_post( $id );
417 -
418 - return rest_ensure_response(
419 - [
420 - 'success' => true,
421 - ]
422 - );
423 - }
424 -}
1 +<?php
2 +
3 +namespace FL\Assistant\Controllers;
4 +
5 +use FL\Assistant\Data\Repository\LabelsRepository;
6 +use FL\Assistant\Data\Repository\PostsRepository;
7 +use FL\Assistant\Data\Transformers\PostTransformer;
8 +use FL\Assistant\System\Contracts\ControllerAbstract;
9 +
10 +use WP_REST_Request;
11 +use WP_REST_Server;
12 +
13 +/**
14 + * REST API logic for posts.
15 + */
16 +class PostsController extends ControllerAbstract {
17 +
18 + protected $labels;
19 + protected $posts;
20 + protected $transformer;
21 +
22 + public function __construct(
23 + LabelsRepository $labels,
24 + PostsRepository $posts,
25 + PostTransformer $transformer
26 + ) {
27 + $this->labels = $labels;
28 + $this->posts = $posts;
29 + $this->transformer = $transformer;
30 + }
31 +
32 + /**
33 + * Register routes.
34 + */
35 + public function register_routes() {
36 + $this->route(
37 + '/posts',
38 + [
39 + [
40 + 'methods' => WP_REST_Server::READABLE,
41 + 'callback' => [ $this, 'posts' ],
42 + 'permission_callback' => function () {
43 + return current_user_can( 'edit_others_posts' );
44 + },
45 + ],
46 + [
47 + 'methods' => WP_REST_Server::CREATABLE,
48 + 'callback' => [ $this, 'create_post' ],
49 + 'permission_callback' => function () {
50 + return current_user_can( 'edit_others_posts' );
51 + },
52 + ],
53 + ]
54 + );
55 +
56 + $this->route(
57 + '/posts/hierarchical',
58 + [
59 + [
60 + 'methods' => WP_REST_Server::READABLE,
61 + 'callback' => [ $this, 'hierarchical_posts' ],
62 + 'permission_callback' => function () {
63 + return current_user_can( 'edit_others_posts' );
64 + },
65 + ],
66 + ]
67 + );
68 +
69 + $this->route(
70 + '/posts/count',
71 + [
72 + [
73 + 'methods' => WP_REST_Server::READABLE,
74 + 'callback' => [ $this, 'posts_count' ],
75 + 'permission_callback' => function () {
76 + return current_user_can( 'edit_others_posts' );
77 + },
78 + ],
79 + ]
80 + );
81 +
82 + $this->route(
83 + '/posts/(?P<id>\d+)',
84 + [
85 + [
86 + 'methods' => WP_REST_Server::READABLE,
87 + 'callback' => [ $this, 'post' ],
88 + 'args' => [
89 + 'id' => [
90 + 'required' => true,
91 + 'type' => 'number',
92 + ],
93 + ],
94 + 'permission_callback' => function () {
95 + return current_user_can( 'edit_others_posts' );
96 + },
97 + ],
98 + [
99 + 'methods' => WP_REST_Server::CREATABLE,
100 + 'callback' => [ $this, 'update_post' ],
101 + 'args' => [
102 + 'id' => [
103 + 'required' => true,
104 + 'type' => 'number',
105 + ],
106 + 'action' => [
107 + 'required' => true,
108 + 'type' => 'string',
109 + ],
110 + ],
111 + 'permission_callback' => function () {
112 + return current_user_can( 'edit_others_posts' );
113 + },
114 + ],
115 + [
116 + 'methods' => WP_REST_Server::DELETABLE,
117 + 'callback' => [ $this, 'delete_post' ],
118 + 'args' => [
119 + 'id' => [
120 + 'required' => true,
121 + 'type' => 'number',
122 + ],
123 + ],
124 + 'permission_callback' => function () {
125 + return current_user_can( 'edit_others_posts' );
126 + },
127 + ],
128 + ]
129 + );
130 +
131 + $this->route(
132 + '/posts/(?P<id>\d+)/clone',
133 + [
134 + [
135 + 'methods' => WP_REST_Server::CREATABLE,
136 + 'callback' => [ $this, 'clone_post' ],
137 + 'args' => [
138 + 'id' => [
139 + 'required' => true,
140 + 'type' => 'number',
141 + ],
142 + ],
143 + 'permission_callback' => function () {
144 + return current_user_can( 'edit_others_posts' );
145 + },
146 + ],
147 + ]
148 + );
149 + }
150 +
151 + /**
152 + * Returns an array of posts and related data.
153 + */
154 + public function posts( WP_REST_Request $request ) {
155 +
156 + $params = $request->get_params();
157 + $params['perm'] = 'editable';
158 +
159 + if ( isset( $params['label'] ) && $params['label'] ) {
160 + $params['post__in'] = $this->labels->get_object_ids( 'post', $params['label'] );
161 + if ( ! count( $params['post__in'] ) ) {
162 + $params['post__in'][] = -1; // post__in returns all posts if empty.
163 + }
164 + }
165 +
166 + $pager = $this->posts->paginate( $params, $this->transformer );
167 +
168 + return rest_ensure_response( $pager->to_array() );
169 + }
170 +
171 + /**
172 + * Returns an array of posts and related data
173 + * with child posts contained in the parent
174 + * post's data array.
175 + */
176 + public function hierarchical_posts( $request ) {
177 + $response = [];
178 + $children = [];
179 + $params = $request->get_params();
180 + $posts = get_posts(
181 + array_merge(
182 + $params,
183 + [
184 + 'perm' => 'editable',
185 + ]
186 + )
187 + );
188 +
189 + foreach ( $posts as $post ) {
190 + if ( $post->post_parent ) {
191 + if ( ! isset( $children[ $post->post_parent ] ) ) {
192 + $children[ $post->post_parent ] = [];
193 + }
194 + $children [ $post->post_parent ][] = $post;
195 + }
196 + }
197 +
198 + foreach ( $posts as $post ) {
199 + if ( ! $post->post_parent ) {
200 + $parent = $this->transform( $post );
201 + $parent['children'] = $this->get_child_posts( $post, $children );
202 + $response[] = $parent;
203 + }
204 + }
205 +
206 + return rest_ensure_response( $response );
207 + }
208 +
209 + /**
210 + * Returns an array of response data for a single post.
211 + */
212 + public function transform( $post ) {
213 + return call_user_func( $this->transformer, $post );
214 + }
215 +
216 + /**
217 + * Returns an array of child posts for the given post.
218 + * A $children array must be passed to search for children.
219 + */
220 + public function get_child_posts( $post, $children ) {
221 + if ( isset( $children[ $post->ID ] ) ) {
222 + $post_children = $children[ $post->ID ];
223 + foreach ( $post_children as $i => $child ) {
224 + $post_children[ $i ] = $this->transform( $child );
225 + $post_children[ $i ]['children'] = $this->get_child_posts( $child, $children );
226 + }
227 +
228 + return $post_children;
229 + }
230 +
231 + return [];
232 + }
233 +
234 + /**
235 + * Returns an array of counts by post type.
236 + */
237 + public function posts_count( $request ) {
238 +
239 + $post_types = $this->posts->get_types();
240 + $response = [];
241 +
242 + foreach ( $post_types as $slug => $label ) {
243 + $counts = wp_count_posts( $slug );
244 + $counts->total = $counts->publish + $counts->draft + $counts->pending + $counts->private + $counts->future;
245 + $response[ $slug ] = $counts;
246 + }
247 +
248 + return rest_ensure_response( $response );
249 + }
250 +
251 + /**
252 + * Returns data for a single post.
253 + */
254 + public function post( $request ) {
255 + $id = $request->get_param( 'id' );
256 +
257 + if ( current_user_can( 'edit_post', $id ) ) {
258 + $post = get_post( $id );
259 + return $this->transform( $post );
260 + }
261 +
262 + return [];
263 + }
264 +
265 + /**
266 + * Creates a single post.
267 + */
268 + public function create_post( $request ) {
269 +
270 + $params = $request->get_params();
271 + if ( isset( $params['meta_input'] ) && is_array( $params['meta_input'] ) ) {
272 + foreach ( $params['meta_input'] as $meta_key => $meta_value ) {
273 + $params['meta_input'][ $meta_key ] = maybe_unserialize( $meta_value );
274 + }
275 + }
276 +
277 + $id = wp_insert_post( $params );
278 +
279 + if ( ! $id || is_wp_error( $id ) ) {
280 + return [
281 + 'error' => true,
282 + ];
283 + }
284 +
285 + return $this->transform( get_post( $id ) );
286 + }
287 +
288 + /**
289 + * Clones a single post.
290 + */
291 + public function clone_post( $request ) {
292 + global $wpdb;
293 +
294 + $post_id = absint( $request->get_param( 'id' ) );
295 + $post = get_post( $post_id );
296 + $current_user = wp_get_current_user();
297 + $post_data = [
298 + 'comment_status' => $post->comment_status,
299 + 'ping_status' => $post->ping_status,
300 + 'post_author' => $current_user->ID,
301 + 'post_content' => $post->post_content,
302 + 'post_excerpt' => $post->post_excerpt,
303 + 'post_name' => $post->post_name . '-copy',
304 + 'post_parent' => $post->post_parent,
305 + 'post_password' => $post->post_password,
306 + 'post_status' => 'draft',
307 + /* translators: %s: post/page title */
308 + 'post_title' => sprintf( _x( '%s - Copy', '%s stands for post/page title.', 'assistant' ), $post->post_title ),
309 + 'post_type' => $post->post_type,
310 + 'to_ping' => $post->to_ping,
311 + 'menu_order' => $post->menu_order,
312 + ];
313 +
314 + $new_post_id = wp_insert_post( $post_data );
315 + $post_meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $post_id ) );
316 + $taxonomies = get_object_taxonomies( $post->post_type );
317 +
318 + if ( count( $post_meta ) !== 0 ) {
319 + foreach ( $post_meta as $meta_info ) {
320 + $meta_key = $meta_info->meta_key;
321 + $meta_value = addslashes( $meta_info->meta_value );
322 + $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) values ({$new_post_id}, '{$meta_key}', '{$meta_value}')" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
323 + }
324 + }
325 +
326 + foreach ( $taxonomies as $taxonomy ) {
327 + $post_terms = wp_get_object_terms( $post_id, $taxonomy );
328 + for ( $i = 0; $i < count( $post_terms ); $i++ ) {
329 + wp_set_object_terms( $new_post_id, $post_terms[ $i ]->slug, $taxonomy, true );
330 + }
331 + }
332 +
333 + return $this->transform( get_post( $new_post_id ) );
334 + }
335 +
336 + /**
337 + * Updates a single post based on the specified action.
338 + */
339 + public function update_post( $request ) {
340 + $id = $request->get_param( 'id' );
341 + $action = $request->get_param( 'action' );
342 +
343 + if ( ! current_user_can( 'edit_post', $id ) ) {
344 + return rest_ensure_response(
345 + [
346 + 'error' => true,
347 + ]
348 + );
349 + }
350 +
351 + switch ( $action ) {
352 + case 'data':
353 + $data = (array) $request->get_param( 'data' );
354 + if ( isset( $data['meta'] ) ) {
355 + $this->update_post_meta( $id, $data['meta'] );
356 + unset( $data['meta'] );
357 + }
358 + if ( isset( $data['terms'] ) ) {
359 + $this->update_post_terms( $id, $data['terms'] );
360 + unset( $data['terms'] );
361 + }
362 + if ( isset( $data['thumbnail'] ) ) {
363 + if ( '0' === $data['thumbnail'] ) {
364 + delete_post_meta( $id, '_thumbnail_id' );
365 + } else {
366 + set_post_thumbnail( $id, $data['thumbnail'] );
367 + }
368 +
369 + unset( $data['thumbnail'] );
370 + }
371 + wp_update_post(
372 + array_merge(
373 + $data,
374 + [
375 + 'ID' => $id,
376 + ]
377 + )
378 + );
379 + break;
380 + case 'meta':
381 + $data = (array) $request->get_param( 'data' );
382 + $this->update_post_meta( $id, $data );
383 + break;
384 + case 'terms':
385 + $data = (array) $request->get_param( 'data' );
386 + $this->update_post_terms( $id, $data );
387 + break;
388 + case 'trash':
389 + if ( ! EMPTY_TRASH_DAYS ) {
390 + wp_delete_post( $id );
391 + } else {
392 + wp_trash_post( $id );
393 + }
394 + break;
395 + case 'untrash':
396 + wp_untrash_post( $id );
397 + break;
398 +
399 + }
400 +
401 + $updated_post = get_post( $id );
402 +
403 + return rest_ensure_response(
404 + [
405 + 'success' => true,
406 + 'post' => $updated_post ? $this->transform( $updated_post ) : null,
407 + ]
408 + );
409 + }
410 +
411 + /**
412 + * Updates post meta values for a post.
413 + */
414 + public function update_post_meta( $id, $meta ) {
415 + foreach ( $meta as $key => $value ) {
416 + update_post_meta( $id, $key, $value );
417 + }
418 + }
419 +
420 + /**
421 + * Updates terms for a post.
422 + */
423 + public function update_post_terms( $id, $terms ) {
424 + foreach ( $terms as $taxonomy => $ids ) {
425 + wp_set_post_terms( $id, $ids, $taxonomy, false );
426 + }
427 + }
428 +
429 + /**
430 + * Deletes a single post based on the specified ID.
431 + */
432 + public function delete_post( $request ) {
433 + $params = $request->get_params();
434 + $id = $params['id'];
435 + $force = isset( $params['force'] ) && $params['force'];
436 +
437 + if ( ! current_user_can( 'edit_post', $id ) ) {
438 + return rest_ensure_response(
439 + [
440 + 'error' => true,
441 + ]
442 + );
443 + }
444 +
445 + wp_delete_post( $id, $force );
446 +
447 + return rest_ensure_response(
448 + [
449 + 'success' => true,
450 + ]
451 + );
452 + }
453 +}