PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
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.13, at includes/api.php

257 lines 6.9 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_count( '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 * Add Custom Mime Type JSON
208 * @since 1.0.0
209 */
210 public function add_custom_mime_type_json($mimes) {
211 $mimes['json'] = 'application/json';
212 // Return the array back to the function with our added mime type.
213 return $mimes;
214 }
215
216
217 /**
218 * Helper function to create Adding Route
219 * @since 1.0.0
220 */
221 private function add_route( $slug, $callBack, $method = 'GET' ) {
222 register_rest_route(
223 $this->token . '/v1',
224 $slug,
225 array(
226 'methods' => $method,
227 'callback' => array( $this, $callBack ),
228 'permission_callback' => array( $this, 'getPermission' ),
229 ) );
230 }
231
232 /**
233 * Permission Callback
234 **/
235 public function getPermission() {
236 if ( current_user_can( 'manage_options' ) ) {
237 return true;
238 } else {
239 return false;
240 }
241 }
242
243 /**
244 * Ensures only one instance of Class is loaded or can be loaded.
245 *
246 * @return Api Class instance
247 * @since 1.0.0
248 * @static
249 */
250 public static function instance(){
251 if (is_null(self::$instance)) {
252 self::$instance = new self();
253 }
254 return self::$instance;
255 }
256 }
257