PluginProbe
Assistant – Every Day Productivity Apps / trunk
Assistant – Every Day Productivity Apps vtrunk
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
assistant / backend / src / Controllers / Cloud / Libraries / LibraryItemImageController.php

LibraryItemImageController.php in Assistant – Every Day Productivity Apps trunk, at backend/src/Controllers/Cloud/Libraries/LibraryItemImageController.php

100 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
100