← All changes
|
backend/src/Controllers/Cloud/Libraries/LibraryItemImageController.php
+87
-99
trunk
→
1.4.3
View file →
| @@ -1,99 +1,87 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace FL\Assistant\Controllers\Cloud\Libraries; | |
| 4 | - | |
| 5 | -use FL\Assistant\System\Contracts\ControllerAbstract; | |
| 6 | -use FL\Assistant\Services\MediaLibraryService; | |
| 7 | -use FL\Assistant\Clients\Cloud\CloudClient; | |
| 8 | - | |
| 9 | -class LibraryItemImageController extends ControllerAbstract { | |
| 10 | - | |
| 11 | - public function register_routes() { | |
| 12 | - $this->route( | |
| 13 | - '/library-items/import/image', [ | |
| 14 | - [ | |
| 15 | - 'methods' => \WP_REST_Server::CREATABLE, | |
| 16 | - 'callback' => [ $this, 'import' ], | |
| 17 | - 'permission_callback' => function () { | |
| 18 | - return current_user_can( 'edit_others_posts' ); | |
| 19 | - }, | |
| 20 | - ], | |
| 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 | - ); | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Import an image library item into the site. | |
| 49 | - */ | |
| 50 | - public function import( $request ) { | |
| 51 | - $item = $request->get_param( 'item' ); | |
| 52 | - | |
| 53 | - if ( ! is_array( $item ) || ! isset( $item['media']['file'] ) ) { | |
| 54 | - return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] ); | |
| 55 | - } | |
| 56 | - | |
| 57 | - $media = (object) $item['media']['file']; | |
| 58 | - $service = new MediaLibraryService(); | |
| 59 | - $response = $service->import_cloud_media( $media ); | |
| 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 | - | |
| 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 | - ); | |
| 98 | - } | |
| 99 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace FL\Assistant\Controllers\Cloud\Libraries; | |
| 4 | + | |
| 5 | +use FL\Assistant\System\Contracts\ControllerAbstract; | |
| 6 | +use FL\Assistant\Services\MediaLibraryService; | |
| 7 | +use FL\Assistant\Clients\Cloud\CloudClient; | |
| 8 | + | |
| 9 | +class LibraryItemImageController extends ControllerAbstract { | |
| 10 | + | |
| 11 | + public function register_routes() { | |
| 12 | + $this->route( | |
| 13 | + '/library-items/import/image', [ | |
| 14 | + [ | |
| 15 | + 'methods' => \WP_REST_Server::CREATABLE, | |
| 16 | + 'callback' => [ $this, 'import' ], | |
| 17 | + 'permission_callback' => function () { | |
| 18 | + return current_user_can( 'edit_others_posts' ); | |
| 19 | + }, | |
| 20 | + ], | |
| 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 | + ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Import an image library item into the site. | |
| 49 | + */ | |
| 50 | + public function import( $request ) { | |
| 51 | + $item = $request->get_param( 'item' ); | |
| 52 | + | |
| 53 | + if ( ! is_array( $item ) || ! isset( $item['media']['file'] ) ) { | |
| 54 | + return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + $media = (object) $item['media']['file']; | |
| 58 | + $service = new MediaLibraryService(); | |
| 59 | + $response = $service->import_cloud_media( $media ); | |
| 60 | + | |
| 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 | + ); | |
| 86 | + } | |
| 87 | +} | |