← All changes
|
backend/src/Controllers/Cloud/Libraries/LibraryItemImageController.php
+59
-0
0.6.0
→
1.4.7
View file →
| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FL\Assistant\Controllers\Cloud\Libraries; |
| 4 | 4 | |
| 5 | 5 | use FL\Assistant\System\Contracts\ControllerAbstract; |
| 6 | 6 | use FL\Assistant\Services\MediaLibraryService; |
| 7 | +use FL\Assistant\Clients\Cloud\CloudClient; | |
| 7 | 8 | |
| 8 | 9 | class LibraryItemImageController extends ControllerAbstract { |
| 9 | 10 | |
| 10 | 11 | public function register_routes() { |
| @@ -18,8 +19,30 @@ | ||
| 18 | 19 | }, |
| 19 | 20 | ], |
| 20 | 21 | ] |
| 21 | 22 | ); |
| 23 | + | |
| 24 | + $this->route( | |
| 25 | + '/images/(?P<id>\d+)/library/(?P<library_id>\d+)', [ | |
| 26 | + [ | |
| 27 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 28 | + 'callback' => [ $this, 'export' ], | |
| 29 | + 'args' => [ | |
| 30 | + 'id' => [ | |
| 31 | + 'required' => true, | |
| 32 | + 'type' => 'number', | |
| 33 | + ], | |
| 34 | + 'library_id' => [ | |
| 35 | + 'required' => true, | |
| 36 | + 'type' => 'number', | |
| 37 | + ], | |
| 38 | + ], | |
| 39 | + 'permission_callback' => function () { | |
| 40 | + return current_user_can( 'edit_others_posts' ); | |
| 41 | + }, | |
| 42 | + ], | |
| 43 | + ] | |
| 44 | + ); | |
| 22 | 45 | } |
| 23 | 46 | |
| 24 | 47 | /** |
| 25 | 48 | * Import an image library item into the site. |
| @@ -34,7 +57,43 @@ | ||
| 34 | 57 | $media = (object) $item['media']['file']; |
| 35 | 58 | $service = new MediaLibraryService(); |
| 36 | 59 | $response = $service->import_cloud_media( $media ); |
| 37 | 60 | |
| 61 | + if ( $response && isset( $response['id'] ) && !empty( $item['data']['alt'] ) ) { | |
| 62 | + update_post_meta( $response['id'], '_wp_attachment_image_alt', $item['data']['alt'] ); | |
| 63 | + } | |
| 64 | + | |
| 38 | 65 | return rest_ensure_response( $response ); |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Export an image from the site to a library. | |
| 70 | + */ | |
| 71 | + public function export( $request ) { | |
| 72 | + $id = $request->get_param( 'id' ); | |
| 73 | + $library_id = $request->get_param( 'library_id' ); | |
| 74 | + $attachment = get_post( $id ); | |
| 75 | + $client = new CloudClient; | |
| 76 | + $path = get_attached_file( $id ); | |
| 77 | + $ext = pathinfo( $path, PATHINFO_EXTENSION ); | |
| 78 | + | |
| 79 | + $data = []; | |
| 80 | + | |
| 81 | + $alt = get_post_meta( $id, '_wp_attachment_image_alt', true); | |
| 82 | + | |
| 83 | + if ( !empty( $alt ) ) { | |
| 84 | + $data['alt'] = $alt; | |
| 85 | + } | |
| 86 | + | |
| 87 | + return $client->libraries->create_item( | |
| 88 | + $library_id, | |
| 89 | + [ | |
| 90 | + 'name' => $attachment->post_title, | |
| 91 | + 'type' => 'svg' === $ext ? 'svg' : 'image', | |
| 92 | + 'data' => $data, | |
| 93 | + 'media' => [ | |
| 94 | + 'file' => $path, | |
| 95 | + ], | |
| 96 | + ] | |
| 97 | + ); | |
| 39 | 98 | } |
| 40 | 99 | } |