NitroPack - Version 1.5.10

Version Description

  • Improvement: Overall stability improvements
  • Bug fix: Fix 'headers already sent' error in CLI and WP_CRON modes
  • Bug fix: Fix cases of missing elements in Fusion Builder
Download this release

Release Info

Developer nitropack
Plugin Icon 128x128 NitroPack
Version 1.5.10
Comparing to
See all releases

Code changes from version 1.5.9 to 1.5.10

classes/Integration.php CHANGED
@@ -35,6 +35,7 @@ class Integration {
35
  "NitroPack/Integration/Plugin/WPCacheHelper",
36
  "NitroPack/Integration/Plugin/CookieNotice",
37
  "NitroPack/Integration/Plugin/BeaverBuilder",
 
38
  ];
39
  private static $loadedModules = [];
40
  private static $stage = "very_early";
35
  "NitroPack/Integration/Plugin/WPCacheHelper",
36
  "NitroPack/Integration/Plugin/CookieNotice",
37
  "NitroPack/Integration/Plugin/BeaverBuilder",
38
+ "NitroPack/Integration/Plugin/FusionBuilder",
39
  ];
40
  private static $loadedModules = [];
41
  private static $stage = "very_early";
classes/Integration/Hosting/Cloudways.php CHANGED
@@ -42,9 +42,9 @@ class Cloudways extends Hosting {
42
  }
43
 
44
  public function setCacheControl() {
45
- header("Vary: sec-ch-ua-mobile");
46
  if (isset($_SERVER["HTTP_SEC_CH_UA_MOBILE"])) {
47
- header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
48
  } else {
49
  return;
50
  }
42
  }
43
 
44
  public function setCacheControl() {
45
+ nitropack_header("Vary: sec-ch-ua-mobile");
46
  if (isset($_SERVER["HTTP_SEC_CH_UA_MOBILE"])) {
47
+ nitropack_header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
48
  } else {
49
  return;
50
  }
classes/Integration/Hosting/DreamHost.php CHANGED
@@ -50,11 +50,11 @@ class DreamHost extends Hosting {
50
  }
51
 
52
  public function setCacheControl() {
53
- header("Vary: sec-ch-ua-mobile");
54
  if (isset($_SERVER["HTTP_SEC_CH_UA_MOBILE"])) {
55
- header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
56
  } else {
57
- header("Cache-Control: no-cache");
58
  return;
59
  }
60
  }
50
  }
51
 
52
  public function setCacheControl() {
53
+ nitropack_header("Vary: sec-ch-ua-mobile");
54
  if (isset($_SERVER["HTTP_SEC_CH_UA_MOBILE"])) {
55
+ nitropack_header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
56
  } else {
57
+ nitropack_header("Cache-Control: no-cache");
58
  return;
59
  }
60
  }
classes/Integration/Hosting/Pagely.php CHANGED
@@ -52,6 +52,6 @@ class Pagely extends Hosting {
52
  }
53
 
54
  public function addCacheControl() {
55
- header("Cache-Control: public, max-age=0, s-maxage=3600");
56
  }
57
  }
52
  }
53
 
54
  public function addCacheControl() {
55
+ nitropack_header("Cache-Control: public, max-age=0, s-maxage=3600");
56
  }
57
  }
classes/Integration/Hosting/Savvii.php CHANGED
@@ -55,9 +55,9 @@ class Savvii extends Hosting
55
  }
56
 
57
  public function setCacheControl() {
58
- header("Vary: sec-ch-ua-mobile");
59
  if (isset($_SERVER["HTTP_SEC_CH_UA_MOBILE"])) {
60
- header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
61
  } else {
62
  return;
63
  }
55
  }
56
 
57
  public function setCacheControl() {
58
+ nitropack_header("Vary: sec-ch-ua-mobile");
59
  if (isset($_SERVER["HTTP_SEC_CH_UA_MOBILE"])) {
60
+ nitropack_header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
61
  } else {
62
  return;
63
  }
classes/Integration/Hosting/SiteGround.php CHANGED
@@ -50,11 +50,11 @@ class SiteGround extends Hosting {
50
  }
51
 
52
  public function setCacheControl() {
53
- header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
54
  }
55
 
56
  public function allowProxyCache() {
57
  $this->setCacheControl();
58
- header('X-Cache-Enabled: True');
59
  }
60
  }
50
  }
51
 
52
  public function setCacheControl() {
53
+ nitropack_header("Cache-Control: public, max-age=0, s-maxage=3600"); // needs to be like that instead of Cache-Control: no-cache in order to allow caching in the provided reverse proxy, but prevent the browsers from doing so
54
  }
55
 
56
  public function allowProxyCache() {
57
  $this->setCacheControl();
58
+ nitropack_header('X-Cache-Enabled: True');
59
  }
60
  }
classes/Integration/Hosting/WPEngine.php CHANGED
@@ -20,7 +20,7 @@ class WPEngine extends Hosting {
20
  define("NITROPACK_USE_MICROTIMEOUT", 20000);
21
  if (isset($_COOKIE["wpengine_no_cache"]) || isset($_SERVER["HTTP_AUTOUPDATER"])) {
22
  add_filter("nitropack_passes_cookie_requirements", function() {
23
- header("X-Nitro-Disabled-Reason: WP Engine SPM bypass");
24
  return false;
25
  });
26
  }
20
  define("NITROPACK_USE_MICROTIMEOUT", 20000);
21
  if (isset($_COOKIE["wpengine_no_cache"]) || isset($_SERVER["HTTP_AUTOUPDATER"])) {
22
  add_filter("nitropack_passes_cookie_requirements", function() {
23
+ nitropack_header("X-Nitro-Disabled-Reason: WP Engine SPM bypass");
24
  return false;
25
  });
26
  }
classes/Integration/Plugin/Cloudflare.php CHANGED
@@ -61,10 +61,10 @@ class Cloudflare {
61
  }
62
 
63
  public function allowApoCache() {
64
- header("cf-edge-cache: cache,platform=wordpress");
65
  }
66
 
67
  public function preventApoCache() {
68
- header("cf-edge-cache: no-cache");
69
  }
70
  }
61
  }
62
 
63
  public function allowApoCache() {
64
+ nitropack_header("cf-edge-cache: cache,platform=wordpress");
65
  }
66
 
67
  public function preventApoCache() {
68
+ nitropack_header("cf-edge-cache: no-cache");
69
  }
70
  }
