PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / my-cloud / REST / MyClouds.php

MyClouds.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/my-cloud/REST/MyClouds.php

252 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\MyCloud\REST;
4
5 use Templately\API\API;
6 use Templately\Core\Platform;
7 use WP_REST_Request;
8
9 use function wp_slash;
10 use function json_encode;
11 use function json_decode;
12 use function file_exists;
13 use function wp_upload_dir;
14
15 class MyClouds extends API {
16
17 public function register_routes() {
18 $this->get( 'clouds', [ $this, 'get_clouds' ] );
19 $this->get( 'clouds/usage', [ $this, 'get_cloud_usage' ] );
20
21 $this->post( 'clouds/copy-move', [ $this, 'copy_or_move' ] );
22 $this->post( 'clouds/upload', [ $this, 'upload' ] );
23
24 $this->get( 'clouds/download/(?P<id>[a-zA-Z0-9-]+)', [ $this, 'download' ] );
25 $this->get( 'clouds/delete/(?P<id>[a-zA-Z0-9-]+)', [ $this, 'delete' ] );
26 }
27
28 public function get_clouds() {
29 $page = $this->get_param( 'page', 1, 'intval' );
30 $per_page = $this->get_param( 'per_page', 12, 'intval' );
31 $platform = $this->get_param( 'platform', 'elementor' );
32 $files_search = $this->get_param( 'search' );
33
34 // { myCloud( api_key: "%s", cloud_files_page: %s, cloud_files_per_page: %s, file_type: "%s"%s ) { limit, usages, name, last_pushed, files { data{ id, name, thumbnail, medium_thumbnail, preview, file_type, created_at }, current_page, total_page } } }
35
36 $funcArgs = [
37 'api_key' => $this->api_key,
38 'cloud_files_page' => $page,
39 'cloud_files_per_page' => $per_page,
40 'file_type' => $platform,
41 ];
42
43 $query = 'limit, usages, last_pushed, files{ data { id, name, last_modified }, current_page, total_page }';
44
45 if( ! empty( $files_search ) ) {
46 $funcArgs['files_search'] = $files_search;
47 // $query = 'files{ data { id, name, last_modified }, current_page, total_page }';
48 }
49
50 $response = $this->http()->query(
51 'myCloud',
52 $query,
53 $funcArgs
54 )->post();
55
56 if( ! is_wp_error( $response ) ) {
57 if( ! empty( $response['last_pushed'] ) ) {
58 $this->options()->set( 'cloud_activity', $response['last_pushed'] );
59 }
60 }
61
62 return $response;
63 }
64
65 public function get_cloud_usage() {
66 $response = $this->http()->query(
67 'myCloudUsage',
68 'data, status, message',
69 [
70 'api_key' => $this->api_key
71 ]
72 )->post();
73
74 if( is_wp_error( $response ) ) {
75 return $this->error(
76 'invalid_response', $response->get_error_message(),
77 'clouds/usage', 404,
78 $response->get_error_data()
79 );
80 }
81
82 return $response;
83 }
84
85 public function copy_or_move(){
86 $my_cloud_file_id = $this->get_param( 'my_cloud_id', 0, 'intval' );
87 $workspace_id = $this->get_param( 'workspace_id', 0, 'intval' );
88 $action = $this->get_param( 'action', 'copy', 'strtolower' );
89
90 $funcArgs = [
91 'api_key' => $this->api_key,
92 'my_cloud_file_id' => $my_cloud_file_id,
93 'workspace_id' => $workspace_id,
94 'action' => $action,
95 ];
96
97 return $this->http()->mutation(
98 'copyOrMoveToWorkspace',
99 'status, message',
100 $funcArgs
101 )->post();
102 }
103
104 public function upload( WP_REST_Request $request ) {
105 $file_content = $request->get_param( 'file_content' );
106 $thumbnails = $request->get_param( 'thumbnails' );
107
108 $name = $this->get_param( 'name' );
109 $file_type = $this->get_param( 'platform', 'elementor' );
110 $id = $this->get_param( 'id', 0, 'intval' );
111 $workspace_id = $this->get_param( 'workspace_id', 0, 'intval' );
112
113 if( $file_type === 'elementor' && ! class_exists('Elementor\TemplateLibrary\Source_Local') ) {
114 return $this->error( 'required_elementor', __("Missing required dependency Elementor, please install before trying again.", "templately"), 'clouds/upload', 400 );
115 }
116
117 if( empty( $file_content ) ) {
118 /**
119 * @var Platform $platform
120 */
121 $platform = $this->platform( $file_type );
122 $template_data = $platform->get_template_data( $id );
123
124 $file_content = $template_data['file_content'];
125 if( empty( $name ) ) {
126 $name = $template_data['name'];
127 }
128 }
129
130 if( $file_type === 'elementor' && ! array_key_exists( 'content', $file_content ) ) {
131 $file_content = [
132 'content' => $file_content,
133 ];
134 }
135
136 $file_content = wp_slash( json_encode(
137 $file_content,
138 JSON_UNESCAPED_LINE_TERMINATORS | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE
139 ) );
140
141 $funcArgs = [
142 'api_key' => $this->api_key,
143 'name' => $name,
144 'file_type' => $file_type,
145 'file_content' => $file_content,
146 ];
147
148 if ( ! empty( $thumbnails ) ) {
149 $funcArgs['thumbnail'] = $thumbnails;
150 }
151
152 if ( $workspace_id > 0 ) {
153 $funcArgs['workspace_id'] = $workspace_id;
154 }
155
156 $response = $this->http()->mutation(
157 'pushToMyCloud',
158 'message, status, data', // 'file, status, message, file_name, file_type',
159 $funcArgs
160 )->post();
161
162 if( $id > 0 && ! is_wp_error( $response ) && $response['status'] === 'success' ) {
163 $data = (array) json_decode($response['data'], true);
164 $data = ! empty( $data['myCloud_item'] ) ? $data['myCloud_item'] : 0;
165 if ( $data > 0 ) {
166 $_templates = $this->utils('options')->get( 'templates_in_clouds', [] );
167 $_templates[ $id ] = $data;
168 $this->utils('options')->set( 'templates_in_clouds', $_templates );
169 }
170 }
171
172 return $response;
173 }
174
175 public function download() {
176 $id = $this->get_param( 'id', 0, 'intval' );
177 $platform = $this->get_param( 'platform', 'elementor' );
178
179 if ( $id <= 0 ) {
180 return $this->error(
181 'invalid_id', __( 'Invalid Item ID for download cloud item.', 'templately' ),
182 'clouds/download/:id', 400
183 );
184 }
185
186 $response = $this->http()->mutation(
187 'downloadMyCloudItem',
188 'file, status, message, file_name, file_type',
189 [
190 'api_key' => $this->api_key,
191 'id' => $id
192 ]
193 )->post();
194
195 if ( ! is_wp_error( $response ) && ! empty( $response['file_name'] ) && ! empty( $response['file'] ) ) {
196 require_once ABSPATH . '/wp-admin/includes/file.php';
197 $upload_dir = wp_upload_dir();
198 $file_content = json_decode( $response['file'], true );
199
200 if( $platform === 'elementor' ) {
201 if( is_string( $file_content ) ) {
202 $file_content = [
203 'content' => $file_content
204 ];
205 }
206
207 if( ! empty( $file_content ) && is_array( $file_content ) ) {
208 $file_content['page_settings'] = ! empty( $file_content['page_settings'] ) ? $file_content['page_settings'] : [] ;
209 $file_content['version'] = ! empty( $file_content['version'] ) ? $file_content['version'] : '';
210 $file_content['type'] = ! empty( $file_content['type'] ) ? $file_content['type'] : 'section';
211 $file_content['title'] = ! empty( $file_content['title'] ) ? $file_content['title'] : \basename( $response['file_name'], '.json' );
212 }
213 }
214
215 $file_content = is_array( $file_content ) ? json_encode( $file_content ) : $file_content;
216
217 $file_name = 'templately-tmp.json';
218 $_temp_file_name = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $file_name;
219 $_temp_file_url = $upload_dir['baseurl'] . DIRECTORY_SEPARATOR . $file_name;
220
221 if ( file_exists( $_temp_file_name ) ) {
222 unlink( $_temp_file_name );
223 }
224
225 $handle = fopen( $_temp_file_name, 'x+' );
226
227 fwrite( $handle, $file_content );
228 fclose( $handle );
229 $response['fileURL'] = $_temp_file_url;
230 }
231
232 return $response;
233 }
234
235 public function delete( WP_REST_Request $request ) {
236 $_id = $this->get_param( 'id', 0, 'intval' );
237
238 if ( $_id <= 0 ) {
239 return $this->error( 'invalid_id', __( 'Invalid Item ID for delete cloud item.', 'templately' ), 'clouds/delete/:id', 400 );
240 }
241
242 return $this->http()->query(
243 'removeFromMyCloud',
244 'status, data, message',
245 [
246 'api_key' => $this->api_key,
247 'file_id' => $_id
248 ]
249 )->post();
250 }
251
252 }