PluginProbe
Assistant – Every Day Productivity Apps / 1.0.2
Assistant – Every Day Productivity Apps v1.0.2
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 +126 -63 0.7.01.0.2 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 ],
@@ -96,21 +90,49 @@
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' ],
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' ],
105 112 'args' => [
106 - 'item_id' => [
113 + 'post_id' => [
107 114 'required' => true,
108 115 'type' => 'number',
109 116 ],
110 - 'import_media' => [
111 - 'type' => 'number',
112 - 'default' => 1,
117 + ],
118 + 'permission_callback' => function () {
119 + return current_user_can( 'edit_others_posts' );
120 + },
121 + ],
122 + ]
123 + );
124 +
125 + $this->route(
126 + '/posts/import_post_media_from_library/(?P<post_id>\d+)',
127 + [
128 + [
129 + 'methods' => \WP_REST_Server::CREATABLE,
130 + 'callback' => [ $this, 'import_post_media_from_library' ],
131 + 'args' => [
132 + 'post_id' => [
133 + 'required' => true,
134 + 'type' => 'number',
113 135 ],
114 136 ],
115 137 'permission_callback' => function () {
116 138 return current_user_can( 'edit_others_posts' );
@@ -119,9 +141,9 @@
119 141 ]
120 142 );
121 143
122 144 $this->route(
123 - '/posts/(?P<id>\d+)/sync_from_library/(?P<item_id>\d+)',
145 + '/posts/(?P<id>\d+)/sync_from_library/',
124 146 [
125 147 [
126 148 'methods' => \WP_REST_Server::CREATABLE,
127 149 'callback' => [ $this, 'sync_from_library' ],
@@ -129,16 +151,8 @@
129 151 'id' => [
130 152 'required' => true,
131 153 'type' => 'number',
132 154 ],
133 - 'item_id' => [
134 - 'required' => true,
135 - 'type' => 'number',
136 - ],
137 - 'import_media' => [
138 - 'type' => 'number',
139 - 'default' => 1,
140 - ],
141 155 ],
142 156 'permission_callback' => function () {
143 157 return current_user_can( 'edit_others_posts' );
144 158 },
@@ -155,16 +169,25 @@
155 169 */
156 170 public function preview_library_post( $request ) {
157 171 global $wpdb;
158 172
159 - $item_id = $request->get_param( 'item_id' );
173 + $data = $request->get_param( 'item' );
174 + $item = $data ? json_decode( $data ) : null;
160 175
176 + if ( ! $item ) {
177 + return rest_ensure_response(
178 + [
179 + 'error' => true,
180 + ]
181 + );
182 + }
183 +
161 184 $meta = $wpdb->get_row(
162 185 $wpdb->prepare(
163 186 "SELECT * FROM $wpdb->postmeta
164 187 WHERE meta_key = '_fl_asst_preview_library_item_id'
165 188 AND meta_value = %s",
166 - $item_id
189 + $item->id
167 190 )
168 191 );
169 192
170 193 if ( $meta ) {
@@ -186,10 +209,12 @@
186 209 'post_status' => 'auto-draft',
187 210 ]
188 211 );
189 212
190 - 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 );
191 214
215 + $this->replace_attachment_urls_with_cloud_urls( $item, $post_id );
216 +
192 217 return rest_ensure_response(
193 218 [
194 219 'url' => PreviewHelper::get_post_preview_url( $post_id ),
195 220 ]
@@ -273,14 +298,21 @@
273 298 * @param object $request
274 299 * @return array
275 300 */
276 301 public function import_from_library( $request ) {
277 - $item_id = $request->get_param( 'item_id' );
278 - $import_media = ! ! $request->get_param( 'import_media' );
279 - $client = new CloudClient;
280 - $response = $client->libraries->get_item( $item_id );
281 - $post_data = $response->data->post;
302 + $data = $request->get_param( 'item' );
303 + $item = $data ? json_decode( $data ) : null;
282 304
305 + if ( ! $item ) {
306 + return rest_ensure_response(
307 + [
308 + 'error' => true,
309 + ]
310 + );
311 + }
312 +
313 + $post_data = $item->data->post;
314 +
283 315 $new_post_id = wp_insert_post(
284 316 [
285 317 'comment_status' => $post_data->comment_status,
286 318 'menu_order' => $post_data->menu_order,
@@ -289,9 +321,9 @@
289 321 'post_content' => $post_data->post_content ? $post_data->post_content : '',
290 322 'post_excerpt' => $post_data->post_excerpt ? $post_data->post_excerpt : '',
291 323 'post_mime_type' => $post_data->post_mime_type,
292 324 'post_name' => $post_data->post_name,
293 - 'post_status' => 'draft',
325 + 'post_status' => 'publish',
294 326 'post_title' => $post_data->post_title,
295 327 'post_type' => $post_data->post_type,
296 328 ]
297 329 );
@@ -303,12 +335,11 @@
303 335 ]
304 336 );
305 337 }
306 338
307 - $this->import_post_meta_from_library( $new_post_id, $response->data->meta );
308 - $this->import_post_terms_from_library( $new_post_id, $response->data->terms );
309 - $this->import_post_media_from_library( $new_post_id, $response->media, $import_media );
310 - $this->regenerate_builder_cache( $response );
339 + $this->import_post_meta_from_library( $new_post_id, $item->data->meta );
340 + $this->import_post_terms_from_library( $new_post_id, $item->data->terms );
341 + $this->regenerate_builder_cache( $item );
311 342
312 343 return rest_ensure_response(
313 344 $this->posts->transform(
314 345 get_post( $new_post_id )
@@ -323,17 +354,23 @@
323 354 * @return array
324 355 */
325 356 public function sync_from_library( $request ) {
326 357 $post_id = $request->get_param( 'id' );
327 - $item_id = $request->get_param( 'item_id' );
328 - $import_media = ! ! $request->get_param( 'import_media' );
329 - $client = new CloudClient;
330 - $response = $client->libraries->get_item( $item_id );
358 + $data = $request->get_param( 'item' );
359 + $item = $data ? json_decode( $data ) : null;
331 360
361 + if ( ! $item ) {
362 + return rest_ensure_response(
363 + [
364 + 'error' => true,
365 + ]
366 + );
367 + }
368 +
332 369 $updated = wp_update_post(
333 370 [
334 371 'ID' => $post_id,
335 - 'post_content' => $response->data->post->post_content,
372 + 'post_content' => $item->data->post->post_content,
336 373 ]
337 374 );
338 375
339 376 if ( is_wp_error( $updated ) ) {
@@ -343,12 +380,11 @@
343 380 ]
344 381 );
345 382 }
346 383
347 - $this->import_post_meta_from_library( $post_id, $response->data->meta );
348 - $this->import_post_terms_from_library( $post_id, $response->data->terms );
349 - $this->import_post_media_from_library( $post_id, $response->media, $import_media );
350 - $this->regenerate_builder_cache( $response );
384 + $this->import_post_meta_from_library( $post_id, $item->data->meta );
385 + $this->import_post_terms_from_library( $post_id, $item->data->terms );
386 + $this->regenerate_builder_cache( $item );
351 387
352 388 return rest_ensure_response(
353 389 $this->posts->transform(
354 390 get_post( $post_id )
@@ -391,8 +427,10 @@
391 427 * @param array $terms
392 428 * @return void
393 429 */
394 430 public function import_post_terms_from_library( $post_id, $terms ) {
431 + $terms = is_object( $terms ) ? [ $terms ] : $terms;
432 +
395 433 if ( ! $terms || ! is_array( $terms ) || 0 === count( $terms ) ) {
396 434 return;
397 435 }
398 436
@@ -466,40 +504,65 @@
466 504 */
467 505 public function get_post_screenshot( $request, $post ) {
468 506 $url = PreviewHelper::get_post_preview_url( $post );
469 507 $url = add_query_arg( 'fl_asst_screenshot', '1', $url );
470 - return ScreenshotHelper::get_for_request( $request, $url );
508 + return ScreenshotHelper::get_for_rest_request( $request, $url );
471 509 }
472 510
473 511 /**
474 - * Imports the media for a post.
512 + * Imports the featured image for a post.
475 513 *
476 - * @param int $post_id
477 - * @param object $media
478 - * @param bool $import
479 - * @return void
514 + * @param object $request
515 + * @return array
480 516 */
481 - public function import_post_media_from_library( $post_id, $media, $import = true ) {
517 + public function import_post_thumb_from_library( $request ) {
518 + $post_id = $request->get_param( 'post_id' );
519 + $thumb = $request->get_param( 'thumb' );
482 520 $service = new MediaLibraryService();
483 521
484 - // Import post thumbnail
485 - if ( $import && isset( $media->thumb ) && 'screenshot.png' !== $media->thumb->file_name ) {
486 - $response = $service->import_cloud_media( $media->thumb, $post_id );
522 + if ( ! preg_match( '/screenshot\.(png|jpg|gif)/', $thumb['file_name'] ) ) {
523 + $response = $service->import_cloud_media( $thumb, $post_id );
487 524 set_post_thumbnail( $post_id, $response['id'] );
488 525 }
489 526
490 - // Import post attachments
491 - if ( isset( $media->attachments ) ) {
527 + return [ 'success' => true ];
528 + }
529 +
530 + /**
531 + * Imports a media item for a post.
532 + *
533 + * @param object $request
534 + * @return array
535 + */
536 + public function import_post_media_from_library( $request ) {
537 + $post_id = $request->get_param( 'post_id' );
538 + $media = $request->get_param( 'media' );
539 + $service = new MediaLibraryService();
540 +
541 + $imported = [];
542 + $response = $service->import_cloud_media( $media, $post_id );
543 + $imported[ $media['file_name'] ] = wp_get_attachment_metadata( $response['id'] );
544 + $imported[ $media['file_name'] ]['id'] = $response['id'];
545 +
546 + $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
547 + $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );
548 +
549 + return [ 'success' => true ];
550 + }
551 +
552 + /**
553 + * Replace attachment urls with urls from the cloud.
554 + *
555 + * @param object $item
556 + * @param int $post_id
557 + * @return void
558 + */
559 + public function replace_attachment_urls_with_cloud_urls( $item, $post_id ) {
560 + if ( isset( $item->media->attachments ) ) {
492 561 $imported = [];
493 562
494 - foreach ( $media->attachments as $attachment ) {
495 - if ( $import ) {
496 - $response = $service->import_cloud_media( $attachment, $post_id );
497 - $imported[ $attachment->file_name ] = wp_get_attachment_metadata( $response['id'] );
498 - $imported[ $attachment->file_name ]['id'] = $response['id'];
499 - } else {
500 - $imported[ $attachment->file_name ] = $attachment;
501 - }
563 + foreach ( $item->media->attachments as $attachment ) {
564 + $imported[ $attachment->file_name ] = $attachment;
502 565 }
503 566
504 567 $this->replace_imported_attachment_urls_in_content( $post_id, $imported );
505 568 $this->replace_imported_attachment_urls_in_meta( $post_id, $imported );