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