PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.0.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.0.3
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 3.1.10 All 111 releases
templately / includes / API / MyClouds.php

MyClouds.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.0.3, at includes/API/MyClouds.php

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