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