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