PluginProbe
Assistant – Every Day Productivity Apps / 1.4.8
Assistant – Every Day Productivity Apps v1.4.8
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 +61 -32 0.3.11.4.8 View file →
@@ -1,12 +1,13 @@
1 1 <?php
2 2
3 3 namespace FL\Assistant\Controllers;
4 4
5 +use FL\Assistant\Data\Repository\LabelsRepository;
5 6 use FL\Assistant\Data\Repository\PostsRepository;
6 7 use FL\Assistant\Data\Transformers\PostTransformer;
7 -use FL\Assistant\System\Integrations\BeaverBuilder;
8 8 use FL\Assistant\System\Contracts\ControllerAbstract;
9 +
9 10 use WP_REST_Request;
10 11 use WP_REST_Server;
11 12
12 13 /**
@@ -13,12 +14,18 @@
13 14 * REST API logic for posts.
14 15 */
15 16 class PostsController extends ControllerAbstract {
16 17
18 + protected $labels;
17 19 protected $posts;
18 20 protected $transformer;
19 21
20 - public function __construct( PostsRepository $posts, PostTransformer $transformer ) {
22 + public function __construct(
23 + LabelsRepository $labels,
24 + PostsRepository $posts,
25 + PostTransformer $transformer
26 + ) {
27 + $this->labels = $labels;
21 28 $this->posts = $posts;
22 29 $this->transformer = $transformer;
23 30 }
24 31
@@ -26,9 +33,10 @@
26 33 * Register routes.
27 34 */
28 35 public function register_routes() {
29 36 $this->route(
30 - '/posts', [
37 + '/posts',
38 + [
31 39 [
32 40 'methods' => WP_REST_Server::READABLE,
33 41 'callback' => [ $this, 'posts' ],
34 42 'permission_callback' => function () {
@@ -45,9 +53,10 @@
45 53 ]
46 54 );
47 55
48 56 $this->route(
49 - '/posts/hierarchical', [
57 + '/posts/hierarchical',
58 + [
50 59 [
51 60 'methods' => WP_REST_Server::READABLE,
52 61 'callback' => [ $this, 'hierarchical_posts' ],
53 62 'permission_callback' => function () {
@@ -57,9 +66,10 @@
57 66 ]
58 67 );
59 68
60 69 $this->route(
61 - '/posts/count', [
70 + '/posts/count',
71 + [
62 72 [
63 73 'methods' => WP_REST_Server::READABLE,
64 74 'callback' => [ $this, 'posts_count' ],
65 75 'permission_callback' => function () {
@@ -69,9 +79,10 @@
69 79 ]
70 80 );
71 81
72 82 $this->route(
73 - '/posts/(?P<id>\d+)', [
83 + '/posts/(?P<id>\d+)',
84 + [
74 85 [
75 86 'methods' => WP_REST_Server::READABLE,
76 87 'callback' => [ $this, 'post' ],
77 88 'args' => [
@@ -117,9 +128,10 @@
117 128 ]
118 129 );
119 130
120 131 $this->route(
121 - '/posts/(?P<id>\d+)/clone', [
132 + '/posts/(?P<id>\d+)/clone',
133 + [
122 134 [
123 135 'methods' => WP_REST_Server::CREATABLE,
124 136 'callback' => [ $this, 'clone_post' ],
125 137 'args' => [
@@ -141,10 +153,16 @@
141 153 */
142 154 public function posts( WP_REST_Request $request ) {
143 155
144 156 $params = $request->get_params();
157 + $params['perm'] = 'editable';
145 158
146 - $params['perm'] = 'editable';
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 + }
147 165
148 166 $pager = $this->posts->paginate( $params, $this->transformer );
149 167
150 168 return rest_ensure_response( $pager->to_array() );
@@ -157,12 +175,13 @@
157 175 */
158 176 public function hierarchical_posts( $request ) {
159 177 $response = [];
160 178 $children = [];
161 - $params = $request->get_params();
162 - $posts = get_posts(
179 + $params = $request->get_params();
180 + $posts = get_posts(
163 181 array_merge(
164 - $params, [
182 + $params,
183 + [
165 184 'perm' => 'editable',
166 185 ]
167 186 )
168 187 );
@@ -171,17 +190,17 @@
171 190 if ( $post->post_parent ) {
172 191 if ( ! isset( $children[ $post->post_parent ] ) ) {
173 192 $children[ $post->post_parent ] = [];
174 193 }
175 - $children[ $post->post_parent ][] = $post;
194 + $children [ $post->post_parent ][] = $post;
176 195 }
177 196 }
178 197
179 198 foreach ( $posts as $post ) {
180 199 if ( ! $post->post_parent ) {
181 - $parent = $this->transform( $post );
200 + $parent = $this->transform( $post );
182 201 $parent['children'] = $this->get_child_posts( $post, $children );
183 - $response[] = $parent;
202 + $response[] = $parent;
184 203 }
185 204 }
186 205
187 206 return rest_ensure_response( $response );
@@ -201,9 +220,9 @@
201 220 public function get_child_posts( $post, $children ) {
202 221 if ( isset( $children[ $post->ID ] ) ) {
203 222 $post_children = $children[ $post->ID ];
204 223 foreach ( $post_children as $i => $child ) {
205 - $post_children[ $i ] = $this->transform( $child );
224 + $post_children[ $i ] = $this->transform( $child );
206 225 $post_children[ $i ]['children'] = $this->get_child_posts( $child, $children );
207 226 }
208 227
209 228 return $post_children;
@@ -217,13 +236,13 @@
217 236 */
218 237 public function posts_count( $request ) {
219 238
220 239 $post_types = $this->posts->get_types();
221 - $response = [];
240 + $response = [];
222 241
223 242 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;
243 + $counts = wp_count_posts( $slug );
244 + $counts->total = $counts->publish + $counts->draft + $counts->pending + $counts->private + $counts->future;
226 245 $response[ $slug ] = $counts;
227 246 }
228 247
229 248 return rest_ensure_response( $response );
@@ -236,9 +255,8 @@
236 255 $id = $request->get_param( 'id' );
237 256
238 257 if ( current_user_can( 'edit_post', $id ) ) {
239 258 $post = get_post( $id );
240 -
241 259 return $this->transform( $post );
242 260 }
243 261
244 262 return [];
@@ -247,10 +265,18 @@
247 265 /**
248 266 * Creates a single post.
249 267 */
250 268 public function create_post( $request ) {
251 - $id = wp_insert_post( $request->get_params() );
252 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 +
253 279 if ( ! $id || is_wp_error( $id ) ) {
254 280 return [
255 281 'error' => true,
256 282 ];
@@ -264,12 +290,12 @@
264 290 */
265 291 public function clone_post( $request ) {
266 292 global $wpdb;
267 293
268 - $post_id = absint( $request->get_param( 'id' ) );
269 - $post = get_post( $post_id );
294 + $post_id = absint( $request->get_param( 'id' ) );
295 + $post = get_post( $post_id );
270 296 $current_user = wp_get_current_user();
271 - $post_data = [
297 + $post_data = [
272 298 'comment_status' => $post->comment_status,
273 299 'ping_status' => $post->ping_status,
274 300 'post_author' => $current_user->ID,
275 301 'post_content' => $post->post_content,
@@ -278,9 +304,9 @@
278 304 'post_parent' => $post->post_parent,
279 305 'post_password' => $post->post_password,
280 306 'post_status' => 'draft',
281 307 /* translators: %s: post/page title */
282 - 'post_title' => sprintf( _x( '%s - Copy', '%s stands for post/page title.', 'fl-assistant' ), $post->post_title ),
308 + 'post_title' => sprintf( _x( '%s - Copy', '%s stands for post/page title.', 'assistant' ), $post->post_title ),
283 309 'post_type' => $post->post_type,
284 310 'to_ping' => $post->to_ping,
285 311 'menu_order' => $post->menu_order,
286 312 ];
@@ -285,14 +311,14 @@
285 311 'menu_order' => $post->menu_order,
286 312 ];
287 313
288 314 $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 );
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 );
291 317
292 318 if ( count( $post_meta ) !== 0 ) {
293 319 foreach ( $post_meta as $meta_info ) {
294 - $meta_key = $meta_info->meta_key;
320 + $meta_key = $meta_info->meta_key;
295 321 $meta_value = addslashes( $meta_info->meta_value );
296 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
297 323 }
298 324 }
@@ -298,9 +324,9 @@
298 324 }
299 325
300 326 foreach ( $taxonomies as $taxonomy ) {
301 327 $post_terms = wp_get_object_terms( $post_id, $taxonomy );
302 - for ( $i = 0; $i < count( $post_terms ); $i ++ ) {
328 + for ( $i = 0; $i < count( $post_terms ); $i++ ) {
303 329 wp_set_object_terms( $new_post_id, $post_terms[ $i ]->slug, $taxonomy, true );
304 330 }
305 331 }
306 332
@@ -310,9 +336,9 @@
310 336 /**
311 337 * Updates a single post based on the specified action.
312 338 */
313 339 public function update_post( $request ) {
314 - $id = $request->get_param( 'id' );
340 + $id = $request->get_param( 'id' );
315 341 $action = $request->get_param( 'action' );
316 342
317 343 if ( ! current_user_can( 'edit_post', $id ) ) {
318 344 return rest_ensure_response(
@@ -343,9 +369,10 @@
343 369 unset( $data['thumbnail'] );
344 370 }
345 371 wp_update_post(
346 372 array_merge(
347 - $data, [
373 + $data,
374 + [
348 375 'ID' => $id,
349 376 ]
350 377 )
351 378 );
@@ -402,9 +429,11 @@
402 429 /**
403 430 * Deletes a single post based on the specified ID.
404 431 */
405 432 public function delete_post( $request ) {
406 - $id = $request->get_param( 'id' );
433 + $params = $request->get_params();
434 + $id = $params['id'];
435 + $force = isset( $params['force'] ) && $params['force'];
407 436
408 437 if ( ! current_user_can( 'edit_post', $id ) ) {
409 438 return rest_ensure_response(
410 439 [
@@ -412,9 +441,9 @@
412 441 ]
413 442 );
414 443 }
415 444
416 - wp_delete_post( $id );
445 + wp_delete_post( $id, $force );
417 446
418 447 return rest_ensure_response(
419 448 [
420 449 'success' => true,