PluginProbe
Assistant – Every Day Productivity Apps / 1.4.7
Assistant – Every Day Productivity Apps v1.4.7
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/Cloud/Libraries/LibraryItemPostController.php +111 -52 0.7.0.8 → 1.4.7 View file →
@@ -31,19 +31,13 @@
31 31 */
32 32 public function register_routes() {
33 33
34 34 $this->route(
35 - '/posts/preview_library_post/(?P<item_id>\d+)',
35 + '/posts/preview_library_post/',
36 36 [
37 37 [
38 38 'methods' => \WP_REST_Server::CREATABLE,
39 39 'callback' => [ $this, 'preview_library_post' ],
40 - 'args' => [
41 - 'item_id' => [
42 - 'required' => true,
43 - 'type' => 'number',
44 - ],
45 - ],
46 40 'permission_callback' => function () {
47 41 return current_user_can( 'edit_others_posts' );
48 42 },
49 43 ],
@@ -79,13 +73,13 @@
79 73 [
80 74 'methods' => \WP_REST_Server::CREATABLE,
81 75 'callback' => [ $this, 'sync_to_library' ],
82 76 'args' => [
83 - 'id' => [
77 + 'id' => [
84 78 'required' => true,
85 79 'type' => 'number',
86 80 ],
87 - 'item_id' => [
81 + 'item_id' => [
88 82 'required' => true,
89 83 'type' => 'number',
90 84 ],
91 85 ],
@@ -96,19 +90,13 @@
96 90 ]
97 91 );
98 92
99 93 $this->route(
100 - '/posts/import_from_library/(?P<item_id>\d+)',
94 + '/posts/import_from_library/',
101 95 [
102 96 [
103 97 'methods' => \WP_REST_Server::CREATABLE,
104 98 'callback' => [ $this, 'import_from_library' ],
105 - 'args' => [
106 - 'item_id' => [
107 - 'required' => true,
108 - 'type' => 'number',
109 - ],
110 - ],
111 99 'permission_callback' => function () {
112 100 return current_user_can( 'edit_others_posts' );
113 101 },
114 102 ],
@@ -121,9 +109,9 @@
121 109 [
122 110 'methods' => \WP_REST_Server::CREATABLE,
123 111 'callback' => [ $this, 'import_post_thumb_from_library' ],
124 112 'args' => [
125 - 'post_id' => [
113 + 'post_id' => [
126 114 'required' => true,
127 115 'type' => 'number',
128 116 ],
129 117 ],
@@ -140,9 +128,9 @@
140 128 [
141 129 'methods' => \WP_REST_Server::CREATABLE,
142 130 'callback' => [ $this, 'import_post_media_from_library' ],
143 131 'args' => [
144 - 'post_id' => [
132 + 'post_id' => [
145 133 'required' => true,
146 134 'type' => 'number',
147 135 ],
148 136 ],
@@ -153,22 +141,18 @@
153 141 ]
154 142 );
155 143
156 144 $this->route(
157 - '/posts/(?P<id>\d+)/sync_from_library/(?P<item_id>\d+)',
145 + '/posts/(?P<id>\d+)/sync_from_library/',
158 146 [
159 147 [
160 148 'methods' => \WP_REST_Server::CREATABLE,
161 149 'callback' => [ $this, 'sync_from_library' ],
162 150 'args' => [
163 - 'id' => [
151 + 'id' => [
164 152 'required' => true,
165 153 'type' => 'number',
166 154 ],
167 - 'item_id' => [
168 - 'required' => true,
169 - 'type' => 'number',
170 - ],
171 155 ],
172 156 'permission_callback' => function () {
173 157 return current_user_can( 'edit_others_posts' );
174 158 },
@@ -185,16 +169,25 @@
185 169 */
186 170 public function preview_library_post( $request ) {
187 171 global $wpdb;
188 172
189 - $item_id = $request->get_param( 'item_id' );
173 + $data = $request->get_param( 'item' );
174 + $item = $data ? json_decode( $data ) : null;
190 175
176 + if ( ! $item ) {
177 + return rest_ensure_response(
178 + [
179 + 'error' => true,
180 + ]
181 + );
182 + }
183 +
191 184 $meta = $wpdb->get_row(
192 185 $wpdb->prepare(
193 186 "SELECT * FROM $wpdb->postmeta
194 187 WHERE meta_key = '_fl_asst_preview_library_item_id'
195 188 AND meta_value = %s",
196 - $item_id
189 + $item->id
197 190 )
198 191 );
199 192
200 193 if ( $meta ) {
@@ -216,10 +209,12 @@
216 209 'post_status' => 'auto-draft',
217 210 ]
218 211 );
219 212
220 - update_post_meta( $post_id, '_fl_asst_preview_library_item_id', $item_id );
213 + update_post_meta( $post_id, '_fl_asst_preview_library_item_id', $item->id );
221 214
215 + $this->replace_attachment_urls_with_cloud_urls( $item, $post_id );
216 +
222 217 return rest_ensure_response(
223 218 [
224 219 'url' => PreviewHelper::get_post_preview_url( $post_id ),
225 220 ]
@@ -233,12 +228,12 @@
233 228 * @param object $post
234 229 * @return array
235 230 */
236 231 public function get_save_data( $request, $post ) {
237 - return [
232 + $data = [
238 233 'name' => $post->post_title,
239 234 'type' => 'post',
240 - 'data' => [
235 + 'post_data' => [
241 236 'post' => [
242 237 'comment_status' => $post->comment_status,
243 238 'menu_order' => $post->menu_order,
244 239 'ping_status' => $post->ping_status,
@@ -253,13 +248,20 @@
253 248 'terms' => $this->get_post_terms( $post ),
254 249
255 250 ],
256 251 'media' => [
257 - 'thumb' => get_attached_file( get_post_thumbnail_id( $post ) ),
258 252 'attachments' => $this->get_post_image_paths( $post ),
259 253 ],
260 254 'screenshot' => $this->get_post_screenshot( $request, $post ),
261 255 ];
256 +
257 + $thumbnail = get_attached_file( get_post_thumbnail_id( $post ) );
258 +
259 + if ($thumbnail) {
260 + $data['media']['thumb'] = $thumbnail;
261 + }
262 +
263 + return $data;
262 264 }
263 265
264 266 /**
265 267 * Saves a post to a cloud library.
@@ -289,12 +291,13 @@
289 291 $id = $request->get_param( 'id' );
290 292 $item_id = $request->get_param( 'item_id' );
291 293 $post = get_post( $id );
292 294 $client = new CloudClient;
295 + $data = $this->get_save_data( $request, $post );
293 296
294 297 return $client->libraries->update_item(
295 298 $item_id,
296 - $this->get_save_data( $request, $post )
299 + $data
297 300 );
298 301 }
299 302
300 303 /**
@@ -303,12 +306,24 @@
303 306 * @param object $request
304 307 * @return array
305 308 */
306 309 public function import_from_library( $request ) {
307 - $item_id = $request->get_param( 'item_id' );
310 + $data = $request->get_param( 'item' );
311 + $item = $data ? json_decode( $data ) : null;
312 +
313 + if ( ! $item ) {
314 + return rest_ensure_response(
315 + [
316 + 'error' => true,
317 + ]
318 + );
319 + }
320 +
308 321 $client = new CloudClient;
309 - $response = $client->libraries->get_item( $item_id );
310 - $post_data = $response->data->post;
322 + $item = $client->libraries->get_item( $item->id );
323 + $post_data = $item->data->post;
324 + $meta_data = $item->data->meta;
325 + $term_data = $item->data->terms;
311 326
312 327 $new_post_id = wp_insert_post(
313 328 [
314 329 'comment_status' => $post_data->comment_status,
@@ -332,11 +347,11 @@
332 347 ]
333 348 );
334 349 }
335 350
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 );
351 + $this->import_post_meta_from_library( $new_post_id, $meta_data );
352 + $this->import_post_terms_from_library( $new_post_id, $term_data );
353 + $this->regenerate_builder_cache( $new_post_id, $item );
339 354
340 355 return rest_ensure_response(
341 356 $this->posts->transform(
342 357 get_post( $new_post_id )
@@ -351,16 +366,29 @@
351 366 * @return array
352 367 */
353 368 public function sync_from_library( $request ) {
354 369 $post_id = $request->get_param( 'id' );
355 - $item_id = $request->get_param( 'item_id' );
370 + $data = $request->get_param( 'item' );
371 + $item = $data ? json_decode( $data ) : null;
372 +
373 + if ( ! $item ) {
374 + return rest_ensure_response(
375 + [
376 + 'error' => true,
377 + ]
378 + );
379 + }
380 +
356 381 $client = new CloudClient;
357 - $response = $client->libraries->get_item( $item_id );
382 + $item = $client->libraries->get_item( $item->id );
383 + $post_data = $item->data->post;
384 + $meta_data = $item->data->meta;
385 + $term_data = $item->data->terms;
358 386
359 387 $updated = wp_update_post(
360 388 [
361 389 'ID' => $post_id,
362 - 'post_content' => $response->data->post->post_content,
390 + 'post_content' => $post_data->post_content,
363 391 ]
364 392 );
365 393
366 394 if ( is_wp_error( $updated ) ) {
@@ -370,11 +398,11 @@
370 398 ]
371 399 );
372 400 }
373 401
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 );
402 + $this->import_post_meta_from_library( $post_id, $meta_data );
403 + $this->import_post_terms_from_library( $post_id, $term_data );
404 + $this->regenerate_builder_cache( $post_id, $item );
377 405
378 406 return rest_ensure_response(
379 407 $this->posts->transform(
380 408 get_post( $post_id )
@@ -395,18 +423,24 @@
395 423 if ( ! $meta || ! is_object( $meta ) ) {
396 424 return;
397 425 }
398 426
427 + if ( isset( $meta->_fl_builder_data ) ) {
428 + $meta->_fl_builder_draft = $meta->_fl_builder_data;
429 + $meta->_fl_builder_draft_settings = $meta->_fl_builder_data_settings;
430 + }
431 +
399 432 foreach ( $meta as $meta_key => $meta_value ) {
400 433 if ( metadata_exists( 'post', $post_id, $meta_key ) ) {
401 434 delete_metadata( 'post', $post_id, $meta_key );
402 435 }
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
436 + if ( is_array( $meta_value ) && count( $meta_value ) !== 0 ) {
437 + foreach ( $meta_value as $value ) {
438 + $value = addslashes( $value );
439 + // @codingStandardsIgnoreStart
440 + $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) values ({$post_id}, '{$meta_key}', '{$value}')" );
441 + // @codingStandardsIgnoreEnd
442 + }
409 443 }
410 444 }
411 445 }
412 446
@@ -417,8 +451,10 @@
417 451 * @param array $terms
418 452 * @return void
419 453 */
420 454 public function import_post_terms_from_library( $post_id, $terms ) {
455 + $terms = is_object( $terms ) ? [ $terms ] : $terms;
456 +
421 457 if ( ! $terms || ! is_array( $terms ) || 0 === count( $terms ) ) {
422 458 return;
423 459 }
424 460
@@ -427,9 +463,9 @@
427 463
428 464 foreach ( $terms as $term ) {
429 465 if ( ! isset( $term->name ) || ! isset( $term->taxonomy ) ) {
430 466 continue;
431 - } else if ( ! taxonomy_exists( $term->taxonomy ) ) {
467 + } elseif ( ! taxonomy_exists( $term->taxonomy ) ) {
432 468 continue;
433 469 } elseif ( ! isset( $taxonomy_terms ) ) {
434 470 $taxonomy_terms[ $term->taxonomy ] = [];
435 471 }
@@ -450,9 +486,9 @@
450 486 );
451 487 $term_id = $is_hierarchical ? $new_term['term_taxonomy_id'] : $term->name;
452 488 }
453 489
454 - $taxonomy_terms[ $term->taxonomy ] = $term_id;
490 + $taxonomy_terms[ $term->taxonomy ][] = $term_id;
455 491 }
456 492
457 493 foreach ( $taxonomy_terms as $taxonomy => $terms ) {
458 494 wp_delete_object_term_relationships( $post_id, $taxonomy );
@@ -537,8 +573,28 @@
537 573 return [ 'success' => true ];
538 574 }
539 575
540 576 /**
577 + * Replace attachment urls with urls from the cloud.
578 + *
579 + * @param object $item
580 + * @param int $post_id
581 + * @return void
582 + */
583 + public function replace_attachment_urls_with_cloud_urls( $item, $post_id ) {
584 + if ( isset( $item->media->attachments ) ) {
585 + $imported = [];
586 +
587 + foreach ( $item->media->attachments as $attachment ) {
588 + $imported[ $attachment->file_name ] = $attachment;
589 + }
590 +
591 + $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
592 + $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
593 + }
594 + }
595 +
596 + /**
541 597 * Replaces the imported attachment urls in post content.
542 598 *
543 599 * @param int $post_id
544 600 * @param array $imported
@@ -606,11 +662,14 @@
606 662 *
607 663 * @param object $item
608 664 * @return void
609 665 */
610 - public function regenerate_builder_cache( $item ) {
666 + public function regenerate_builder_cache( $post_id, $item ) {
611 667 $meta = (array) $item->data->meta;
612 668
669 + if ( isset( $meta['_fl_builder_data'] ) && class_exists( 'FLBuilderModel' ) ) {
670 + \FLBuilderModel::delete_all_asset_cache( $post_id );
671 + }
613 672 if ( isset( $meta['_elementor_data'] ) && class_exists( 'Elementor\Plugin' ) ) {
614 673 \Elementor\Plugin::$instance->files_manager->clear_cache();
615 674 }
616 675 }