-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathclass-connect.php
More file actions
350 lines (306 loc) · 9.65 KB
/
class-connect.php
File metadata and controls
350 lines (306 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* Connect class for Cloudinary.
*
* @package Cloudinary
*/
namespace Cloudinary;
use Cloudinary\Component\Config;
use Cloudinary\Component\Notice;
use Cloudinary\Component\Setup;
use Cloudinary\Connect\Api;
/**
* Cloudinary connection class.
*
* Sets up the initial cloudinary connection and makes the API object available for some uses.
*/
class Connect implements Config, Setup, Notice {
/**
* Holds the plugin instance.
*
* @since 0.1
*
* @var Plugin Instance of the global plugin.
*/
private $plugin;
/**
* Holds the cloudinary API instance
*
* @since 0.1
*
* @var \Cloudinary\Connect\Api
*/
public $api;
/**
* Holds the cloudinary usage info.
*
* @since 0.1
*
* @var array
*/
public $usage;
/**
* Holds the cloudinary credentials.
*
* @since 0.1
*
* @var array
*/
private $credentials = array();
/**
* Holds the handle for the media page.
*
* @var string
*/
public $handle;
/**
* Initiate the plugin resources.
*
* @param \Cloudinary\Plugin $plugin Instance of the plugin.
*/
public function __construct( Plugin $plugin ) {
$this->plugin = $plugin;
add_filter( 'pre_update_option_cloudinary_connect', array( $this, 'verify_connection' ) );
}
/**
* Add the Cloudinary media library scripts.
*/
public function media_library_script() {
$screen = get_current_screen();
if ( is_object( $screen ) && $screen->id === $this->handle ) {
// External assets.
wp_enqueue_script( 'cloudinary-media-library', 'https://media-library.cloudinary.com/global/all.js', array(), $this->plugin->version, true );
$params = array(
'nonce' => wp_create_nonce( 'wp_rest' ),
'mloptions' => array(
'cloud_name' => $this->credentials['cloud_name'],
'api_key' => $this->credentials['api_key'],
'remove_header' => true,
),
);
// sign maybe.
if ( ! empty( $this->credentials['user_email'] ) ) {
$timestamp = current_time( 'timestamp' );
$params['mloptions']['username'] = $this->credentials['user_email'];
$params['mloptions']['timestamp'] = (string) $timestamp;
$query = array(
'cloud_name' => $this->credentials['cloud_name'],
'timestamp' => $timestamp,
'username' => $this->credentials['user_email'] . $this->credentials['api_secret'],
);
$params['mloptions']['signature'] = hash( 'sha256', build_query( $query ) );
}
$params['mloptions']['insert_transformation'] = true;
$params['mloptions']['inline_container'] = '#cloudinary-embed';
wp_add_inline_script( 'cloudinary-media-library', 'var CLD_ML = ' . wp_json_encode( $params ), 'before' );
}
}
/**
* Verify that the connection details are correct.
*
* @param array $data The submitted data to verify.
*
* @return array The data if cleared.
*/
public function verify_connection( $data ) {
if ( empty( $data['cloudinary_url'] ) ) {
delete_option( 'cloudinary_connection_signature' );
add_settings_error( 'cloudinary_connect', 'connection_error', __( 'Connection to Cloudinary has been removed.', 'cloudinary' ), 'notice-warning' );
return $data;
}
$current = $this->plugin->config['settings']['connect'];
if ( $current['cloudinary_url'] === $data['cloudinary_url'] ) {
return $data;
}
if ( ! preg_match( '~^cloudinary://~', $data['cloudinary_url'] ) ) {
add_settings_error( 'cloudinary_connect', 'format_mismatch', __( 'The Environment variable URL must be in this format: cloudinary://API_Key:API_Secret@Cloud_Name', 'cloudinary' ), 'error' );
return $current;
}
$result = $this->test_connection( $data['cloudinary_url'] );
if ( ! empty( $result['message'] ) ) {
add_settings_error( 'cloudinary_connect', $result['type'], $result['message'], 'error' );
return $current;
}
add_settings_error( 'cloudinary_connect', 'connection_success', __( 'Successfully connected to Cloudinary.', 'cloudinary' ), 'updated' );
update_option( 'cloudinary_connection_signature', md5( $data['cloudinary_url'] ) );
return $data;
}
/**
* Test the connection url.
*
* @param string $url The url to test.
*
* @return mixed
*/
public function test_connection( $url ) {
$result = array(
'type' => 'connection_success',
'message' => null,
);
$test = wp_parse_url( $url );
$valid = array_filter(
array_keys( $test ),
function ( $a ) {
return in_array( $a, [ 'scheme', 'host', 'user', 'pass' ], true );
}
);
if ( 4 > count( $valid ) ) {
$result['type'] = 'invalid_url';
$result['message'] = sprintf(
// translators: Placeholder refers to the expected URL format.
__( 'Incorrect Format. Expecting: %s', 'cloudinary' ),
'<code>cloudinary://API_Key:API_Secret@Cloud_Name</code>'
);
return $result;
}
// Test if has a cname and is valid.
if ( ! empty( $test['query'] ) ) {
$config_params = array();
wp_parse_str( $test['query'], $config_params );
if ( ! empty( $config_params['cname'] ) ) {
if ( defined( 'FILTER_VALIDATE_DOMAIN' ) ) {
$is_valid = filter_var( $config_params['cname'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME );
} else {
$cname = 'https://' . $config_params['cname'];
$is_valid = filter_var( $cname, FILTER_VALIDATE_URL );
}
if ( ! substr_count( $is_valid, '.' ) || false === $is_valid ) {
$result['type'] = 'invalid_cname';
$result['message'] = __( 'CNAME is not a valid domain name.', 'cloudinary' );
return $result;
}
}
}
$this->config_from_url( $url );
$test = new Connect\Api( $this, $this->plugin->version );
$test = $test->ping();
if ( is_wp_error( $test ) ) {
$result['type'] = 'connection_error';
$result['message'] = ucwords( str_replace( '_', ' ', $test->get_error_message() ) );
}
return $result;
}
/**
* Get the Cloudinary credentials.
*
* @return array
*/
public function get_credentials() {
return $this->credentials;
}
/**
* Set the config credentials from an array.
*
* @param array $data The config array data.
*
* @return array
*/
public function set_credentials( $data = array() ) {
$this->credentials = array_merge( $this->credentials, $data );
return $this->credentials;
}
/**
* Set the credentials from the cloudinary url.
*
* @param string $url The Cloudinary URL.
*/
public function config_from_url( $url ) {
$parts = wp_parse_url( $url );
$creds = array();
foreach ( $parts as $type => $part ) {
switch ( $type ) {
case 'host':
$creds['cloud_name'] = $part;
break;
case 'user':
$creds['api_key'] = $part;
break;
case 'pass':
$creds['api_secret'] = $part;
break;
}
}
$this->set_credentials( $creds );
// Check for and Append query params.
if ( ! empty( $parts['query'] ) ) {
$config_params = array();
wp_parse_str( $parts['query'], $config_params );
if ( ! empty( $config_params ) ) {
$this->set_credentials( $config_params );
}
}
}
/**
* Setup connection
*
* @since 0.1
*/
public function setup() {
// Get the cloudinary url from plugin config.
$config = $this->plugin->config['settings']['connect'];
if ( ! empty( $config['cloudinary_url'] ) ) {
$this->config_from_url( $config['cloudinary_url'] );
$this->api = new Connect\Api( $this, $this->plugin->version );
$stats = get_transient( '_cloudinary_usage' );
if ( empty( $stats ) ) {
// Get users plan.
$stats = $this->plugin->components['connect']->api->usage();
if ( ! is_wp_error( $stats ) && ! empty( $stats['media_limits'] ) ) {
$stats['max_image_size'] = $stats['media_limits']['image_max_size_bytes'];
$stats['max_video_size'] = $stats['media_limits']['video_max_size_bytes'];
set_transient( '_cloudinary_usage', $stats, HOUR_IN_SECONDS );
}
}
$this->usage = $stats;
}
}
/**
* Gets the config of a connection.
*
* @since 0.1
*
* @return array The array of the config options stored.
*/
public function get_config() {
$signature = get_option( 'cloudinary_connection_signature', null );
if ( empty( $signature ) ) {
// Check if theres a previous version.
$version = get_option( 'cloudinary_version' );
if ( version_compare( $this->plugin->version, $version, '>' ) ) {
$data = array(
'cloudinary_url' => get_option( 'cloudinary_url' ),
);
$test = $this->test_connection( $data['cloudinary_url'] );
if ( 'connection_success' === $test['type'] ) {
$signature = md5( $data['cloudinary_url'] );
// remove filters as we've already verified it and 'add_settings_error()' isin't available yet.
remove_filter( 'pre_update_option_cloudinary_connect', array( $this, 'verify_connection' ) );
update_option( 'cloudinary_connect', $data );
update_option( 'cloudinary_connection_signature', $signature );
update_option( 'cloudinary_version', $this->plugin->version );
delete_option( 'cloudinary_settings_cache' ); // remove the cache.
$this->plugin->config['settings']['connect'] = $data; // Set the connection url for this round.
}
}
}
return $signature;
}
/**
* Get admin notices.
*/
public function get_notices() {
$screen = get_current_screen();
$notices = array();
if ( empty( $this->plugin->config['connect'] ) ) {
if ( is_object( $screen ) && in_array( $screen->id, $this->plugin->components['settings']->handles, true ) ) {
$link = '<a href="' . esc_url( admin_url( 'admin.php?page=cld_connect' ) ) . '">' . __( 'Connect', 'cloudinary' ) . '</a> ';
$notices[] = array(
'message' => $link . __( 'your Cloudinary account with WordPress to get started.', 'cloudinary' ),
'type' => 'error',
'dismissible' => true,
);
}
}
return $notices;
}
}