PluginProbe
Assistant – Every Day Productivity Apps / 1.4.7
Assistant – Every Day Productivity Apps v1.4.7
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/Cloud/Libraries/LibraryItemPostController.php +258 -356 0.6.01.4.7 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
@@ -184,33 +276,28 @@
184 276 $client = new CloudClient;
185 277
186 278 return $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 + );
282 + }
205 283
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 - ]
284 + /**
285 + * Replaces a library post with a post on this site.
286 + *
287 + * @param object $request
288 + * @return array
289 + */
290 + public function sync_to_library( $request ) {
291 + $id = $request->get_param( 'id' );
292 + $item_id = $request->get_param( 'item_id' );
293 + $post = get_post( $id );
294 + $client = new CloudClient;
295 + $data = $this->get_save_data( $request, $post );
296 +
297 + return $client->libraries->update_item(
298 + $item_id,
299 + $data
213 300 );
214 301 }
215 302
216 303 /**
@@ -219,13 +306,24 @@
219 306 * @param object $request
220 307 * @return array
221 308 */
222 309 public function import_from_library( $request ) {
223 - $item_id = $request->get_param( 'item_id' );
224 - $import_media = ! ! $request->get_param( 'import_media' );
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 +
225 321 $client = new CloudClient;
226 - $response = $client->libraries->get_item( $item_id );
227 - $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;
228 326
229 327 $new_post_id = wp_insert_post(
230 328 [
231 329 'comment_status' => $post_data->comment_status,
@@ -235,9 +333,9 @@
235 333 'post_content' => $post_data->post_content ? $post_data->post_content : '',
236 334 'post_excerpt' => $post_data->post_excerpt ? $post_data->post_excerpt : '',
237 335 'post_mime_type' => $post_data->post_mime_type,
238 336 'post_name' => $post_data->post_name,
239 - 'post_status' => 'draft',
337 + 'post_status' => 'publish',
240 338 'post_title' => $post_data->post_title,
241 339 'post_type' => $post_data->post_type,
242 340 ]
243 341 );
@@ -249,12 +347,11 @@
249 347 ]
250 348 );
251 349 }
252 350
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 );
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 );
257 354
258 355 return rest_ensure_response(
259 356 $this->posts->transform(
260 357 get_post( $new_post_id )
@@ -262,9 +359,9 @@
262 359 );
263 360 }
264 361
265 362 /**
266 - * Syncs a library post with a post on the site.
363 + * Replaces a post on this site with a post from a library.
267 364 *
268 365 * @param object $request
269 366 * @return array
270 367 */
@@ -269,17 +366,29 @@
269 366 * @return array
270 367 */
271 368 public function sync_from_library( $request ) {
272 369 $post_id = $request->get_param( 'id' );
273 - $item_id = $request->get_param( 'item_id' );
274 - $import_media = ! ! $request->get_param( 'import_media' );
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 +
275 381 $client = new CloudClient;
276 - $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;
277 386
278 387 $updated = wp_update_post(
279 388 [
280 389 'ID' => $post_id,
281 - 'post_content' => $response->data->post->post_content,
390 + 'post_content' => $post_data->post_content,
282 391 ]
283 392 );
284 393
285 394 if ( is_wp_error( $updated ) ) {
@@ -289,12 +398,11 @@
289 398 ]
290 399 );
291 400 }
292 401
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 );
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 );
297 405
298 406 return rest_ensure_response(
299 407 $this->posts->transform(
300 408 get_post( $post_id )
@@ -315,18 +423,24 @@
315 423 if ( ! $meta || ! is_object( $meta ) ) {
316 424 return;
317 425 }
318 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 +
319 432 foreach ( $meta as $meta_key => $meta_value ) {
320 433 if ( metadata_exists( 'post', $post_id, $meta_key ) ) {
321 434 delete_metadata( 'post', $post_id, $meta_key );
322 435 }
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
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 + }
329 443 }
330 444 }
331 445 }
332 446
@@ -337,17 +451,24 @@
337 451 * @param array $terms
338 452 * @return void
339 453 */
340 454 public function import_post_terms_from_library( $post_id, $terms ) {
455 + $terms = is_object( $terms ) ? [ $terms ] : $terms;
456 +
341 457 if ( ! $terms || ! is_array( $terms ) || 0 === count( $terms ) ) {
342 458 return;
343 459 }
344 460
345 461 $taxonomies = get_taxonomies( '', 'objects' );
462 + $taxonomy_terms = [];
346 463
347 464 foreach ( $terms as $term ) {
348 - if ( ! taxonomy_exists( $term->taxonomy ) ) {
465 + if ( ! isset( $term->name ) || ! isset( $term->taxonomy ) ) {
349 466 continue;
467 + } elseif ( ! taxonomy_exists( $term->taxonomy ) ) {
468 + continue;
469 + } elseif ( ! isset( $taxonomy_terms ) ) {
470 + $taxonomy_terms[ $term->taxonomy ] = [];
350 471 }
351 472
352 473 $existing_term = term_exists( $term->slug, $term->taxonomy );
353 474 $is_hierarchical = ! ! $taxonomies[ $term->taxonomy ]->hierarchical;
@@ -353,9 +474,8 @@
353 474 $is_hierarchical = ! ! $taxonomies[ $term->taxonomy ]->hierarchical;
354 475
355 476 if ( is_array( $existing_term ) ) {
356 477 $term_id = $is_hierarchical ? $existing_term['term_taxonomy_id'] : $term->name;
357 - wp_set_post_terms( $post_id, [ $term_id ], $term->taxonomy, true );
358 478 } else {
359 479 $new_term = wp_insert_term(
360 480 $term->name,
361 481 $term->taxonomy,
@@ -364,11 +484,17 @@
364 484 'slug' => $term->slug,
365 485 ]
366 486 );
367 487 $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 488 }
489 +
490 + $taxonomy_terms[ $term->taxonomy ][] = $term_id;
370 491 }
492 +
493 + foreach ( $taxonomy_terms as $taxonomy => $terms ) {
494 + wp_delete_object_term_relationships( $post_id, $taxonomy );
495 + wp_set_post_terms( $post_id, $terms, $taxonomy, true );
496 + }
371 497 }
372 498
373 499 /**
374 500 * Returns an array of all terms for a post.
@@ -400,59 +526,67 @@
400 526 * @param object $post
401 527 * @return array
402 528 */
403 529 public function get_post_screenshot( $request, $post ) {
404 - $screenshot = $request->get_param( 'screenshot' );
530 + $url = PreviewHelper::get_post_preview_url( $post );
531 + $url = add_query_arg( 'fl_asst_screenshot', '1', $url );
532 + return ScreenshotHelper::get_for_rest_request( $request, $url );
533 + }
405 534
406 - if ( $screenshot ) {
407 - return [
408 - 'type' => 'base64',
409 - 'data' => $screenshot,
410 - ];
535 + /**
536 + * Imports the featured image for a post.
537 + *
538 + * @param object $request
539 + * @return array
540 + */
541 + public function import_post_thumb_from_library( $request ) {
542 + $post_id = $request->get_param( 'post_id' );
543 + $thumb = $request->get_param( 'thumb' );
544 + $service = new MediaLibraryService();
545 +
546 + if ( ! preg_match( '/screenshot\.(png|jpg|gif)/', $thumb['file_name'] ) ) {
547 + $response = $service->import_cloud_media( $thumb, $post_id );
548 + set_post_thumbnail( $post_id, $response['id'] );
411 549 }
412 550
413 - $url = PostHelper::get_preview_url( $post );
414 - $response = wp_remote_get(
415 - $url, [
416 - 'cookies' => $_COOKIE,
417 - ]
418 - );
551 + return [ 'success' => true ];
552 + }
419 553
420 - return [
421 - 'type' => 'html',
422 - 'html' => wp_remote_retrieve_body( $response ),
423 - ];
554 + /**
555 + * Imports a media item for a post.
556 + *
557 + * @param object $request
558 + * @return array
559 + */
560 + public function import_post_media_from_library( $request ) {
561 + $post_id = $request->get_param( 'post_id' );
562 + $media = $request->get_param( 'media' );
563 + $service = new MediaLibraryService();
564 +
565 + $imported = [];
566 + $response = $service->import_cloud_media( $media, $post_id );
567 + $imported[ $media['file_name'] ] = wp_get_attachment_metadata( $response['id'] );
568 + $imported[ $media['file_name'] ]['id'] = $response['id'];
569 +
570 + $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
571 + $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
572 +
573 + return [ 'success' => true ];
424 574 }
425 575
426 576 /**
427 - * Imports the media for a post.
577 + * Replace attachment urls with urls from the cloud.
428 578 *
579 + * @param object $item
429 580 * @param int $post_id
430 - * @param object $media
431 - * @param bool $import
432 581 * @return void
433 582 */
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 ) ) {
583 + public function replace_attachment_urls_with_cloud_urls( $item, $post_id ) {
584 + if ( isset( $item->media->attachments ) ) {
445 585 $imported = [];
446 586
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 - }
587 + foreach ( $item->media->attachments as $attachment ) {
588 + $imported[ $attachment->file_name ] = $attachment;
455 589 }
456 590
457 591 $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
458 592 $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
@@ -467,9 +601,9 @@
467 601 * @return void
468 602 */
469 603 public function replace_imported_attachment_urls_in_content( $post_id, $imported ) {
470 604 $content = get_post_field( 'post_content', $post_id );
471 - $content = $this->replace_imported_attachment_urls_in_string( $content, $imported );
605 + $content = MediaPathHelper::replace_imported_attachment_urls_in_string( $content, $imported );
472 606
473 607 wp_update_post(
474 608 [
475 609 'ID' => $post_id,
@@ -491,15 +625,15 @@
491 625 foreach ( $meta as $key => $val ) {
492 626 $val = maybe_unserialize( $val[0] );
493 627
494 628 if ( is_object( $val ) || is_array( $val ) ) {
495 - $val = $this->replace_imported_attachment_urls_in_data( $val, $imported );
629 + $val = MediaPathHelper::replace_imported_attachment_urls_in_data( $val, $imported );
496 630 } elseif ( JsonHelper::is_string_json( $val ) ) {
497 631 $val = json_decode( $val );
498 - $val = $this->replace_imported_attachment_urls_in_data( $val, $imported );
632 + $val = MediaPathHelper::replace_imported_attachment_urls_in_data( $val, $imported );
499 633 $val = wp_slash( json_encode( $val ) );
500 634 } else {
501 - $val = $this->replace_imported_attachment_urls_in_string( $val, $imported );
635 + $val = MediaPathHelper::replace_imported_attachment_urls_in_string( $val, $imported );
502 636 }
503 637
504 638 update_post_meta( $post_id, $key, $val );
505 639 }
@@ -505,120 +639,8 @@
505 639 }
506 640 }
507 641
508 642 /**
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 643 * Get the server paths to all images associated with a post.
622 644 *
623 645 * @param object $post
624 646 * @return array
@@ -623,16 +645,14 @@
623 645 * @param object $post
624 646 * @return array
625 647 */
626 648 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 );
649 + $content_paths = MediaPathHelper::get_image_paths_from_string( $post->post_content );
629 650 $meta = get_post_meta( $post->ID );
630 651 $meta_paths = [];
631 652
632 653 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 );
654 + $meta_paths = MediaPathHelper::get_image_paths_from_data( $meta );
635 655 }
636 656
637 657 return array_unique( array_merge( $content_paths, $meta_paths ) );
638 658 }
@@ -637,137 +657,19 @@
637 657 return array_unique( array_merge( $content_paths, $meta_paths ) );
638 658 }
639 659
640 660 /**
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 661 * Regenerate the asset cache for posts created with a builder.
763 662 *
764 663 * @param object $item
765 664 * @return void
766 665 */
767 - public function regenerate_builder_cache( $item ) {
666 + public function regenerate_builder_cache( $post_id, $item ) {
768 667 $meta = (array) $item->data->meta;
769 668
669 + if ( isset( $meta['_fl_builder_data'] ) && class_exists( 'FLBuilderModel' ) ) {
670 + \FLBuilderModel::delete_all_asset_cache( $post_id );
671 + }
770 672 if ( isset( $meta['_elementor_data'] ) && class_exists( 'Elementor\Plugin' ) ) {
771 673 \Elementor\Plugin::$instance->files_manager->clear_cache();
772 674 }
773 675 }