← All changes
|
backend/src/Controllers/Cloud/Libraries/LibraryItemImageController.php
+47
-0
0.7.0
→
1.0.2
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. |
| @@ -35,6 +58,30 @@ | ||
| 35 | 58 | $service = new MediaLibraryService(); |
| 36 | 59 | $response = $service->import_cloud_media( $media ); |
| 37 | 60 | |
| 38 | 61 | return rest_ensure_response( $response ); |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Export an image from the site to a library. | |
| 66 | + */ | |
| 67 | + public function export( $request ) { | |
| 68 | + $id = $request->get_param( 'id' ); | |
| 69 | + $library_id = $request->get_param( 'library_id' ); | |
| 70 | + $attachment = get_post( $id ); | |
| 71 | + $client = new CloudClient; | |
| 72 | + $path = get_attached_file( $id ); | |
| 73 | + $ext = pathinfo( $path, PATHINFO_EXTENSION ); | |
| 74 | + | |
| 75 | + return $client->libraries->create_item( | |
| 76 | + $library_id, | |
| 77 | + [ | |
| 78 | + 'name' => $attachment->post_title, | |
| 79 | + 'type' => 'svg' === $ext ? 'svg' : 'image', | |
| 80 | + 'data' => [], | |
| 81 | + 'media' => [ | |
| 82 | + 'file' => $path, | |
| 83 | + ], | |
| 84 | + ] | |
| 85 | + ); | |
| 39 | 86 | } |
| 40 | 87 | } |