forked from froger-me/wp-packages-update-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
473 lines (368 loc) · 13.6 KB
/
functions.php
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! function_exists( 'php_log' ) ) {
function php_log( $message = '', $prefix = '' ) {
$prefix = $prefix ? ' ' . $prefix . ' => ' : ' => ';
$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
$caller = end( $trace );
$class = isset( $caller['class'] ) ? $caller['class'] : '';
$type = isset( $caller['type'] ) ? $caller['type'] : '';
$function = isset( $caller['function'] ) ? $caller['function'] : '';
$context = $class . $type . $function . $prefix;
error_log( $context . print_r( $message, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r, WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
}
if ( ! function_exists( 'cidr_match' ) ) {
function cidr_match( $ip, $range ) {
list ( $subnet, $bits ) = explode( '/', $range );
$ip = ip2long( $ip );
$subnet = ip2long( $subnet );
if ( ! $ip || ! $subnet || ! $bits ) {
return false;
}
$mask = -1 << ( 32 - $bits );
$subnet &= $mask; // in case the supplied subnet was not correctly aligned
return ( $ip & $mask ) === $subnet;
}
}
if ( ! function_exists( 'wppus_assets_suffix' ) ) {
function wppus_assets_suffix() {
return (bool) ( constant( 'WP_DEBUG' ) ) ? '' : '.min';
}
}
if ( ! function_exists( 'wppus_is_doing_license_api_request' ) ) {
function wppus_is_doing_license_api_request() {
return WPPUS_License_API::is_doing_api_request();
}
}
if ( ! function_exists( 'wppus_is_doing_update_api_request' ) ) {
function wppus_is_doing_update_api_request() {
return WPPUS_Update_API::is_doing_api_request();
}
}
if ( ! function_exists( 'wppus_is_doing_webhook_api_request' ) ) {
function wppus_is_doing_webhook_api_request() {
return WPPUS_Webhook_API::is_doing_api_request();
}
}
if ( ! function_exists( 'wppus_is_doing_package_api_request' ) ) {
function wppus_is_doing_package_api_request() {
return WPPUS_Package_API::is_doing_api_request();
}
}
if ( ! function_exists( 'wppus_is_doing_api_request' ) ) {
function wppus_is_doing_api_request() {
$is_api_request = (
wppus_is_doing_license_api_request() ||
wppus_is_doing_update_api_request() ||
wppus_is_doing_webhook_api_request() ||
wppus_is_doing_package_api_request()
);
return apply_filters( 'wppus_is_api_request', $is_api_request );
}
}
if ( ! function_exists( 'wppus_get_root_data_dir' ) ) {
function wppus_get_root_data_dir() {
return WPPUS_Data_Manager::get_data_dir();
}
}
if ( ! function_exists( 'wppus_get_packages_data_dir' ) ) {
function wppus_get_packages_data_dir() {
return WPPUS_Data_Manager::get_data_dir( 'packages' );
}
}
if ( ! function_exists( 'wppus_get_logs_data_dir' ) ) {
function wppus_get_logs_data_dir() {
return WPPUS_Data_Manager::get_data_dir( 'logs' );
}
}
if ( ! function_exists( 'wppus_force_cleanup_cache' ) ) {
function wppus_force_cleanup_cache() {
return WPPUS_Data_Manager::maybe_cleanup( 'cache', true );
}
}
if ( ! function_exists( 'wppus_force_cleanup_logs' ) ) {
function wppus_force_cleanup_logs() {
return WPPUS_Data_Manager::maybe_cleanup( 'logs', true );
}
}
if ( ! function_exists( 'wppus_force_cleanup_tmp' ) ) {
function wppus_force_cleanup_tmp() {
return WPPUS_Data_Manager::maybe_cleanup( 'tmp', true );
}
}
if ( ! function_exists( 'wppus_check_remote_plugin_update' ) ) {
function wppus_check_remote_plugin_update( $slug ) {
return wppus_check_remote_package_update( $slug, 'plugin' );
}
}
if ( ! function_exists( 'wppus_check_remote_theme_update' ) ) {
function wppus_check_remote_theme_update( $slug ) {
return wppus_check_remote_package_update( $slug, 'theme' );
}
}
if ( ! function_exists( 'wppus_check_remote_package_update' ) ) {
function wppus_check_remote_package_update( $slug, $type ) {
$api = WPPUS_Update_API::get_instance();
return $api->check_remote_update( $slug, $type );
}
}
if ( ! function_exists( 'wppus_download_remote_plugin' ) ) {
function wppus_download_remote_plugin( $slug ) {
return wppus_download_remote_package( $slug, 'plugin' );
}
}
if ( ! function_exists( 'wppus_download_remote_theme' ) ) {
function wppus_download_remote_theme( $slug ) {
return wppus_download_remote_package( $slug, 'theme' );
}
}
if ( ! function_exists( 'wppus_download_remote_package' ) ) {
function wppus_download_remote_package( $slug, $type ) {
$api = WPPUS_Update_API::get_instance();
return $api->download_remote_package( $slug, $type, true );
}
}
if ( ! function_exists( 'wppus_delete_package' ) ) {
function wppus_delete_package( $slug ) {
$api = WPPUS_Package_Manager::get_instance();
return (bool) $api->delete_packages_bulk( array( $slug ) );
}
}
if ( ! function_exists( 'wppus_get_package_info' ) ) {
function wppus_get_package_info( $package_slug, $json_encode = true ) {
require_once WPPUS_PLUGIN_PATH . 'inc/class-wppus-package-manager.php';
$result = $json_encode ? '{}' : array();
$package_manager = new WPPUS_Package_Manager();
$package_info = $package_manager->get_package_info( $package_slug );
if ( $package_info ) {
$result = $json_encode ? wp_json_encode( $package_info, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) : $package_info;
}
return $result;
}
}
if ( ! function_exists( 'wppus_get_batch_package_info' ) ) {
function wppus_get_batch_package_info( $search, $json_encode = true ) {
require_once WPPUS_PLUGIN_PATH . 'inc/class-wppus-package-manager.php';
$result = $json_encode ? '{}' : array();
$package_manager = new WPPUS_Package_Manager();
$package_info = $package_manager->get_batch_package_info( $search );
if ( $package_info ) {
$result = $json_encode ? wp_json_encode( $package_info, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ) : $package_info;
}
return $result;
}
}
if ( ! function_exists( 'wppus_download_local_package' ) ) {
function wppus_download_local_package( $package_slug, $package_path = null, $exit_or_die = true ) {
require_once WPPUS_PLUGIN_PATH . 'inc/class-wppus-package-manager.php';
$package_manager = new WPPUS_Package_Manager();
if ( null === $package_path ) {
$package_path = wppus_get_local_package_path( $package_slug );
}
$package_manager->trigger_packages_download( $package_slug, $package_path, $exit_or_die );
}
}
if ( ! function_exists( 'wppus_get_local_package_path' ) ) {
function wppus_get_local_package_path( $package_slug ) {
WP_Filesystem();
global $wp_filesystem;
if ( ! $wp_filesystem ) {
wp_die( __FUNCTION__ . ' - WP_Filesystem not available.' );
}
$package_path = trailingslashit( WPPUS_Data_Manager::get_data_dir( 'packages' ) ) . $package_slug . '.zip';
if ( $wp_filesystem->is_file( $package_path ) ) {
return $package_path;
}
return false;
}
}
if ( ! function_exists( 'wppus_browse_licenses' ) ) {
function wppus_browse_licenses( $browse_query ) {
$api = WPPUS_License_API::get_instance();
return $api->browse( $browse_query );
}
}
if ( ! function_exists( 'wppus_read_license' ) ) {
function wppus_read_license( $license_data ) {
$api = WPPUS_License_API::get_instance();
return $api->read( $license_data );
}
}
if ( ! function_exists( 'wppus_add_license' ) ) {
function wppus_add_license( $license_data ) {
if ( is_array( $license_data ) && ! isset( $license_data['data'] ) ) {
$license_data['data'] = array();
}
$license_data['data']['operation_timestamp'] = time();
$license_data['data']['operation'] = 'add';
$license_data['data']['operation_id'] = bin2hex( random_bytes( 16 ) );
$api = WPPUS_License_API::get_instance();
return $api->add( $license_data );
}
}
if ( ! function_exists( 'wppus_edit_license' ) ) {
function wppus_edit_license( $license_data ) {
if ( is_array( $license_data ) && ! isset( $license_data['data'] ) ) {
$license_data['data'] = array();
}
$license_data['data']['operation_timestamp'] = time();
$license_data['data']['operation'] = 'edit';
$license_data['data']['operation_id'] = bin2hex( random_bytes( 16 ) );
$api = WPPUS_License_API::get_instance();
return $api->edit( $license_data );
}
}
if ( ! function_exists( 'wppus_delete_license' ) ) {
function wppus_delete_license( $license_data ) {
if ( is_array( $license_data ) && ! isset( $license_data['data'] ) ) {
$license_data['data'] = array();
}
$license_data['data']['operation_timestamp'] = time();
$license_data['data']['operation'] = 'delete';
$license_data['data']['operation_id'] = bin2hex( random_bytes( 16 ) );
$api = WPPUS_License_API::get_instance();
return $api->delete( $license_data );
}
}
if ( ! function_exists( 'wppus_check_license' ) ) {
function wppus_check_license( $license_data ) {
$api = WPPUS_License_API::get_instance();
return $api->check( $license_data );
}
}
if ( ! function_exists( 'wppus_activate_license' ) ) {
function wppus_activate_license( $license_data ) {
$api = WPPUS_License_API::get_instance();
return $api->activate( $license_data );
}
}
if ( ! function_exists( 'wppus_deactivate_license' ) ) {
function wppus_deactivate_license( $license_data ) {
$api = WPPUS_License_API::get_instance();
return $api->deactivate( $license_data );
}
}
if ( ! function_exists( 'wppus_get_template' ) ) {
function wppus_get_template( $template_name, $args = array(), $load = true, $require_file = false ) {
$template_name = apply_filters( 'wppus_get_template_name', $template_name, $args );
$template_args = apply_filters( 'wppus_get_template_args', $args, $template_name );
if ( ! empty( $template_args ) ) {
foreach ( $template_args as $key => $arg ) {
$key = is_numeric( $key ) ? 'var_' . $key : $key;
set_query_var( $key, $arg );
}
}
return WP_Packages_Update_Server::locate_template( $template_name, $load, $require_file );
}
}
if ( ! function_exists( 'wppus_get_admin_template' ) ) {
function wppus_get_admin_template( $template_name, $args = array(), $load = true, $require_file = false ) {
$template_name = apply_filters( 'wppus_get_admin_template_name', $template_name, $args );
$template_args = apply_filters( 'wppus_get_admin_template_args', $args, $template_name );
if ( ! empty( $template_args ) ) {
foreach ( $template_args as $key => $arg ) {
$key = is_numeric( $key ) ? 'var_' . $key : $key;
set_query_var( $key, $arg );
}
}
return WP_Packages_Update_Server::locate_admin_template( $template_name, $load, $require_file );
}
}
if ( ! function_exists( 'wppus_init_nonce_auth' ) ) {
function wppus_init_nonce_auth( $private_auth_key ) {
require_once WPPUS_PLUGIN_PATH . 'inc/class-wppus-nonce.php';
WPPUS_Nonce::init_auth( $private_auth_key );
}
}
if ( ! function_exists( 'wppus_create_nonce' ) ) {
function wppus_create_nonce(
$true_nonce = true,
$expiry_length = WPPUS_Nonce::DEFAULT_EXPIRY_LENGTH,
$data = array(),
$return_type = WPPUS_Nonce::NONCE_ONLY,
$store = true
) {
require_once WPPUS_PLUGIN_PATH . 'inc/class-wppus-nonce.php';
return WPPUS_Nonce::create_nonce( $true_nonce, $expiry_length, $data, $return_type, $store );
}
}
if ( ! function_exists( 'wppus_get_nonce_expiry' ) ) {
function wppus_get_nonce_expiry( $nonce ) {
return WPPUS_Nonce::get_nonce_expiry( $nonce );
}
}
if ( ! function_exists( 'wppus_get_nonce_data' ) ) {
function wppus_get_nonce_data( $nonce ) {
return WPPUS_Nonce::get_nonce_data( $nonce );
}
}
if ( ! function_exists( 'wppus_validate_nonce' ) ) {
function wppus_validate_nonce( $value ) {
return WPPUS_Nonce::validate_nonce( $value );
}
}
if ( ! function_exists( 'wppus_delete_nonce' ) ) {
function wppus_delete_nonce( $value ) {
return WPPUS_Nonce::delete_nonce( $value );
}
}
if ( ! function_exists( 'wppus_clear_nonces' ) ) {
function wppus_clear_nonces() {
return WPPUS_Nonce::wppus_nonce_cleanup();
}
}
if ( ! function_exists( 'wppus_build_nonce_api_signature' ) ) {
function wppus_build_nonce_api_signature( $api_key_id, $api_key, $timestamp, $payload ) {
unset( $payload['api_signature'] );
unset( $payload['api_credentials'] );
( function ( &$arr ) {
$recur_ksort = function ( &$arr ) use ( &$recur_ksort ) {
foreach ( $arr as &$value ) {
if ( is_array( $value ) ) {
$recur_ksort( $value );
}
}
ksort( $arr );
};
$recur_ksort( $arr );
} )( $payload );
$str = base64_encode( $api_key_id . json_encode( $payload, JSON_NUMERIC_CHECK ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode, WordPress.WP.AlternativeFunctions.json_encode_json_encode
$credentials = $timestamp . '/' . $api_key_id;
$time_key = hash_hmac( 'sha256', $timestamp, $api_key, true );
$signature = hash_hmac( 'sha256', $str, $time_key );
return array(
'credentials' => $credentials,
'signature' => $signature,
);
}
}
if ( ! function_exists( 'wppus_schedule_webhook' ) ) {
function wppus_schedule_webhook( $payload, $event_type ) {
if ( isset( $payload['event'], $payload['content'] ) ) {
$api = WPPUS_Webhook_API::get_instance();
return $api->schedule_webhook( $payload, $event_type );
}
return new WP_Error(
__FUNCTION__,
__( 'The webhook payload must contain an event string and a content.', 'wppus' )
);
}
}
if ( ! function_exists( 'wppus_fire_webhook' ) ) {
function wppus_fire_webhook( $url, $secret, $body, $action ) {
if (
filter_var( $url, FILTER_VALIDATE_URL ) &&
null !== json_decode( $body )
) {
$api = WPPUS_Webhook_API::get_instance();
return $api->fire_webhook( $url, $secret, $body, $action );
}
return new WP_Error(
__FUNCTION__,
__( '$url must be a valid url and $body must be a JSON string.', 'wppus' )
);
}
}