| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
use WP_REST_Response; |
| 5 |
|
| 6 |
defined('ABSPATH') || exit; |
| 7 |
|
| 8 |
class Api { |
| 9 |
private static $instance = null; |
| 10 |
private $token; |
| 11 |
private $version; |
| 12 |
private $assets_url; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 21 |
$this->version = WPMCS_VERSION; |
| 22 |
$this->token = WPMCS_TOKEN; |
| 23 |
|
| 24 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Register API routes |
| 30 |
*/ |
| 31 |
|
| 32 |
public function register_routes() { |
| 33 |
$this->add_route( '/verifyCredentials', 'verifyCredentials', 'POST' ); |
| 34 |
$this->add_route( '/permissions/checkExist', 'verifyBucketExist', 'POST' ); |
| 35 |
$this->add_route( '/permissions/addNewBucket', 'createBucket', 'POST' ); |
| 36 |
$this->add_route( '/permissions/write', 'verifyObjectWrite', 'POST' ); |
| 37 |
$this->add_route( '/permissions/delete', 'verifyObjectDelete', 'POST' ); |
| 38 |
$this->add_route( '/permissions/read', 'verifyObjectRead', 'POST' ); |
| 39 |
|
| 40 |
$this->add_route( '/security/get_security', 'getBucketSecuritySettings', 'POST' ); |
| 41 |
$this->add_route( '/security/block_public_access', 'changePublicAccess', 'POST' ); |
| 42 |
$this->add_route( '/security/object_ownership_enforce', 'changeObjectOwnership', 'POST' ); |
| 43 |
|
| 44 |
$this->add_route( '/saveConfig', 'saveConfig', 'POST' ); |
| 45 |
$this->add_route( '/getSettings', 'getSettings' ); |
| 46 |
$this->add_route( '/uploadCredentials', 'uploadCredentials', 'POST' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Verify the Service Credentials |
| 51 |
*/ |
| 52 |
public function verifyCredentials( $data ) { |
| 53 |
return new WP_REST_Response( Service::instance()->verifyCredentials($data->get_params()), 200 ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Verify the Bucket exist |
| 58 |
*/ |
| 59 |
public function verifyBucketExist( $data ) { |
| 60 |
return new WP_REST_Response( Service::instance()->verifyBucketExist($data->get_params()), 200 ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Create the bucket |
| 65 |
*/ |
| 66 |
public function createBucket( $data ) { |
| 67 |
return new WP_REST_Response( Service::instance()->createBucket($data->get_params()), 200 ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Verify the Bucket Write Permission |
| 73 |
*/ |
| 74 |
public function verifyObjectWrite( $data ) { |
| 75 |
return new WP_REST_Response( Service::instance()->verifyObjectWritePermission($data->get_params()), 200 ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Verify the Bucket delete Permission |
| 80 |
*/ |
| 81 |
public function verifyObjectDelete( $data ) { |
| 82 |
return new WP_REST_Response( Service::instance()->verifyObjectDeletePermission($data->get_params()), 200 ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Verify the Bucket Read Permission |
| 87 |
*/ |
| 88 |
public function verifyObjectRead( $data ) { |
| 89 |
return new WP_REST_Response( Service::instance()->verifyObjectReadPermission($data->get_params()), 200 ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get the Security Settings |
| 94 |
*/ |
| 95 |
public function getBucketSecuritySettings( $data ) { |
| 96 |
return new WP_REST_Response( Service::instance()->getBucketSecuritySettings($data->get_params()), 200 ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Change the Public Access |
| 102 |
*/ |
| 103 |
public function changePublicAccess( $data ) { |
| 104 |
return new WP_REST_Response( Service::instance()->changePublicAccess($data->get_params()), 200 ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* Change the Object Ownership |
| 110 |
*/ |
| 111 |
public function changeObjectOwnership( $data ) { |
| 112 |
return new WP_REST_Response( Service::instance()->changeObjectOwnership($data->get_params()), 200 ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get Settings |
| 117 |
*/ |
| 118 |
public function getSettings( ) { |
| 119 |
$data = [ |
| 120 |
'credentials' => Utils::get_credentials(), |
| 121 |
'common' => [ |
| 122 |
'version' => WPMCS_VERSION, |
| 123 |
'uploaded' => Counter::get( 'uploaded' ), |
| 124 |
'status' => Utils::get_status('', false), |
| 125 |
'settings' => Utils::get_settings(), |
| 126 |
] |
| 127 |
]; |
| 128 |
|
| 129 |
return new WP_REST_Response( $data, 200 ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Save the Configurations |
| 134 |
*/ |
| 135 |
public function saveConfig( $data ) { |
| 136 |
$saveData = $data->get_params(); |
| 137 |
$result = [ |
| 138 |
'success' => false, |
| 139 |
'message' => esc_html__('Something went wrong. Invalid data recieved', 'media-cloud-sync') |
| 140 |
]; |
| 141 |
|
| 142 |
$action = isset($saveData['action']) ? $saveData['action'] : false; |
| 143 |
if($action === false) return new WP_REST_Response( $result, 200 ); |
| 144 |
|
| 145 |
$service = isset($saveData['service']) ? $saveData['service'] : false; |
| 146 |
$serviceLabel = isset($saveData['serviceLabel']) ? $saveData['serviceLabel'] : ''; |
| 147 |
$cdn = isset($saveData['cdn']) ? $saveData['cdn'] : []; |
| 148 |
$config = isset($saveData['config']) ? $saveData['config'] : false; |
| 149 |
$bucketConfig = isset($saveData['bucketConfig']) ? $saveData['bucketConfig'] : []; |
| 150 |
$security = isset($saveData['security']) ? $saveData['security'] : []; |
| 151 |
$settings = isset($saveData['settings']) ? $saveData['settings'] : []; |
| 152 |
|
| 153 |
$serviceOk = true; |
| 154 |
$settingsOk = true; |
| 155 |
|
| 156 |
if( $action === 'cdn' ){ |
| 157 |
$existing = Utils::get_credentials(); |
| 158 |
$existing['cdn'] = $cdn; |
| 159 |
|
| 160 |
$updated = Utils::update_option('credentials', $existing, Schema::getConstant('GLOBAL_SETTINGS_KEY')); |
| 161 |
$updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY')); |
| 162 |
|
| 163 |
if(!$updated) $serviceOk = false; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
if($action === 'all' || $action === 'service'){ |
| 168 |
if ( $service === false || empty($config) || empty($bucketConfig)) return new WP_REST_Response( $result, 200 ); |
| 169 |
|
| 170 |
$updated = Utils::update_option( |
| 171 |
'credentials', |
| 172 |
[ |
| 173 |
'service' => $service, |
| 174 |
'serviceLabel' => $serviceLabel, |
| 175 |
'cdn' => $cdn, |
| 176 |
'config' => $config, |
| 177 |
'bucketConfig' => $bucketConfig, |
| 178 |
'security' => $security |
| 179 |
], |
| 180 |
Schema::getConstant('GLOBAL_SETTINGS_KEY') |
| 181 |
); |
| 182 |
if(!$updated) $serviceOk = false; |
| 183 |
} |
| 184 |
|
| 185 |
if(($action === 'all' || $action === 'settings')) { |
| 186 |
$updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY')); |
| 187 |
if(!$updatedSettings) $settingsOk = false; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
if($serviceOk && $settingsOk) { |
| 192 |
Utils::set_status('cdnRead', [ |
| 193 |
'status' => false, |
| 194 |
'message' => '', |
| 195 |
'lastChecked' => null |
| 196 |
]); |
| 197 |
$result = [ |
| 198 |
'success' => true, |
| 199 |
'message' => esc_html__('Configuration saved successfully', 'media-cloud-sync') |
| 200 |
]; |
| 201 |
} |
| 202 |
|
| 203 |
return new WP_REST_Response( $result, 200 ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Upload Credentials |
| 209 |
* @since 1.0.0 |
| 210 |
*/ |
| 211 |
public function uploadCredentials($request) { |
| 212 |
if (isset($_FILES['file']) && !empty($_FILES['file'])) { |
| 213 |
$config_file = $_FILES['file']; |
| 214 |
if (isset($config_file['type']) && $config_file['type'] == 'application/json') { |
| 215 |
if (file_exists($config_file["tmp_name"])) { |
| 216 |
|
| 217 |
add_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]); |
| 218 |
add_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]); |
| 219 |
|
| 220 |
if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 221 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 222 |
} |
| 223 |
|
| 224 |
$upload_overrides = [ |
| 225 |
'test_form' => false, |
| 226 |
'test_type' => true, |
| 227 |
'mimes' => [ 'json'=>'application/json' ] |
| 228 |
]; |
| 229 |
|
| 230 |
$movefile = wp_handle_upload( $config_file, $upload_overrides ); |
| 231 |
|
| 232 |
remove_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]); |
| 233 |
remove_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]); |
| 234 |
|
| 235 |
if ($movefile && !isset($movefile['error'])) { |
| 236 |
$result = [ |
| 237 |
'success' => true, |
| 238 |
'file_path' => $movefile['file'], |
| 239 |
'file_name' => basename($movefile['file']), |
| 240 |
]; |
| 241 |
return new WP_REST_Response($result, 200); |
| 242 |
} else { |
| 243 |
$result = [ |
| 244 |
'success' => false, |
| 245 |
'message' => $movefile['error'], |
| 246 |
]; |
| 247 |
return new WP_REST_Response($result, 200); |
| 248 |
} |
| 249 |
} |
| 250 |
} else { |
| 251 |
$result = [ |
| 252 |
'success' => false, |
| 253 |
'message' => esc_html__('Invalid file format', 'media-cloud-sync'), |
| 254 |
]; |
| 255 |
return new WP_REST_Response($result, 200); |
| 256 |
} |
| 257 |
} else { |
| 258 |
$result = [ |
| 259 |
'success' => false, |
| 260 |
'message' => esc_html__('Insufficient data. Please try again', 'media-cloud-sync'), |
| 261 |
]; |
| 262 |
return new WP_REST_Response($result, 200); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Change File Upload Directory |
| 268 |
* @since 1.0.0 |
| 269 |
*/ |
| 270 |
public function change_file_upload_dir($upload) { |
| 271 |
$wpmcs_path = '/' . Schema::getConstant('UPLOADS'); |
| 272 |
$path = $upload['basedir'] . $wpmcs_path; |
| 273 |
$url = $upload['baseurl'] . $wpmcs_path; |
| 274 |
|
| 275 |
if (!is_dir($path)) { |
| 276 |
do_action( $this->token.'_create_plugin_dir' ); |
| 277 |
} |
| 278 |
|
| 279 |
$upload['subdir'] = $wpmcs_path; |
| 280 |
$upload['path'] = $upload['basedir'] . $wpmcs_path; |
| 281 |
$upload['url'] = $upload['baseurl'] . $wpmcs_path; |
| 282 |
|
| 283 |
return $upload; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Add Custom Mime Type JSON |
| 288 |
* @since 1.0.0 |
| 289 |
*/ |
| 290 |
public function add_custom_mime_type_json($mimes) { |
| 291 |
$mimes['json'] = 'application/json'; |
| 292 |
// Return the array back to the function with our added mime type. |
| 293 |
return $mimes; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Helper function to create Adding Route |
| 299 |
* @since 1.0.0 |
| 300 |
*/ |
| 301 |
private function add_route( $slug, $callBack, $method = 'GET' ) { |
| 302 |
register_rest_route( |
| 303 |
$this->token . '/v1', |
| 304 |
$slug, |
| 305 |
array( |
| 306 |
'methods' => $method, |
| 307 |
'callback' => array( $this, $callBack ), |
| 308 |
'permission_callback' => array( $this, 'getPermission' ), |
| 309 |
) ); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Permission Callback |
| 314 |
**/ |
| 315 |
public function getPermission() { |
| 316 |
if ( current_user_can( 'manage_options' ) ) { |
| 317 |
return true; |
| 318 |
} else { |
| 319 |
return false; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Ensures only one instance of Class is loaded or can be loaded. |
| 325 |
* |
| 326 |
* @return Api Class instance |
| 327 |
* @since 1.0.0 |
| 328 |
* @static |
| 329 |
*/ |
| 330 |
public static function instance(){ |
| 331 |
if (is_null(self::$instance)) { |
| 332 |
self::$instance = new self(); |
| 333 |
} |
| 334 |
return self::$instance; |
| 335 |
} |
| 336 |
} |
| 337 |
|