PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.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 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.7.0, at includes/API/MyClouds.php

251 lines 7.7 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 $response->get_error_data()
78 );
79 }
80
81 return $response;
82 }
83
84 public function copy_or_move(){
85 $my_cloud_file_id = $this->get_param( 'my_cloud_id', 0, 'intval' );
86 $workspace_id = $this->get_param( 'workspace_id', 0, 'intval' );
87 $action = $this->get_param( 'action', 'copy', 'strtolower' );
88
89 $funcArgs = [
90 'api_key' => $this->api_key,
91 'my_cloud_file_id' => $my_cloud_file_id,
92 'workspace_id' => $workspace_id,
93 'action' => $action,
94 ];
95
96 return $this->http()->mutation(
97 'copyOrMoveToWorkspace',
98 'status, message',
99 $funcArgs
100 )->post();
101 }
102
103 public function upload( WP_REST_Request $request ) {
104 $file_content = $request->get_param( 'file_content' );
105 $thumbnails = $request->get_param( 'thumbnails' );
106
107 $name = $this->get_param( 'name' );
108 $file_type = $this->get_param( 'platform', 'elementor' );
109 $id = $this->get_param( 'id', 0, 'intval' );
110 $workspace_id = $this->get_param( 'workspace_id', 0, 'intval' );
111
112 if( $file_type === 'elementor' && ! class_exists('Elementor\TemplateLibrary\Source_Local') ) {
113 return $this->error( 'required_elementor', __("Missing required dependency Elementor, please install before trying again.", "templately"), 'clouds/upload', 400 );
114 }
115
116 if( empty( $file_content ) ) {
117 /**
118 * @var Platform $platform
119 */
120 $platform = $this->platform( $file_type );
121 $template_data = $platform->get_template_data( $id );
122
123 $file_content = $template_data['file_content'];
124 if( empty( $name ) ) {
125 $name = $template_data['name'];
126 }
127 }
128
129 if( $file_type === 'elementor' && ! array_key_exists( 'content', $file_content ) ) {
130 $file_content = [
131 'content' => $file_content,
132 ];
133 }
134
135 $file_content = wp_slash( json_encode(
136 $file_content,
137 JSON_UNESCAPED_LINE_TERMINATORS | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE
138 ) );
139
140 $funcArgs = [
141 'api_key' => $this->api_key,
142 'name' => $name,
143 'file_type' => $file_type,
144 'file_content' => $file_content,
145 ];
146
147 if ( ! empty( $thumbnails ) ) {
148 $funcArgs['thumbnail'] = $thumbnails;
149 }
150
151 if ( $workspace_id > 0 ) {
152 $funcArgs['workspace_id'] = $workspace_id;
153 }
154
155 $response = $this->http()->mutation(
156 'pushToMyCloud',
157 'message, status, data', // 'file, status, message, file_name, file_type',
158 $funcArgs
159 )->post();
160
161 if( $id > 0 && ! is_wp_error( $response ) && $response['status'] === 'success' ) {
162 $data = (array) json_decode($response['data'], true);
163 $data = ! empty( $data['myCloud_item'] ) ? $data['myCloud_item'] : 0;
164 if ( $data > 0 ) {
165 $_templates = $this->utils('options')->get( 'templates_in_clouds', [] );
166 $_templates[ $id ] = $data;
167 $this->utils('options')->set( 'templates_in_clouds', $_templates );
168 }
169 }
170
171 return $response;
172 }
173
174 public function download() {
175 $id = $this->get_param( 'id', 0, 'intval' );
176 $platform = $this->get_param( 'platform', 'elementor' );
177
178 if ( $id <= 0 ) {
179 return $this->error(
180 'invalid_id', __( 'Invalid Item ID for download cloud item.', 'templately' ),
181 'clouds/download/:id', 400
182 );
183 }
184
185 $response = $this->http()->mutation(
186 'downloadMyCloudItem',
187 'file, status, message, file_name, file_type',
188 [
189 'api_key' => $this->api_key,
190 'id' => $id
191 ]
192 )->post();
193
194 if ( ! is_wp_error( $response ) && ! empty( $response['file_name'] ) && ! empty( $response['file'] ) ) {
195 require_once ABSPATH . '/wp-admin/includes/file.php';
196 $upload_dir = wp_upload_dir();
197 $file_content = json_decode( $response['file'], true );
198
199 if( $platform === 'elementor' ) {
200 if( is_string( $file_content ) ) {
201 $file_content = [
202 'content' => $file_content
203 ];
204 }
205
206 if( ! empty( $file_content ) && is_array( $file_content ) ) {
207 $file_content['page_settings'] = ! empty( $file_content['page_settings'] ) ? $file_content['page_settings'] : [] ;
208 $file_content['version'] = ! empty( $file_content['version'] ) ? $file_content['version'] : '';
209 $file_content['type'] = ! empty( $file_content['type'] ) ? $file_content['type'] : 'section';
210 $file_content['title'] = ! empty( $file_content['title'] ) ? $file_content['title'] : \basename( $response['file_name'], '.json' );
211 }
212 }
213
214 $file_content = is_array( $file_content ) ? json_encode( $file_content ) : $file_content;
215
216 $file_name = 'templately-tmp.json';
217 $_temp_file_name = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $file_name;
218 $_temp_file_url = $upload_dir['baseurl'] . DIRECTORY_SEPARATOR . $file_name;
219
220 if ( file_exists( $_temp_file_name ) ) {
221 unlink( $_temp_file_name );
222 }
223
224 $handle = fopen( $_temp_file_name, 'x+' );
225
226 fwrite( $handle, $file_content );
227 fclose( $handle );
228 $response['fileURL'] = $_temp_file_url;
229 }
230
231 return $response;
232 }
233
234 public function delete( WP_REST_Request $request ) {
235 $_id = $this->get_param( 'id', 0, 'intval' );
236
237 if ( $_id <= 0 ) {
238 return $this->error( 'invalid_id', __( 'Invalid Item ID for delete cloud item.', 'templately' ), 'clouds/delete/:id', 400 );
239 }
240
241 return $this->http()->query(
242 'removeFromMyCloud',
243 'status, data, message',
244 [
245 'api_key' => $this->api_key,
246 'file_id' => $_id
247 ]
248 )->post();
249 }
250
251 }