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

263 lines 7.3 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( '/verifyBucket', 'verifyBucket', 'POST' );
35 $this->add_route( '/saveConfig', 'saveConfig', 'POST' );
36 $this->add_route( '/createBucket', 'createBucket', 'POST' );
37 $this->add_route( '/getSettings', 'getSettings' );
38 $this->add_route( '/uploadCredentials', 'uploadCredentials', 'POST' );
39 }
40
41 /**
42 * Verify the Service Credentials
43 */
44 public function verifyCredentials( $data ) {
45 global $wpmcsService;
46 return new WP_REST_Response( $wpmcsService->verifyCredentials($data->get_params()), 200 );
47 }
48
49 /**
50 * Verify the Bucket Credentials
51 */
52 public function verifyBucket( $data ) {
53 global $wpmcsService;
54 return new WP_REST_Response( $wpmcsService->verifyBucket($data->get_params()), 200 );
55 }
56
57 /**
58 * Create the bucket
59 */
60 public function createBucket( $data ) {
61 global $wpmcsService;
62 return new WP_REST_Response( $wpmcsService->createBucket($data->get_params()), 200 );
63 }
64
65 /**
66 * Get Settings
67 */
68 public function getSettings( ) {
69 $data = [
70 'credentials' => Utils::get_credentials(),
71 'common' => [
72 'version' => WPMCS_VERSION,
73 'uploaded' => Counter::get( 'uploaded' ),
74 'settings' => Utils::get_settings(),
75 ]
76 ];
77
78 return new WP_REST_Response( $data, 200 );
79 }
80
81 /**
82 * Save the Configurations
83 */
84 public function saveConfig( $data ) {
85 $saveData = $data->get_params();
86 $result = [
87 'success' => false,
88 'message' => esc_html__('Something went wrong. Invalid data recieved', 'media-cloud-sync')
89 ];
90
91 $action = isset($saveData['action']) ? $saveData['action'] : false;
92 if($action === false) return new WP_REST_Response( $result, 200 );
93
94 $service = isset($saveData['service']) ? $saveData['service'] : false;
95 $config = isset($saveData['config']) ? $saveData['config'] : false;
96 $bucketConfig = isset($saveData['bucketConfig']) ? $saveData['bucketConfig'] : [];
97 $settings = isset($saveData['settings']) ? $saveData['settings'] : [];
98
99 $serviceOk = true;
100 $settingsOk = true;
101 if($action === 'all' || $action === 'service'){
102 if ( $service === false || empty($config) || empty($bucketConfig)) return new WP_REST_Response( $result, 200 );
103
104 $updated = Utils::update_option(
105 'credentials',
106 [
107 'service' => $service,
108 'config' => $config,
109 'bucketConfig' => $bucketConfig
110 ],
111 Schema::getConstant('GLOBAL_SETTINGS_KEY')
112 );
113 if(!$updated) $serviceOk = false;
114 }
115
116 if(($action === 'all' || $action === 'settings')) {
117 $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
118 if(!$updatedSettings) $settingsOk = false;
119 }
120
121
122 if($serviceOk && $settingsOk) {
123 $result = [
124 'success' => true,
125 'message' => esc_html__('Configuration saved successfully', 'media-cloud-sync')
126 ];
127 }
128
129 return new WP_REST_Response( $result, 200 );
130 }
131
132
133 /**
134 * Upload Credentials
135 * @since 1.0.0
136 */
137 public function uploadCredentials($request) {
138 if (isset($_FILES['file']) && !empty($_FILES['file'])) {
139 $config_file = $_FILES['file'];
140 if (isset($config_file['type']) && $config_file['type'] == 'application/json') {
141 if (file_exists($config_file["tmp_name"])) {
142
143 add_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]);
144 add_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]);
145
146 if ( ! function_exists( 'wp_handle_upload' ) ) {
147 require_once( ABSPATH . 'wp-admin/includes/file.php' );
148 }
149
150 $upload_overrides = [
151 'test_form' => false,
152 'test_type' => true,
153 'mimes' => [ 'json'=>'application/json' ]
154 ];
155
156 $movefile = wp_handle_upload( $config_file, $upload_overrides );
157
158 remove_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]);
159 remove_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]);
160
161 if ($movefile && !isset($movefile['error'])) {
162 $result = [
163 'success' => true,
164 'file_path' => $movefile['file'],
165 'file_name' => basename($movefile['file']),
166 ];
167 return new WP_REST_Response($result, 200);
168 } else {
169 $result = [
170 'success' => false,
171 'message' => $movefile['error'],
172 ];
173 return new WP_REST_Response($result, 200);
174 }
175 }
176 } else {
177 $result = [
178 'success' => false,
179 'message' => esc_html__('Invalid file format', 'media-cloud-sync'),
180 ];
181 return new WP_REST_Response($result, 200);
182 }
183 } else {
184 $result = [
185 'success' => false,
186 'message' => esc_html__('Insufficient data. Please try again', 'media-cloud-sync'),
187 ];
188 return new WP_REST_Response($result, 200);
189 }
190 }
191
192 /**
193 * Change File Upload Directory
194 * @since 1.0.0
195 */
196 public function change_file_upload_dir($upload) {
197 $wpmcs_path = '/' . Schema::getConstant('UPLOADS');
198 $path = $upload['basedir'] . $wpmcs_path;
199 $url = $upload['baseurl'] . $wpmcs_path;
200
201 if (!is_dir($path)) {
202 do_action( $this->token.'_create_plugin_dir' );
203 }
204
205 $upload['subdir'] = $wpmcs_path;
206 $upload['path'] = $upload['basedir'] . $wpmcs_path;
207 $upload['url'] = $upload['baseurl'] . $wpmcs_path;
208
209 return $upload;
210 }
211
212 /**
213 * Add Custom Mime Type JSON
214 * @since 1.0.0
215 */
216 public function add_custom_mime_type_json($mimes) {
217 $mimes['json'] = 'application/json';
218 // Return the array back to the function with our added mime type.
219 return $mimes;
220 }
221
222
223 /**
224 * Helper function to create Adding Route
225 * @since 1.0.0
226 */
227 private function add_route( $slug, $callBack, $method = 'GET' ) {
228 register_rest_route(
229 $this->token . '/v1',
230 $slug,
231 array(
232 'methods' => $method,
233 'callback' => array( $this, $callBack ),
234 'permission_callback' => array( $this, 'getPermission' ),
235 ) );
236 }
237
238 /**
239 * Permission Callback
240 **/
241 public function getPermission() {
242 if ( current_user_can( 'administrator' ) ) {
243 return true;
244 } else {
245 return true;
246 }
247 }
248
249 /**
250 * Ensures only one instance of Class is loaded or can be loaded.
251 *
252 * @return Api Class instance
253 * @since 1.0.0
254 * @static
255 */
256 public static function instance(){
257 if (is_null(self::$instance)) {
258 self::$instance = new self();
259 }
260 return self::$instance;
261 }
262 }
263