PluginProbe
Assistant – Every Day Productivity Apps / 1.5.5.1
Assistant – Every Day Productivity Apps v1.5.5.1
1.5.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 All 72 releases
← All changes | backend/src/Controllers/PostsController.php +69 -32 0.3.0 → 1.5.5.1 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,26 @@
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() );
269 + global $wpdb;
252 270
271 + $params = $request->get_params();
272 + $meta_inputs = isset( $params['meta_input'] ) ? $params['meta_input'] : [];
273 + unset( $params['meta_input'] );
274 + $id = wp_insert_post( $params );
275 +
276 + if ( isset( $meta_inputs ) && is_array( $meta_inputs ) && ! empty( $meta_inputs ) ) {
277 +
278 + foreach ( $meta_inputs as $meta_key => $meta_value ) {
279 + $meta_key = sanitize_key( $meta_key );
280 + if ( null !== $meta_value ) {
281 + $meta_value = addslashes( $meta_value );
282 + }
283 + $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) values ({$id}, '{$meta_key}', '{$meta_value}')" );
284 + }
285 + }
286 +
253 287 if ( ! $id || is_wp_error( $id ) ) {
254 288 return [
255 289 'error' => true,
256 290 ];
@@ -264,12 +298,12 @@
264 298 */
265 299 public function clone_post( $request ) {
266 300 global $wpdb;
267 301
268 - $post_id = absint( $request->get_param( 'id' ) );
269 - $post = get_post( $post_id );
302 + $post_id = absint( $request->get_param( 'id' ) );
303 + $post = get_post( $post_id );
270 304 $current_user = wp_get_current_user();
271 - $post_data = [
305 + $post_data = [
272 306 'comment_status' => $post->comment_status,
273 307 'ping_status' => $post->ping_status,
274 308 'post_author' => $current_user->ID,
275 309 'post_content' => $post->post_content,
@@ -278,9 +312,9 @@
278 312 'post_parent' => $post->post_parent,
279 313 'post_password' => $post->post_password,
280 314 'post_status' => 'draft',
281 315 /* translators: %s: post/page title */
282 - 'post_title' => sprintf( _x( '%s - Copy', '%s stands for post/page title.', 'fl-assistant' ), $post->post_title ),
316 + 'post_title' => sprintf( _x( '%s - Copy', '%s stands for post/page title.', 'assistant' ), $post->post_title ),
283 317 'post_type' => $post->post_type,
284 318 'to_ping' => $post->to_ping,
285 319 'menu_order' => $post->menu_order,
286 320 ];
@@ -285,14 +319,14 @@
285 319 'menu_order' => $post->menu_order,
286 320 ];
287 321
288 322 $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 );
323 + $post_meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $post_id ) );
324 + $taxonomies = get_object_taxonomies( $post->post_type );
291 325
292 326 if ( count( $post_meta ) !== 0 ) {
293 327 foreach ( $post_meta as $meta_info ) {
294 - $meta_key = $meta_info->meta_key;
328 + $meta_key = $meta_info->meta_key;
295 329 $meta_value = addslashes( $meta_info->meta_value );
296 330 $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 331 }
298 332 }
@@ -298,9 +332,9 @@
298 332 }
299 333
300 334 foreach ( $taxonomies as $taxonomy ) {
301 335 $post_terms = wp_get_object_terms( $post_id, $taxonomy );
302 - for ( $i = 0; $i < count( $post_terms ); $i ++ ) {
336 + for ( $i = 0; $i < count( $post_terms ); $i++ ) {
303 337 wp_set_object_terms( $new_post_id, $post_terms[ $i ]->slug, $taxonomy, true );
304 338 }
305 339 }
306 340
@@ -310,9 +344,9 @@
310 344 /**
311 345 * Updates a single post based on the specified action.
312 346 */
313 347 public function update_post( $request ) {
314 - $id = $request->get_param( 'id' );
348 + $id = $request->get_param( 'id' );
315 349 $action = $request->get_param( 'action' );
316 350
317 351 if ( ! current_user_can( 'edit_post', $id ) ) {
318 352 return rest_ensure_response(
@@ -343,9 +377,10 @@
343 377 unset( $data['thumbnail'] );
344 378 }
345 379 wp_update_post(
346 380 array_merge(
347 - $data, [
381 + $data,
382 + [
348 383 'ID' => $id,
349 384 ]
350 385 )
351 386 );
@@ -402,9 +437,11 @@
402 437 /**
403 438 * Deletes a single post based on the specified ID.
404 439 */
405 440 public function delete_post( $request ) {
406 - $id = $request->get_param( 'id' );
441 + $params = $request->get_params();
442 + $id = $params['id'];
443 + $force = isset( $params['force'] ) && $params['force'];
407 444
408 445 if ( ! current_user_can( 'edit_post', $id ) ) {
409 446 return rest_ensure_response(
410 447 [
@@ -412,9 +449,9 @@
412 449 ]
413 450 );
414 451 }
415 452
416 - wp_delete_post( $id );
453 + wp_delete_post( $id, $force );
417 454
418 455 return rest_ensure_response(
419 456 [
420 457 'success' => true,