PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.0
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 / Notifications / Notifications.php
nitropack / classes / WordPress / Notifications Last commit date
AppNotifications.php 1 year ago Notifications.php 1 year ago
Notifications.php
612 lines
1 <?php
2 namespace NitroPack\WordPress\Notifications;
3
4 use NitroPack\WordPress\Settings\TestMode;
5 /*
6 * Class Notifications
7 *
8 * This class handles the notifications for the NitroPack plugin in WordPress.
9 *
10 * @package NitroPack\WordPress\Notifications
11 */
12 class Notifications {
13 private static $instance = NULL;
14 public function __construct() {
15 add_action( 'admin_notices', [ $this, 'nitropack_admin_notices' ] );
16
17 /* Using 'init' because it fixes issue when get_home_url() in updateCurrentBlogConfig() is not found in multisites */
18 add_action( 'init', function () {
19 add_action( 'plugins_loaded', [ $this, 'nitropack_plugin_notices' ] );
20 });
21
22 //ajax
23 add_action( 'wp_ajax_nitropack_safemode_notification', [ $this, 'nitropack_safemode_notification' ] );
24 add_action( 'wp_ajax_nitropack_dismiss_notification', [ $this, 'nitropack_dismiss_notification' ] );
25 //WC notice
26 add_action( 'wp_ajax_nitropack_dismiss_woocommerce_notice', [ $this, 'nitropack_dismiss_woocommerce_notice' ] );
27 //modal for non-optimized CPTs notice
28 add_action( 'wp_ajax_nitropack_dismiss_notice_forever', [ $this, 'nitropack_dismiss_notice_forever' ] );
29
30 }
31 public static function getInstance() {
32 if ( ! self::$instance ) {
33 self::$instance = new Notifications();
34 }
35
36 return self::$instance;
37 }
38 /**
39 * Displays general admin notices for the NitroPack plugin in WordPress dashboard.
40 *
41 * @return void
42 */
43 public function nitropack_admin_notices() {
44 $components = new \NitroPack\WordPress\Settings\Components;
45 if ( defined( 'NITROPACK_DATA_DIR_WARNING' ) ) {
46 $components->render_notification( NITROPACK_DATA_DIR_WARNING, 'warning', 'Unable to initialize cache dir' );
47 }
48
49 if ( defined( 'NITROPACK_PLUGIN_DATA_DIR_WARNING' ) ) {
50 $components->render_notification( NITROPACK_PLUGIN_DATA_DIR_WARNING, 'warning', 'Unable to initialize plugin data dir' );
51 }
52
53 if ( ! empty( $_COOKIE["nitropack_after_activate_notice"] ) ) {
54 $components->render_notification( "<script>document.cookie = 'nitropack_after_activate_notice=1; expires=Thu, 01 Jan 1970 00:00:01 GMT;';</script> Go to its' settings to connect it in order to start optimizing your site.",
55 'info',
56 esc_html__( 'NitroPack activated but not connected', 'nitropack' ),
57 '<a href="' . admin_url( 'admin.php?page=nitropack' ) . '">' . esc_html__( 'NitroPack Settings', 'nitropack' ) . '</a>' );
58 }
59
60 $this->render_app_notifications();
61 $this->nitropack_print_hosting_notice();
62 $this->nitropack_print_woocommerce_notice();
63 }
64 /**
65 * Display NitroPack plugin notices in the WordPress admin area.
66 *
67 * This function is responsible for showing various notifications related to the NitroPack plugin.
68 *
69 * @return null|array
70 */
71 public function nitropack_plugin_notices() {
72 if ( ! $this->pass_notification_capabilities() )
73 return;
74
75 static $npPluginNotices = NULL;
76
77 if ( $npPluginNotices !== NULL ) {
78 return $npPluginNotices;
79 }
80
81 $errors = [];
82 $warnings = [];
83 $infos = [];
84
85 /* Sets a warning if there are any conflicit plugins - mostly caching plugins. */
86 $conflictingPlugins = nitropack_get_conflicting_plugins();
87 foreach ( $conflictingPlugins as $clashingPlugin ) {
88 $warnings[] = array(
89 'title' => sprintf( "%s is active", $clashingPlugin ),
90 'msg' => sprintf( __( "NitroPack and %s have overlapping functionality and can interfere with each other. Please deactivate %s for best results in NitroPack.", 'nitropack' ), $clashingPlugin, $clashingPlugin ),
91 'actions' => '<a href="' . admin_url() . 'plugins.php" target="_blank" class="btn btn-secondary">Plugins page</a>',
92 'classes' => [ 'conflicting-plugins plugin-' . sanitize_title( $clashingPlugin ) ],
93 );
94 }
95
96 /* Add residual cache notices if found */
97 $residualCachePlugins = \NitroPack\Integration\Plugin\RC::detectThirdPartyCaches();
98 foreach ( $residualCachePlugins as $rcpName ) {
99 $warnings[] = array(
100 'title' => esc_html__( "Residual cache files", 'nitropack' ),
101 'msg' => sprintf( esc_html__( 'We found residual cache files from %s. These files can interfere with the caching process and must be deleted.', 'nitropack' ), $rcpName, $rcpName ),
102 'actions' => '<a class="btn btn-warning" nitropack-rc-data="' . $rcpName . '">' . esc_html__( 'Delete now', 'nitropack' ) . '</a>',
103 );
104 }
105 /* Sets a warning if there is any activity in the plugins such as new activations, updates, or deletions. */
106 if ( isset( $_COOKIE['nitropack_apwarning'] ) ) {
107 $cookie_path = nitropack_cookiepath();
108 $warnings[] = array(
109 'title' => esc_html__( "Plugins activity", 'nitropack' ),
110 'msg' => esc_html__( 'It seems plugins have been activated, deactivated or updated. It is recommended that you purge the cache to reflect the latest changes.', 'nitropack' ),
111 'actions' => "<a class=\"btn btn-secondary\" href=\"javascript:void(0);\" id=\"np-onstate-cache-purge\" onclick=\"document.cookie = 'nitropack_apwarning=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=$cookie_path';window.location.reload();\">" . esc_html__( 'Dismiss', 'nitropack' ) . "</a>",
112 'classes' => [ 'plugins-state' ],
113 );
114 }
115
116 /* Sets a warning if the Test Mode is enabled. */
117 $smStatus = get_option( 'nitropack-safeModeStatus', "-1" );
118 if ( $smStatus === "-1" ){
119 $smStatus = TestMode::getInstance()->nitropack_safemode_status(true);
120 }
121 if ( $smStatus ) {
122 $safeModeMessage = __( 'Visitors are accessing your unoptimized pages. Make sure to disable it once you are done testing.', 'nitropack' );
123 if ( get_nitropack()->getDistribution() == "oneclick" ) {
124 $safeModeMessage = apply_filters( "nitropack_oneclick_safemode_message", $safeModeMessage );
125 }
126
127 $warnings[] = array(
128 'title' => esc_html__( "Test Mode Enabled", 'nitropack' ),
129 'msg' => $safeModeMessage,
130 'classes' => [ 'test-mode' ],
131 );
132 }
133
134 $nitropackIsConnected = get_nitropack()->isConnected();
135
136 if ( $nitropackIsConnected ) {
137 if ( nitropack_is_advanced_cache_allowed() ) {
138 $notification_title = esc_html__( "File advanced-cache.php cannot be created", 'nitropack' );
139 $notification_class = [ 'advanced-cache' ];
140
141 if ( ! nitropack_has_advanced_cache() ) {
142
143 $advancedCacheFile = nitropack_trailingslashit( WP_CONTENT_DIR ) . 'advanced-cache.php';
144 if ( ! file_exists( $advancedCacheFile ) || strpos( file_get_contents( $advancedCacheFile ), "NITROPACK_ADVANCED_CACHE" ) === false ) { // For some reason we get the notice right after connecting (even though the advanced-cache file is already in place). This check works around this issue :(
145
146 if ( nitropack_install_advanced_cache() ) {
147 if ( ! \NitroPack\Integration\Hosting\WPEngine::detect() ) { // The advanced-cache.php file in WP Engine is reset fairly often and we don't want to show the notice every time. This is an info we can skip in this case.
148
149 /* Sets an info notifications if the advanced-cache.php file was re-installed. */
150 $infos[] = array(
151 'title' => esc_html__( 'File advanced-cache.php re-installed', 'nitropack' ),
152 'msg' => esc_html__( 'The file /wp-content/advanced-cache.php was either missing or not the one generated by NitroPack. NitroPack re-installed its version of the file, so it can function properly. Possibly, there is another active page caching plugin in your system. For correct operation, please deactivate any other page caching plugins.', 'nitropack' ),
153 'actions' => '<a href="' . admin_url() . 'plugins.php" target="_blank" class="btn btn-secondary">' . esc_html__( 'Plugins page' ) . '</a>',
154 'classes' => $notification_class,
155 );
156 }
157 } else {
158 if ( ! nitropack_is_conflicting_plugin_active() ) {
159
160 /* Sets an error notifications for not being able to create the advanced-cache.php file due to conflicting caching plugins */
161
162 $errors[] = array(
163 'title' => $notification_title,
164 'msg' => __( 'Please make sure that the /wp-content/ directory is writable and refresh this page.', 'nitropack' ),
165 'classes' => $notification_class,
166 );
167 }
168 }
169 }
170 } else {
171 if ( ! defined( "NITROPACK_ADVANCED_CACHE_VERSION" ) || NITROPACK_VERSION != NITROPACK_ADVANCED_CACHE_VERSION ) {
172 if ( ! nitropack_install_advanced_cache() ) {
173 if ( nitropack_is_conflicting_plugin_active() ) {
174 $errors[] = array(
175 'title' => $notification_title,
176 'msg' => esc_html__( 'The file /wp-content/advanced-cache.php cannot be created because a conflicting plugin is active. Please make sure to disable all conflicting plugins.', 'nitropack' ),
177 'actions' => '<a href="' . admin_url() . 'plugins.php" target="_blank" class="btn btn-primary">Plugins page</a>',
178 'classes' => $notification_class,
179 );
180 } else {
181 $errors[] = array(
182 'title' => $notification_title,
183 'msg' => esc_html__( 'The file /wp-content/advanced-cache.php cannot be created. Please make sure that the /wp-content/ directory is writable and refresh this page.', 'nitropack' ),
184 'classes' => $notification_class,
185 );
186 }
187 }
188 }
189 }
190 } else {
191 if ( nitropack_has_advanced_cache() ) {
192 nitropack_uninstall_advanced_cache();
193 }
194 }
195
196 if ( ( ! defined( "WP_CACHE" ) || ! WP_CACHE ) ) {
197 $notification_class = [ 'wp-cache' ];
198 if ( \NitroPack\Integration\Hosting\Flywheel::detect() ) { // Flywheel: This is configured throught the FW control panel
199 $warnings[] = array(
200 'title' => esc_html__( "WP_CACHE not enabled", 'nitropack' ),
201 'msg' => esc_html__( "Please go to your FlyWheel control panel and enable this setting.", 'nitropack' ),
202 'actions' => '<a href="https://getflywheel.com/wordpress-support/how-to-enable-wp_cache/" target="_blank" class="btn btn-primary">View more</a>',
203 'classes' => $notification_class,
204 );
205 } else if ( ! nitropack_set_wp_cache_const( true ) ) {
206 $errors[] = array(
207 'title' => esc_html__( 'Constant WP_CACHE cannot be set', 'nitropack' ),
208 'msg' => esc_html__( 'This can lead to slower cache delivery. Please make sure that the /wp-config.php file is writable and refresh this page.', 'nitropack' ),
209 'classes' => $notification_class,
210 );
211 }
212 }
213
214 if ( apply_filters( 'nitropack_needs_htaccess_changes', false ) ) {
215 if ( ! nitropack_set_htaccess_rules( true ) ) {
216 $warnings[] = array(
217 'title' => esc_html__( "File .htaccess is not writable", 'nitropack' ),
218 'msg' => esc_html__( 'Unable to configure LiteSpeed specific rules for maximum performance. Please make sure your .htaccess file is writable or contact support.', 'nitropack' ),
219 'classes' => [ 'htaccess' ],
220 );
221 }
222 }
223
224 if ( ! get_nitropack()->dataDirExists() && ! get_nitropack()->initDataDir() ) {
225 $errors[] = array(
226 'title' => esc_html__( 'NitroPack data directory cannot be created', 'nitropack' ),
227 'msg' => esc_html__( 'Please make sure that the /wp-content/ directory is writable and refresh this page.', 'nitropack' ),
228 'classes' => [ 'np-data-dir' ],
229 );
230 return [
231 'error' => $errors,
232 'warning' => $warnings,
233 'info' => $infos
234 ];
235 }
236
237 if ( ! get_nitropack()->pluginDataDirExists() && ! get_nitropack()->initPluginDataDir() ) {
238 $errors[] = array(
239 'title' => esc_html__( 'NitroPack plugin data directory cannot be created', 'nitropack' ),
240 'msg' => esc_html__( 'Please make sure that the /wp-content/ directory is writable and refresh this page.', 'nitropack' ),
241
242 'classes' => [ 'np-data-dir' ],
243 );
244 return [
245 'error' => $errors,
246 'warning' => $warnings,
247 'info' => $infos
248 ];
249 }
250
251 $siteConfig = nitropack_get_site_config();
252 $siteId = $siteConfig ? $siteConfig["siteId"] : NULL;
253 $siteSecret = $siteConfig ? $siteConfig["siteSecret"] : NULL;
254 $webhookToken = esc_attr( get_option( 'nitropack-webhookToken' ) );
255 $blogId = get_current_blog_id();
256 $isConfigOutdated = ! nitropack_is_config_up_to_date();
257
258 if ( ! get_nitropack()->Config->exists() && ! get_nitropack()->updateCurrentBlogConfig( $siteId, $siteSecret, $blogId ) ) {
259 $errors[] = array(
260 'title' => esc_html__( "NitroPack static config file cannot be created", 'nitropack' ),
261 'msg' => esc_html__( 'Please make sure that the /wp-content/config-nitropack/ directory is writable and refresh this page.', 'nitropack' ),
262 );
263 } else if ( $isConfigOutdated ) {
264 if ( ! get_nitropack()->updateCurrentBlogConfig( $siteId, $siteSecret, $blogId ) ) {
265 $errors[] = array(
266 'title' => esc_html__( "NitroPack static config file cannot be updated", 'nitropack' ),
267 'msg' => esc_html__( 'Please make sure that the /wp-content/config-nitropack/ directory is writable and refresh this page.', 'nitropack' ),
268 );
269 } else {
270 if ( ! $siteConfig ) {
271 nitropack_event( "update" );
272 } else {
273 $prevVersion = ! empty( $siteConfig["pluginVersion"] ) ? $siteConfig["pluginVersion"] : "1.1.4 or older";
274 nitropack_event( "update", null, array( "previous_version" => $prevVersion ) );
275
276 if ( empty( $siteConfig["pluginVersion"] ) || version_compare( $siteConfig["pluginVersion"], "1.4", "<" ) ) {
277 $nitropack_v1_3_notice_id = 'nitropack_upgrade_to_1_3';
278 if ( ! headers_sent() ) {
279 setcookie( "dismissed_notice_" . $nitropack_v1_3_notice_id, 1, time() + 3600 );
280 }
281 $_COOKIE[ "dismissed_notice_" . $nitropack_v1_3_notice_id ] = 1;
282 }
283 }
284 }
285
286 try {
287 nitropack_setup_webhooks( get_nitropack_sdk(), $webhookToken );
288 } catch (\NitroPack\SDK\WebhookException $e) {
289 $warnings[] = array(
290 'title' => esc_html__( "Unable to configure webhooks", 'nitropack' ),
291 'msg' => esc_html__( 'This can impact the stability of the plugin. Please disconnect and connect again in order to retry configuring the webhooks.', 'nitropack' ),
292 );
293 }
294 } else {
295 $optionsMismatch = false;
296 if ( array_key_exists( 'options_cache', $siteConfig ) ) {
297 foreach ( \NitroPack\WordPress\NitroPack::$optionsToCache as $opt ) {
298 if ( is_array( $opt ) ) {
299 foreach ( $opt as $option => $suboption ) {
300 // Handle both nested and flat structures
301 if ( is_array( $suboption ) ) {
302 // Nested structure
303 if ( ! isset( $siteConfig['options_cache'][ $option ] ) || ! is_array( $siteConfig['options_cache'][ $option ] ) ) {
304 $optionsMismatch = true;
305 break 2;
306 }
307 foreach ( $suboption as $subkey => $subvalue ) {
308 if (
309 ! isset( $siteConfig['options_cache'][ $option ][ $subkey ] ) ||
310 $siteConfig['options_cache'][ $option ][ $subkey ] !== get_option( $option )[ $subkey ]
311 ) {
312 $optionsMismatch = true;
313 break 3;
314 }
315 }
316 } else {
317 // Flat structure within the nested loop
318 if (
319 ! isset( $siteConfig['options_cache'][ $option ] ) ||
320 $siteConfig['options_cache'][ $option ] !== get_option( $option )
321 ) {
322 $optionsMismatch = true;
323 break 2;
324 }
325 }
326 }
327 } else {
328 // Flat structure outside the nested loop
329 if (
330 ! isset( $siteConfig['options_cache'][ $opt ] ) ||
331 is_bool( $siteConfig['options_cache'][ $opt ] ) ||
332 $siteConfig['options_cache'][ $opt ] !== get_option( $opt )
333 ) {
334 $optionsMismatch = true;
335 break;
336 }
337 }
338 }
339 } else {
340 $optionsMismatch = true;
341 }
342
343 if (
344 $optionsMismatch ||
345 ( ! array_key_exists( "isEzoicActive", $siteConfig ) || $siteConfig["isEzoicActive"] !== \NitroPack\Integration\Plugin\Ezoic::isActive() ) ||
346 ( ! array_key_exists( "isLateIntegrationInitRequired", $siteConfig ) || $siteConfig["isLateIntegrationInitRequired"] !== nitropack_is_late_integration_init_required() ) ||
347 ( ! array_key_exists( "isDlmActive", $siteConfig ) || $siteConfig["isDlmActive"] !== \NitroPack\Integration\Plugin\DownloadManager::isActive() ) ||
348 ( ! array_key_exists( "isAeliaCurrencySwitcherActive", $siteConfig ) || $siteConfig["isAeliaCurrencySwitcherActive"] !== \NitroPack\Integration\Plugin\AeliaCurrencySwitcher::isActive() ) ||
349 ( ! array_key_exists( "isGeoTargetingWPActive", $siteConfig ) || $siteConfig["isGeoTargetingWPActive"] !== \NitroPack\Integration\Plugin\GeoTargetingWP::isActive() ) ||
350 ( ! array_key_exists( "isWoocommerceActive", $siteConfig ) || $siteConfig["isWoocommerceActive"] !== \NitroPack\Integration\Plugin\WooCommerce::isActive() ) ||
351 ( ! array_key_exists( "isWoocommerceCacheHandlerActive", $siteConfig ) || $siteConfig["isWoocommerceCacheHandlerActive"] !== \NitroPack\Integration\Plugin\WoocommerceCacheHandler::isActive() )
352 ) {
353 if ( ! get_nitropack()->updateCurrentBlogConfig( $siteId, $siteSecret, $blogId ) ) {
354 $errors[] = array(
355 'title' => esc_html__( "NitroPack static config file cannot be updated", 'nitropack' ),
356 'msg' => esc_html__( 'Please make sure that the /wp-content/config-nitropack/ directory is writable and refresh this page.', 'nitropack' ),
357 );
358 }
359 }
360
361 if ( empty( $_COOKIE["nitropack_webhook_sync"] ) || !$siteConfig["webhookToken"]) {
362 if ( null !== $nitro = get_nitropack_sdk() ) {
363 try {
364 if ( ! headers_sent() ) {
365 nitropack_setcookie( "nitropack_webhook_sync", "1", time() + 300 ); // Do these checks in 5 minute intervals.
366 }
367 $configWebhook = $nitro->getApi()->getWebhook( "config" );
368 if ( ! empty( $configWebhook ) ) {
369 $query = parse_url( $configWebhook, PHP_URL_QUERY );
370 if ( $query ) {
371 parse_str( $query, $webhookParams );
372 if ( empty( $webhookParams["token"] ) || $webhookParams["token"] != $webhookToken ) {
373 $warnings[] = array(
374 'title' => esc_html__( "Connection problems detected", 'nitropack' ),
375 'msg' => esc_html__( 'Most likely you have used the same API credentials to connect another website (e.g. dev or staging). Click to restore the connection to this site.', 'nitropaack' ),
376 'actions' => '<a id="nitro-restore-connection-btn" class="btn btn-warning">Restore connection</a>',
377 );
378 }
379 }
380 }
381 } catch (\Exception $e) {
382 //Do nothing
383 }
384 }
385 }
386
387 if ( apply_filters( 'nitropack_should_modify_htaccess', false ) && ( empty( $_SERVER["NitroPackHtaccessVersion"] ) || NITROPACK_VERSION != $_SERVER["NitroPackHtaccessVersion"] ) ) {
388 if ( ! nitropack_set_htaccess_rules( true ) ) {
389 $errors[] = array(
390 'title' => esc_html__( "The .htaccess file cannot be modified", 'nitropack' ),
391 'msg' => esc_html__( 'Please make sure that it is writable and refresh this page.', 'nitropack' ),
392 );
393 }
394 }
395 }
396 if ( isset( $nitropack_v1_3_notice_id ) && $this->nitropack_is_dismissed_notice( $nitropack_v1_3_notice_id ) ) {
397 $warnings[] = array(
398 'title' => esc_html__( "NitroPack upgraded to 1.3", 'nitropack' ),
399 'msg' => esc_html__( 'Your new version of NitroPack has a new better way of recaching updated content. However, it is incompatible with the page relationships built by your previous version. Please invalidate your cache manually one-time so that content updates start working with the updated logic.', 'nitropack' ),
400 'dismissableId' => $nitropack_v1_3_notice_id,
401 );
402 }
403
404 if ( \NitroPack\Integration\Plugin\Cloudflare::isApoActive() && ! \NitroPack\Integration\Plugin\Cloudflare::isApoCacheByDeviceTypeEnabled() ) {
405 $warnings[] = array(
406 'title' => esc_html__( "Cache By Device Type is not activate", 'nitropack' ),
407 'msg' => esc_html__( 'It seems Cache By Device Type is not activate with the Cloudflare APO. It is recommended that you enable it for a more optimized experience.', 'nitropack' ),
408 );
409 }
410 }
411
412 $npPluginNotices = [
413 'error' => $errors,
414 'warning' => $warnings,
415 'info' => $infos
416 ];
417
418
419 return $npPluginNotices;
420 }
421
422 /**
423 * Display admin notices in the NitroPack -> Dashboard plugin page.
424 *
425 * This function checks if the current user has the necessary capabilities to view the notices.
426 * It renders specific notifications related to hosting information, system, compatibilities and notifications coming from the NitroPack app
427 *
428 * @return void
429 */
430 public function nitropack_display_admin_notices() {
431 if ( ! $this->pass_notification_capabilities() )
432 return;
433
434 $noticesArray = $this->nitropack_plugin_notices();
435 $components = new \NitroPack\WordPress\Settings\Components;
436 foreach ( $noticesArray as $type => $notices ) {
437 foreach ( $notices as $notice ) {
438 $components->render_notification( $notice['msg'], $type, $notice['title'], isset( $notice['actions'] ) ? $notice['actions'] : null, isset( $notice['classes'] ) ? $notice['classes'] : null, isset( $notice['dismissibleId'] ) ? $notice['dismissibleId'] : null );
439 }
440 }
441 //render app notifications
442 $this->render_app_notifications();
443 }
444
445 /**
446 * Render notifications coming from notifications.json file such as ones from the NitroPack app.
447 *
448 * @return void
449 */
450 public function render_app_notifications() {
451 $components = new \NitroPack\WordPress\Settings\Components();
452 $app_notifications = AppNotifications::getInstance();
453 foreach ( $app_notifications->get( 'system' ) as $notification ) {
454 $msg = $notification['message'];
455 $type = 'info';
456 $title = '';
457
458 if ( ! empty( $notification['type'] ) ) {
459 $type = $notification['type'];
460 }
461 if ( ! empty( $notification['message_details']['title'] ) ) {
462 $title = $notification['message_details']['title'];
463 }
464 if ( ! empty( $notification['message_details']['message'] ) ) {
465 $msg = $notification['message_details']['message'];
466 }
467
468 $components->render_notification( $msg, $type, $title, '', [ 'app-notification' ], $notification['id'], $notification );
469 }
470 }
471 /**
472 * Prints a hosting notice for NitroPack.
473 *
474 * @return void
475 */
476 private function nitropack_print_hosting_notice() {
477
478 $hostingNoticeFile = nitropack_get_hosting_notice_file();
479 if ( ! get_nitropack()->isConnected() || file_exists( $hostingNoticeFile ) )
480 return;
481
482 $documentedHostingSetups = array(
483 "flywheel" => array(
484 "name" => "Flywheel",
485 "helpUrl" => "https://getflywheel.com/wordpress-support/how-to-enable-wp_cache/"
486 ),
487 "cloudways" => array(
488 "name" => "Cloudways",
489 "helpUrl" => "https://support.nitropack.io/hc/en-us/articles/360060916674-Cloudways-Hosting-Configuration-for-NitroPack"
490 )
491 );
492
493 $siteConfig = nitropack_get_site_config();
494 if ( $siteConfig && ! empty( $siteConfig["hosting"] ) && array_key_exists( $siteConfig["hosting"], $documentedHostingSetups ) ) {
495 $hostingInfo = $documentedHostingSetups[ $siteConfig["hosting"] ];
496 $showNotice = true;
497 if ( $siteConfig["hosting"] == "flywheel" && defined( "WP_CACHE" ) && WP_CACHE )
498 $showNotice = false;
499
500 if ( $showNotice ) {
501 $components = new \NitroPack\WordPress\Settings\Components;
502 $components->render_notification( esc_html__( "Please follow the instructions in order to make sure that everything works correctly.", 'nitropack' ), 'info',
503 sprintf( esc_html__( 'It looks like you are hosted on %s', 'nitropack' ), $hostingInfo['name'] ),
504 '<a href="' . $hostingInfo["helpUrl"] . '" target="_blank" class="btn btn-info btn-ghost">' . esc_html__( 'Read Instructions', 'nitropack' ) . '</a>',
505 [ 'hosting-notice' ] );
506 }
507 }
508 }
509 /**
510 * Prints a WooCommerce notice for NitroPack across WordPress admin
511 * @return void
512 */
513 private function nitropack_print_woocommerce_notice() {
514 if ( get_nitropack()->isConnected() ) {
515 if ( class_exists( 'WooCommerce' ) ) {
516 $wcOneTimeNotice = get_option( 'nitropack-wcNotice' );
517
518 if ( ! $wcOneTimeNotice ) {
519 $components = new \NitroPack\WordPress\Settings\Components;
520 $components->render_notification( __( 'Your <strong>account</strong>, <strong>cart</strong>, and <strong>checkout</strong> pages are automatically excluded from optimization.', 'nitropack' ),
521 'success',
522 esc_html__( 'WooCommerce detected', 'nitropack' ),
523 '<a class="btn btn-secondary" href="' . admin_url( 'admin.php?page=nitropack' ) . '">' . esc_html__( 'Settings', 'nitropack' ) . '</a>',
524 [ 'woocommerce-notice' ],
525 'nitropack_dismiss_woocommerce_notice' );
526 }
527 }
528 }
529 }
530
531
532 /**
533 * Checks if the user has capabilities to manage options - administrators typically have this capability.
534 * @return void|bool
535 */
536 private function pass_notification_capabilities() {
537 if ( ! current_user_can( 'manage_options' ) )
538 return;
539 else {
540 return true;
541 }
542 }
543
544 public function test_mode_notification_html() {
545 $components = new \NitroPack\WordPress\Settings\Components;
546 $components->render_notification( 'Visitors are accessing your unoptimized pages. Make sure to disable it once you are done testing.', 'warning', 'Test Mode Enabled', false, [ 'test-mode' ] );
547 }
548 public function nitropack_safemode_notification() {
549 nitropack_verify_ajax_nonce( $_REQUEST );
550 $this->test_mode_notification_html();
551 wp_die();
552 }
553 public function nitropack_is_dismissed_notice( $id ) {
554 return isset( $_COOKIE[ "dismissed_notice_" . $id ] );
555 }
556 /* Dissmiss notification by using set_transient -> temporary dismissal with auto-expiry */
557 public function nitropack_dismiss_notification() {
558 if ( ! $this->pass_notification_capabilities() ) {
559 wp_die( __( 'You do not have sufficient permissions.' ) );
560 }
561
562 if ( ! isset( $_POST ) ) {
563 return;
564 }
565
566 nitropack_verify_ajax_nonce( $_REQUEST );
567
568 $notification_id = $_POST['notification_id'];
569 $notification_end = $_POST['notification_end'];
570 $midpoint = get_date_midpoint( $notification_end );
571 $notification_end = strtotime( $notification_end ) - time();
572 $transient_status = set_transient( $notification_id, $midpoint, $notification_end );
573
574 nitropack_json_and_exit( array(
575 "transient_status" => $transient_status,
576 ) );
577 }
578
579 /* Dissmiss permanent a notification by using update_option - db way */
580 public function nitropack_dismiss_woocommerce_notice() {
581 if ( ! $this->pass_notification_capabilities() ) {
582 wp_die( __( 'You do not have sufficient permissions.' ) );
583 }
584
585 nitropack_verify_ajax_nonce( $_REQUEST );
586 $option = update_option( 'nitropack-wcNotice', 1 );
587 if ( $option ) {
588 nitropack_json_and_exit( array(
589 "type" => "success",
590 ) );
591 } else {
592 nitropack_json_and_exit( array(
593 "type" => "error",
594 ) );
595 }
596 }
597 /* Dissmiss permanent a notification by using update_option - db way */
598 public function nitropack_dismiss_notice_forever() {
599 if ( ! $this->pass_notification_capabilities() ) {
600 wp_die( __( 'You do not have sufficient permissions.' ) );
601 }
602
603 nitropack_verify_ajax_nonce( $_REQUEST );
604
605 update_option( 'nitropack-noticeOptimizeCPT', 1 );
606
607 nitropack_json_and_exit( array(
608 "type" => "success",
609 ) );
610 }
611 }
612