PluginProbe
Assistant – Every Day Productivity Apps / 0.7.0
Assistant – Every Day Productivity Apps v0.7.0
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 / LibraryItemThemeSettingsController.php

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

108 lines 3.0 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\Clients\Cloud\CloudClient;
7 use FL\Assistant\Services\CustomizerService;
8 use FL\Assistant\Services\MediaLibraryService;
9 use FL\Assistant\Helpers\MediaPathHelper;
10 use FL\Assistant\Helpers\ScreenshotHelper;
11
12 class LibraryItemThemeSettingsController extends ControllerAbstract {
13
14 public function register_routes() {
15 $this->route(
16 '/library/(?P<library_id>\d+)/library-items/export/theme-settings', [
17 [
18 'methods' => \WP_REST_Server::CREATABLE,
19 'callback' => [ $this, 'export' ],
20 'args' => [
21 'library_id' => [
22 'required' => true,
23 'type' => 'number',
24 ],
25 ],
26 'permission_callback' => function () {
27 return current_user_can( 'edit_others_posts' );
28 },
29 ],
30 ]
31 );
32
33 $this->route(
34 '/library-items/import/theme-settings', [
35 [
36 'methods' => \WP_REST_Server::CREATABLE,
37 'callback' => [ $this, 'import' ],
38 'permission_callback' => function () {
39 return current_user_can( 'edit_others_posts' );
40 },
41 ],
42 ]
43 );
44 }
45
46 public function export( $request ) {
47 $library_id = $request->get_param( 'library_id' );
48 $client = new CloudClient;
49 $service = new CustomizerService();
50 $data = $service->export_settings();
51 $theme = wp_get_theme();
52
53 return $client->libraries->create_item(
54 $library_id,
55 [
56 'name' => sprintf( _x( '%s Settings', '%s theme name', 'fl-assistant' ), $theme->Name ),
57 'type' => 'theme_settings',
58 'data' => $data,
59 'media' => [
60 'attachments' => MediaPathHelper::get_image_paths_from_data( $data )
61 ],
62 'screenshot' => ScreenshotHelper::get_for_request( $request, home_url(), false ),
63 ]
64 );
65 }
66
67 public function import( $request ) {
68 $item = $request->get_param( 'item' );
69 $service = new CustomizerService();
70
71 if ( ! is_array( $item ) || empty( $item['data'] ) ) {
72 return rest_ensure_response( [ 'error' => __( 'Missing item data.' ) ] );
73 }
74
75 $can_import = $service->can_import_settings( $item['data'] );
76
77 if ( is_wp_error( $can_import ) ) {
78 return rest_ensure_response( [
79 'error' => $can_import->get_error_message()
80 ] );
81 }
82
83 $data = $this->import_media_from_library( $item );
84 $service->import_settings( $data );
85
86 return rest_ensure_response( [
87 'success' => true
88 ] );
89 }
90
91 public function import_media_from_library( $item ) {
92 $service = new MediaLibraryService();
93 $data = $item['data'];
94 $media = $item['media'];
95
96 if ( isset( $media['attachments'] ) ) {
97 $imported = [];
98 foreach ( $media['attachments'] as $attachment ) {
99 $response = $service->import_cloud_media( $attachment );
100 $imported[ $attachment['file_name'] ] = wp_get_attachment_metadata( $response['id'] );
101 $imported[ $attachment['file_name'] ]['id'] = $response['id'];
102 }
103 }
104
105 return MediaPathHelper::replace_imported_attachment_urls_in_data( $data, $imported );
106 }
107 }
108