PluginProbe
Assistant – Every Day Productivity Apps / 1.3.8
Assistant – Every Day Productivity Apps v1.3.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 +47 -28 0.3.11.3.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 [];
@@ -264,12 +282,12 @@
264 282 */
265 283 public function clone_post( $request ) {
266 284 global $wpdb;
267 285
268 - $post_id = absint( $request->get_param( 'id' ) );
269 - $post = get_post( $post_id );
286 + $post_id = absint( $request->get_param( 'id' ) );
287 + $post = get_post( $post_id );
270 288 $current_user = wp_get_current_user();
271 - $post_data = [
289 + $post_data = [
272 290 'comment_status' => $post->comment_status,
273 291 'ping_status' => $post->ping_status,
274 292 'post_author' => $current_user->ID,
275 293 'post_content' => $post->post_content,
@@ -285,14 +303,14 @@
285 303 'menu_order' => $post->menu_order,
286 304 ];
287 305
288 306 $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 );
307 + $post_meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $post_id ) );
308 + $taxonomies = get_object_taxonomies( $post->post_type );
291 309
292 310 if ( count( $post_meta ) !== 0 ) {
293 311 foreach ( $post_meta as $meta_info ) {
294 - $meta_key = $meta_info->meta_key;
312 + $meta_key = $meta_info->meta_key;
295 313 $meta_value = addslashes( $meta_info->meta_value );
296 314 $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 315 }
298 316 }
@@ -298,9 +316,9 @@
298 316 }
299 317
300 318 foreach ( $taxonomies as $taxonomy ) {
301 319 $post_terms = wp_get_object_terms( $post_id, $taxonomy );
302 - for ( $i = 0; $i < count( $post_terms ); $i ++ ) {
320 + for ( $i = 0; $i < count( $post_terms ); $i++ ) {
303 321 wp_set_object_terms( $new_post_id, $post_terms[ $i ]->slug, $taxonomy, true );
304 322 }
305 323 }
306 324
@@ -310,9 +328,9 @@
310 328 /**
311 329 * Updates a single post based on the specified action.
312 330 */
313 331 public function update_post( $request ) {
314 - $id = $request->get_param( 'id' );
332 + $id = $request->get_param( 'id' );
315 333 $action = $request->get_param( 'action' );
316 334
317 335 if ( ! current_user_can( 'edit_post', $id ) ) {
318 336 return rest_ensure_response(
@@ -343,9 +361,10 @@
343 361 unset( $data['thumbnail'] );
344 362 }
345 363 wp_update_post(
346 364 array_merge(
347 - $data, [
365 + $data,
366 + [
348 367 'ID' => $id,
349 368 ]
350 369 )
351 370 );