-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin-functions.php
486 lines (360 loc) · 13.5 KB
/
admin-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
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
function PageCDN_is_backend( )
{
static $is_backend = null;
if( $is_backend === null )
{
$is_backend = is_admin( );
if( !$is_backend )
{
$is_backend = ( PHP_SAPI === 'cli' ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) || ( defined( 'WP_CLI' ) && WP_CLI );
if( !$is_backend )
{
$script = isset( $_SERVER['PHP_SELF'] ) ? basename( $_SERVER['PHP_SELF'] ) : '';
$is_backend = in_array( $script, array( 'xmlrpc.php', 'wp-cron.php' ) );
}
}
}
return $is_backend;
}
function PageCDN_add_action_link( $data )
{
if( !current_user_can( 'manage_options' ) )
{
return $data;
}
$settings_url = add_query_arg( array( 'page' => 'pagecdn' ) , admin_url('options-general.php') );
$settings_text = __("Settings");
if( PageCDN_private_cdn_enabled( ) )
{
return array_merge( array( "<a href=\"{$settings_url}\">{$settings_text}</a>" , "<span style=\"color:green;\" class=\"dashicons-before dashicons-yes\">Premium</span>" ) , $data );
}
else
{
//return array_merge( array( "<a href=\"{$settings_url}\">{$settings_text}</a>" , "<a href=\"https://pagecdn.com/signup\" target=\"_blank\" style=\"color:green;\">Optimize</a>" ) , $data );
return array_merge( array( "<a href=\"{$settings_url}\">{$settings_text}</a>" ) , $data );
}
}
function PageCDN_compat_check( )
{
global $wp_version;
if( version_compare( $wp_version , PAGECDN_MIN_WP , '<' ) )
{
show_message( '<div class="notice notice-error "><p>'. __("<strong>".PAGECDN_FULL_NAME."</strong> plugin works with WordPress ". PAGECDN_MIN_WP .". Please disable the plugin or upgrade your WordPress installation (recommended).", "pagecdn") .'</p></div>' );
}
//if( !PageCDN_private_cdn_enabled( ) )
//{
// show_message( '<div class="notice notice-warning is-dismissible"><p>Your website is not fully optimized. Please activate <a href="'.add_query_arg(array('page'=>'pagecdn'),admin_url('options-general.php')).'">Premium CDN</a> to get best performance for your website.</p></div>' );
//}
}
function PageCDN_admin_init( )
{
load_plugin_textdomain( 'pagecdn' , false , 'pagecdn/lang' );
register_setting( 'pagecdn' , 'pagecdn' , 'PageCDN_settings_validate' );
}
function PageCDN_admin_links( $wp_admin_bar )
{
if( !PageCDN_private_cdn_enabled( ) || !is_admin_bar_showing( ) || !apply_filters( 'user_can_clear_cache' , current_user_can( 'manage_options' ) ) )
{
return;
}
$current_url = ( isset( $_SERVER['HTTPS'] ) ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$goto_url = get_admin_url( );
if( stristr( $current_url , $goto_url ) )
{
$goto_url = $current_url;
}
// add admin purge link
$wp_admin_bar->add_menu(
array(
'id' => 'purge-pagecdn',
'href' => wp_nonce_url( add_query_arg('pagecdn_action', 'purge', $goto_url ) , '_cdn__purge_nonce' ),
'parent' => 'top-secondary',
'parent' => false,
'title' => '<span class="ab-item">'.esc_html__('Purge CDN', 'pagecdn').'</span>',
'meta' => array('title' => esc_html__('Purge CDN', 'pagecdn')),
)
);
}
function PageCDN_purge( $data = null )
{
if( !( isset( $_GET['pagecdn_action'] ) && $_GET['pagecdn_action'] === 'purge' ) )
{
return;
}
if( !PageCDN_private_cdn_enabled( ) )
{
return;
}
if( !( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'] , '_cdn__purge_nonce' ) ) )
{
return;
}
if( !is_admin_bar_showing( ) )
{
return;
}
// load if network
//if( !function_exists( 'is_plugin_active_for_network' ) )
//{
// require_once ABSPATH . 'wp-admin/includes/plugin.php';
//}
# Purge local cache
# Purge CDN Cache
# Deleting files internally triggers a purge operation
PageCDN_purge_local( );
//$empty_file = json_encode( array() );
//
//file_put_contents( PAGECDN_CACHE , $empty_file );
//
//file_put_contents( PAGECDN_IMG_CACHE , $empty_file );
//
//file_put_contents( PAGECDN_WEBP_CACHE , $empty_file );
$args = array();
$args['repo'] = trim( parse_url( PageCDN_options( 'url' ) , PHP_URL_PATH ) , '/' );
$args['apikey'] = PageCDN_options( 'pagecdn_api_key' );
if( $response = PageCDN_get_API_response( '/private/repo/delete-files' , $args ) )
{
if( !PageCDN_display_API_error( $response ) )
{
if( $response['status'] == 200 )
{
printf( '<div class="notice notice-success is-dismissible"><p class="dashicons-before dashicons-trash">%s</p></div>',
esc_html__('PageCDN cache purged. Changes may take 15 seconds to take effect.')
);
}
}
}
}
function PageCDN_purge_local( )
{
update_option( 'pagecdn-cache' , json_encode( array( ) ) , $autoload = false );
update_option( 'pagecdn-image-cache' , json_encode( array( ) ) , $autoload = false );
update_option( 'pagecdn-webp-cache' , json_encode( array( ) ) , $autoload = false );
}
# Settings
#-------------------------------------------
function PageCDN_settings_validate( $data )
{
Global $PageCDN_settings_error;
$PageCDN_settings_error = true;
$repo_created = false;
$data['relative'] = isset( $data['relative'] ) ? $data['relative'] : 1;
$data['https'] = isset( $data['https'] ) ? $data['https'] : 1;
$data['fonts'] = isset( $data['fonts'] ) ? $data['fonts'] : 0;
$data['replace_cdns'] = isset( $data['replace_cdns'] ) ? $data['replace_cdns'] : 0;
$data['reuse_libs'] = isset( $data['reuse_libs'] ) ? $data['reuse_libs'] : 0;
$data['pagecdn_api_key'] = isset( $data['pagecdn_api_key'] ) ? $data['pagecdn_api_key'] : '';
$data['url'] = isset( $data['url'] ) ? $data['url'] : '';
$data['pagecdn_api_key'] = trim( $data['pagecdn_api_key'] );
$data['url'] = trim( $data['url'] );
//Premium features
$data['optimize_images'] = isset( $data['optimize_images'] ) ? $data['optimize_images'] : 0;
$data['min_files'] = isset( $data['min_files'] ) ? $data['min_files'] : 0;
$data['preconnect_hosts'] = isset( $data['preconnect_hosts'] ) ? $data['preconnect_hosts'] : 0;
$data['preconnect_hosts_list'] = trim( $data['preconnect_hosts_list'] );
if( ( $data['url'] == '' ) && strlen( $data['pagecdn_api_key'] ) )
{
$post = array();
$post['apikey'] = $data['pagecdn_api_key'];
$post['repo_name'] = get_bloginfo( 'name' );
$post['origin_url'] = home_url();
$post['privacy'] = 'private';
$post['update_css_paths'] = '1';
if( $response = PageCDN_post_API_response( '/private/repo/create' , $post ) )
{
if( PageCDN_display_API_error( $response ) )
{
$data['pagecdn_api_key'] = '';
$data['url'] = '';
}
$response = $response['response'];
if( isset( $response['cdn_base'] ) && strlen( $response['cdn_base'] ) )
{
$data['url'] = $response['cdn_base'];
$repo_created = true;
PageCDN_purge_local( );
}
}
else
{
$data['pagecdn_api_key'] = '';
$data['url'] = '';
}
}
if( strlen( $data['url'] ) && !strlen( trim( $data['pagecdn_api_key'] ) ) )
{
# API Key removed by the user
# Reset some of the premium options
$data['pagecdn_api_key'] = '';
$data['url'] = '';
$data['optimize_images'] = 0;
$data['min_files'] = 0;
# Purge local cache to take effect of the change
PageCDN_purge_local( );
}
if( strlen( $data['url'] ) && strlen( $data['pagecdn_api_key'] ) )
{
if( !$repo_created )
{
# By default the cache control fields are no set. Sending this request without user's intent will cause
# bad user experience.
# Not sending this request will ensure that CDN's defaults are used unless user explicitly changes them.
//Update the repo
$post = array();
$post['compression_level'] = isset( $data['compression_level'] ) ? $data['compression_level'] : 'moderate';
$post['update_css_paths'] = isset( $data['update_css_paths'] ) ? $data['update_css_paths'] : 0;
$post['http_cache_ttl'] = isset( $data['http_cache_ttl'] ) ? $data['http_cache_ttl'] : 0;
$post['cache_control'] = isset( $data['cache_control'] ) ? $data['cache_control'] : 0;
if( $post['cache_control'] == 0 )
{
$post['http_cache_ttl'] = '0'; //Honor origin's headers
}
$post['apikey'] = $data['pagecdn_api_key'];
$post['repo'] = trim( parse_url( $data['url'] , PHP_URL_PATH ) , '/' );
if( $response = PageCDN_post_API_response( '/private/repo/configure' , $post ) )
{
PageCDN_display_API_error( $response );
}
}
}
$PageCDN_settings_error = false;
if( strlen( $data['preconnect_hosts_list'] ) )
{
$str = $data['preconnect_hosts_list'];
$str = str_replace( " " , "\n" , str_replace( "\r" , "\n" , str_replace( "\r\n" , "\n" , $str ) ) );
$str = implode( "\n" , array_filter( explode( "\n" , $str ) ) ) . "\n";
$data['preconnect_hosts_list'] = $str;
}
return array(
'url' => esc_url( rtrim( $data['url'] , '/' ) ) , //clean the URL before storing
'dirs' => $data['dirs'] ,
'excludes' => $data['excludes'] ,
'pagecdn_api_key' => $data['pagecdn_api_key'] ,
'fonts' => ( int ) $data['fonts'] ,
'replace_cdns' => ( int ) $data['replace_cdns'] ,
'reuse_libs' => ( int ) $data['reuse_libs'] ,
'relative' => ( int ) $data['relative'] ,
'https' => ( int ) $data['https'] ,
//Premium features
'optimize_images' => ( int ) $data['optimize_images'] ,
'min_files' => ( int ) $data['min_files'] ,
'preconnect_hosts' => ( int ) $data['preconnect_hosts'] ,
'preconnect_hosts_list' => $data['preconnect_hosts_list']
);
}
function PageCDN_settings_add_page( )
{
add_options_page( 'Easy Speedup' , 'Easy Speedup' , 'manage_options' , 'pagecdn' , 'PageCDN_settings_page' );
}
function PageCDN_get_API_response( $endpoint , $args )
{
$args = http_build_query( array_merge( $args , PageCDN_integration_info( ) ) );
$response = wp_remote_get( "https://pagecdn.com/api/v2{$endpoint}?{$args}" , array( 'timeout' => 30 ) );
if( is_wp_error( $response ) )
{
PageCDN_display_error( 'Error connecting to PageCDN API - '. $response->get_error_message( ) );
return false;
}
if( is_array( $response ) )
{
$rc = ( int ) wp_remote_retrieve_response_code( $response );
if( $rc == 200 )
{
return json_decode( $response['body'] , true );
}
else
{
PageCDN_display_error( 'PageCDN Error - Server returned error code ' . $rc . '.' );
}
}
return false;
}
function PageCDN_post_API_response( $endpoint , $data )
{
$data = array_merge( $data , PageCDN_integration_info( ) );
$response = wp_remote_post( "https://pagecdn.com/api/v2{$endpoint}" , array( 'timeout' => 20 , 'body' => $data ) );
if( is_wp_error( $response ) )
{
PageCDN_display_error( 'Error connecting to PageCDN API - '. $response->get_error_message( ) );
return false;
}
if( is_array( $response ) )
{
$rc = ( int ) wp_remote_retrieve_response_code( $response );
if( $rc == 200 )
{
return json_decode( $response['body'] , true );
}
else
{
PageCDN_display_error( 'PageCDN Error - Server returned error code ' . $rc . '.' );
}
}
}
function PageCDN_display_error( $message )
{
Global $PageCDN_settings_error;
if( $PageCDN_settings_error )
{
add_settings_error( 'pagecdn_settings' , esc_attr( 'pagecdn-settings-error' ) , $message , 'error' );
}
else
{
$error_HTML = '<div class="notice notice-error is-dismissible"><p>%s</p></div>';
printf( $error_HTML , $message );
}
}
function PageCDN_display_API_error( $response )
{
if( is_array( $response ) && ( $response['status'] != 200 ) )
{
PageCDN_display_error( "PageCDN Error - {$response['message']}: {$response['details']} Code: {$response['status']}" );
return true;
}
return false;
}
function PageCDN_settings_page( )
{
Global $PageCDN_theme_code;
$checked_if_premium = '';
$disabled_if_not_premium = 'disabled';
if( PageCDN_private_cdn_enabled( ) )
{
$checked_if_premium = 'checked';
$disabled_if_not_premium = '';
}
PageCDN_collect_repo_data();
$options = PageCDN_options();
require PAGECDN_DIR . '/settings.php';
}
function PageCDN_integration_info( )
{
Global $wp_version;
$tool['integration_name'] = 'easy-speedup';
$tool['integration_version'] = PAGECDN_VER;
$tool['integration_cms_name'] = 'wordpress';
$tool['integration_cms_version'] = $wp_version;
return $tool;
}
function PageCDN_collect_repo_data( )
{
Global $PageCDN_options;
if( strlen( $PageCDN_options['pagecdn_api_key'] ) && strlen( $PageCDN_options['url'] ) )
{
$repo = trim( parse_url( $PageCDN_options['url'] , PHP_URL_PATH ) , '/' );
$apikey = $PageCDN_options['pagecdn_api_key'];
if( $response = PageCDN_get_API_response( '/private/repo/info' , array( 'apikey' => $apikey , 'repo' => $repo ) ) )
{
if( !PageCDN_display_API_error( $response ) )
{
$response = $response['response'];
$PageCDN_options['compression_level'] = $response['compression_level'];
$PageCDN_options['http2_server_push'] = $response['server_push'] && $response['server_push_trigger'];
$PageCDN_options['update_css_paths'] = $response['update_css_paths'];
$PageCDN_options['http_cache_ttl'] = strtotime( "+{$response['browser_cache_number']} {$response['browser_cache_period']}" , 0 );
$PageCDN_options['cache_control'] = !!$PageCDN_options['http_cache_ttl'];
}
}
}
}