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/Cloud/Libraries/LibraryItemPostController.php +141 -55 0.7.0.8 → 1.5.5.1 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.
@@ -272,12 +274,18 @@
272 274 $library_id = $request->get_param( 'library_id' );
273 275 $post = get_post( $id );
274 276 $client = new CloudClient;
275 277
276 - return $client->libraries->create_item(
278 + $response = $client->libraries->create_item(
277 279 $library_id,
278 280 $this->get_save_data( $request, $post )
279 281 );
282 +
283 + if (isset($response->success) && $response->success === false) {
284 + return new \WP_Error( 'API Error', $response->message, [ 'status' => 400 ] );
285 + }
286 +
287 + return $response;
280 288 }
281 289
282 290 /**
283 291 * Replaces a library post with a post on this site.
@@ -289,13 +297,20 @@
289 297 $id = $request->get_param( 'id' );
290 298 $item_id = $request->get_param( 'item_id' );
291 299 $post = get_post( $id );
292 300 $client = new CloudClient;
301 + $data = $this->get_save_data( $request, $post );
293 302
294 - return $client->libraries->update_item(
303 + $response = $client->libraries->update_item(
295 304 $item_id,
296 - $this->get_save_data( $request, $post )
305 + $data
297 306 );
307 +
308 + if (isset($response->success) && $response->success === false) {
309 + return new \WP_Error( 'API Error', $response->message, [ 'status' => 400 ] );
310 + }
311 +
312 + return $response;
298 313 }
299 314
300 315 /**
301 316 * Imports a post from a library to the site.
@@ -303,13 +318,38 @@
303 318 * @param object $request
304 319 * @return array
305 320 */
306 321 public function import_from_library( $request ) {
307 - $item_id = $request->get_param( 'item_id' );
322 + $data = $request->get_param( 'item' );
323 + $item = $data ? json_decode( $data ) : null;
324 +
325 + if ( ! $item ) {
326 + return rest_ensure_response(
327 + [
328 + 'error' => true,
329 + ]
330 + );
331 + }
332 +
308 333 $client = new CloudClient;
309 - $response = $client->libraries->get_item( $item_id );
310 - $post_data = $response->data->post;
334 + $item = $client->libraries->get_item( $item->id );
311 335
336 + $registered_post_types = get_post_types();
337 +
338 + if ( ! in_array( $item->data->post->post_type, $registered_post_types ) ) {
339 + return rest_ensure_response(
340 + [
341 + 'error' => true,
342 + 'error_code' => 'post_type_not_registered',
343 + 'post_type' => $item->data->post->post_type,
344 + ]
345 + );
346 + }
347 +
348 + $post_data = $item->data->post;
349 + $meta_data = $item->data->meta;
350 + $term_data = $item->data->terms;
351 +
312 352 $new_post_id = wp_insert_post(
313 353 [
314 354 'comment_status' => $post_data->comment_status,
315 355 'menu_order' => $post_data->menu_order,
@@ -316,9 +356,9 @@
316 356 'ping_status' => $post_data->ping_status,
317 357 'post_author' => wp_get_current_user()->ID,
318 358 'post_content' => $post_data->post_content ? $post_data->post_content : '',
319 359 'post_excerpt' => $post_data->post_excerpt ? $post_data->post_excerpt : '',
320 - 'post_mime_type' => $post_data->post_mime_type,
360 + 'post_mime_type' => $post_data->post_mime_type ? $post_data->post_mime_type : '',
321 361 'post_name' => $post_data->post_name,
322 362 'post_status' => 'publish',
323 363 'post_title' => $post_data->post_title,
324 364 'post_type' => $post_data->post_type,
@@ -332,11 +372,11 @@
332 372 ]
333 373 );
334 374 }
335 375
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 );
376 + $this->import_post_meta_from_library( $new_post_id, $meta_data );
377 + $this->import_post_terms_from_library( $new_post_id, $term_data );
378 + $this->regenerate_builder_cache( $new_post_id, $item );
339 379
340 380 return rest_ensure_response(
341 381 $this->posts->transform(
342 382 get_post( $new_post_id )
@@ -351,16 +391,29 @@
351 391 * @return array
352 392 */
353 393 public function sync_from_library( $request ) {
354 394 $post_id = $request->get_param( 'id' );
355 - $item_id = $request->get_param( 'item_id' );
395 + $data = $request->get_param( 'item' );
396 + $item = $data ? json_decode( $data ) : null;
397 +
398 + if ( ! $item ) {
399 + return rest_ensure_response(
400 + [
401 + 'error' => true,
402 + ]
403 + );
404 + }
405 +
356 406 $client = new CloudClient;
357 - $response = $client->libraries->get_item( $item_id );
407 + $item = $client->libraries->get_item( $item->id );
408 + $post_data = $item->data->post;
409 + $meta_data = $item->data->meta;
410 + $term_data = $item->data->terms;
358 411
359 412 $updated = wp_update_post(
360 413 [
361 414 'ID' => $post_id,
362 - 'post_content' => $response->data->post->post_content,
415 + 'post_content' => $post_data->post_content,
363 416 ]
364 417 );
365 418
366 419 if ( is_wp_error( $updated ) ) {
@@ -370,11 +423,11 @@
370 423 ]
371 424 );
372 425 }
373 426
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 );
427 + $this->import_post_meta_from_library( $post_id, $meta_data );
428 + $this->import_post_terms_from_library( $post_id, $term_data );
429 + $this->regenerate_builder_cache( $post_id, $item );
377 430
378 431 return rest_ensure_response(
379 432 $this->posts->transform(
380 433 get_post( $post_id )
@@ -395,18 +448,26 @@
395 448 if ( ! $meta || ! is_object( $meta ) ) {
396 449 return;
397 450 }
398 451
452 + if ( isset( $meta->_fl_builder_data ) ) {
453 + $meta->_fl_builder_draft = $meta->_fl_builder_data;
454 + $meta->_fl_builder_draft_settings = $meta->_fl_builder_data_settings;
455 + }
456 +
399 457 foreach ( $meta as $meta_key => $meta_value ) {
400 458 if ( metadata_exists( 'post', $post_id, $meta_key ) ) {
401 459 delete_metadata( 'post', $post_id, $meta_key );
402 460 }
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
461 + if ( is_array( $meta_value ) && count( $meta_value ) !== 0 ) {
462 + foreach ( $meta_value as $value ) {
463 + if ( $value !== null ) {
464 + $value = addslashes( $value );
465 + }
466 + // @codingStandardsIgnoreStart
467 + $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) values ({$post_id}, '{$meta_key}', '{$value}')" );
468 + // @codingStandardsIgnoreEnd
469 + }
409 470 }
410 471 }
411 472 }
412 473
@@ -417,8 +478,10 @@
417 478 * @param array $terms
418 479 * @return void
419 480 */
420 481 public function import_post_terms_from_library( $post_id, $terms ) {
482 + $terms = is_object( $terms ) ? [ $terms ] : $terms;
483 +
421 484 if ( ! $terms || ! is_array( $terms ) || 0 === count( $terms ) ) {
422 485 return;
423 486 }
424 487
@@ -427,9 +490,9 @@
427 490
428 491 foreach ( $terms as $term ) {
429 492 if ( ! isset( $term->name ) || ! isset( $term->taxonomy ) ) {
430 493 continue;
431 - } else if ( ! taxonomy_exists( $term->taxonomy ) ) {
494 + } elseif ( ! taxonomy_exists( $term->taxonomy ) ) {
432 495 continue;
433 496 } elseif ( ! isset( $taxonomy_terms ) ) {
434 497 $taxonomy_terms[ $term->taxonomy ] = [];
435 498 }
@@ -450,9 +513,9 @@
450 513 );
451 514 $term_id = $is_hierarchical ? $new_term['term_taxonomy_id'] : $term->name;
452 515 }
453 516
454 - $taxonomy_terms[ $term->taxonomy ] = $term_id;
517 + $taxonomy_terms[ $term->taxonomy ][] = $term_id;
455 518 }
456 519
457 520 foreach ( $taxonomy_terms as $taxonomy => $terms ) {
458 521 wp_delete_object_term_relationships( $post_id, $taxonomy );
@@ -537,8 +600,28 @@
537 600 return [ 'success' => true ];
538 601 }
539 602
540 603 /**
604 + * Replace attachment urls with urls from the cloud.
605 + *
606 + * @param object $item
607 + * @param int $post_id
608 + * @return void
609 + */
610 + public function replace_attachment_urls_with_cloud_urls( $item, $post_id ) {
611 + if ( isset( $item->media->attachments ) ) {
612 + $imported = [];
613 +
614 + foreach ( $item->media->attachments as $attachment ) {
615 + $imported[ $attachment->file_name ] = $attachment;
616 + }
617 +
618 + $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
619 + $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
620 + }
621 + }
622 +
623 + /**
541 624 * Replaces the imported attachment urls in post content.
542 625 *
543 626 * @param int $post_id
544 627 * @param array $imported
@@ -606,11 +689,14 @@
606 689 *
607 690 * @param object $item
608 691 * @return void
609 692 */
610 - public function regenerate_builder_cache( $item ) {
693 + public function regenerate_builder_cache( $post_id, $item ) {
611 694 $meta = (array) $item->data->meta;
612 695
696 + if ( isset( $meta['_fl_builder_data'] ) && class_exists( '\FLBuilderModel' ) ) {
697 + \FLBuilderModel::delete_all_asset_cache( $post_id );
698 + }
613 699 if ( isset( $meta['_elementor_data'] ) && class_exists( 'Elementor\Plugin' ) ) {
614 700 \Elementor\Plugin::$instance->files_manager->clear_cache();
615 701 }
616 702 }