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