PluginProbe
Media Cloud Sync / 1.2.11
Media Cloud Sync v1.2.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / api.php

api.php in Media Cloud Sync 1.2.11, at includes/api.php

278 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
47
48 /**
49 * Verify the Service Credentials
50 */
51 public function verifyCredentials( $data ) {
52 return new WP_REST_Response( Service::instance()->verifyCredentials($data->get_params()), 200 );
53 }
54
55 /**
56 * Verify the Bucket exist
57 */
58 public function verifyBucketExist( $data ) {
59 return new WP_REST_Response( Service::instance()->verifyBucketExist($data->get_params()), 200 );
60 }
61
62 /**
63 * Create the bucket
64 */
65 public function createBucket( $data ) {
66 return new WP_REST_Response( Service::instance()->createBucket($data->get_params()), 200 );
67 }
68
69
70 /**
71 * Verify the Bucket Write Permission
72 */
73 public function verifyObjectWrite( $data ) {
74 return new WP_REST_Response( Service::instance()->verifyObjectWritePermission($data->get_params()), 200 );
75 }
76
77 /**
78 * Verify the Bucket delete Permission
79 */
80 public function verifyObjectDelete( $data ) {
81 return new WP_REST_Response( Service::instance()->verifyObjectDeletePermission($data->get_params()), 200 );
82 }
83
84 /**
85 * Verify the Bucket Read Permission
86 */
87 public function verifyObjectRead( $data ) {
88 return new WP_REST_Response( Service::instance()->verifyObjectReadPermission($data->get_params()), 200 );
89 }
90
91 /**
92 * Get the Security Settings
93 */
94 public function getBucketSecuritySettings( $data ) {
95 return new WP_REST_Response( Service::instance()->getBucketSecuritySettings($data->get_params()), 200 );
96 }
97
98
99 /**
100 * Change the Public Access
101 */
102 public function changePublicAccess( $data ) {
103 return new WP_REST_Response( Service::instance()->changePublicAccess($data->get_params()), 200 );
104 }
105
106
107 /**
108 * Change the Object Ownership
109 */
110 public function changeObjectOwnership( $data ) {
111 return new WP_REST_Response( Service::instance()->changeObjectOwnership($data->get_params()), 200 );
112 }
113
114 /**
115 * Get Settings
116 */
117 public function getSettings( ) {
118 $data = [
119 'credentials' => Utils::get_credentials(),
120 'common' => [
121 'version' => WPMCS_VERSION,
122 'uploaded' => Counter::get( 'uploaded' ),
123 'status' => Utils::get_status('', false),
124 'settings' => Utils::get_settings(),
125 ]
126 ];
127
128 return new WP_REST_Response( $data, 200 );
129 }
130
131 /**
132 * Save the Configurations
133 */
134 public function saveConfig( $data ) {
135 $saveData = $data->get_params();
136 $result = [
137 'success' => false,
138 'message' => esc_html__('Something went wrong. Invalid data recieved', 'media-cloud-sync')
139 ];
140
141 $action = isset($saveData['action']) ? $saveData['action'] : false;
142 if($action === false) return new WP_REST_Response( $result, 200 );
143
144 $service = isset($saveData['service']) ? $saveData['service'] : false;
145 $serviceLabel = isset($saveData['serviceLabel']) ? $saveData['serviceLabel'] : '';
146 $cdn = isset($saveData['cdn']) ? $saveData['cdn'] : [];
147 $config = isset($saveData['config']) ? $saveData['config'] : false;
148 $bucketConfig = isset($saveData['bucketConfig']) ? $saveData['bucketConfig'] : [];
149 $security = isset($saveData['security']) ? $saveData['security'] : [];
150 $settings = isset($saveData['settings']) ? $saveData['settings'] : [];
151
152 $serviceOk = true;
153 $settingsOk = true;
154
155 if( $action === 'cdn' ){
156 $existing = Utils::get_credentials();
157 $existing['cdn'] = $cdn;
158
159 $updated = Utils::update_option('credentials', $existing, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
160 $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
161
162 if(!$updated) $serviceOk = false;
163 }
164
165
166 if($action === 'all' || $action === 'service'){
167 if ( $service === false || empty($config) || empty($bucketConfig)) return new WP_REST_Response( $result, 200 );
168
169 $updated = Utils::update_option(
170 'credentials',
171 [
172 'service' => $service,
173 'serviceLabel' => $serviceLabel,
174 'cdn' => $cdn,
175 'config' => $config,
176 'bucketConfig' => $bucketConfig,
177 'security' => $security
178 ],
179 Schema::getConstant('GLOBAL_SETTINGS_KEY')
180 );
181 if(!$updated) $serviceOk = false;
182 }
183
184 if(($action === 'all' || $action === 'settings')) {
185 $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
186 if(!$updatedSettings) $settingsOk = false;
187 }
188
189
190 if($serviceOk && $settingsOk) {
191 Utils::set_status('cdnRead', [
192 'status' => false,
193 'message' => '',
194 'lastChecked' => null
195 ]);
196 $result = [
197 'success' => true,
198 'message' => esc_html__('Configuration saved successfully', 'media-cloud-sync')
199 ];
200 }
201
202 return new WP_REST_Response( $result, 200 );
203 }
204
205
206
207 /**
208 * Change File Upload Directory
209 * @since 1.0.0
210 */
211 public function change_file_upload_dir($upload) {
212 $wpmcs_path = '/' . Schema::getConstant('UPLOADS');
213 $path = $upload['basedir'] . $wpmcs_path;
214 $url = $upload['baseurl'] . $wpmcs_path;
215
216 if (!is_dir($path)) {
217 do_action( $this->token.'_create_plugin_dir' );
218 }
219
220 $upload['subdir'] = $wpmcs_path;
221 $upload['path'] = $upload['basedir'] . $wpmcs_path;
222 $upload['url'] = $upload['baseurl'] . $wpmcs_path;
223
224 return $upload;
225 }
226
227 /**
228 * Add Custom Mime Type JSON
229 * @since 1.0.0
230 */
231 public function add_custom_mime_type_json($mimes) {
232 $mimes['json'] = 'application/json';
233 // Return the array back to the function with our added mime type.
234 return $mimes;
235 }
236
237
238 /**
239 * Helper function to create Adding Route
240 * @since 1.0.0
241 */
242 private function add_route( $slug, $callBack, $method = 'GET' ) {
243 register_rest_route(
244 $this->token . '/v1',
245 $slug,
246 array(
247 'methods' => $method,
248 'callback' => array( $this, $callBack ),
249 'permission_callback' => array( $this, 'getPermission' ),
250 ) );
251 }
252
253 /**
254 * Permission Callback
255 **/
256 public function getPermission() {
257 if ( current_user_can( 'manage_options' ) ) {
258 return true;
259 } else {
260 return false;
261 }
262 }
263
264 /**
265 * Ensures only one instance of Class is loaded or can be loaded.
266 *
267 * @return Api Class instance
268 * @since 1.0.0
269 * @static
270 */
271 public static function instance(){
272 if (is_null(self::$instance)) {
273 self::$instance = new self();
274 }
275 return self::$instance;
276 }
277 }
278