CDN Enabler – WordPress CDN Plugin - Version 1.0.8

Version Description

  • Purge CDN redirects to admin dashboard to avoid error messages
  • more helpful error messages
  • do not display nag notice when KeyCDN API credentials are set
Download this release

Release Info

Developer keycdn
Plugin Icon 128x128 CDN Enabler – WordPress CDN Plugin
Version 1.0.8
Comparing to
See all releases

Code changes from version 1.0.7 to 1.0.8

cdn-enabler.php CHANGED
@@ -6,7 +6,7 @@
6
  Author: KeyCDN
7
  Author URI: https://www.keycdn.com
8
  License: GPLv2 or later
9
- Version: 1.0.7
10
  */
11
 
12
  /*
6
  Author: KeyCDN
7
  Author URI: https://www.keycdn.com
8
  License: GPLv2 or later
9
+ Version: 1.0.8
10
  */
11
 
12
  /*
inc/cdn_enabler.class.php CHANGED
@@ -89,7 +89,7 @@ class CDN_Enabler
89
  );
90
  /* process purge request */
91
  add_action(
92
- 'init',
93
  [
94
  __CLASS__,
95
  'process_purge_request',
@@ -110,6 +110,7 @@ class CDN_Enabler
110
  */
111
 
112
  public static function add_admin_links($wp_admin_bar) {
 
113
  $options = self::get_options();
114
 
115
  // check user role
@@ -127,11 +128,20 @@ class CDN_Enabler
127
  return;
128
  }
129
 
 
 
 
 
 
 
 
 
 
130
  // add admin purge link
131
  $wp_admin_bar->add_menu(
132
  [
133
  'id' => 'purge-cdn',
134
- 'href' => wp_nonce_url( add_query_arg('_cdn', 'purge'), '_cdn__purge_nonce'),
135
  'parent' => 'top-secondary',
136
  'title' => '<span class="ab-item">'.esc_html__('Purge CDN', 'cdn-enabler').'</span>',
137
  'meta' => ['title' => esc_html__('Purge CDN', 'cdn-enabler')],
@@ -143,7 +153,7 @@ class CDN_Enabler
143
  $wp_admin_bar->add_menu(
144
  [
145
  'id' => 'purge-cdn',
146
- 'href' => wp_nonce_url( add_query_arg('_cdn', 'purge'), '_cdn__purge_nonce'),
147
  'parent' => 'top-secondary',
148
  'title' => '<span class="ab-item">'.esc_html__('Purge CDN', 'cdn-enabler').'</span>',
149
  'meta' => ['title' => esc_html__('Purge CDN', 'cdn-enabler')],
@@ -207,9 +217,10 @@ class CDN_Enabler
207
  // check HTTP response
208
  if ( is_array( $response ) and is_admin_bar_showing()) {
209
  $json = json_decode($response['body'], true);
 
210
 
211
  // success
212
- if ( wp_remote_retrieve_response_code( $response ) == 200
213
  and is_array($json)
214
  and array_key_exists('description', $json) )
215
  {
@@ -219,7 +230,7 @@ class CDN_Enabler
219
  );
220
 
221
  return;
222
- } elseif ( wp_remote_retrieve_response_code( $response ) == 200 ) {
223
  // return code 200 but no message
224
  printf(
225
  '<div class="notice notice-warning is-dismissible"><p>%s</p></div>',
@@ -229,19 +240,35 @@ class CDN_Enabler
229
  return;
230
  }
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  // API call returned != 200 and also a status message
233
  if ( is_array($json)
234
  and array_key_exists('status', $json)
235
  and $json['status'] != "" ) {
236
  printf(
237
  '<div class="notice notice-error is-dismissible"><p>%s</p></div>',
238
- esc_html__('HTTP returned '. wp_remote_retrieve_response_code( $response ) .': '.$json['description'], 'cdn-enabler')
239
  );
240
  } else {
241
  // Something else went wrong - show HTTP error code
242
  printf(
243
  '<div class="notice notice-error is-dismissible"><p>%s</p></div>',
244
- esc_html__('HTTP returned '. wp_remote_retrieve_response_code( $response ))
245
  );
246
  }
247
  }
89
  );
90
  /* process purge request */
91
  add_action(
92
+ 'admin_notices',
93
  [
94
  __CLASS__,
95
  'process_purge_request',
110
  */
111
 
112
  public static function add_admin_links($wp_admin_bar) {
113
+ global $wp;
114
  $options = self::get_options();
115
 
116
  // check user role
128
  return;
129
  }
130
 
131
+ // redirect to admin page if necessary so we can display notification
132
+ $current_url = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' .
133
+ $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
134
+ $goto_url = get_admin_url();
135
+
136
+ if ( stristr($current_url, get_admin_url()) ) {
137
+ $goto_url = $current_url;
138
+ }
139
+
140
  // add admin purge link
141
  $wp_admin_bar->add_menu(
142
  [
143
  'id' => 'purge-cdn',
144
+ 'href' => wp_nonce_url( add_query_arg('_cdn', 'purge', $goto_url), '_cdn__purge_nonce'),
145
  'parent' => 'top-secondary',
146
  'title' => '<span class="ab-item">'.esc_html__('Purge CDN', 'cdn-enabler').'</span>',
147
  'meta' => ['title' => esc_html__('Purge CDN', 'cdn-enabler')],
153
  $wp_admin_bar->add_menu(
154
  [
155
  'id' => 'purge-cdn',
156
+ 'href' => wp_nonce_url( add_query_arg('_cdn', 'purge', $goto_url), '_cdn__purge_nonce'),
157
  'parent' => 'top-secondary',
158
  'title' => '<span class="ab-item">'.esc_html__('Purge CDN', 'cdn-enabler').'</span>',
159
  'meta' => ['title' => esc_html__('Purge CDN', 'cdn-enabler')],
217
  // check HTTP response
218
  if ( is_array( $response ) and is_admin_bar_showing()) {
219
  $json = json_decode($response['body'], true);
220
+ $rc = wp_remote_retrieve_response_code( $response );
221
 
222
  // success
223
+ if ( $rc == 200
224
  and is_array($json)
225
  and array_key_exists('description', $json) )
226
  {
230
  );
231
 
232
  return;
233
+ } elseif ( $rc == 200 ) {
234
  // return code 200 but no message
235
  printf(
236
  '<div class="notice notice-warning is-dismissible"><p>%s</p></div>',
240
  return;
241
  }
242
 
243
+ // For some API errors we return custom error messages
244
+ $custom_messages = array(
245
+ 401 => "invalid API key",
246
+ 403 => "invalid zone id",
247
+ 451 => "too many failed attempts",
248
+ );
249
+
250
+ if ( array_key_exists($rc, $custom_messages) ) {
251
+ printf(
252
+ '<div class="notice notice-error is-dismissible"><p>%s</p></div>',
253
+ esc_html__('HTTP returned '. $rc .': '.$custom_messages[$rc], 'cdn-enabler')
254
+ );
255
+
256
+ return;
257
+ }
258
+
259
  // API call returned != 200 and also a status message
260
  if ( is_array($json)
261
  and array_key_exists('status', $json)
262
  and $json['status'] != "" ) {
263
  printf(
264
  '<div class="notice notice-error is-dismissible"><p>%s</p></div>',
265
+ esc_html__('HTTP returned '. $rc .': '.$json['description'], 'cdn-enabler')
266
  );
267
  } else {
268
  // Something else went wrong - show HTTP error code
269
  printf(
270
  '<div class="notice notice-error is-dismissible"><p>%s</p></div>',
271
+ esc_html__('HTTP returned '. $rc)
272
  );
273
  }
274
  }
inc/cdn_enabler_settings.class.php CHANGED
@@ -99,21 +99,33 @@ class CDN_Enabler_Settings
99
  */
100
 
101
  public static function settings_page()
102
- { ?>
 
 
 
 
103
  <div class="wrap">
104
  <h2>
105
  <?php _e("CDN Enabler Settings", "cdn-enabler"); ?>
106
  </h2>
107
 
 
 
 
 
 
 
 
 
108
  <div class="notice notice-info">
109
- <p><?php printf( __('Combine CDN Enabler with <b><a href="%s">%s</a></b> for even faster WordPress performance.', 'cdn-enabler'), 'https://www.keycdn.com?utm_source=wp-admin&utm_medium=plugins&utm_campaign=cdn-enabler', 'KeyCDN'); ?></p>
110
- </div>
 
 
111
 
112
  <form method="post" action="options.php">
113
  <?php settings_fields('cdn_enabler') ?>
114
 
115
- <?php $options = CDN_Enabler::get_options() ?>
116
-
117
  <table class="form-table">
118
 
119
  <tr valign="top">
99
  */
100
 
101
  public static function settings_page()
102
+ {
103
+ $options = CDN_Enabler::get_options()
104
+
105
+
106
+ ?>
107
  <div class="wrap">
108
  <h2>
109
  <?php _e("CDN Enabler Settings", "cdn-enabler"); ?>
110
  </h2>
111
 
112
+ <?php
113
+ if (( ! is_int($options['keycdn_zone_id'])
114
+ or $options['keycdn_zone_id'] <= 0 )
115
+ or
116
+ ( ! array_key_exists('keycdn_api_key', $options)
117
+ or strlen($options['keycdn_api_key']) < 20 ))
118
+ {
119
+ printf(__('
120
  <div class="notice notice-info">
121
+ <p>Combine CDN Enabler with <b><a href="%s">%s</a></b> for even faster WordPress performance.</p>
122
+ </div>'), 'https://www.keycdn.com?utm_source=wp-admin&utm_medium=plugins&utm_campaign=cdn-enabler', 'KeyCDN');
123
+ }
124
+ ?>
125
 
126
  <form method="post" action="options.php">
127
  <?php settings_fields('cdn_enabler') ?>
128
 
 
 
129
  <table class="form-table">
130
 
131
  <tr valign="top">
readme.txt CHANGED
@@ -47,6 +47,11 @@ The CDN Enabler plugin has been developed to link your content to the CDN URLs.
47
 
48
  == Changelog ==
49
 
 
 
 
 
 
50
  = 1.0.7 =
51
  * Minor bug fixes (pass-by-reference)
52
 
47
 
48
  == Changelog ==
49
 
50
+ = 1.0.8 =
51
+ * Purge CDN redirects to admin dashboard to avoid error messages
52
+ * more helpful error messages
53
+ * do not display nag notice when KeyCDN API credentials are set
54
+
55
  = 1.0.7 =
56
  * Minor bug fixes (pass-by-reference)
57