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

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

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