PluginProbe
Assistant – Every Day Productivity Apps / 0.7.0.8
Assistant – Every Day Productivity Apps v0.7.0.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
assistant / backend / src / Controllers / Cloud / Libraries / LibraryItemPostController.php

LibraryItemPostController.php in Assistant – Every Day Productivity Apps 0.7.0.8, at backend/src/Controllers/Cloud/Libraries/LibraryItemPostController.php

618 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FL\Assistant\Controllers\Cloud\Libraries;
4
5 use FL\Assistant\System\Contracts\ControllerAbstract;
6 use FL\Assistant\Data\Transformers\PostTransformer;
7 use FL\Assistant\Helpers\PreviewHelper;
8 use FL\Assistant\Helpers\JsonHelper;
9 use FL\Assistant\Helpers\MediaPathHelper;
10 use FL\Assistant\Helpers\ScreenshotHelper;
11 use FL\Assistant\Services\MediaLibraryService;
12 use FL\Assistant\Clients\Cloud\CloudClient;
13
14 class LibraryItemPostController extends ControllerAbstract {
15
16 protected $posts;
17
18 /**
19 * Controller constructor.
20 *
21 * @return void
22 */
23 public function __construct( PostTransformer $posts ) {
24 $this->posts = $posts;
25 }
26
27 /**
28 * Register routes.
29 *
30 * @return void
31 */
32 public function register_routes() {
33
34 $this->route(
35 '/posts/preview_library_post/(?P<item_id>\d+)',
36 [
37 [
38 'methods' => \WP_REST_Server::CREATABLE,
39 'callback' => [ $this, 'preview_library_post' ],
40 'args' => [
41 'item_id' => [
42 'required' => true,
43 'type' => 'number',
44 ],
45 ],
46 'permission_callback' => function () {
47 return current_user_can( 'edit_others_posts' );
48 },
49 ],
50 ]
51 );
52
53 $this->route(
54 '/posts/(?P<id>\d+)/library/(?P<library_id>\d+)',
55 [
56 [
57 'methods' => \WP_REST_Server::CREATABLE,
58 'callback' => [ $this, 'save_to_library' ],
59 'args' => [
60 'id' => [
61 'required' => true,
62 'type' => 'number',
63 ],
64 'library_id' => [
65 'required' => true,
66 'type' => 'number',
67 ],
68 ],
69 'permission_callback' => function () {
70 return current_user_can( 'edit_others_posts' );
71 },
72 ],
73 ]
74 );
75
76 $this->route(
77 '/posts/(?P<id>\d+)/sync_to_library/(?P<item_id>\d+)',
78 [
79 [
80 'methods' => \WP_REST_Server::CREATABLE,
81 'callback' => [ $this, 'sync_to_library' ],
82 'args' => [
83 'id' => [
84 'required' => true,
85 'type' => 'number',
86 ],
87 'item_id' => [
88 'required' => true,
89 'type' => 'number',
90 ],
91 ],
92 'permission_callback' => function () {
93 return current_user_can( 'edit_others_posts' );
94 },
95 ],
96 ]
97 );
98
99 $this->route(
100 '/posts/import_from_library/(?P<item_id>\d+)',
101 [
102 [
103 'methods' => \WP_REST_Server::CREATABLE,
104 'callback' => [ $this, 'import_from_library' ],
105 'args' => [
106 'item_id' => [
107 'required' => true,
108 'type' => 'number',
109 ],
110 ],
111 'permission_callback' => function () {
112 return current_user_can( 'edit_others_posts' );
113 },
114 ],
115 ]
116 );
117
118 $this->route(
119 '/posts/import_post_thumb_from_library/(?P<post_id>\d+)',
120 [
121 [
122 'methods' => \WP_REST_Server::CREATABLE,
123 'callback' => [ $this, 'import_post_thumb_from_library' ],
124 'args' => [
125 'post_id' => [
126 'required' => true,
127 'type' => 'number',
128 ],
129 ],
130 'permission_callback' => function () {
131 return current_user_can( 'edit_others_posts' );
132 },
133 ],
134 ]
135 );
136
137 $this->route(
138 '/posts/import_post_media_from_library/(?P<post_id>\d+)',
139 [
140 [
141 'methods' => \WP_REST_Server::CREATABLE,
142 'callback' => [ $this, 'import_post_media_from_library' ],
143 'args' => [
144 'post_id' => [
145 'required' => true,
146 'type' => 'number',
147 ],
148 ],
149 'permission_callback' => function () {
150 return current_user_can( 'edit_others_posts' );
151 },
152 ],
153 ]
154 );
155
156 $this->route(
157 '/posts/(?P<id>\d+)/sync_from_library/(?P<item_id>\d+)',
158 [
159 [
160 'methods' => \WP_REST_Server::CREATABLE,
161 'callback' => [ $this, 'sync_from_library' ],
162 'args' => [
163 'id' => [
164 'required' => true,
165 'type' => 'number',
166 ],
167 'item_id' => [
168 'required' => true,
169 'type' => 'number',
170 ],
171 ],
172 'permission_callback' => function () {
173 return current_user_can( 'edit_others_posts' );
174 },
175 ],
176 ]
177 );
178 }
179
180 /**
181 * Handles previewing a post from a cloud library.
182 *
183 * @param object $request
184 * @return array
185 */
186 public function preview_library_post( $request ) {
187 global $wpdb;
188
189 $item_id = $request->get_param( 'item_id' );
190
191 $meta = $wpdb->get_row(
192 $wpdb->prepare(
193 "SELECT * FROM $wpdb->postmeta
194 WHERE meta_key = '_fl_asst_preview_library_item_id'
195 AND meta_value = %s",
196 $item_id
197 )
198 );
199
200 if ( $meta ) {
201 $request->set_param( 'id', $meta->post_id );
202 $response = $this->sync_from_library( $request );
203 } else {
204 $response = $this->import_from_library( $request );
205 }
206
207 if ( isset( $response->data['error'] ) ) {
208 return $response;
209 }
210
211 $post_id = $response->data['id'];
212
213 wp_update_post(
214 [
215 'ID' => $post_id,
216 'post_status' => 'auto-draft',
217 ]
218 );
219
220 update_post_meta( $post_id, '_fl_asst_preview_library_item_id', $item_id );
221
222 return rest_ensure_response(
223 [
224 'url' => PreviewHelper::get_post_preview_url( $post_id ),
225 ]
226 );
227 }
228
229 /**
230 * Gets the data for saving to a library or updating a library item.
231 *
232 * @param object $request
233 * @param object $post
234 * @return array
235 */
236 public function get_save_data( $request, $post ) {
237 return [
238 'name' => $post->post_title,
239 'type' => 'post',
240 'data' => [
241 'post' => [
242 'comment_status' => $post->comment_status,
243 'menu_order' => $post->menu_order,
244 'ping_status' => $post->ping_status,
245 'post_content' => $post->post_content,
246 'post_excerpt' => $post->post_excerpt,
247 'post_mime_type' => $post->post_mime_type,
248 'post_name' => $post->post_name,
249 'post_title' => $post->post_title,
250 'post_type' => $post->post_type,
251 ],
252 'meta' => get_post_meta( $post->ID ),
253 'terms' => $this->get_post_terms( $post ),
254
255 ],
256 'media' => [
257 'thumb' => get_attached_file( get_post_thumbnail_id( $post ) ),
258 'attachments' => $this->get_post_image_paths( $post ),
259 ],
260 'screenshot' => $this->get_post_screenshot( $request, $post ),
261 ];
262 }
263
264 /**
265 * Saves a post to a cloud library.
266 *
267 * @param object $request
268 * @return array
269 */
270 public function save_to_library( $request ) {
271 $id = $request->get_param( 'id' );
272 $library_id = $request->get_param( 'library_id' );
273 $post = get_post( $id );
274 $client = new CloudClient;
275
276 return $client->libraries->create_item(
277 $library_id,
278 $this->get_save_data( $request, $post )
279 );
280 }
281
282 /**
283 * Replaces a library post with a post on this site.
284 *
285 * @param object $request
286 * @return array
287 */
288 public function sync_to_library( $request ) {
289 $id = $request->get_param( 'id' );
290 $item_id = $request->get_param( 'item_id' );
291 $post = get_post( $id );
292 $client = new CloudClient;
293
294 return $client->libraries->update_item(
295 $item_id,
296 $this->get_save_data( $request, $post )
297 );
298 }
299
300 /**
301 * Imports a post from a library to the site.
302 *
303 * @param object $request
304 * @return array
305 */
306 public function import_from_library( $request ) {
307 $item_id = $request->get_param( 'item_id' );
308 $client = new CloudClient;
309 $response = $client->libraries->get_item( $item_id );
310 $post_data = $response->data->post;
311
312 $new_post_id = wp_insert_post(
313 [
314 'comment_status' => $post_data->comment_status,
315 'menu_order' => $post_data->menu_order,
316 'ping_status' => $post_data->ping_status,
317 'post_author' => wp_get_current_user()->ID,
318 'post_content' => $post_data->post_content ? $post_data->post_content : '',
319 'post_excerpt' => $post_data->post_excerpt ? $post_data->post_excerpt : '',
320 'post_mime_type' => $post_data->post_mime_type,
321 'post_name' => $post_data->post_name,
322 'post_status' => 'publish',
323 'post_title' => $post_data->post_title,
324 'post_type' => $post_data->post_type,
325 ]
326 );
327
328 if ( is_wp_error( $new_post_id ) ) {
329 return rest_ensure_response(
330 [
331 'error' => true,
332 ]
333 );
334 }
335
336 $this->import_post_meta_from_library( $new_post_id, $response->data->meta );
337 $this->import_post_terms_from_library( $new_post_id, $response->data->terms );
338 $this->regenerate_builder_cache( $response );
339
340 return rest_ensure_response(
341 $this->posts->transform(
342 get_post( $new_post_id )
343 )
344 );
345 }
346
347 /**
348 * Replaces a post on this site with a post from a library.
349 *
350 * @param object $request
351 * @return array
352 */
353 public function sync_from_library( $request ) {
354 $post_id = $request->get_param( 'id' );
355 $item_id = $request->get_param( 'item_id' );
356 $client = new CloudClient;
357 $response = $client->libraries->get_item( $item_id );
358
359 $updated = wp_update_post(
360 [
361 'ID' => $post_id,
362 'post_content' => $response->data->post->post_content,
363 ]
364 );
365
366 if ( is_wp_error( $updated ) ) {
367 return rest_ensure_response(
368 [
369 'error' => true,
370 ]
371 );
372 }
373
374 $this->import_post_meta_from_library( $post_id, $response->data->meta );
375 $this->import_post_terms_from_library( $post_id, $response->data->terms );
376 $this->regenerate_builder_cache( $response );
377
378 return rest_ensure_response(
379 $this->posts->transform(
380 get_post( $post_id )
381 )
382 );
383 }
384
385 /**
386 * Imports meta for a library post to the site.
387 *
388 * @param int $post_id
389 * @param object $meta
390 * @return void
391 */
392 public function import_post_meta_from_library( $post_id, $meta ) {
393 global $wpdb;
394
395 if ( ! $meta || ! is_object( $meta ) ) {
396 return;
397 }
398
399 foreach ( $meta as $meta_key => $meta_value ) {
400 if ( metadata_exists( 'post', $post_id, $meta_key ) ) {
401 delete_metadata( 'post', $post_id, $meta_key );
402 }
403
404 foreach ( $meta_value as $value ) {
405 $value = addslashes( $value );
406 // @codingStandardsIgnoreStart
407 $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) values ({$post_id}, '{$meta_key}', '{$value}')" );
408 // @codingStandardsIgnoreEnd
409 }
410 }
411 }
412
413 /**
414 * Imports terms for a library post to the site.
415 *
416 * @param int $post_id
417 * @param array $terms
418 * @return void
419 */
420 public function import_post_terms_from_library( $post_id, $terms ) {
421 if ( ! $terms || ! is_array( $terms ) || 0 === count( $terms ) ) {
422 return;
423 }
424
425 $taxonomies = get_taxonomies( '', 'objects' );
426 $taxonomy_terms = [];
427
428 foreach ( $terms as $term ) {
429 if ( ! isset( $term->name ) || ! isset( $term->taxonomy ) ) {
430 continue;
431 } else if ( ! taxonomy_exists( $term->taxonomy ) ) {
432 continue;
433 } elseif ( ! isset( $taxonomy_terms ) ) {
434 $taxonomy_terms[ $term->taxonomy ] = [];
435 }
436
437 $existing_term = term_exists( $term->slug, $term->taxonomy );
438 $is_hierarchical = ! ! $taxonomies[ $term->taxonomy ]->hierarchical;
439
440 if ( is_array( $existing_term ) ) {
441 $term_id = $is_hierarchical ? $existing_term['term_taxonomy_id'] : $term->name;
442 } else {
443 $new_term = wp_insert_term(
444 $term->name,
445 $term->taxonomy,
446 [
447 'description' => $term->description,
448 'slug' => $term->slug,
449 ]
450 );
451 $term_id = $is_hierarchical ? $new_term['term_taxonomy_id'] : $term->name;
452 }
453
454 $taxonomy_terms[ $term->taxonomy ] = $term_id;
455 }
456
457 foreach ( $taxonomy_terms as $taxonomy => $terms ) {
458 wp_delete_object_term_relationships( $post_id, $taxonomy );
459 wp_set_post_terms( $post_id, $terms, $taxonomy, true );
460 }
461 }
462
463 /**
464 * Returns an array of all terms for a post.
465 *
466 * @param object $post
467 * @return array
468 */
469 public function get_post_terms( $post ) {
470 $taxonomies = get_taxonomies( '', 'names' );
471 $object_terms = wp_get_object_terms( $post->ID, $taxonomies );
472 $terms = [];
473
474 foreach ( $object_terms as $term ) {
475 $terms[] = [
476 'description' => $term->description,
477 'name' => $term->name,
478 'slug' => $term->slug,
479 'taxonomy' => $term->taxonomy,
480 ];
481 }
482
483 return $terms;
484 }
485
486 /**
487 * Returns the screenshot data for a post.
488 *
489 * @param object $request
490 * @param object $post
491 * @return array
492 */
493 public function get_post_screenshot( $request, $post ) {
494 $url = PreviewHelper::get_post_preview_url( $post );
495 $url = add_query_arg( 'fl_asst_screenshot', '1', $url );
496 return ScreenshotHelper::get_for_rest_request( $request, $url );
497 }
498
499 /**
500 * Imports the featured image for a post.
501 *
502 * @param object $request
503 * @return array
504 */
505 public function import_post_thumb_from_library( $request ) {
506 $post_id = $request->get_param( 'post_id' );
507 $thumb = $request->get_param( 'thumb' );
508 $service = new MediaLibraryService();
509
510 if ( ! preg_match( '/screenshot\.(png|jpg|gif)/', $thumb['file_name'] ) ) {
511 $response = $service->import_cloud_media( $thumb, $post_id );
512 set_post_thumbnail( $post_id, $response['id'] );
513 }
514
515 return [ 'success' => true ];
516 }
517
518 /**
519 * Imports a media item for a post.
520 *
521 * @param object $request
522 * @return array
523 */
524 public function import_post_media_from_library( $request ) {
525 $post_id = $request->get_param( 'post_id' );
526 $media = $request->get_param( 'media' );
527 $service = new MediaLibraryService();
528
529 $imported = [];
530 $response = $service->import_cloud_media( $media, $post_id );
531 $imported[ $media['file_name'] ] = wp_get_attachment_metadata( $response['id'] );
532 $imported[ $media['file_name'] ]['id'] = $response['id'];
533
534 $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
535 $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
536
537 return [ 'success' => true ];
538 }
539
540 /**
541 * Replaces the imported attachment urls in post content.
542 *
543 * @param int $post_id
544 * @param array $imported
545 * @return void
546 */
547 public function replace_imported_attachment_urls_in_content( $post_id, $imported ) {
548 $content = get_post_field( 'post_content', $post_id );
549 $content = MediaPathHelper::replace_imported_attachment_urls_in_string( $content, $imported );
550
551 wp_update_post(
552 [
553 'ID' => $post_id,
554 'post_content' => $content,
555 ]
556 );
557 }
558
559 /**
560 * Replaces the imported attachment urls in post meta.
561 *
562 * @param int $post_id
563 * @param array $imported
564 * @return void
565 */
566 public function replace_imported_attachment_urls_in_meta( $post_id, $imported ) {
567 $meta = get_post_meta( $post_id );
568
569 foreach ( $meta as $key => $val ) {
570 $val = maybe_unserialize( $val[0] );
571
572 if ( is_object( $val ) || is_array( $val ) ) {
573 $val = MediaPathHelper::replace_imported_attachment_urls_in_data( $val, $imported );
574 } elseif ( JsonHelper::is_string_json( $val ) ) {
575 $val = json_decode( $val );
576 $val = MediaPathHelper::replace_imported_attachment_urls_in_data( $val, $imported );
577 $val = wp_slash( json_encode( $val ) );
578 } else {
579 $val = MediaPathHelper::replace_imported_attachment_urls_in_string( $val, $imported );
580 }
581
582 update_post_meta( $post_id, $key, $val );
583 }
584 }
585
586 /**
587 * Get the server paths to all images associated with a post.
588 *
589 * @param object $post
590 * @return array
591 */
592 public function get_post_image_paths( $post ) {
593 $content_paths = MediaPathHelper::get_image_paths_from_string( $post->post_content );
594 $meta = get_post_meta( $post->ID );
595 $meta_paths = [];
596
597 if ( $meta && count( $meta ) > 0 ) {
598 $meta_paths = MediaPathHelper::get_image_paths_from_data( $meta );
599 }
600
601 return array_unique( array_merge( $content_paths, $meta_paths ) );
602 }
603
604 /**
605 * Regenerate the asset cache for posts created with a builder.
606 *
607 * @param object $item
608 * @return void
609 */
610 public function regenerate_builder_cache( $item ) {
611 $meta = (array) $item->data->meta;
612
613 if ( isset( $meta['_elementor_data'] ) && class_exists( 'Elementor\Plugin' ) ) {
614 \Elementor\Plugin::$instance->files_manager->clear_cache();
615 }
616 }
617 }
618