| 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 The actual error message. |
| 25 |
* @param $endpoint The 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, 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 |
$post_args = [ |
| 179 |
'api_key' => $this->api_key, |
| 180 |
'id' => $id, |
| 181 |
'name' => $title, |
| 182 |
'file_type' => $platform |
| 183 |
]; |
| 184 |
|
| 185 |
if ( ! empty( $share_with ) ) { |
| 186 |
$post_args['share_with'] = wp_slash( json_encode( $share_with ) ); |
| 187 |
} |
| 188 |
|
| 189 |
if ( ! empty( $remove ) ) { |
| 190 |
$post_args['remove'] = wp_slash( json_encode( $remove ) ); |
| 191 |
} |
| 192 |
|
| 193 |
return $this->http()->mutation( |
| 194 |
'editWorkspace', |
| 195 |
'status, message, workspace{ id, name, slug, sharedWith{ id, name, profile_photo }, owner{ id, name, profile_photo }, pending_invitations }', |
| 196 |
$post_args |
| 197 |
)->post(); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Deletes an workspace from the cloud. |
| 202 |
* |
| 203 |
* @return void|WP_Error|array |
| 204 |
*/ |
| 205 |
public function delete_workspaces() { |
| 206 |
$_id = $this->get_param( 'id', false, 'intval' ); |
| 207 |
$delete_files = $this->get_param( 'delete_files', false, 'boolval' ); |
| 208 |
|
| 209 |
if ( ! $_id ) { |
| 210 |
return $this->error( |
| 211 |
'invalid_workspace_id', |
| 212 |
__( 'You should provide a valid WorkSpace ID.', 'templately' ), |
| 213 |
'workspaces/:slug', 404 |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
$funcArgs = [ |
| 218 |
'api_key' => $this->api_key, |
| 219 |
'id' => $_id |
| 220 |
]; |
| 221 |
|
| 222 |
if ( $delete_files ) { |
| 223 |
$funcArgs['delete_files'] = 'true'; |
| 224 |
} |
| 225 |
|
| 226 |
return $this->http()->mutation( |
| 227 |
'deleteWorkspace', |
| 228 |
'status, message', |
| 229 |
$funcArgs |
| 230 |
)->post(); |
| 231 |
} |
| 232 |
|
| 233 |
public function add_item() { |
| 234 |
$workspace_id = $this->get_param( 'workspace_id', 0, 'intval' ); |
| 235 |
$files = $this->get_param( 'files', false, 'intval' ); |
| 236 |
|
| 237 |
if ( $workspace_id <= 0 ) { |
| 238 |
return $this->error( |
| 239 |
'invalid_workspace_id', |
| 240 |
__( 'Invalid Workspace ID.', 'templately' ), '/workspaces/item/add', 501 ); |
| 241 |
} |
| 242 |
|
| 243 |
if ( ! $files ) { |
| 244 |
return $this->error( 'invalid_files_id', __( 'Invalid Files ID.', 'templately' ), '/workspaces/item/add', 501 ); |
| 245 |
} |
| 246 |
|
| 247 |
$funcArgs = [ |
| 248 |
'api_key' => $this->api_key, |
| 249 |
'workspace_id' => $workspace_id, |
| 250 |
'files' => json_encode( $files ) |
| 251 |
]; |
| 252 |
|
| 253 |
return $this->http()->mutation( |
| 254 |
'addFileToWorkspace', |
| 255 |
'data, status', |
| 256 |
$funcArgs |
| 257 |
)->post(); |
| 258 |
} |
| 259 |
|
| 260 |
public function get_item( WP_REST_Request $request ) { |
| 261 |
$_id = intval( $request->get_param( 'id' ) ); |
| 262 |
|
| 263 |
if ( $_id <= 0 ) { |
| 264 |
return $this->error( 'invalid_id', __( 'Invalid item id for get cloud item.', 'templately' ), 'workspaces/item/:id', 400 ); |
| 265 |
} |
| 266 |
|
| 267 |
return $this->http()->mutation( |
| 268 |
'downloadMyCloudItem', |
| 269 |
'file, status, message, file_name, file_type', |
| 270 |
[ |
| 271 |
'api_key' => $this->api_key, |
| 272 |
'id' => $_id |
| 273 |
] |
| 274 |
)->post(); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Delete an item from WorkSpace |
| 279 |
* |
| 280 |
* @since 2.0.0 |
| 281 |
* |
| 282 |
* @param WP_REST_Request $request REST request arguments |
| 283 |
* |
| 284 |
* @return mixed |
| 285 |
*/ |
| 286 |
public function delete_item( WP_REST_Request $request ) { |
| 287 |
$_id = intval( $request->get_param( 'id' ) ); |
| 288 |
|
| 289 |
if ( $_id <= 0 ) { |
| 290 |
return $this->error( |
| 291 |
'invalid_id', |
| 292 |
__( 'Invalid item id for delete cloud item.', 'templately' ), |
| 293 |
'workspaces/item/delete/:id', |
| 294 |
400 |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
return $this->http()->mutation( |
| 299 |
'deleteWorkspaceFile', |
| 300 |
'status, data, message', |
| 301 |
[ |
| 302 |
'api_key' => $this->api_key, |
| 303 |
'file_id' => $_id |
| 304 |
] |
| 305 |
)->post(); |
| 306 |
} |
| 307 |
} |