PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
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 / workspaces / REST / WorkSpaces.php

WorkSpaces.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/workspaces/REST/WorkSpaces.php

317 lines 9.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\Workspaces\REST;
4
5 use Templately\API\API;
6 use WP_Error;
7 use WP_REST_Request;
8
9 class WorkSpaces extends API {
10 private $endpoint = 'workspaces';
11
12 public function register_routes() {
13 $this->get( $this->endpoint, [ $this, 'get_workspaces' ] );
14 $this->get( $this->endpoint . '/(?P<slug>[a-zA-Z0-9-]+)', [ $this, 'get_workspaces_details' ] );
15 $this->get( $this->endpoint . '/item/(?P<id>[a-zA-Z0-9-]+)', [ $this, 'get_item' ] );
16 $this->get( $this->endpoint . '/item/delete/(?P<id>[a-zA-Z0-9-]+)', [ $this, 'delete_item' ] );
17
18 $this->post( $this->endpoint . '/create', [ $this, 'create_workspace' ] );
19 $this->post( $this->endpoint . '/(?P<slug>[a-zA-Z0-9-]+)', [ $this, 'edit_workspace' ] );
20 $this->post( $this->endpoint . '/delete/(?P<id>[a-zA-Z0-9-]+)', [ $this, 'delete_workspaces' ] );
21 $this->post( $this->endpoint . '/item/add', [ $this, 'add_item' ] );
22 }
23
24 /**
25 * @param $message mixed actual error message.
26 * @param $endpoint string endpoint, which for the error is triggering.
27 *
28 * @return WP_Error
29 */
30 protected function permission_error( $message, $endpoint = '' ) {
31 $_message = '';
32 if ( $endpoint === $this->get_namespace( 'workspaces' ) ) {
33 $_message = __( 'Sorry, you need to login/re-login to get your workspace list.', 'templately' );
34 }
35
36 return parent::permission_error( $_message, $endpoint );
37 }
38
39 /**
40 * Get WorkSpace List.
41 * This will give paged lists for WorkSpaces.
42 *
43 * @return mixed
44 */
45 public function get_workspaces() {
46 $shared = $this->get_param( 'shared', false, 'rest_sanitize_boolean' );
47 $per_page = $this->get_param( 'per_page', 15, 'intval' );
48 $page = $this->get_param( 'page', 1, 'intval' );
49 $platform = $this->get_param( 'platform', 'elementor' );
50 $list_only = $this->get_param( 'list_only', false, 'rest_sanitize_boolean' );
51
52 $query = 'data{ id, name, slug, sharedWith{ id, name, profile_photo }, owner { id, name, profile_photo }, pending_invitations }, total_page, current_page';
53
54 if ( $list_only ) {
55 $per_page = -1;
56 $query = 'data{ id, name, slug }';
57 }
58
59 return $this->http()->query(
60 $shared ? 'sharedWorkspaces' : 'workspaces',
61 $query,
62 [
63 'api_key' => $this->api_key,
64 'page' => $page,
65 'file_type' => $platform,
66 'per_page' => $per_page
67 ]
68 )->post();
69 }
70
71 public function get_workspaces_details() {
72 $_slug = $this->get_param( 'slug' );
73
74 if ( empty( $_slug ) ) {
75 return $this->error(
76 'invalid_id',
77 __( 'Invalid workspace slug for get workspace.', 'templately' ), 'workspaces/:id', 400
78 );
79 }
80
81 $directLoad = $this->get_param( 'directLoad', false, 'boolval' );
82 $files_per_page = $this->get_param( 'per_page', 10, 'intval' );
83 $files_page = $this->get_param( 'page', 1, 'intval' );
84 $file_type = $this->get_param( 'file_type', 'elementor' );
85 $search_keyword = $this->get_param( 'search' );
86
87 $funcArgs = [
88 'api_key' => $this->api_key,
89 'slug' => $_slug,
90 'files_page' => $files_page,
91 'files_per_page' => $files_per_page,
92 'file_type' => $file_type
93 ];
94
95 if ( ! empty( $search_keyword ) ) {
96 $funcArgs['files_search'] = $search_keyword;
97 }
98
99 $query_params = 'files{ total_page, current_page, data{ id, my_cloud_id, name, owner{ id }, last_modified } }';
100
101 if ( $directLoad ) {
102 $query_params = 'id, name, slug, sharedWith{ id, name, email, profile_photo }, owner{ id, name, profile_photo }, pending_invitations,' . $query_params;
103 }
104
105 $response = $this->http()->query(
106 'workspaceDetails',
107 $query_params,
108 $funcArgs
109 )->post();
110
111 if ( is_null( $response ) ) {
112 return $this->error(
113 'workspace_not_exists',
114 __( 'The workspace you looking for is not exists anymore.', 'templately' ),
115 'workspaces/:slug',
116 404
117 );
118 }
119
120 return $response;
121 }
122
123 /**
124 * Creating a workspace in clouds.
125 *
126 * @return mixed
127 */
128 public function create_workspace() {
129 $name = $this->get_param( 'title' );
130
131 if ( empty( $name ) ) {
132 return $this->error(
133 'invalid_name',
134 __( 'Workspace name cannot be empty.', 'templately' ),
135 'workspaces/create',
136 400
137 );
138 }
139
140 $shared_with = $this->get_param( 'share_with' );
141
142 $funcArgs = [
143 'api_key' => $this->api_key,
144 'name' => $name
145 ];
146
147 if ( ! empty( $shared_with ) ) {
148 $funcArgs['share_with'] = wp_slash( json_encode( $shared_with ) );
149 }
150
151 return $this->http()->mutation(
152 'createWorkspace',
153 'status, message, workspace{ id, name, slug, sharedWith{ id, name, profile_photo }, owner{ id, name, profile_photo }, pending_invitations }',
154 $funcArgs
155 )->post();
156 }
157
158 /**
159 * Editing WorkSpace.
160 *
161 * @return mixed
162 */
163 public function edit_workspace() {
164 $isCreate = $this->get_param( 'isCreate', false, 'rest_sanitize_boolean' );
165
166 if ( $isCreate ) {
167 /**
168 * Passes the call to self::create_workspace if the request is for create.
169 */
170 return $this->create_workspace();
171 }
172
173 $id = $this->get_param( 'id', false, 'intval' );
174 $title = $this->get_param( 'title' );
175 $platform = $this->get_param( 'platform', 'elementor' );
176 $share_with = $this->get_param( 'share_with' );
177 $remove = $this->get_param( 'remove' );
178
179 if ( empty( $title ) ) {
180 return $this->error(
181 'invalid_name',
182 __( 'Workspace name cannot be empty.', 'templately' ),
183 'workspaces/create',
184 400
185 );
186 }
187
188 $post_args = [
189 'api_key' => $this->api_key,
190 'id' => $id,
191 'name' => $title,
192 'file_type' => $platform
193 ];
194
195 if ( ! empty( $share_with ) ) {
196 $post_args['share_with'] = wp_slash( json_encode( $share_with ) );
197 }
198
199 if ( ! empty( $remove ) ) {
200 $post_args['remove'] = wp_slash( json_encode( $remove ) );
201 }
202
203 return $this->http()->mutation(
204 'editWorkspace',
205 'status, message, workspace{ id, name, slug, sharedWith{ id, name, profile_photo }, owner{ id, name, profile_photo }, pending_invitations }',
206 $post_args
207 )->post();
208 }
209
210 /**
211 * Deletes an workspace from the cloud.
212 *
213 * @return void|WP_Error|array
214 */
215 public function delete_workspaces() {
216 $_id = $this->get_param( 'id', false, 'intval' );
217 $delete_files = $this->get_param( 'delete_files', false, 'boolval' );
218
219 if ( ! $_id ) {
220 return $this->error(
221 'invalid_workspace_id',
222 __( 'You should provide a valid WorkSpace ID.', 'templately' ),
223 'workspaces/:slug', 404
224 );
225 }
226
227 $funcArgs = [
228 'api_key' => $this->api_key,
229 'id' => $_id
230 ];
231
232 if ( $delete_files ) {
233 $funcArgs['delete_files'] = 'true';
234 }
235
236 return $this->http()->mutation(
237 'deleteWorkspace',
238 'status, message',
239 $funcArgs
240 )->post();
241 }
242
243 public function add_item() {
244 $workspace_id = $this->get_param( 'workspace_id', 0, 'intval' );
245 $files = $this->get_param( 'files', false, 'intval' );
246
247 if ( $workspace_id <= 0 ) {
248 return $this->error(
249 'invalid_workspace_id',
250 __( 'Invalid Workspace ID.', 'templately' ), '/workspaces/item/add', 501 );
251 }
252
253 if ( ! $files ) {
254 return $this->error( 'invalid_files_id', __( 'Invalid Files ID.', 'templately' ), '/workspaces/item/add', 501 );
255 }
256
257 $funcArgs = [
258 'api_key' => $this->api_key,
259 'workspace_id' => $workspace_id,
260 'files' => json_encode( $files )
261 ];
262
263 return $this->http()->mutation(
264 'addFileToWorkspace',
265 'data, status',
266 $funcArgs
267 )->post();
268 }
269
270 public function get_item( WP_REST_Request $request ) {
271 $_id = intval( $request->get_param( 'id' ) );
272
273 if ( $_id <= 0 ) {
274 return $this->error( 'invalid_id', __( 'Invalid item id for get cloud item.', 'templately' ), 'workspaces/item/:id', 400 );
275 }
276
277 return $this->http()->mutation(
278 'downloadMyCloudItem',
279 'file, status, message, file_name, file_type',
280 [
281 'api_key' => $this->api_key,
282 'id' => $_id
283 ]
284 )->post();
285 }
286
287 /**
288 * Delete an item from WorkSpace
289 *
290 * @since 2.0.0
291 *
292 * @param WP_REST_Request $request REST request arguments
293 *
294 * @return mixed
295 */
296 public function delete_item( WP_REST_Request $request ) {
297 $_id = intval( $request->get_param( 'id' ) );
298
299 if ( $_id <= 0 ) {
300 return $this->error(
301 'invalid_id',
302 __( 'Invalid item id for delete cloud item.', 'templately' ),
303 'workspaces/item/delete/:id',
304 400
305 );
306 }
307
308 return $this->http()->mutation(
309 'deleteWorkspaceFile',
310 'status, data, message',
311 [
312 'api_key' => $this->api_key,
313 'file_id' => $_id
314 ]
315 )->post();
316 }
317 }