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 +287 -358 0.6.0 → 1.5.5.1 View file →
@@ -3,10 +3,12 @@
3 3 namespace FL\Assistant\Controllers\Cloud\Libraries;
4 4
5 5 use FL\Assistant\System\Contracts\ControllerAbstract;
6 6 use FL\Assistant\Data\Transformers\PostTransformer;
7 -use FL\Assistant\Helpers\PostHelper;
7 +use FL\Assistant\Helpers\PreviewHelper;
8 8 use FL\Assistant\Helpers\JsonHelper;
9 +use FL\Assistant\Helpers\MediaPathHelper;
10 +use FL\Assistant\Helpers\ScreenshotHelper;
9 11 use FL\Assistant\Services\MediaLibraryService;
10 12 use FL\Assistant\Clients\Cloud\CloudClient;
11 13
12 14 class LibraryItemPostController extends ControllerAbstract {
@@ -29,18 +31,35 @@
29 31 */
30 32 public function register_routes() {
31 33
32 34 $this->route(
33 - '/posts/preview_library_post/(?P<item_id>\d+)',
35 + '/posts/preview_library_post/',
34 36 [
35 37 [
36 38 'methods' => \WP_REST_Server::CREATABLE,
37 39 'callback' => [ $this, 'preview_library_post' ],
40 + 'permission_callback' => function () {
41 + return current_user_can( 'edit_others_posts' );
42 + },
43 + ],
44 + ]
45 + );
46 +
47 + $this->route(
48 + '/posts/(?P<id>\d+)/library/(?P<library_id>\d+)',
49 + [
50 + [
51 + 'methods' => \WP_REST_Server::CREATABLE,
52 + 'callback' => [ $this, 'save_to_library' ],
38 53 'args' => [
39 - 'item_id' => [
54 + 'id' => [
40 55 'required' => true,
41 56 'type' => 'number',
42 57 ],
58 + 'library_id' => [
59 + 'required' => true,
60 + 'type' => 'number',
61 + ],
43 62 ],
44 63 'permission_callback' => function () {
45 64 return current_user_can( 'edit_others_posts' );
46 65 },
@@ -48,19 +67,19 @@
48 67 ]
49 68 );
50 69
51 70 $this->route(
52 - '/posts/(?P<id>\d+)/library/(?P<library_id>\d+)',
71 + '/posts/(?P<id>\d+)/sync_to_library/(?P<item_id>\d+)',
53 72 [
54 73 [
55 74 'methods' => \WP_REST_Server::CREATABLE,
56 - 'callback' => [ $this, 'save_to_library' ],
75 + 'callback' => [ $this, 'sync_to_library' ],
57 76 'args' => [
58 - 'id' => [
77 + 'id' => [
59 78 'required' => true,
60 79 'type' => 'number',
61 80 ],
62 - 'library_id' => [
81 + 'item_id' => [
63 82 'required' => true,
64 83 'type' => 'number',
65 84 ],
66 85 ],
@@ -71,22 +90,31 @@
71 90 ]
72 91 );
73 92
74 93 $this->route(
75 - '/posts/import_from_library/(?P<item_id>\d+)',
94 + '/posts/import_from_library/',
76 95 [
77 96 [
78 97 'methods' => \WP_REST_Server::CREATABLE,
79 98 'callback' => [ $this, 'import_from_library' ],
99 + 'permission_callback' => function () {
100 + return current_user_can( 'edit_others_posts' );
101 + },
102 + ],
103 + ]
104 + );
105 +
106 + $this->route(
107 + '/posts/import_post_thumb_from_library/(?P<post_id>\d+)',
108 + [
109 + [
110 + 'methods' => \WP_REST_Server::CREATABLE,
111 + 'callback' => [ $this, 'import_post_thumb_from_library' ],
80 112 'args' => [
81 - 'item_id' => [
113 + 'post_id' => [
82 114 'required' => true,
83 115 'type' => 'number',
84 116 ],
85 - 'import_media' => [
86 - 'type' => 'number',
87 - 'default' => 1,
88 - ],
89 117 ],
90 118 'permission_callback' => function () {
91 119 return current_user_can( 'edit_others_posts' );
92 120 },
@@ -94,26 +122,37 @@
94 122 ]
95 123 );
96 124
97 125 $this->route(
98 - '/posts/(?P<id>\d+)/sync_from_library/(?P<item_id>\d+)',
126 + '/posts/import_post_media_from_library/(?P<post_id>\d+)',
99 127 [
100 128 [
101 129 'methods' => \WP_REST_Server::CREATABLE,
102 - 'callback' => [ $this, 'sync_from_library' ],
130 + 'callback' => [ $this, 'import_post_media_from_library' ],
103 131 'args' => [
104 - 'id' => [
132 + 'post_id' => [
105 133 'required' => true,
106 134 'type' => 'number',
107 135 ],
108 - 'item_id' => [
136 + ],
137 + 'permission_callback' => function () {
138 + return current_user_can( 'edit_others_posts' );
139 + },
140 + ],
141 + ]
142 + );
143 +
144 + $this->route(
145 + '/posts/(?P<id>\d+)/sync_from_library/',
146 + [
147 + [
148 + 'methods' => \WP_REST_Server::CREATABLE,
149 + 'callback' => [ $this, 'sync_from_library' ],
150 + 'args' => [
151 + 'id' => [
109 152 'required' => true,
110 153 'type' => 'number',
111 154 ],
112 - 'import_media' => [
113 - 'type' => 'number',
114 - 'default' => 1,
115 - ],
116 155 ],
117 156 'permission_callback' => function () {
118 157 return current_user_can( 'edit_others_posts' );
119 158 },
@@ -130,16 +169,25 @@
130 169 */
131 170 public function preview_library_post( $request ) {
132 171 global $wpdb;
133 172
134 - $item_id = $request->get_param( 'item_id' );
173 + $data = $request->get_param( 'item' );
174 + $item = $data ? json_decode( $data ) : null;
135 175
176 + if ( ! $item ) {
177 + return rest_ensure_response(
178 + [
179 + 'error' => true,
180 + ]
181 + );
182 + }
183 +
136 184 $meta = $wpdb->get_row(
137 185 $wpdb->prepare(
138 186 "SELECT * FROM $wpdb->postmeta
139 187 WHERE meta_key = '_fl_asst_preview_library_item_id'
140 188 AND meta_value = %s",
141 - $item_id
189 + $item->id
142 190 )
143 191 );
144 192
145 193 if ( $meta ) {
@@ -161,18 +209,62 @@
161 209 'post_status' => 'auto-draft',
162 210 ]
163 211 );
164 212
165 - 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 );
166 214
215 + $this->replace_attachment_urls_with_cloud_urls( $item, $post_id );
216 +
167 217 return rest_ensure_response(
168 218 [
169 - 'url' => PostHelper::get_preview_url( $post_id ),
219 + 'url' => PreviewHelper::get_post_preview_url( $post_id ),
170 220 ]
171 221 );
172 222 }
173 223
174 224 /**
225 + * Gets the data for saving to a library or updating a library item.
226 + *
227 + * @param object $request
228 + * @param object $post
229 + * @return array
230 + */
231 + public function get_save_data( $request, $post ) {
232 + $data = [
233 + 'name' => $post->post_title,
234 + 'type' => 'post',
235 + 'post_data' => [
236 + 'post' => [
237 + 'comment_status' => $post->comment_status,
238 + 'menu_order' => $post->menu_order,
239 + 'ping_status' => $post->ping_status,
240 + 'post_content' => $post->post_content,
241 + 'post_excerpt' => $post->post_excerpt,
242 + 'post_mime_type' => $post->post_mime_type,
243 + 'post_name' => $post->post_name,
244 + 'post_title' => $post->post_title,
245 + 'post_type' => $post->post_type,
246 + ],
247 + 'meta' => get_post_meta( $post->ID ),
248 + 'terms' => $this->get_post_terms( $post ),
249 +
250 + ],
251 + 'media' => [
252 + 'attachments' => $this->get_post_image_paths( $post ),
253 + ],
254 + 'screenshot' => $this->get_post_screenshot( $request, $post ),
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;
264 + }
265 +
266 + /**
175 267 * Saves a post to a cloud library.
176 268 *
177 269 * @param object $request
178 270 * @return array
@@ -182,36 +274,43 @@
182 274 $library_id = $request->get_param( 'library_id' );
183 275 $post = get_post( $id );
184 276 $client = new CloudClient;
185 277
186 - return $client->libraries->create_item(
278 + $response = $client->libraries->create_item(
187 279 $library_id,
188 - [
189 - 'name' => $post->post_title,
190 - 'type' => 'post',
191 - 'data' => [
192 - 'post' => [
193 - 'comment_status' => $post->comment_status,
194 - 'menu_order' => $post->menu_order,
195 - 'ping_status' => $post->ping_status,
196 - 'post_content' => $post->post_content,
197 - 'post_excerpt' => $post->post_excerpt,
198 - 'post_mime_type' => $post->post_mime_type,
199 - 'post_name' => $post->post_name,
200 - 'post_title' => $post->post_title,
201 - 'post_type' => $post->post_type,
202 - ],
203 - 'meta' => get_post_meta( $id ),
204 - 'terms' => $this->get_post_terms( $post ),
280 + $this->get_save_data( $request, $post )
281 + );
205 282
206 - ],
207 - 'media' => [
208 - 'thumb' => get_attached_file( get_post_thumbnail_id( $post ) ),
209 - 'attachments' => $this->get_post_image_paths( $post ),
210 - ],
211 - 'screenshot' => $this->get_post_screenshot( $request, $post ),
212 - ]
283 + if (isset($response->success) && $response->success === false) {
284 + return new \WP_Error( 'API Error', $response->message, [ 'status' => 400 ] );
285 + }
286 +
287 + return $response;
288 + }
289 +
290 + /**
291 + * Replaces a library post with a post on this site.
292 + *
293 + * @param object $request
294 + * @return array
295 + */
296 + public function sync_to_library( $request ) {
297 + $id = $request->get_param( 'id' );
298 + $item_id = $request->get_param( 'item_id' );
299 + $post = get_post( $id );
300 + $client = new CloudClient;
301 + $data = $this->get_save_data( $request, $post );
302 +
303 + $response = $client->libraries->update_item(
304 + $item_id,
305 + $data
213 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;
214 313 }
215 314
216 315 /**
217 316 * Imports a post from a library to the site.
@@ -219,14 +318,38 @@
219 318 * @param object $request
220 319 * @return array
221 320 */
222 321 public function import_from_library( $request ) {
223 - $item_id = $request->get_param( 'item_id' );
224 - $import_media = ! ! $request->get_param( 'import_media' );
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 +
225 333 $client = new CloudClient;
226 - $response = $client->libraries->get_item( $item_id );
227 - $post_data = $response->data->post;
334 + $item = $client->libraries->get_item( $item->id );
228 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 +
229 352 $new_post_id = wp_insert_post(
230 353 [
231 354 'comment_status' => $post_data->comment_status,
232 355 'menu_order' => $post_data->menu_order,
@@ -233,11 +356,11 @@
233 356 'ping_status' => $post_data->ping_status,
234 357 'post_author' => wp_get_current_user()->ID,
235 358 'post_content' => $post_data->post_content ? $post_data->post_content : '',
236 359 'post_excerpt' => $post_data->post_excerpt ? $post_data->post_excerpt : '',
237 - 'post_mime_type' => $post_data->post_mime_type,
360 + 'post_mime_type' => $post_data->post_mime_type ? $post_data->post_mime_type : '',
238 361 'post_name' => $post_data->post_name,
239 - 'post_status' => 'draft',
362 + 'post_status' => 'publish',
240 363 'post_title' => $post_data->post_title,
241 364 'post_type' => $post_data->post_type,
242 365 ]
243 366 );
@@ -249,12 +372,11 @@
249 372 ]
250 373 );
251 374 }
252 375
253 - $this->import_post_meta_from_library( $new_post_id, $response->data->meta );
254 - $this->import_post_terms_from_library( $new_post_id, $response->data->terms );
255 - $this->import_post_media_from_library( $new_post_id, $response->media, $import_media );
256 - $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 );
257 379
258 380 return rest_ensure_response(
259 381 $this->posts->transform(
260 382 get_post( $new_post_id )
@@ -262,9 +384,9 @@
262 384 );
263 385 }
264 386
265 387 /**
266 - * Syncs a library post with a post on the site.
388 + * Replaces a post on this site with a post from a library.
267 389 *
268 390 * @param object $request
269 391 * @return array
270 392 */
@@ -269,17 +391,29 @@
269 391 * @return array
270 392 */
271 393 public function sync_from_library( $request ) {
272 394 $post_id = $request->get_param( 'id' );
273 - $item_id = $request->get_param( 'item_id' );
274 - $import_media = ! ! $request->get_param( 'import_media' );
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 +
275 406 $client = new CloudClient;
276 - $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;
277 411
278 412 $updated = wp_update_post(
279 413 [
280 414 'ID' => $post_id,
281 - 'post_content' => $response->data->post->post_content,
415 + 'post_content' => $post_data->post_content,
282 416 ]
283 417 );
284 418
285 419 if ( is_wp_error( $updated ) ) {
@@ -289,12 +423,11 @@
289 423 ]
290 424 );
291 425 }
292 426
293 - $this->import_post_meta_from_library( $post_id, $response->data->meta );
294 - $this->import_post_terms_from_library( $post_id, $response->data->terms );
295 - $this->import_post_media_from_library( $post_id, $response->media, $import_media );
296 - $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 );
297 430
298 431 return rest_ensure_response(
299 432 $this->posts->transform(
300 433 get_post( $post_id )
@@ -315,18 +448,26 @@
315 448 if ( ! $meta || ! is_object( $meta ) ) {
316 449 return;
317 450 }
318 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 +
319 457 foreach ( $meta as $meta_key => $meta_value ) {
320 458 if ( metadata_exists( 'post', $post_id, $meta_key ) ) {
321 459 delete_metadata( 'post', $post_id, $meta_key );
322 460 }
323 -
324 - foreach ( $meta_value as $value ) {
325 - $value = addslashes( $value );
326 - // @codingStandardsIgnoreStart
327 - $wpdb->query( "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) values ({$post_id}, '{$meta_key}', '{$value}')" );
328 - // @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 + }
329 470 }
330 471 }
331 472 }
332 473
@@ -337,17 +478,24 @@
337 478 * @param array $terms
338 479 * @return void
339 480 */
340 481 public function import_post_terms_from_library( $post_id, $terms ) {
482 + $terms = is_object( $terms ) ? [ $terms ] : $terms;
483 +
341 484 if ( ! $terms || ! is_array( $terms ) || 0 === count( $terms ) ) {
342 485 return;
343 486 }
344 487
345 488 $taxonomies = get_taxonomies( '', 'objects' );
489 + $taxonomy_terms = [];
346 490
347 491 foreach ( $terms as $term ) {
348 - if ( ! taxonomy_exists( $term->taxonomy ) ) {
492 + if ( ! isset( $term->name ) || ! isset( $term->taxonomy ) ) {
349 493 continue;
494 + } elseif ( ! taxonomy_exists( $term->taxonomy ) ) {
495 + continue;
496 + } elseif ( ! isset( $taxonomy_terms ) ) {
497 + $taxonomy_terms[ $term->taxonomy ] = [];
350 498 }
351 499
352 500 $existing_term = term_exists( $term->slug, $term->taxonomy );
353 501 $is_hierarchical = ! ! $taxonomies[ $term->taxonomy ]->hierarchical;
@@ -353,9 +501,8 @@
353 501 $is_hierarchical = ! ! $taxonomies[ $term->taxonomy ]->hierarchical;
354 502
355 503 if ( is_array( $existing_term ) ) {
356 504 $term_id = $is_hierarchical ? $existing_term['term_taxonomy_id'] : $term->name;
357 - wp_set_post_terms( $post_id, [ $term_id ], $term->taxonomy, true );
358 505 } else {
359 506 $new_term = wp_insert_term(
360 507 $term->name,
361 508 $term->taxonomy,
@@ -364,11 +511,17 @@
364 511 'slug' => $term->slug,
365 512 ]
366 513 );
367 514 $term_id = $is_hierarchical ? $new_term['term_taxonomy_id'] : $term->name;
368 - wp_set_post_terms( $post_id, [ $new_term['term_taxonomy_id'] ], $term->taxonomy, true );
369 515 }
516 +
517 + $taxonomy_terms[ $term->taxonomy ][] = $term_id;
370 518 }
519 +
520 + foreach ( $taxonomy_terms as $taxonomy => $terms ) {
521 + wp_delete_object_term_relationships( $post_id, $taxonomy );
522 + wp_set_post_terms( $post_id, $terms, $taxonomy, true );
523 + }
371 524 }
372 525
373 526 /**
374 527 * Returns an array of all terms for a post.
@@ -400,59 +553,67 @@
400 553 * @param object $post
401 554 * @return array
402 555 */
403 556 public function get_post_screenshot( $request, $post ) {
404 - $screenshot = $request->get_param( 'screenshot' );
557 + $url = PreviewHelper::get_post_preview_url( $post );
558 + $url = add_query_arg( 'fl_asst_screenshot', '1', $url );
559 + return ScreenshotHelper::get_for_rest_request( $request, $url );
560 + }
405 561
406 - if ( $screenshot ) {
407 - return [
408 - 'type' => 'base64',
409 - 'data' => $screenshot,
410 - ];
562 + /**
563 + * Imports the featured image for a post.
564 + *
565 + * @param object $request
566 + * @return array
567 + */
568 + public function import_post_thumb_from_library( $request ) {
569 + $post_id = $request->get_param( 'post_id' );
570 + $thumb = $request->get_param( 'thumb' );
571 + $service = new MediaLibraryService();
572 +
573 + if ( ! preg_match( '/screenshot\.(png|jpg|gif)/', $thumb['file_name'] ) ) {
574 + $response = $service->import_cloud_media( $thumb, $post_id );
575 + set_post_thumbnail( $post_id, $response['id'] );
411 576 }
412 577
413 - $url = PostHelper::get_preview_url( $post );
414 - $response = wp_remote_get(
415 - $url, [
416 - 'cookies' => $_COOKIE,
417 - ]
418 - );
578 + return [ 'success' => true ];
579 + }
419 580
420 - return [
421 - 'type' => 'html',
422 - 'html' => wp_remote_retrieve_body( $response ),
423 - ];
581 + /**
582 + * Imports a media item for a post.
583 + *
584 + * @param object $request
585 + * @return array
586 + */
587 + public function import_post_media_from_library( $request ) {
588 + $post_id = $request->get_param( 'post_id' );
589 + $media = $request->get_param( 'media' );
590 + $service = new MediaLibraryService();
591 +
592 + $imported = [];
593 + $response = $service->import_cloud_media( $media, $post_id );
594 + $imported[ $media['file_name'] ] = wp_get_attachment_metadata( $response['id'] );
595 + $imported[ $media['file_name'] ]['id'] = $response['id'];
596 +
597 + $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
598 + $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
599 +
600 + return [ 'success' => true ];
424 601 }
425 602
426 603 /**
427 - * Imports the media for a post.
604 + * Replace attachment urls with urls from the cloud.
428 605 *
606 + * @param object $item
429 607 * @param int $post_id
430 - * @param object $media
431 - * @param bool $import
432 608 * @return void
433 609 */
434 - public function import_post_media_from_library( $post_id, $media, $import = true ) {
435 - $service = new MediaLibraryService();
436 -
437 - // Import post thumbnail
438 - if ( $import && isset( $media->thumb ) && 'screenshot.png' !== $media->thumb->file_name ) {
439 - $response = $service->import_cloud_media( $media->thumb, $post_id );
440 - set_post_thumbnail( $post_id, $response['id'] );
441 - }
442 -
443 - // Import post attachments
444 - if ( isset( $media->attachments ) ) {
610 + public function replace_attachment_urls_with_cloud_urls( $item, $post_id ) {
611 + if ( isset( $item->media->attachments ) ) {
445 612 $imported = [];
446 613
447 - foreach ( $media->attachments as $attachment ) {
448 - if ( $import ) {
449 - $response = $service->import_cloud_media( $attachment, $post_id );
450 - $imported[ $attachment->file_name ] = wp_get_attachment_metadata( $response['id'] );
451 - $imported[ $attachment->file_name ]['id'] = $response['id'];
452 - } else {
453 - $imported[ $attachment->file_name ] = $attachment;
454 - }
614 + foreach ( $item->media->attachments as $attachment ) {
615 + $imported[ $attachment->file_name ] = $attachment;
455 616 }
456 617
457 618 $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
458 619 $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
@@ -467,9 +628,9 @@
467 628 * @return void
468 629 */
469 630 public function replace_imported_attachment_urls_in_content( $post_id, $imported ) {
470 631 $content = get_post_field( 'post_content', $post_id );
471 - $content = $this->replace_imported_attachment_urls_in_string( $content, $imported );
632 + $content = MediaPathHelper::replace_imported_attachment_urls_in_string( $content, $imported );
472 633
473 634 wp_update_post(
474 635 [
475 636 'ID' => $post_id,
@@ -491,15 +652,15 @@
491 652 foreach ( $meta as $key => $val ) {
492 653 $val = maybe_unserialize( $val[0] );
493 654
494 655 if ( is_object( $val ) || is_array( $val ) ) {
495 - $val = $this->replace_imported_attachment_urls_in_data( $val, $imported );
656 + $val = MediaPathHelper::replace_imported_attachment_urls_in_data( $val, $imported );
496 657 } elseif ( JsonHelper::is_string_json( $val ) ) {
497 658 $val = json_decode( $val );
498 - $val = $this->replace_imported_attachment_urls_in_data( $val, $imported );
659 + $val = MediaPathHelper::replace_imported_attachment_urls_in_data( $val, $imported );
499 660 $val = wp_slash( json_encode( $val ) );
500 661 } else {
501 - $val = $this->replace_imported_attachment_urls_in_string( $val, $imported );
662 + $val = MediaPathHelper::replace_imported_attachment_urls_in_string( $val, $imported );
502 663 }
503 664
504 665 update_post_meta( $post_id, $key, $val );
505 666 }
@@ -505,120 +666,8 @@
505 666 }
506 667 }
507 668
508 669 /**
509 - * Replaces the imported attachment in an object or array.
510 - *
511 - * @param object|array|string $data
512 - * @param array $imported
513 - * @return object|array|string
514 - */
515 - public function replace_imported_attachment_urls_in_data( $data, $imported ) {
516 - if ( is_object( $data ) || is_array( $data ) ) {
517 - foreach ( $data as $key => $val ) {
518 - if ( is_object( $val ) || is_array( $val ) ) {
519 - $new_val = $this->replace_imported_attachment_urls_in_data( $val, $imported );
520 - } else {
521 - $new_val = $this->replace_imported_attachment_urls_in_string( $val, $imported );
522 - }
523 -
524 - if ( is_object( $data ) ) {
525 - $data->$key = $new_val;
526 - } else {
527 - $data[ $key ] = $new_val;
528 - }
529 - }
530 - } else {
531 - $data = $this->replace_imported_attachment_urls_in_string( $data, $imported );
532 - }
533 -
534 - return $data;
535 - }
536 -
537 - /**
538 - * Replaces the imported attachment in a string.
539 - *
540 - * @param string $string
541 - * @param array $imported
542 - * @return string
543 - */
544 - public function replace_imported_attachment_urls_in_string( $string, $imported ) {
545 - $urls = $this->get_image_urls_from_string( $string );
546 -
547 - foreach ( $urls as $url ) {
548 - $url_info = $this->get_image_info_from_url( $url );
549 -
550 - if ( ! isset( $imported[ $url_info['file_name'] ] ) ) {
551 - continue;
552 - }
553 -
554 - $import_data = (array) $imported[ $url_info['file_name'] ];
555 - $sizes = $this->get_ordered_image_sizes( (array) $import_data['sizes'] );
556 - $new_url = null;
557 -
558 - if ( $url_info['width'] && $url_info['height'] ) {
559 - foreach ( $sizes as $size => $size_data ) {
560 - $size_data = (array) $size_data;
561 - if ( $url_info['width'] <= $size_data['width'] && $url_info['height'] <= $size_data['height'] ) {
562 - if ( isset( $size_data['url'] ) ) {
563 - $new_url = $size_data['url'];
564 - break;
565 - } elseif ( isset( $import_data['id'] ) ) {
566 - $size_src = wp_get_attachment_image_src( $import_data['id'], $size );
567 - if ( $size_src ) {
568 - $new_url = $size_src[0];
569 - break;
570 - }
571 - }
572 - }
573 - }
574 - }
575 -
576 - if ( ! $new_url ) {
577 - if ( isset( $import_data['url'] ) ) {
578 - $new_url = $import_data['url'];
579 - } elseif ( isset( $import_data['id'] ) ) {
580 - $new_url = wp_get_attachment_url( $import_data['id'] );
581 - }
582 - }
583 -
584 - if ( $new_url ) {
585 - $string = str_replace( $url, $new_url, $string );
586 - }
587 - }
588 -
589 - return $string;
590 - }
591 -
592 - /**
593 - * Returns an array or ordered image sizes.
594 - *
595 - * @param array $sizes
596 - * @return array
597 - */
598 - public function get_ordered_image_sizes( $sizes ) {
599 - $keys = [];
600 - $ordered = [];
601 -
602 - foreach ( $sizes as $size => $data ) {
603 - if ( 'thumb' === $size || 'thumbnail' === $size ) {
604 - continue;
605 - }
606 - $data = (array) $data;
607 - $key = $data['width'] + $data['height'];
608 - $keys[ $key ] = $size;
609 - }
610 -
611 - ksort( $keys );
612 -
613 - foreach ( $keys as $key ) {
614 - $ordered[ $key ] = $sizes[ $key ];
615 - }
616 -
617 - return $ordered;
618 - }
619 -
620 - /**
621 670 * Get the server paths to all images associated with a post.
622 671 *
623 672 * @param object $post
624 673 * @return array
@@ -623,16 +672,14 @@
623 672 * @param object $post
624 673 * @return array
625 674 */
626 675 public function get_post_image_paths( $post ) {
627 - $content_urls = $this->get_image_urls_from_string( $post->post_content );
628 - $content_paths = $this->get_image_paths_from_urls( $content_urls );
676 + $content_paths = MediaPathHelper::get_image_paths_from_string( $post->post_content );
629 677 $meta = get_post_meta( $post->ID );
630 678 $meta_paths = [];
631 679
632 680 if ( $meta && count( $meta ) > 0 ) {
633 - $meta_urls = $this->get_image_urls_from_meta( $meta );
634 - $meta_paths = $this->get_image_paths_from_urls( $meta_urls );
681 + $meta_paths = MediaPathHelper::get_image_paths_from_data( $meta );
635 682 }
636 683
637 684 return array_unique( array_merge( $content_paths, $meta_paths ) );
638 685 }
@@ -637,137 +684,19 @@
637 684 return array_unique( array_merge( $content_paths, $meta_paths ) );
638 685 }
639 686
640 687 /**
641 - * Get the server paths to all images in an array of urls.
642 - *
643 - * @param array $urls
644 - * @return array
645 - */
646 - public function get_image_paths_from_urls( $urls = [] ) {
647 - $paths = [];
648 -
649 - foreach ( $urls as $url ) {
650 - $path = $this->get_image_path_from_url( $url );
651 - if ( $path ) {
652 - $paths[] = $path;
653 - }
654 - }
655 -
656 - return $paths;
657 - }
658 -
659 - /**
660 - * Converts a media library URL to the server path of the full size image.
661 - *
662 - * @param string $url
663 - * @return string
664 - */
665 - public function get_image_path_from_url( $url ) {
666 - $upload_dir = wp_get_upload_dir();
667 - $base_url = preg_replace( '/https?:\/\//', '', $upload_dir['baseurl'] );
668 - $url_info = $this->get_image_info_from_url( $url );
669 - $url = preg_replace( '/https?:\/\//', '', $url_info['full_size_url'] );
670 - $path = null;
671 -
672 - if ( stristr( $url, $base_url ) ) {
673 - $path = $upload_dir['basedir'] . str_ireplace( $base_url, '', $url );
674 - }
675 -
676 - return $path;
677 - }
678 -
679 - /**
680 - * Get the urls to all images in a post meta object.
681 - *
682 - * @param mixed $meta
683 - * @return array
684 - */
685 - public function get_image_urls_from_meta( $meta ) {
686 - $urls = [];
687 -
688 - foreach ( $meta as $key => $val ) {
689 - $val = maybe_unserialize( $val );
690 -
691 - if ( is_object( $val ) || is_array( $val ) ) {
692 - $urls = array_merge( $urls, $this->get_image_urls_from_meta( $val ) );
693 - } elseif ( JsonHelper::is_string_json( $val ) ) {
694 - $val = wp_unslash( $val );
695 - $urls = array_merge( $urls, $this->get_image_urls_from_string( $val ) );
696 - } else {
697 - $urls = array_merge( $urls, $this->get_image_urls_from_string( $val ) );
698 - }
699 - }
700 -
701 - return $urls;
702 - }
703 -
704 - /**
705 - * Get the urls to all images in a string.
706 - *
707 - * @param string $string
708 - * @return array
709 - */
710 - public function get_image_urls_from_string( $string ) {
711 - $pattern = '#https?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/)\.(jpg|jpeg|png|gif))#';
712 - $urls = [];
713 -
714 - if ( preg_match_all( $pattern, $string, $matches ) ) {
715 - if ( isset( $matches[0] ) && ! empty( $matches[0] ) ) {
716 - $urls = $matches[0];
717 - }
718 - }
719 -
720 - return $urls;
721 - }
722 -
723 - /**
724 - * Get the info for a media library image from a url.
725 - *
726 - * @param string $url
727 - * @return array
728 - */
729 - public function get_image_info_from_url( $url ) {
730 - $info = [
731 - 'url' => $url,
732 - 'full_size_url' => $url,
733 - 'file_name' => null,
734 - 'width' => null,
735 - 'height' => null,
736 - 'ext' => null,
737 - ];
738 -
739 - preg_match_all( '/(-(\d+)x(\d+))\.(jpg|jpeg|png|gif)/', $url, $matches );
740 -
741 - if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
742 - $info['full_size_url'] = str_replace( $matches[1], '', $url );
743 - }
744 -
745 - $parts = explode( '/', $info['full_size_url'] );
746 - $info['file_name'] = array_pop( $parts );
747 -
748 - if ( isset( $matches[2] ) && ! empty( $matches[2] ) ) {
749 - $info['width'] = $matches[2][0];
750 - }
751 - if ( isset( $matches[3] ) && ! empty( $matches[3] ) ) {
752 - $info['height'] = $matches[3][0];
753 - }
754 - if ( isset( $matches[4] ) && ! empty( $matches[4] ) ) {
755 - $info['ext'] = $matches[4][0];
756 - }
757 -
758 - return $info;
759 - }
760 -
761 - /**
762 688 * Regenerate the asset cache for posts created with a builder.
763 689 *
764 690 * @param object $item
765 691 * @return void
766 692 */
767 - public function regenerate_builder_cache( $item ) {
693 + public function regenerate_builder_cache( $post_id, $item ) {
768 694 $meta = (array) $item->data->meta;
769 695
696 + if ( isset( $meta['_fl_builder_data'] ) && class_exists( '\FLBuilderModel' ) ) {
697 + \FLBuilderModel::delete_all_asset_cache( $post_id );
698 + }
770 699 if ( isset( $meta['_elementor_data'] ) && class_exists( 'Elementor\Plugin' ) ) {
771 700 \Elementor\Plugin::$instance->files_manager->clear_cache();
772 701 }
773 702 }