classes/Integration/Plugin/FusionBuilder.php ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace NitroPack\Integration\Plugin;
4
+
5
+ class FusionBuilder {
6
+ const STAGE = "late";
7
+
8
+ public function init($stage) {
9
+ add_action( 'init', function() {
10
+ if (defined('FUSION_BUILDER_VERSION')) {
11
+ add_filter("jetpack_device_detection_get_info", function($info, $ua, $ua_info) {
12
+ $agent = $ua_info->useragent;
13
+ if ($agent
14
+ && stripos($agent, "Nitro-Optimizer-Agent") !== false
15
+ && stripos($agent, "Android") === false
16
+ && stripos($agent, "iPad") === false
17
+ ) {
18
+ $info["is_phone"] = false;
19
+ $info["is_phone_matched_ua"] = "";
20
+ $info["is_handheld"] = false;
21
+ $info["is_desktop"] = true;
22
+ }
23
+ return $info;
24
+ }, 10, 3);
25
+ }
26
+ }, 5);
27
+ }
28
+ }
classes/Integration/Server/Cloudflare.php CHANGED
@@ -22,7 +22,7 @@ class Cloudflare {
22
 
23
  public function init($stage) {
24
  if (self::detect()) {
25
- header("Accept-CH: Sec-CH-UA-Mobile");
26
 
27
  if (self::isCacheEnabled()) {
28
  add_action('nitropack_cacheable_cache_headers', [$this, 'allowProxyCache'], PHP_INT_MAX-1);
@@ -37,14 +37,14 @@ class Cloudflare {
37
  public function allowProxyCache() {
38
  $siteConfig = get_nitropack()->getSiteConfig();
39
  if ($siteConfig && !empty($siteConfig["hosting"]) && $siteConfig["hosting"] == "rocketnet") {
40
- header("Cloudflare-CDN-Cache-Control: public, max-age=0, s-maxage=300, stale-while-revalidate=3600");
41
  } else {
42
- header("Vary: sec-ch-ua-mobile");
43
- header("Cloudflare-CDN-Cache-Control: public, max-age=0, s-maxage=15, stale-while-revalidate=3600");
44
  }
45
  }
46
 
47
  public function preventProxyCache() {
48
- header("Cloudflare-CDN-Cache-Control: no-cache");
49
  }
50
  }
22
 
23
  public function init($stage) {
24
  if (self::detect()) {
25
+ nitropack_header("Accept-CH: Sec-CH-UA-Mobile");
26
 
27
  if (self::isCacheEnabled()) {
28
  add_action('nitropack_cacheable_cache_headers', [$this, 'allowProxyCache'], PHP_INT_MAX-1);
37
  public function allowProxyCache() {
38
  $siteConfig = get_nitropack()->getSiteConfig();
39
  if ($siteConfig && !empty($siteConfig["hosting"]) && $siteConfig["hosting"] == "rocketnet") {
40
+ nitropack_header("Cloudflare-CDN-Cache-Control: public, max-age=0, s-maxage=300, stale-while-revalidate=3600");
41
  } else {
42
+ nitropack_header("Vary: sec-ch-ua-mobile");
43
+ nitropack_header("Cloudflare-CDN-Cache-Control: public, max-age=0, s-maxage=15, stale-while-revalidate=3600");
44
  }
45
  }
46
 
47
  public function preventProxyCache() {
48
+ nitropack_header("Cloudflare-CDN-Cache-Control: no-cache");
49
  }
50
  }
classes/Integration/Server/Fastly.php CHANGED
@@ -15,7 +15,7 @@ class Fastly {
15
 
16
  public function init($stage) {
17
  if (self::detect()) {
18
- header("Accept-CH: Sec-CH-UA-Mobile");
19
 
20
  if (self::isCacheEnabled()) {
21
  add_action('nitropack_early_cache_headers', [$this, 'allowProxyCache']);
@@ -26,12 +26,12 @@ class Fastly {
26
  }
27
 
28
  public function allowProxyCache() {
29
- header("Vary: sec-ch-ua-mobile");
30
- header("Surrogate-Control: max-age=5, stale-while-revalidate=3600");
31
  }
32
 
33
  public function preventProxyCache() {
34
- header("Surrogate-Control: max-age=0, must-revalidate");
35
  }
36
  }
37
 
15
 
16
  public function init($stage) {
17
  if (self::detect()) {
18
+ nitropack_header("Accept-CH: Sec-CH-UA-Mobile");
19
 
20
  if (self::isCacheEnabled()) {
21
  add_action('nitropack_early_cache_headers', [$this, 'allowProxyCache']);
26
  }
27
 
28
  public function allowProxyCache() {
29
+ nitropack_header("Vary: sec-ch-ua-mobile");
30
+ nitropack_header("Surrogate-Control: max-age=5, stale-while-revalidate=3600");
31
  }
32
 
33
  public function preventProxyCache() {
34
+ nitropack_header("Surrogate-Control: max-age=0, must-revalidate");
35
  }
36
  }
37
 
classes/Integration/Server/LiteSpeed.php CHANGED
@@ -21,9 +21,9 @@ class LiteSpeed {
21
 
22
  public static function sendCacheHeader($maxAge = NULL) {
23
  if (!$maxAge) {
24
- header("X-LiteSpeed-Cache-Control: public");
25
  } else if (is_numeric($maxAge)) {
26
- header("X-LiteSpeed-Cache-Control: public,max-age=" . (int)$maxAge);
27
  }
28
  }
29
 
@@ -44,9 +44,9 @@ class LiteSpeed {
44
  $headerValues[] = "tag=" . $tag;
45
  }
46
 
47
- header("X-LiteSpeed-Purge: " . implode(", ", $headerValues), false);
48
  } else {
49
- header("X-LiteSpeed-Purge: *", false);
50
  }
51
  }
52
 
@@ -69,7 +69,7 @@ class LiteSpeed {
69
  }
70
 
71
  public function setupVary() {
72
- header("X-LiteSpeed-Vary: cookie=" . self::DEVICE_COOKIE);
73
  }
74
 
75
  public function allowProxyCache() {
21
 
22
  public static function sendCacheHeader($maxAge = NULL) {
23
  if (!$maxAge) {
24
+ nitropack_header("X-LiteSpeed-Cache-Control: public");
25
  } else if (is_numeric($maxAge)) {
26
+ nitropack_header("X-LiteSpeed-Cache-Control: public,max-age=" . (int)$maxAge);
27
  }
28
  }
29
 
44
  $headerValues[] = "tag=" . $tag;
45
  }
46
 
47
+ nitropack_header("X-LiteSpeed-Purge: " . implode(", ", $headerValues), false);
48
  } else {
49
+ nitropack_header("X-LiteSpeed-Purge: *", false);
50
  }
51
  }
52
 
69
  }
70
 
71
  public function setupVary() {
72
+ nitropack_header("X-LiteSpeed-Vary: cookie=" . self::DEVICE_COOKIE);
73
  }
74
 
75
  public function allowProxyCache() {
classes/Integration/Server/Sucuri.php CHANGED
@@ -16,7 +16,7 @@ class Sucuri {
16
 
17
  public function init($stage) {
18
  if (self::detect()) {
19
- header("Accept-CH: Sec-CH-UA-Mobile");
20
 
21
  if (self::isCacheEnabled()) {
22
  add_action('nitropack_cacheable_cache_headers', [$this, 'allowProxyCache'], PHP_INT_MAX-1);
@@ -29,12 +29,12 @@ class Sucuri {
29
  }
30
 
31
  public function allowProxyCache() {
32
- header("Vary: sec-ch-ua-mobile");
33
- header("Cache-Control: public, max-age=0, s-maxage=15, stale-while-revalidate=3600");
34
  }
35
 
36
  public function preventProxyCache() {
37
- header("Cache-Control: no-cache");
38
  }
39
  }
40
 
16
 
17
  public function init($stage) {
18
  if (self::detect()) {
19
+ nitropack_header("Accept-CH: Sec-CH-UA-Mobile");
20
 
21
  if (self::isCacheEnabled()) {
22
  add_action('nitropack_cacheable_cache_headers', [$this, 'allowProxyCache'], PHP_INT_MAX-1);
29
  }
30
 
31
  public function allowProxyCache() {
32
+ nitropack_header("Vary: sec-ch-ua-mobile");
33
+ nitropack_header("Cache-Control: public, max-age=0, s-maxage=15, stale-while-revalidate=3600");
34
  }
35
 
36
  public function preventProxyCache() {
37
+ nitropack_header("Cache-Control: no-cache");
38
  }
39
  }
40
 
constants.php CHANGED
@@ -6,7 +6,7 @@ function nitropack_trailingslashit($string) {
6
  return rtrim( $string, '/\\' ) . '/';
7
  }
8
 
9
- define( 'NITROPACK_VERSION', '1.5.9' );
10
  define( 'NITROPACK_OPTION_GROUP', 'nitropack' );
11
  define( 'NITROPACK_DATA_DIR', nitropack_trailingslashit(WP_CONTENT_DIR) . 'nitropack' );
12
  define( 'NITROPACK_CONFIG_FILE', nitropack_trailingslashit(NITROPACK_DATA_DIR) . 'config.json' );
6
  return rtrim( $string, '/\\' ) . '/';
7
  }
8
 
9
+ define( 'NITROPACK_VERSION', '1.5.10' );
10
  define( 'NITROPACK_OPTION_GROUP', 'nitropack' );
11
  define( 'NITROPACK_DATA_DIR', nitropack_trailingslashit(WP_CONTENT_DIR) . 'nitropack' );
12
  define( 'NITROPACK_CONFIG_FILE', nitropack_trailingslashit(NITROPACK_DATA_DIR) . 'config.json' );
diagnostics.php CHANGED
@@ -201,9 +201,9 @@ function nitropack_generate_report() {
201
  }
202
  $str = json_encode($diag_data, JSON_PRETTY_PRINT);
203
  $filename = 'nitropack_diag_file.txt';
204
- header('Content-Disposition: attachment; filename="'.$filename.'"');
205
- header("Content-Type: text/plain");
206
- header("Content-Length: " . strlen($str));
207
  echo $str;
208
  exit;
209
  }
201
  }
202
  $str = json_encode($diag_data, JSON_PRETTY_PRINT);
203
  $filename = 'nitropack_diag_file.txt';
204
+ nitropack_header('Content-Disposition: attachment; filename="'.$filename.'"');
205
+ nitropack_header("Content-Type: text/plain");
206
+ nitropack_header("Content-Length: " . strlen($str));
207
  echo $str;
208
  exit;
209
  }
functions.php CHANGED
@@ -27,8 +27,8 @@ function nitropack_is_logged_in() {
27
 
28
  function nitropack_passes_cookie_requirements() {
29
  $cookieStr = implode("|", array_keys($_COOKIE));
30
- $safeCookie = (strpos($cookieStr, "comment_author") === false && strpos($cookieStr, "wp-postpass_") === false && empty($_COOKIE["woocommerce_items_in_cart"])) || !!header("X-Nitro-Disabled-Reason: cookie bypass");
31
- $isUserLoggedIn = nitropack_is_logged_in() && !header("X-Nitro-Disabled-Reason: logged in");
32
 
33
  return apply_filters("nitropack_passes_cookie_requirements", $safeCookie && !$isUserLoggedIn);
34
  }
@@ -232,24 +232,29 @@ function nitropack_handle_beacon() {
232
  if (null !== $nitro = get_nitropack_sdk($siteConfig["siteId"], $siteConfig["siteSecret"], $url) ) {
233
  try {
234
  $hasLocalCache = $nitro->hasLocalCache(false);
 
235
  $proxyPurgeOnly = !empty($_POST["proxyPurgeOnly"]);
236
  $layout = !empty($_POST["layout"]) ? $_POST["layout"] : "default";
237
  $output = "";
238
 
239
  if (!$proxyPurgeOnly) {
240
  if (!$hasLocalCache) {
241
- header("X-Nitro-Beacon: FORWARD");
242
- $hasCache = $nitro->hasRemoteCache($layout, false); // Download the new cache file
243
- $hasLocalCache = $hasCache;
244
- $output = sprintf("Cache %s", $hasCache ? "fetched" : "requested");
 
 
 
 
245
  } else {
246
- header("X-Nitro-Beacon: SKIP");
247
  $output = sprintf("Cache exists already");
248
  }
249
  }
250
 
251
- if ($hasLocalCache || $proxyPurgeOnly) { // proxyPurgeOnly is set for unsupported browsers, in which case we need to purge the cache regardless of the existence of local NP cache
252
- header("X-Nitro-Proxy-Purge: true");
253
  $nitro->purgeProxyCache($url);
254
  do_action('nitropack_integration_purge_url', $url);
255
  }
@@ -314,6 +319,7 @@ function nitropack_handle_webhook() {
314
  do_action('nitropack_integration_purge_all');
315
  } else {
316
  nitropack_sdk_purge_local();
 
317
  }
318
  }
319
  break;
@@ -343,22 +349,23 @@ function nitropack_passes_page_requirements() {
343
  $reduceCartChecks = defined("NITROPACK_REDUCE_CART_CHECKS") && NITROPACK_REDUCE_CART_CHECKS;
344
 
345
  return !(
346
- ( is_404() && !header("X-Nitro-Disabled-Reason: 404") ) ||
347
- ( is_preview() && !header("X-Nitro-Disabled-Reason: preview page") ) ||
348
- ( is_feed() && !header("X-Nitro-Disabled-Reason: feed") ) ||
349
- ( is_comment_feed() && !header("X-Nitro-Disabled-Reason: comment feed") ) ||
350
- ( is_trackback() && !header("X-Nitro-Disabled-Reason: trackback") ) ||
351
- ( is_user_logged_in() && !header("X-Nitro-Disabled-Reason: logged in") ) ||
352
- ( is_search() && !header("X-Nitro-Disabled-Reason: search") ) ||
353
- ( nitropack_is_ajax() && !header("X-Nitro-Disabled-Reason: ajax") ) ||
354
- ( nitropack_is_post() && !header("X-Nitro-Disabled-Reason: post request") ) ||
355
- ( nitropack_is_xmlrpc() && !header("X-Nitro-Disabled-Reason: xmlrpc") ) ||
356
- ( nitropack_is_robots() && !header("X-Nitro-Disabled-Reason: robots") ) ||
357
  !nitropack_is_allowed_request() ||
358
- ( defined('DOING_CRON') && DOING_CRON && !header("X-Nitro-Disabled-Reason: doing cron") ) || // CRON request
359
- ( defined('WC_PLUGIN_FILE') && (is_page( 'cart' ) || ( !$reduceCartChecks && is_cart()) ) && !header("X-Nitro-Disabled-Reason: cart page") ) || // WooCommerce
360
- ( defined('WC_PLUGIN_FILE') && (is_page( 'checkout' ) || ( !$reduceCheckoutChecks && is_checkout()) ) && !header("X-Nitro-Disabled-Reason: checkout page") ) || // WooCommerce
361
- ( defined('WC_PLUGIN_FILE') && is_account_page() && !header("X-Nitro-Disabled-Reason: account page") ) // WooCommerce
 
362
  );
363
  }
364
 
@@ -376,26 +383,26 @@ function nitropack_is_allowed_request() {
376
  if (is_array($cacheableObjectTypes)) {
377
  if (nitropack_is_home()) {
378
  if (!in_array('home', $cacheableObjectTypes)) {
379
- header("X-Nitro-Disabled-Reason: page type not allowed (home)");
380
  return false;
381
  }
382
  } else {
383
  if (is_tax() || is_category() || is_tag()) {
384
  $np_queriedObj = get_queried_object();
385
  if (!empty($np_queriedObj) && !in_array($np_queriedObj->taxonomy, $cacheableObjectTypes)) {
386
- header("X-Nitro-Disabled-Reason: page type not allowed ({$np_queriedObj->taxonomy})");
387
  return false;
388
  }
389
  } else {
390
  if (nitropack_is_archive()) {
391
  if (!in_array('archive', $cacheableObjectTypes)) {
392
- header("X-Nitro-Disabled-Reason: page type not allowed (archive)");
393
  return false;
394
  }
395
  } else {
396
  $postType = get_post_type();
397
  if (!empty($postType) && !in_array($postType, $cacheableObjectTypes)) {
398
- header("X-Nitro-Disabled-Reason: page type not allowed ($postType)");
399
  return false;
400
  }
401
  }
@@ -405,11 +412,11 @@ function nitropack_is_allowed_request() {
405
 
406
  if (null !== $nitro = get_nitropack_sdk() ) {
407
  return
408
- ( $nitro->isAllowedUrl($nitro->getUrl()) || header("X-Nitro-Disabled-Reason: url not allowed") ) &&
409
- ( $nitro->isAllowedRequest(true) || header("X-Nitro-Disabled-Reason: request type not allowed") );
410
  }
411
 
412
- header("X-Nitro-Disabled-Reason: site not connected");
413
  return false;
414
  }
415
 
@@ -426,6 +433,10 @@ function nitropack_is_wp_cli() {
426
  return defined("WP_CLI") && WP_CLI;
427
  }
428
 
 
 
 
 
429
  function nitropack_is_rest() {
430
  // Source: https://wordpress.stackexchange.com/a/317041
431
  $prefix = rest_get_url_prefix( );
@@ -496,7 +507,7 @@ function nitropack_is_optimizer_request() {
496
 
497
  function nitropack_init() {
498
  global $np_queriedObj;
499
- header('X-Nitro-Cache: MISS');
500
  $GLOBALS["NitroPack.tags"] = array();
501
 
502
  if (is_valid_nitropack_webhook()) {
@@ -545,7 +556,7 @@ function nitropack_init() {
545
  add_action('wp_footer', 'nitropack_log_tags');
546
  }
547
  } else {
548
- header("X-Nitro-Disabled: 1");
549
  if ((null !== $nitro = get_nitropack_sdk()) && !$nitro->isAllowedBrowser()) { // This clears any proxy cache when a proxy cached non-optimized request due to unsupported browser
550
  add_action('wp_footer', 'nitropack_print_beacon_script');
551
  add_action('get_footer', 'nitropack_print_beacon_script');
@@ -622,7 +633,7 @@ function nitropack_set_custom_expiration() {
622
 
623
  if (!empty($np_customExpirationTimes)) {
624
  sort($np_customExpirationTimes, SORT_NUMERIC);
625
- header("X-Nitro-Expires: " . $np_customExpirationTimes[0]);
626
  }
627
  }
628
 
@@ -1109,8 +1120,14 @@ function nitropack_sdk_invalidate($url = NULL, $tag = NULL, $reason = NULL) {
1109
  }
1110
 
1111
  /* Start Heartbeat Related Functions */
 
 
 
 
 
 
1112
  function nitropack_print_heartbeat_script() {
1113
- if (!nitropack_is_optimizer_request() && !nitropack_is_heartbeat_running() && time() - nitropack_last_heartbeat() > NITROPACK_HEARTBEAT_INTERVAL) {
1114
  if (defined("NITROPACK_HEARTBEAT_PRINTED")) return;
1115
  define("NITROPACK_HEARTBEAT_PRINTED", true);
1116
  echo nitropack_get_heartbeat_script();
@@ -1171,16 +1188,36 @@ function nitropack_is_heartbeat_running() {
1171
  }
1172
  }
1173
 
 
 
 
 
 
 
 
 
 
 
 
1174
  function nitropack_handle_heartbeat() {
 
 
 
1175
  session_write_close();
1176
  if (null !== $nitro = get_nitropack_sdk()) {
1177
  try {
 
1178
  \NitroPack\SDK\Filesystem::filePutContents(nitropack_get_heartbeat_file(), 1);
1179
  if (nitropack_healthcheck()) {
1180
- nitropack_flush_backlog();
 
 
 
 
 
 
 
1181
  }
1182
- nitropack_cache_cleanup();
1183
- \NitroPack\SDK\Filesystem::filePutContents(nitropack_get_heartbeat_file(), 0);
1184
  } catch (\Exception $e) {
1185
  return false;
1186
  }
@@ -1199,8 +1236,11 @@ function nitropack_flush_backlog() {
1199
  if (null !== $nitro = get_nitropack_sdk()) {
1200
  try {
1201
  if ($nitro->backlog->exists()) {
1202
- $nitro->backlog->replay(30);
1203
  }
 
 
 
1204
  } catch (\Exception $e) {
1205
  return false;
1206
  }
@@ -1219,10 +1259,12 @@ function nitropack_cache_cleanup() {
1219
  \NitroPack\SDK\Filesystem::deleteDir($cacheDir);
1220
  } catch (\Exception $e) {
1221
  // TODO: Log this
 
1222
  }
1223
  }
1224
  }
1225
  }
 
1226
  }
1227
  /* End Heartbeat Related Functions */
1228
 
@@ -1294,6 +1336,22 @@ function nitropack_sdk_purge_local($url = NULL) {
1294
  return false;
1295
  }
1296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1297
  function nitropack_purge($url = NULL, $tag = NULL, $reason = NULL) {
1298
  if ($tag != "pageType:home") {
1299
  $siteConfig = nitropack_get_site_config();
@@ -1961,10 +2019,20 @@ function nitropack_verify_connect($siteId, $siteSecret) {
1961
  try {
1962
  $blogId = get_current_blog_id();
1963
  if (null !== $nitro = get_nitropack_sdk($siteId, $siteSecret, NULL, true)) {
 
 
 
 
 
 
 
1964
  $preventParing = apply_filters('nitropack_prevent_connect', nitropack_prevent_connecting($nitro));
1965
  if ($preventParing) {
1966
- nitropack_json_and_exit(array("status" => "error", "message" => "It looks like another site <strong>({$preventParing['remote']})</strong> is already connected using these credentials. Either disconnect it or register a new site in your NitroPack dashboard.<br/>
1967
- <a href='https://support.nitropack.io/hc/en-us/articles/4405254569745' target='_blank' rel='noreferrer noopener'>Read more</a>"));
 
 
 
1968
  }
1969
  $token = nitropack_generate_webhook_token($siteId);
1970
  update_option("nitropack-webhookToken", $token);
@@ -2433,7 +2501,7 @@ function nitropack_handle_request($servedFrom = "unknown") {
2433
  $_SERVER['REQUEST_URI'] = nitropack_removeCacheBustParam($_SERVER['REQUEST_URI']);
2434
  }
2435
 
2436
- header('Cache-Control: no-cache');
2437
  do_action("nitropack_early_cache_headers"); // Overrides the Cache-Control header on supported platforms
2438
  $isManageWpRequest = !empty($_GET["mwprid"]);
2439
  $isWpCli = nitropack_is_wp_cli();
@@ -2476,11 +2544,11 @@ function nitropack_handle_request($servedFrom = "unknown") {
2476
  do_action("nitropack_cachehit_cache_headers"); // TODO: Pass the remaining TTL here
2477
  $cacheControlOverride = defined("NITROPACK_CACHE_CONTROL_OVERRIDE") ? NITROPACK_CACHE_CONTROL_OVERRIDE : NULL;
2478
  if ($cacheControlOverride) {
2479
- header('Cache-Control: ' . $cacheControlOverride);
2480
  }
2481
 
2482
- header('X-Nitro-Cache: HIT');
2483
- header('X-Nitro-Cache-From: ' . $servedFrom);
2484
  $nitro->pageCache->readfile();
2485
  exit;
2486
  } else {
@@ -2494,8 +2562,8 @@ function nitropack_handle_request($servedFrom = "unknown") {
2494
 
2495
  $nitro->pageCache->useInvalidated(true);
2496
  if ($nitro->hasLocalCache()) {
2497
- header('X-Nitro-Cache: STALE');
2498
- header('X-Nitro-Cache-From: ' . $servedFrom);
2499
  $nitro->pageCache->readfile();
2500
  exit;
2501
  } else {
@@ -2658,9 +2726,16 @@ function nitropack_setcookie($name, $value, $expires = NULL, $options = []) {
2658
  foreach ($options as $optName => $optValue) {
2659
  $cookie_options .= "$optName=$optValue; ";
2660
  }
2661
- header("set-cookie: $name=$value; Path=$cookie_path; " . $cookie_options, false);
2662
  }
2663
 
 
 
 
 
 
 
 
2664
  function nitropack_upgrade_handler($entity) {
2665
  $np = 'nitropack/main.php';
2666
  $trigger = $entity;
27
 
28
  function nitropack_passes_cookie_requirements() {
29
  $cookieStr = implode("|", array_keys($_COOKIE));
30
+ $safeCookie = (strpos($cookieStr, "comment_author") === false && strpos($cookieStr, "wp-postpass_") === false && empty($_COOKIE["woocommerce_items_in_cart"])) || !!nitropack_header("X-Nitro-Disabled-Reason: cookie bypass");
31
+ $isUserLoggedIn = nitropack_is_logged_in() && !nitropack_header("X-Nitro-Disabled-Reason: logged in");
32
 
33
  return apply_filters("nitropack_passes_cookie_requirements", $safeCookie && !$isUserLoggedIn);
34
  }
232
  if (null !== $nitro = get_nitropack_sdk($siteConfig["siteId"], $siteConfig["siteSecret"], $url) ) {
233
  try {
234
  $hasLocalCache = $nitro->hasLocalCache(false);
235
+ $needsHeartbeat = nitropack_is_heartbeat_needed();
236
  $proxyPurgeOnly = !empty($_POST["proxyPurgeOnly"]);
237
  $layout = !empty($_POST["layout"]) ? $_POST["layout"] : "default";
238
  $output = "";
239
 
240
  if (!$proxyPurgeOnly) {
241
  if (!$hasLocalCache) {
242
+ nitropack_header("X-Nitro-Beacon: FORWARD");
243
+ try {
244
+ $hasCache = $nitro->hasRemoteCache($layout, false); // Download the new cache file
245
+ $hasLocalCache = $hasCache;
246
+ $output = sprintf("Cache %s", $hasCache ? "fetched" : "requested");
247
+ } catch (\Exception $e) {
248
+ // not a critical error, do nothing
249
+ }
250
  } else {
251
+ nitropack_header("X-Nitro-Beacon: SKIP");
252
  $output = sprintf("Cache exists already");
253
  }
254
  }
255
 
256
+ if ($hasLocalCache || $proxyPurgeOnly/* || $needsHeartbeat*/) { // proxyPurgeOnly is set for unsupported browsers, in which case we need to purge the cache regardless of the existence of local NP cache
257
+ nitropack_header("X-Nitro-Proxy-Purge: true");
258
  $nitro->purgeProxyCache($url);
259
  do_action('nitropack_integration_purge_url', $url);
260
  }
319
  do_action('nitropack_integration_purge_all');
320
  } else {
321
  nitropack_sdk_purge_local();
322
+ nitropack_sdk_delete_backlog();
323
  }
324
  }
325
  break;
349
  $reduceCartChecks = defined("NITROPACK_REDUCE_CART_CHECKS") && NITROPACK_REDUCE_CART_CHECKS;
350
 
351
  return !(
352
+ ( is_404() && !nitropack_header("X-Nitro-Disabled-Reason: 404") ) ||
353
+ ( is_preview() && !nitropack_header("X-Nitro-Disabled-Reason: preview page") ) ||
354
+ ( is_feed() && !nitropack_header("X-Nitro-Disabled-Reason: feed") ) ||
355
+ ( is_comment_feed() && !nitropack_header("X-Nitro-Disabled-Reason: comment feed") ) ||
356
+ ( is_trackback() && !nitropack_header("X-Nitro-Disabled-Reason: trackback") ) ||
357
+ ( is_user_logged_in() && !nitropack_header("X-Nitro-Disabled-Reason: logged in") ) ||
358
+ ( is_search() && !nitropack_header("X-Nitro-Disabled-Reason: search") ) ||
359
+ ( nitropack_is_ajax() && !nitropack_header("X-Nitro-Disabled-Reason: ajax") ) ||
360
+ ( nitropack_is_post() && !nitropack_header("X-Nitro-Disabled-Reason: post request") ) ||
361
+ ( nitropack_is_xmlrpc() && !nitropack_header("X-Nitro-Disabled-Reason: xmlrpc") ) ||
362
+ ( nitropack_is_robots() && !nitropack_header("X-Nitro-Disabled-Reason: robots") ) ||
363
  !nitropack_is_allowed_request() ||
364
+ ( nitropack_is_wp_cron() && !nitropack_header("X-Nitro-Disabled-Reason: doing cron") ) || // CRON request
365
+ ( nitropack_is_wp_cli() ) || // CLI request
366
+ ( defined('WC_PLUGIN_FILE') && (is_page( 'cart' ) || ( !$reduceCartChecks && is_cart()) ) && !nitropack_header("X-Nitro-Disabled-Reason: cart page") ) || // WooCommerce
367
+ ( defined('WC_PLUGIN_FILE') && (is_page( 'checkout' ) || ( !$reduceCheckoutChecks && is_checkout()) ) && !nitropack_header("X-Nitro-Disabled-Reason: checkout page") ) || // WooCommerce
368
+ ( defined('WC_PLUGIN_FILE') && is_account_page() && !nitropack_header("X-Nitro-Disabled-Reason: account page") ) // WooCommerce
369
  );
370
  }
371
 
383
  if (is_array($cacheableObjectTypes)) {
384
  if (nitropack_is_home()) {
385
  if (!in_array('home', $cacheableObjectTypes)) {
386
+ nitropack_header("X-Nitro-Disabled-Reason: page type not allowed (home)");
387
  return false;
388
  }
389
  } else {
390
  if (is_tax() || is_category() || is_tag()) {
391
  $np_queriedObj = get_queried_object();
392
  if (!empty($np_queriedObj) && !in_array($np_queriedObj->taxonomy, $cacheableObjectTypes)) {
393
+ nitropack_header("X-Nitro-Disabled-Reason: page type not allowed ({$np_queriedObj->taxonomy})");
394
  return false;
395
  }
396
  } else {
397
  if (nitropack_is_archive()) {
398
  if (!in_array('archive', $cacheableObjectTypes)) {
399
+ nitropack_header("X-Nitro-Disabled-Reason: page type not allowed (archive)");
400
  return false;
401
  }
402
  } else {
403
  $postType = get_post_type();
404
  if (!empty($postType) && !in_array($postType, $cacheableObjectTypes)) {
405
+ nitropack_header("X-Nitro-Disabled-Reason: page type not allowed ($postType)");
406
  return false;
407
  }
408
  }
412
 
413
  if (null !== $nitro = get_nitropack_sdk() ) {
414
  return
415
+ ( $nitro->isAllowedUrl($nitro->getUrl()) || nitropack_header("X-Nitro-Disabled-Reason: url not allowed") ) &&
416
+ ( $nitro->isAllowedRequest(true) || nitropack_header("X-Nitro-Disabled-Reason: request type not allowed") );
417
  }
418
 
419
+ nitropack_header("X-Nitro-Disabled-Reason: site not connected");
420
  return false;
421
  }
422
 
433
  return defined("WP_CLI") && WP_CLI;
434
  }
435
 
436
+ function nitropack_is_wp_cron() {
437
+ return defined('DOING_CRON') && DOING_CRON;
438
+ }
439
+
440
  function nitropack_is_rest() {
441
  // Source: https://wordpress.stackexchange.com/a/317041
442
  $prefix = rest_get_url_prefix( );
507
 
508
  function nitropack_init() {
509
  global $np_queriedObj;
510
+ nitropack_header('X-Nitro-Cache: MISS');
511
  $GLOBALS["NitroPack.tags"] = array();
512
 
513
  if (is_valid_nitropack_webhook()) {
556
  add_action('wp_footer', 'nitropack_log_tags');
557
  }
558
  } else {
559
+ nitropack_header("X-Nitro-Disabled: 1");
560
  if ((null !== $nitro = get_nitropack_sdk()) && !$nitro->isAllowedBrowser()) { // This clears any proxy cache when a proxy cached non-optimized request due to unsupported browser
561
  add_action('wp_footer', 'nitropack_print_beacon_script');
562
  add_action('get_footer', 'nitropack_print_beacon_script');
633
 
634
  if (!empty($np_customExpirationTimes)) {
635
  sort($np_customExpirationTimes, SORT_NUMERIC);
636
+ nitropack_header("X-Nitro-Expires: " . $np_customExpirationTimes[0]);
637
  }
638
  }
639
 
1120
  }
1121
 
1122
  /* Start Heartbeat Related Functions */
1123
+ function nitropack_is_heartbeat_needed() {
1124
+ return !nitropack_is_optimizer_request() &&
1125
+ !nitropack_is_heartbeat_running() &&
1126
+ (!nitropack_is_heartbeat_completed() || time() - nitropack_last_heartbeat() > NITROPACK_HEARTBEAT_INTERVAL);
1127
+ }
1128
+
1129
  function nitropack_print_heartbeat_script() {
1130
+ if (nitropack_is_heartbeat_needed()) {
1131
  if (defined("NITROPACK_HEARTBEAT_PRINTED")) return;
1132
  define("NITROPACK_HEARTBEAT_PRINTED", true);
1133
  echo nitropack_get_heartbeat_script();
1188
  }
1189
  }
1190
 
1191
+ function nitropack_is_heartbeat_completed() {
1192
+ if (null !== $nitro = get_nitropack_sdk()) {
1193
+ try {
1194
+ $heartbeatContent = \NitroPack\SDK\Filesystem::fileGetContents(nitropack_get_heartbeat_file());
1195
+ return $heartbeatContent == "0"; // 0 - Job Done, 1 - Job Running, 2 - Job Needs Repeat
1196
+ } catch (\Exception $e) {
1197
+ return true;
1198
+ }
1199
+ }
1200
+ }
1201
+
1202
  function nitropack_handle_heartbeat() {
1203
+ // TODO: Lock the file before checking this
1204
+ if (nitropack_is_heartbeat_running()) return;
1205
+
1206
  session_write_close();
1207
  if (null !== $nitro = get_nitropack_sdk()) {
1208
  try {
1209
+ $success = true;
1210
  \NitroPack\SDK\Filesystem::filePutContents(nitropack_get_heartbeat_file(), 1);
1211
  if (nitropack_healthcheck()) {
1212
+ $success &= nitropack_flush_backlog();
1213
+ }
1214
+ $success &= nitropack_cache_cleanup();
1215
+
1216
+ if ($success) {
1217
+ \NitroPack\SDK\Filesystem::filePutContents(nitropack_get_heartbeat_file(), 0);
1218
+ } else {
1219
+ \NitroPack\SDK\Filesystem::filePutContents(nitropack_get_heartbeat_file(), 2);
1220
  }
 
 
1221
  } catch (\Exception $e) {
1222
  return false;
1223
  }
1236
  if (null !== $nitro = get_nitropack_sdk()) {
1237
  try {
1238
  if ($nitro->backlog->exists()) {
1239
+ return $nitro->backlog->replay(30);
1240
  }
1241
+ } catch (\NitroPack\SDK\BacklogReplayTimeoutException $e) {
1242
+ $nitro->backlog->delete();
1243
+ return nitropack_sdk_purge(NULL, NULL, "Full purge after backlog timeout");
1244
  } catch (\Exception $e) {
1245
  return false;
1246
  }
1259
  \NitroPack\SDK\Filesystem::deleteDir($cacheDir);
1260
  } catch (\Exception $e) {
1261
  // TODO: Log this
1262
+ return false;
1263
  }
1264
  }
1265
  }
1266
  }
1267
+ return true;
1268
  }
1269
  /* End Heartbeat Related Functions */
1270
 
1336
  return false;
1337
  }
1338
 
1339
+ function nitropack_sdk_delete_backlog() {
1340
+ if (null !== $nitro = get_nitropack_sdk()) {
1341
+ try {
1342
+ if ($nitro->backlog->exists()) {
1343
+ $nitro->backlog->delete();
1344
+ }
1345
+ } catch (\Exception $e) {
1346
+ return false;
1347
+ }
1348
+
1349
+ return true;
1350
+ }
1351
+
1352
+ return false;
1353
+ }
1354
+
1355
  function nitropack_purge($url = NULL, $tag = NULL, $reason = NULL) {
1356
  if ($tag != "pageType:home") {
1357
  $siteConfig = nitropack_get_site_config();
2019
  try {
2020
  $blogId = get_current_blog_id();
2021
  if (null !== $nitro = get_nitropack_sdk($siteId, $siteSecret, NULL, true)) {
2022
+ if (!$nitro->checkHealthStatus()) {
2023
+ nitropack_json_and_exit(array(
2024
+ "status" => "error",
2025
+ "message" => "Error when trying to communicate with NitroPack's servers. Please try again in a few minutes. If the issue persists, please <a href='https://support.nitropack.io/hc/en-us' target='_blank'>contact us</a>."
2026
+ ));
2027
+ }
2028
+
2029
  $preventParing = apply_filters('nitropack_prevent_connect', nitropack_prevent_connecting($nitro));
2030
  if ($preventParing) {
2031
+ nitropack_json_and_exit(array(
2032
+ "status" => "error",
2033
+ "message" => "It looks like another site <strong>({$preventParing['remote']})</strong> is already connected using these credentials. Either disconnect it or register a new site in your NitroPack dashboard.<br/>
2034
+ <a href='https://support.nitropack.io/hc/en-us/articles/4405254569745' target='_blank' rel='noreferrer noopener'>Read more</a>"
2035
+ ));
2036
  }
2037
  $token = nitropack_generate_webhook_token($siteId);
2038
  update_option("nitropack-webhookToken", $token);
2501
  $_SERVER['REQUEST_URI'] = nitropack_removeCacheBustParam($_SERVER['REQUEST_URI']);
2502
  }
2503
 
2504
+ nitropack_header('Cache-Control: no-cache');
2505
  do_action("nitropack_early_cache_headers"); // Overrides the Cache-Control header on supported platforms
2506
  $isManageWpRequest = !empty($_GET["mwprid"]);
2507
  $isWpCli = nitropack_is_wp_cli();
2544
  do_action("nitropack_cachehit_cache_headers"); // TODO: Pass the remaining TTL here
2545
  $cacheControlOverride = defined("NITROPACK_CACHE_CONTROL_OVERRIDE") ? NITROPACK_CACHE_CONTROL_OVERRIDE : NULL;
2546
  if ($cacheControlOverride) {
2547
+ nitropack_header('Cache-Control: ' . $cacheControlOverride);
2548
  }
2549
 
2550
+ nitropack_header('X-Nitro-Cache: HIT');
2551
+ nitropack_header('X-Nitro-Cache-From: ' . $servedFrom);
2552
  $nitro->pageCache->readfile();
2553
  exit;
2554
  } else {
2562
 
2563
  $nitro->pageCache->useInvalidated(true);
2564
  if ($nitro->hasLocalCache()) {
2565
+ nitropack_header('X-Nitro-Cache: STALE');
2566
+ nitropack_header('X-Nitro-Cache-From: ' . $servedFrom);
2567
  $nitro->pageCache->readfile();
2568
  exit;
2569
  } else {
2726
  foreach ($options as $optName => $optValue) {
2727
  $cookie_options .= "$optName=$optValue; ";
2728
  }
2729
+ nitropack_header("set-cookie: $name=$value; Path=$cookie_path; " . $cookie_options, false);
2730
  }
2731
 
2732
+ function nitropack_header($header, $replace = true, $response_code = 0) {
2733
+ if (!nitropack_is_wp_cron() && !nitropack_is_wp_cli()) {
2734
+ header($header, $replace, $response_code);
2735
+ }
2736
+ }
2737
+
2738
+
2739
  function nitropack_upgrade_handler($entity) {
2740
  $np = 'nitropack/main.php';
2741
  $trigger = $entity;
main.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: NitroPack
4
  Plugin URI: https://nitropack.io/platform/wordpress
5
  Description: Everything you need for a fast website. Simple set up, easy to use, awesome support. Caching, Lazy Loading, Minification, Defer CSS/JS, CDN and more!
6
- Version: 1.5.9
7
  Author: NitroPack LLC
8
  Author URI: https://nitropack.io/
9
  License: GPL2
3
  Plugin Name: NitroPack
4
  Plugin URI: https://nitropack.io/platform/wordpress
5
  Description: Everything you need for a fast website. Simple set up, easy to use, awesome support. Caching, Lazy Loading, Minification, Defer CSS/JS, CDN and more!
6
+ Version: 1.5.10
7
  Author: NitroPack LLC
8
  Author URI: https://nitropack.io/
9
  License: GPL2
nitropack-sdk/NitroPack/SDK/Api/Base.php CHANGED
@@ -47,7 +47,7 @@ class Base {
47
  );
48
 
49
  if ($this->nitropack && $this->nitropack->getHealthStatus() !== HealthStatus::HEALTHY) {
50
- $unhealthyMsg = "Connection to NitroPack is not reliable at the moment.";
51
  if ($this->isBacklogEnabled) {
52
  $this->addToBacklog($backlogEntry);
53
  $unhealthyMsg .= " Request has been added to the backlog for delayed processing.";
47
  );
48
 
49
  if ($this->nitropack && $this->nitropack->getHealthStatus() !== HealthStatus::HEALTHY) {
50
+ $unhealthyMsg = "Connection to NitroPack is not reliable at the moment. Please try again in a few minutes.";
51
  if ($this->isBacklogEnabled) {
52
  $this->addToBacklog($backlogEntry);
53
  $unhealthyMsg .= " Request has been added to the backlog for delayed processing.";
nitropack-sdk/NitroPack/SDK/Backlog.php CHANGED
@@ -4,7 +4,7 @@ namespace NitroPack\SDK;
4
  use NitroPack\SDK\Api\ResponseStatus;
5
 
6
  class Backlog {
7
- const TTL = 86400; // 1 day in seconds
8
 
9
  private $dataDir;
10
  private $nitropack;
@@ -34,6 +34,10 @@ class Backlog {
34
  $this->closeHandle();
35
  }
36
 
 
 
 
 
37
  public function append($entry) {
38
  if (defined("NITROPACK_DISABLE_BACKLOG")) return;
39
  $fh = $this->getHandle();
@@ -49,13 +53,14 @@ class Backlog {
49
  Filesystem::flock($fh, LOCK_EX);
50
  $lastProcessingTimestamp = $this->header->lastProcessingTimestamp;
51
  if (time() - $lastProcessingTimestamp <= $timeLimit) {
52
- return;
53
  }
54
  $this->acquireBacklog($fh);
55
  Filesystem::flock($fh, LOCK_UN);
56
 
57
  $initialProcesssTime = $this->header->firstProcessingTimestamp;
58
  if (time() - $initialProcesssTime > self::TTL) {
 
59
  // In case there have been previous attempts at clearing the backlog and these attempts started more than the specified TTL seconds ago
60
  // Perform a full purge and clear the backlog
61
  }
@@ -75,8 +80,11 @@ class Backlog {
75
 
76
  if ($this->isEndOfQueue($fh)) {
77
  $this->closeHandle();
78
- Filesystem::deleteFile($this->queuePath);
 
79
  }
 
 
80
  }
81
 
82
  private function isEndOfQueue($fh) {
4
  use NitroPack\SDK\Api\ResponseStatus;
5
 
6
  class Backlog {
7
+ const TTL = 3600; // 1 hour in seconds
8
 
9
  private $dataDir;
10
  private $nitropack;
34
  $this->closeHandle();
35
  }
36
 
37
+ public function delete() {
38
+ Filesystem::deleteFile($this->queuePath);
39
+ }
40
+
41
  public function append($entry) {
42
  if (defined("NITROPACK_DISABLE_BACKLOG")) return;
43
  $fh = $this->getHandle();
53
  Filesystem::flock($fh, LOCK_EX);
54
  $lastProcessingTimestamp = $this->header->lastProcessingTimestamp;
55
  if (time() - $lastProcessingTimestamp <= $timeLimit) {
56
+ return false;
57
  }
58
  $this->acquireBacklog($fh);
59
  Filesystem::flock($fh, LOCK_UN);
60
 
61
  $initialProcesssTime = $this->header->firstProcessingTimestamp;
62
  if (time() - $initialProcesssTime > self::TTL) {
63
+ throw new BacklogReplayTimeoutException(sprintf("Backlog replay did not complete within %s seconds", self::TTL));
64
  // In case there have been previous attempts at clearing the backlog and these attempts started more than the specified TTL seconds ago
65
  // Perform a full purge and clear the backlog
66
  }
80
 
81
  if ($this->isEndOfQueue($fh)) {
82
  $this->closeHandle();
83
+ $this->delete();
84
+ return true;
85
  }
86
+
87
+ return false;
88
  }
89
 
90
  private function isEndOfQueue($fh) {
nitropack-sdk/NitroPack/SDK/BacklogReplayTimeoutException.php ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ <?php
2
+ namespace NitroPack\SDK;
3
+
4
+ class BacklogReplayTimeoutException extends \RuntimeException {}
nitropack-sdk/NitroPack/SDK/NitroPack.php CHANGED
@@ -2,7 +2,7 @@
2
  namespace NitroPack\SDK;
3
 
4
  class NitroPack {
5
- const VERSION = '0.30.2';
6
  const PAGECACHE_LOCK_EXPIRATION_TIME = 300; // in seconds
7
  private $dataDir;
8
  private $cachePath = array('data', 'pagecache');
@@ -252,7 +252,8 @@ class NitroPack {
252
  $this->setHealthStatus(HealthStatus::HEALTHY);
253
  return HealthStatus::HEALTHY;
254
  } catch (\Exception $e) {
255
- return $this->getHealthStatus();
 
256
  }
257
  }
258
 
@@ -745,7 +746,7 @@ class NitroPack {
745
  $file = $this->getConfigFile();
746
 
747
  $config = array();
748
- if (Filesystem::fileExists($file) || $this->fetchConfig()) {
749
  $config = json_decode(Filesystem::fileGetContents($file));
750
  if (empty($config->SDKVersion) || $config->SDKVersion !== NitroPack::VERSION || empty($config->LastFetch) || time() - $config->LastFetch >= $this->configTTL) {
751
  if ($this->getHealthStatus() === HealthStatus::HEALTHY) {
2
  namespace NitroPack\SDK;
3
 
4
  class NitroPack {
5
+ const VERSION = '0.40.0';
6
  const PAGECACHE_LOCK_EXPIRATION_TIME = 300; // in seconds
7
  private $dataDir;
8
  private $cachePath = array('data', 'pagecache');
252
  $this->setHealthStatus(HealthStatus::HEALTHY);
253
  return HealthStatus::HEALTHY;
254
  } catch (\Exception $e) {
255
+ $this->setHealthStatus(HealthStatus::SICK);
256
+ return HealthStatus::SICK;
257
  }
258
  }
259
 
746
  $file = $this->getConfigFile();
747
 
748
  $config = array();
749
+ if (Filesystem::fileExists($file) || $this->fetchConfig(true)) {
750
  $config = json_decode(Filesystem::fileGetContents($file));
751
  if (empty($config->SDKVersion) || $config->SDKVersion !== NitroPack::VERSION || empty($config->LastFetch) || time() - $config->LastFetch >= $this->configTTL) {
752
  if ($this->getHealthStatus() === HealthStatus::HEALTHY) {
nitropack-sdk/bootstrap.php CHANGED
@@ -2,7 +2,11 @@
2
 
3
  require_once dirname(__FILE__) . "/autoload.php";
4
 
5
- $np_originalRequestCookies = $_COOKIE;
 
 
 
 
6
  /* You must define these constants before requiring this file for easier installation of SDK updates
7
  define("NITROPACK_HOME_URL", "Your home page URL");
8
  define("NITROPACK_SITE_ID", "your site ID");
@@ -16,6 +20,8 @@ if (!defined("NITROPACK_REDIS_HOST")) define("NITROPACK_REDIS_HOST", "127.0.0.1"
16
  if (!defined("NITROPACK_REDIS_PORT")) define("NITROPACK_REDIS_PORT", 6379); // Set this to the port of your Redis server
17
  if (!defined("NITROPACK_REDIS_PASS")) define("NITROPACK_REDIS_PASS", NULL); // Set this to the password of your redis server if authentication is needed
18
  if (!defined("NITROPACK_REDIS_DB")) define("NITROPACK_REDIS_DB", NULL); // Set this to the number of the Redis DB if you'd like to not use the default one
 
 
19
 
20
  if (NITROPACK_USE_REDIS) {
21
  NitroPack\SDK\Filesystem::setStorageDriver(new NitroPack\SDK\StorageDriver\Redis(
@@ -27,8 +33,7 @@ if (NITROPACK_USE_REDIS) {
27
  }
28
 
29
  function nitropack_filter_non_original_cookies(&$cookies) {
30
- global $np_originalRequestCookies;
31
- $ogNames = is_array($np_originalRequestCookies) ? array_keys($np_originalRequestCookies) : array();
32
  foreach ($cookies as $name=>$val) {
33
  if (!in_array($name, $ogNames)) {
34
  unset($cookies[$name]);
@@ -44,7 +49,11 @@ function nitropack_get_instance($siteId = NULL, $siteSecret = NULL, $url = NULL)
44
  NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies");
45
  $siteId = $siteId !== NULL ? $siteId : NITROPACK_SITE_ID;
46
  $siteSecret = $siteSecret !== NULL ? $siteSecret : NITROPACK_SITE_SECRET;
47
- $instances[$key] = new NitroPack\SDK\NitroPack($siteId, $siteSecret, NULL, $url);
 
 
 
 
48
  } catch(\Exception $e) {
49
  $instances[$key] = NULL;
50
  }
@@ -129,12 +138,11 @@ function is_valid_nitropack_beacon() {
129
  }
130
 
131
  function nitropack_handle_beacon() {
132
- global $np_originalRequestCookies;
133
  if (!empty($_POST["nitroBeaconUrl"])) {
134
  $url = base64_decode($_POST["nitroBeaconUrl"]);
135
 
136
  if (!empty($_POST["nitroBeaconCookies"])) {
137
- $np_originalRequestCookies = json_decode(base64_decode($_POST["nitroBeaconCookies"]), true);
138
  }
139
 
140
  if (null !== $nitro = nitropack_get_instance(NITROPACK_SITE_ID, NITROPACK_SITE_SECRET, $url) ) {
@@ -185,6 +193,7 @@ function nitropack_handle_webhook() {
185
  }
186
  } else {
187
  nitropack_sdk_purge_local();
 
188
  }
189
  break;
190
  }
@@ -272,6 +281,22 @@ function nitropack_sdk_purge_local($url = NULL) {
272
  return false;
273
  }
274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  function nitropack_filter_tag($tag) {
276
  return preg_replace("/[^a-zA-Z0-9:]/", ":", $tag);
277
  }
2
 
3
  require_once dirname(__FILE__) . "/autoload.php";
4
 
5
+ class NitroPack_CookieStore {
6
+ static $cookies = array();
7
+ }
8
+
9
+ NitroPack_CookieStore::$cookies = $_COOKIE;
10
  /* You must define these constants before requiring this file for easier installation of SDK updates
11
  define("NITROPACK_HOME_URL", "Your home page URL");
12
  define("NITROPACK_SITE_ID", "your site ID");
20
  if (!defined("NITROPACK_REDIS_PORT")) define("NITROPACK_REDIS_PORT", 6379); // Set this to the port of your Redis server
21
  if (!defined("NITROPACK_REDIS_PASS")) define("NITROPACK_REDIS_PASS", NULL); // Set this to the password of your redis server if authentication is needed
22
  if (!defined("NITROPACK_REDIS_DB")) define("NITROPACK_REDIS_DB", NULL); // Set this to the number of the Redis DB if you'd like to not use the default one
23
+ if (!defined("NITROPACK_DATA_DIR")) define("NITROPACK_DATA_DIR", NULL); // Set this to the number of the Redis DB if you'd like to not use the default one
24
+ if (!defined("NITROPACK_DISABLE_BACKLOG")) define("NITROPACK_DISABLE_BACKLOG", true); // Only allow backlog use if you've prepared a way to replay backlogged entries. Otherwise you might end up with a permanently disabled cache layer and an infinitely growing backlog file
25
 
26
  if (NITROPACK_USE_REDIS) {
27
  NitroPack\SDK\Filesystem::setStorageDriver(new NitroPack\SDK\StorageDriver\Redis(
33
  }
34
 
35
  function nitropack_filter_non_original_cookies(&$cookies) {
36
+ $ogNames = is_array(NitroPack_CookieStore::$cookies) ? array_keys(NitroPack_CookieStore::$cookies) : array();
 
37
  foreach ($cookies as $name=>$val) {
38
  if (!in_array($name, $ogNames)) {
39
  unset($cookies[$name]);
49
  NitroPack\SDK\NitroPack::addCookieFilter("nitropack_filter_non_original_cookies");
50
  $siteId = $siteId !== NULL ? $siteId : NITROPACK_SITE_ID;
51
  $siteSecret = $siteSecret !== NULL ? $siteSecret : NITROPACK_SITE_SECRET;
52
+ if (NITROPACK_DATA_DIR) {
53
+ $instances[$key] = new NitroPack\SDK\NitroPack($siteId, $siteSecret, NULL, $url, NITROPACK_DATA_DIR);
54
+ } else {
55
+ $instances[$key] = new NitroPack\SDK\NitroPack($siteId, $siteSecret, NULL, $url);
56
+ }
57
  } catch(\Exception $e) {
58
  $instances[$key] = NULL;
59
  }
138
  }
139
 
140
  function nitropack_handle_beacon() {
 
141
  if (!empty($_POST["nitroBeaconUrl"])) {
142
  $url = base64_decode($_POST["nitroBeaconUrl"]);
143
 
144
  if (!empty($_POST["nitroBeaconCookies"])) {
145
+ NitroPack_CookieStore::$cookies = json_decode(base64_decode($_POST["nitroBeaconCookies"]), true);
146
  }
147
 
148
  if (null !== $nitro = nitropack_get_instance(NITROPACK_SITE_ID, NITROPACK_SITE_SECRET, $url) ) {
193
  }
194
  } else {
195
  nitropack_sdk_purge_local();
196
+ nitropack_sdk_delete_backlog();
197
  }
198
  break;
199
  }
281
  return false;
282
  }
283
 
284
+ function nitropack_sdk_delete_backlog() {
285
+ if (null !== $nitro = nitropack_get_instance()) {
286
+ try {
287
+ if ($nitro->backlog->exists()) {
288
+ $nitro->backlog->delete();
289
+ }
290
+ } catch (\Exception $e) {
291
+ return false;
292
+ }
293
+
294
+ return true;
295
+ }
296
+
297
+ return false;
298
+ }
299
+
300
  function nitropack_filter_tag($tag) {
301
  return preg_replace("/[^a-zA-Z0-9:]/", ":", $tag);
302
  }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: cache,perfomance,optimize,pagespeed,lazy load,cdn,critical css,compression
4
  Requires at least: 4.7
5
  Tested up to: 5.9
6
  Requires PHP: 5.3
7
- Stable tag: 1.5.9
8
  License: GNU General Public License, version 2
9
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -169,6 +169,11 @@ No. We’ve designed NitroPack to be a very lightweight solution that adds no CP
169
 
170
  == Changelog ==
171
 
 
 
 
 
 
172
  = 1.5.9 =
173
  * New Feature: Compatibility with DreamPress hosting
174
  * Improvement: Full compatibility with Pressable's caching layer
4
  Requires at least: 4.7
5
  Tested up to: 5.9
6
  Requires PHP: 5.3
7
+ Stable tag: 1.5.10
8
  License: GNU General Public License, version 2
9
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
10
 
169
 
170
  == Changelog ==
171
 
172
+ = 1.5.10 =
173
+ * Improvement: Overall stability improvements
174
+ * Bug fix: Fix 'headers already sent' error in CLI and WP_CRON modes
175
+ * Bug fix: Fix cases of missing elements in Fusion Builder
176
+
177
  = 1.5.9 =
178
  * New Feature: Compatibility with DreamPress hosting
179
  * Improvement: Full compatibility with Pressable's caching layer
view/connect.php CHANGED
@@ -1,4 +1,4 @@
1
- <div id="nitropack-container" class="wrap">
2
  <div class="row">
3
  <div class="col-md-12">
4
  <div id="login-container">
1
+ <div id="nitropack-container" class="wrap" style="visibility: hidden">
2
  <div class="row">
3
  <div class="col-md-12">
4
  <div id="login-container">
view/stylesheet/nitropack.css CHANGED
@@ -941,3 +941,7 @@ input:checked + .slider:before {
941
  li.list-group-item-indented {
942
  padding-left: 30px!important;
943
  }
 
 
 
 
941
  li.list-group-item-indented {
942
  padding-left: 30px!important;
943
  }
944
+
945
+ #nitropack-container {
946
+ visibility:visible !important;
947
+ }