PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.7
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.7
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / WordPress / NitroPack.php
nitropack / classes / WordPress Last commit date
Notifications 2 months ago Settings 1 month ago Admin.php 1 month ago CLI.php 2 weeks ago Config.php 1 year ago ConflictingPlugins.php 10 months ago Connect.php 2 weeks ago Cron.php 1 year ago Invalidations.php 2 months ago NitroPack.php 2 weeks ago Settings.php 4 months ago
NitroPack.php
649 lines
1 <?php
2
3 /**
4 * Nitropack Class
5 *
6 * @package nitropack
7 */
8
9 namespace NitroPack\WordPress;
10
11 use \NitroPack\SDK\Filesystem;
12 use \NitroPack\Feature\Logger\Logger as Logger;
13 use \NitroPack\Feature\Logger\LoggingEvents as LoggingEvents;
14 use NitroPack\WordPress\Connect;
15
16 class NitroPack {
17 private static $instance = NULL;
18 public static $nitroDirMigrated = false;
19 public static $nitroConfigMigrated = false;
20 public static $preUpdatePosts = array();
21 public static $preUpdateTaxonomies = array();
22 public static $preUpdateMeta = array();
23 public static $ignoreUpdatePostIDs = array();
24 public static $np_loggedWarmups = array();
25 public static $optionsToCache = [
26 'cache_handler_cache_handler',
27 'woocommerce_default_customer_address',
28 [ 'ajaxShortcodes' => [ 'enabled' => false, 'shortcodes' => [] ] ],
29 [ "wc_aelia_currency_switcher" => "ipgeolocation_enabled" ]
30 ];
31
32 public static function getInstance() {
33 if ( ! self::$instance ) {
34 self::$instance = new NitroPack();
35 }
36
37 return self::$instance;
38 }
39
40 private $sdkObjects;
41 private $disabledReason;
42
43 /**
44 * Page type.
45 *
46 * @var null
47 */
48 private $pageType;
49 /**
50 * Logger instance.
51 *
52 * @var Logger
53 */
54 private $logger;
55 /**
56 * LoggingEvents instance.
57 *
58 * @var LoggingEvents
59 */
60 private $loggingEvents;
61 /**
62 * Logger instance.
63 *
64 * @var Connect
65 */
66 private $connect;
67 /**
68 * Config instance.
69 *
70 * @var Config
71 */
72 public $Config;
73
74 /**
75 * Notifications instance.
76 *
77 * @var Notifications
78 */
79 public $Notifications;
80
81 /**
82 * Notifications instance.
83 *
84 * @var AppNotifications
85 */
86 public $AppNotifications;
87
88 /**
89 * @var array $settings Configuration settings for NitroPack.
90 */
91 public $settings;
92 public function __construct() {
93 $this->Config = new Config();
94 $this->Notifications = Notifications\Notifications::getInstance();
95 $this->settings = new Settings( $this->Config );
96 $this->logger = new Logger( $this );
97 $this->connect = new Connect();
98 $this->loggingEvents = new LoggingEvents( $this->logger );
99 $this->sdkObjects = array();
100 $this->disabledReason = NULL;
101 $this->pageType = NULL;
102 }
103
104 public static function getDataDir() {
105 $isRaidBoxes = \NitroPack\Integration\Hosting\Raidboxes::detect();
106 $isPantheon = \NitroPack\Integration\Hosting\Pantheon::detect();
107 $isWpe = \NitroPack\Integration\Hosting\WPEngine::detect();
108 $isCloudways = \NitroPack\Integration\Hosting\Cloudways::detect();
109 $isRocketNet = \NitroPack\Integration\Hosting\RocketNet::detect();
110 $currentFilePath = __FILE__;
111 $wpContentDir = WP_CONTENT_DIR;
112 $pathResolved = self::resolvePathNavigation( $wpContentDir );
113
114 if ( $pathResolved != $wpContentDir ) {
115 // Handle existing installations which used to have either /./ or /../ in their content dir paths
116 $wpContentDir = $pathResolved;
117 self::$nitroDirMigrated = true;
118 }
119
120 if ( $isWpe ) {
121 $currentFilePath = preg_replace( "@^/sites/@", "/nas/content/live/", $currentFilePath );
122 $wpContentDir = preg_replace( "@^/sites/@", "/nas/content/live/", $wpContentDir );
123 }
124
125 $oldNitroDirs = [
126 nitropack_trailingslashit( $wpContentDir ) . 'nitropack',
127 nitropack_trailingslashit( $wpContentDir ) . 'cache/' . substr( md5( $currentFilePath ), 0, 7 ) . "-nitropack",
128 ];
129 $newNitroDir = nitropack_trailingslashit( $wpContentDir ) . 'cache/' . NITROPACK_CACHE_DIR_NAME;
130 $nitroDir = $newNitroDir;
131
132 if ( $isRaidBoxes || $isRocketNet || $isCloudways ) {
133 $nitroDir = $oldNitroDirs[0];
134 } else if ( $isPantheon ) {
135 $nitroDir = nitropack_trailingslashit( $wpContentDir ) . 'uploads/' . NITROPACK_CACHE_DIR_NAME;
136 }
137
138 $possibleDirs = array_unique( array_merge( $oldNitroDirs, [ $newNitroDir ] ) );
139 $existingDirs = [];
140
141 foreach ( $possibleDirs as $possibleDir ) {
142 if ( Filesystem::fileExists( $possibleDir ) ) {
143 $existingDirs[] = $possibleDir;
144 }
145 }
146
147 if ( count( $existingDirs ) == 1 ) {
148 $existingDir = array_shift( $existingDirs );
149 if ( is_link( $existingDir ) ) {
150 $existingDir = readlink( $existingDir );
151 }
152
153 if ( $existingDir != $nitroDir ) {
154 if ( ! Filesystem::fileExists( $nitroDir ) && ! NITROPACK_USE_REDIS ) {
155 // Existing installation, move to the new location
156
157 if ( Filesystem::createDir( dirname( $nitroDir ) ) && Filesystem::rename( $existingDir, $nitroDir ) ) {
158 self::$nitroDirMigrated = true;
159 } else {
160 define( 'NITROPACK_DATA_DIR_WARNING', 'Unable to initialize cache dir because the PHP user does not have permission to create/rename directories under wp-content/. Running in legacy mode. Please contact support for help.' );
161 $nitroDir = $existingDir;
162 }
163 }
164 }
165 }
166
167 return $nitroDir;
168 }
169
170 public static function getPluginDataDir() {
171 $isPantheon = \NitroPack\Integration\Hosting\Pantheon::detect();
172 $isWpe = \NitroPack\Integration\Hosting\WPEngine::detect();
173 $nitroDir = nitropack_trailingslashit( WP_CONTENT_DIR ) . 'config-' . NITROPACK_CACHE_DIR_NAME;
174 $expectedNitroDir = $nitroDir;
175
176 $nitroDir = self::resolvePathNavigation( $nitroDir );
177 if ( $nitroDir != $expectedNitroDir ) {
178 // Handle existing installations which used to have either /./ or /../ in their config paths
179 $expectedNitroDir = $nitroDir;
180 self::$nitroConfigMigrated = true;
181 }
182
183 if ( $isWpe ) {
184 $nitroDir = preg_replace( "@^/sites/@", "/nas/content/live/", $nitroDir );
185 $expectedNitroDir = $nitroDir;
186 }
187
188 if ( $isPantheon ) {
189 $nitroDir = nitropack_trailingslashit( WP_CONTENT_DIR ) . 'uploads/config-' . NITROPACK_CACHE_DIR_NAME;
190 $expectedNitroDir = $nitroDir;
191 }
192
193 $oldConfigFile = nitropack_trailingslashit( NITROPACK_DATA_DIR ) . 'config.json';
194 $newConfigFile = nitropack_trailingslashit( $nitroDir ) . 'config.json';
195
196 if ( $isWpe ) {
197 if ( ! Filesystem::fileExists( $newConfigFile ) && Filesystem::fileExists( preg_replace( "@^/nas/content/live/@", "/sites/", $oldConfigFile ) ) ) {
198 $newConfigFile = preg_replace( "@^/nas/content/live/@", "/sites/", $newConfigFile );
199 }
200 }
201
202 if ( Filesystem::fileExists( $oldConfigFile ) && ! Filesystem::fileExists( $newConfigFile ) ) {
203 // Existing installation, move config to the new location
204
205 if ( Filesystem::createDir( $nitroDir ) && Filesystem::rename( $oldConfigFile, $newConfigFile ) ) {
206 self::$nitroConfigMigrated = true;
207 $oldHtaccessFile = nitropack_trailingslashit( NITROPACK_DATA_DIR ) . '.htaccess';
208 $newHtaccessFile = nitropack_trailingslashit( $nitroDir ) . '.htaccess';
209 if ( Filesystem::fileExists( $oldHtaccessFile ) && ! Filesystem::fileExists( $newHtaccessFile ) ) {
210 Filesystem::filePutContents( $newHtaccessFile, Filesystem::fileGetContents( $oldHtaccessFile ) );
211 }
212 } else {
213 define( 'NITROPACK_PLUGIN_DATA_DIR_WARNING', 'Unable to initialize plugin data dir because the PHP user does not have permission to create/rename directories under wp-content/. Running in legacy mode. Please contact support for help.' );
214 $nitroDir = NITROPACK_DATA_DIR;
215 }
216 }
217
218 return $nitroDir;
219 }
220
221 public static function isWpCli() {
222 return defined( "WP_CLI" ) && WP_CLI;
223 }
224 public function getDistribution() {
225 $dist = "regular";
226 $dbDist = NULL;
227
228 try {
229 if ( function_exists( "get_option" ) ) {
230 $dbDist = get_option( "nitropack-distribution", NULL );
231 }
232
233 if ( $this->isConnected() ) {
234 $sdk = $this->getSdk();
235 if ( ! $sdk )
236 return $dbDist ? $dbDist : $dist;
237
238 $config = $sdk->getConfig();
239 if ( $config ) {
240 $dist = $config->Distribution;
241 }
242 } else if ( $dbDist !== NULL ) {
243 $dist = $dbDist;
244 }
245
246 if ( $dbDist != $dist && function_exists( "update_option" ) ) {
247 update_option( "nitropack-distribution", $dist );
248 }
249
250 return $dist;
251 } catch (Exception $e) {
252 return $dist;
253 }
254 }
255
256 public function getSiteConfig() {
257 $siteConfig = null;
258 $npConfig = $this->Config->get();
259 $currentUrl = $this->getCurrentUrl();
260
261 $matchLength = 0;
262
263 foreach ( $npConfig as $siteUrl => $config ) {
264 if ( stripos( $siteUrl, "www." ) === 0 ) {
265 $siteUrl = substr( $siteUrl, 4 );
266 }
267
268 if ( stripos( $currentUrl, $siteUrl ) === 0 && strlen( $siteUrl ) > $matchLength ) {
269 $siteConfig = $config;
270 $matchLength = strlen( $siteUrl );
271 }
272 }
273
274 if ( ! $siteConfig ) {
275 $matchLength = 0;
276 foreach ( $npConfig as $siteUrl => $config ) {
277 if ( isset( $config['additional_domains'] ) ) {
278 foreach ( $config['additional_domains'] as $additionalDomain ) {
279 if ( stripos( $additionalDomain, "www." ) === 0 ) {
280 $additionalDomain = substr( $additionalDomain, 4 );
281 }
282
283 if ( stripos( $currentUrl, $additionalDomain ) === 0 && strlen( $additionalDomain ) > $matchLength ) {
284 $siteConfig = $config;
285 $matchLength = strlen( $additionalDomain );
286 }
287 }
288 }
289 }
290 }
291
292 return $siteConfig;
293 }
294
295 public function getSiteId() {
296 $siteConfig = $this->getSiteConfig();
297 return $siteConfig ? $siteConfig["siteId"] : NULL;
298 }
299
300 public function getSiteSecret() {
301 $siteConfig = $this->getSiteConfig();
302 return $siteConfig ? $siteConfig["siteSecret"] : NULL;
303 }
304
305 /**
306 * Bear in mind that get_home_url() is not defined in the context of advanced_cache.php
307 * so this will throw a fatal error if you call it at that point!
308 */
309 public static function getConfigKey() {
310 return preg_replace( "/^https?:\/\/(.*)/", "$1", get_home_url() );
311 }
312
313 public function getAdditionalDomains( $siteId, $siteSecret ) {
314 if ( null !== $nitro = $this->getSdk( $siteId, $siteSecret ) ) {
315 $config = $nitro->getConfig();
316 if ( ! property_exists( $config->AdditionalDomains, 'Domains' ) ) {
317 $nitro->fetchConfig();
318 }
319 return $config->AdditionalDomains->Domains;
320 }
321
322 return [];
323 }
324
325 public function isConnected() {
326 return ! empty( $this->getSiteId() ) && ! empty( $this->getSiteSecret() );
327 }
328
329 public function updateCurrentBlogConfig( $siteId, $siteSecret, $blogId, $enableCompression = null ) {
330 if ( $enableCompression === null ) {
331 $enableCompression = ( get_option( 'nitropack-enableCompression' ) == 1 );
332 }
333
334 $webhookToken = get_option( 'nitropack-webhookToken' );
335 $hosting = nitropack_detect_hosting();
336
337 $home_url = get_home_url();
338 $admin_url = admin_url();
339 $alwaysBuffer = defined( "NITROPACK_ALWAYS_BUFFER" ) ? NITROPACK_ALWAYS_BUFFER : true;
340 $configKey = self::getConfigKey();
341 $staticConfig = $this->Config->get();
342 //grab existing ajaxShortcodes settings
343 if ( isset( $staticConfig[ $configKey ]['options_cache']['ajaxShortcodes'] ) ) {
344 $ajaxShortcodes_settings = $staticConfig[ $configKey ]['options_cache']['ajaxShortcodes'];
345 }
346 //default value is null, stored is int
347 $minimumLogLevel = (int) get_option( 'nitropack-minimumLogLevel', null );
348 if ( ! $minimumLogLevel ) {
349 $minimumLogLevel = null;
350 }
351 $staticConfig[ $configKey ] = array(
352 "siteId" => $siteId,
353 "siteSecret" => $siteSecret,
354 "blogId" => $blogId,
355 "compression" => $enableCompression,
356 "webhookToken" => $webhookToken,
357 "home_url" => $home_url,
358 "admin_url" => $admin_url,
359 "hosting" => $hosting,
360 "alwaysBuffer" => $alwaysBuffer,
361 "isEzoicActive" => \NitroPack\Integration\Plugin\Ezoic::isActive(),
362 "isApoActive" => \NitroPack\Integration\Plugin\Cloudflare::isApoActive(),
363 "isNginxHelperActive" => \NitroPack\Integration\Plugin\NginxHelper::isActive(),
364 "isLateIntegrationInitRequired" => nitropack_is_late_integration_init_required(),
365 "isDlmActive" => \NitroPack\Integration\Plugin\DownloadManager::isActive(),
366 "isWoocommerceCacheHandlerActive" => \NitroPack\Integration\Plugin\WoocommerceCacheHandler::isActive(),
367 "isWoocommerceActive" => \NitroPack\Integration\Plugin\WooCommerce::isActive(),
368 "isAeliaCurrencySwitcherActive" => \NitroPack\Integration\Plugin\AeliaCurrencySwitcher::isActive(),
369 "isGeoTargetingWPActive" => \NitroPack\Integration\Plugin\GeoTargetingWP::isActive(),
370 "dlm_downloading_url" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadingUrl() : NULL,
371 "dlm_download_endpoint" => \NitroPack\Integration\Plugin\DownloadManager::isActive() ? \NitroPack\Integration\Plugin\DownloadManager::downloadEndpoint() : NULL,
372 "pluginVersion" => NITROPACK_VERSION,
373 "options_cache" => [],
374 "additional_domains" => $this->getAdditionalDomains( $siteId, $siteSecret ),
375 "minimumLogLevel" => $minimumLogLevel,
376 );
377
378 foreach ( self::$optionsToCache as $opt ) {
379 if ( is_array( $opt ) ) {
380 foreach ( $opt as $option => $suboption ) {
381 // Ensure the top-level option array exists
382 if ( ! isset( $staticConfig[ $configKey ]["options_cache"][ $option ] ) || ! is_array( $staticConfig[ $configKey ]["options_cache"][ $option ] ) ) {
383 $staticConfig[ $configKey ]["options_cache"][ $option ] = [];
384 }
385
386 // Handle cases where the suboption is itself an array (e.g., 'shortcodes')
387 if ( is_array( $suboption ) ) {
388 foreach ( $suboption as $suboptKey => $suboptValue ) {
389 // Ensure the nested array exists
390 if ( ! isset( $staticConfig[ $configKey ]["options_cache"][ $option ][ $suboptKey ] ) || ! is_array( $staticConfig[ $configKey ]["options_cache"][ $option ][ $suboptKey ] ) ) {
391 $staticConfig[ $configKey ]["options_cache"][ $option ][ $suboptKey ] = [];
392 }
393 $optionValue = get_option( $option );
394
395 if ( ! empty( $optionValue ) && is_array( $optionValue ) && isset( $optionValue[ $suboptKey ] ) ) {
396 $staticConfig[ $configKey ]["options_cache"][ $option ][ $suboptKey ] = $optionValue[ $suboptKey ];
397 } else {
398 $staticConfig[ $configKey ]["options_cache"][ $option ][ $suboptKey ] = $suboptValue;
399 }
400 }
401 } else {
402 $optionValue = get_option( $option );
403
404 if ( ! empty( $optionValue ) && is_array( $optionValue ) && isset( $optionValue[ $suboption ] ) ) {
405 $staticConfig[ $configKey ]["options_cache"][ $option ][ $suboption ] = $optionValue[ $suboption ];
406 } else {
407 $staticConfig[ $configKey ]["options_cache"][ $option ][ $suboption ] = null;
408 }
409 }
410 }
411 } else {
412 // Handle non-array options directly
413 $staticConfig[ $configKey ]["options_cache"][ $opt ] = get_option( $opt );
414 }
415 }
416 //handle existing options which we want to re-use
417 if ( ! empty( $ajaxShortcodes_settings ) )
418 $staticConfig[ $configKey ]["options_cache"]['ajaxShortcodes'] = $ajaxShortcodes_settings;
419
420 $configSetResult = $this->Config->set( $staticConfig );
421
422 if ( \NitroPack\Integration\Plugin\GeoTargetingWP::isActive() ) {
423 try {
424 \NitroPack\Integration\Plugin\GeoTargetingWP::configureVariationCookies();
425 } catch (\Exception $e) {
426 $this->getLogger()->error( $e->getMessage() );
427 }
428 }
429
430 return $configSetResult;
431 }
432
433 public function unsetCurrentBlogConfig() {
434 $configKey = self::getConfigKey();
435 $staticConfig = $this->Config->get();
436 if ( ! empty( $staticConfig[ $configKey ] ) ) {
437 unset( $staticConfig[ $configKey ] );
438 return $this->Config->set( $staticConfig );
439 }
440
441 return true;
442 }
443
444 public function resetSdkInstances() {
445 $this->sdkObjects = [];
446 }
447
448 public function getSdk( $siteId = null, $siteSecret = null, $urlOverride = NULL, $forwardExceptions = false ) {
449 $siteConfig = $this->getSiteConfig();
450
451 $siteId = $siteId ?: ( ! empty( $siteConfig ) ? $siteConfig['siteId'] : NULL );
452 $siteSecret = $siteSecret ?: ( ! empty( $siteConfig ) ? $siteConfig['siteSecret'] : NULL );
453
454 if ( $siteId && $siteSecret ) {
455 try {
456 $userAgent = NULL; // It will be automatically detected by the SDK
457 $dataDir = nitropack_trailingslashit( NITROPACK_DATA_DIR ) . $siteId; // dir without a trailing slash, because this is how the SDK expects it
458 $cacheKey = "{$siteId}:{$siteSecret}:{$dataDir}";
459
460 if ( $urlOverride ) {
461 $cacheKey .= ":{$urlOverride}";
462 }
463
464 if ( ! empty( $this->sdkObjects[ $cacheKey ] ) ) {
465 $nitro = $this->sdkObjects[ $cacheKey ];
466 } else {
467 if ( ! defined( "NP_COOKIE_FILTER" ) ) {
468 \NitroPack\SDK\NitroPack::addCookieFilter( "nitropack_filter_non_original_cookies" );
469 define( "NP_COOKIE_FILTER", true );
470 do_action( 'np_set_cookie_filter' );
471 }
472 if ( ! defined( "NP_STORAGE_CONFIGURED" ) ) {
473 if ( defined( "NITROPACK_USE_REDIS" ) && NITROPACK_USE_REDIS ) {
474 \NitroPack\SDK\Filesystem::setStorageDriver( new \NitroPack\SDK\StorageDriver\Redis(
475 NITROPACK_REDIS_HOST,
476 NITROPACK_REDIS_PORT,
477 NITROPACK_REDIS_PASS,
478 NITROPACK_REDIS_DB
479 ) );
480 }
481 define( "NP_STORAGE_CONFIGURED", true );
482 }
483
484 if ( ! defined( 'NP_GEOLOCATION_PREFIX_DEFINED' ) ) {
485 do_action( 'set_nitropack_geo_cache_prefix' );
486 define( 'NP_GEOLOCATION_PREFIX_DEFINED', true );
487 }
488
489 if ( defined( "NITROPACK_CUSTOM_CACHE_PREFIX" ) && ! defined( 'NP_CUSTOM_CACHE_PREFIX_SET' ) ) {
490 \NitroPack\SDK\NitroPack::addCustomCachePrefix( NITROPACK_CUSTOM_CACHE_PREFIX );
491 define( 'NP_CUSTOM_CACHE_PREFIX_SET', true );
492 }
493
494 $nitro = new \NitroPack\SDK\NitroPack( $siteId, $siteSecret, $userAgent, $urlOverride, $dataDir );
495 $this->sdkObjects[ $cacheKey ] = $nitro;
496 }
497 } catch (\Exception $e) {
498 if ( $forwardExceptions ) {
499 throw $e;
500 }
501 return NULL;
502 }
503
504 return $nitro;
505 }
506
507 return NULL;
508 }
509
510 /**
511 * Check if the data directory exists
512 *
513 * @return bool
514 */
515 public function dataDirExists() {
516 return defined( "NITROPACK_DATA_DIR" ) && is_dir( NITROPACK_DATA_DIR ); // TODO: Convert this to use the Filesystem abstraction for better Redis support
517 }
518
519 /**
520 * Initialize the data directory
521 *
522 * @return bool
523 */
524 public function initDataDir() {
525 return $this->dataDirExists() || @mkdir( NITROPACK_DATA_DIR, 0755, true ); // TODO: Convert this to use the Filesystem abstraction for better Redis support
526 }
527
528 public function pluginDataDirExists() {
529 return defined( "NITROPACK_PLUGIN_DATA_DIR" ) && is_dir( NITROPACK_PLUGIN_DATA_DIR ); // TODO: Convert this to use the Filesystem abstraction for better Redis support
530 }
531
532 public function initPluginDataDir() {
533 return $this->pluginDataDirExists() || @mkdir( NITROPACK_PLUGIN_DATA_DIR, 0755, true ); // TODO: Convert this to use the Filesystem abstraction for better Redis support
534 }
535
536 public function setDisabledReason( $reason ) {
537 $this->disabledReason = $reason;
538 nitropack_header( "X-Nitro-Disabled-Reason: $reason" );
539 }
540
541 public function getDisabledReason() {
542 return $this->disabledReason;
543 }
544
545 public function setPageType( $type ) {
546 $this->pageType = $type;
547 }
548
549 public function getPageType() {
550 return $this->pageType;
551 }
552
553 /**
554 * Get current url
555 *
556 * @return string The current url
557 */
558 public function getCurrentUrl() {
559
560 if ( defined( 'NITROPACK_HOST' ) ) {
561
562 return NITROPACK_HOST;
563 }
564
565 if ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
566 $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
567 } else {
568 $host = ! empty( $_SERVER["HTTP_HOST"] ) ? $_SERVER["HTTP_HOST"] : "";
569 }
570
571 $uri = ! empty( $_SERVER["REQUEST_URI"] ) ? $_SERVER["REQUEST_URI"] : "";
572 $currentUrl = $host . $uri;
573
574 if ( empty( $currentUrl ) || ( defined( 'WP_CLI' ) && WP_CLI && trim( $currentUrl ) == "localhost" ) ) {
575
576 if ( function_exists( 'get_site_url' ) ) {
577 $host = apply_filters( 'nitropack_current_host', get_site_url() );
578 } elseif ( function_exists( 'get_option' ) ) {
579 $host = apply_filters( 'nitropack_current_host', get_option( 'siteurl' ) );
580 }
581
582 if ( $host != '' ) {
583 $site_url = parse_url( $host );
584 if ( is_array( $site_url ) && isset( $site_url["host"] ) && ! empty( $site_url["host"] ) ) {
585 $currentUrl = $site_url["host"];
586 }
587 }
588 }
589
590 if ( stripos( $currentUrl, "www." ) === 0 ) {
591 $currentUrl = substr( $currentUrl, 4 );
592 }
593
594 return $currentUrl;
595 }
596
597 /**
598 * Get current host
599 *
600 * @return string The current host
601 */
602 public function getCurrentHost() {
603
604 if ( ! empty( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) {
605 $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
606 } else {
607 $host = ! empty( $_SERVER["HTTP_HOST"] ) ? $_SERVER["HTTP_HOST"] : "";
608 }
609
610 if ( empty( $host ) ) {
611
612 $site_url = parse_url( apply_filters( 'nitropack_current_host', ( function_exists( 'get_site_url' ) ? get_site_url() : '' ) ) );
613 $host = $site_url["host"];
614 }
615
616 return $host;
617 }
618
619 /**
620 * Get logger instance
621 *
622 * @return Logger
623 */
624 public function getLogger() {
625 return $this->logger;
626 }
627
628 private static function resolvePathNavigation( $path ) {
629 if ( strpos( $path, './' ) !== false ) {
630 $path_parts = explode( '/', $path );
631 $final_parts = array();
632
633 foreach ( $path_parts as $part ) {
634 if ( $part == "." ) {
635 continue;
636 } else if ( $part == '..' ) {
637 array_pop( $final_parts );
638 } else {
639 $final_parts[] = $part;
640 }
641 }
642
643 $path = implode( '/', $final_parts );
644 }
645
646 return $path;
647 }
648 }
649