PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.4
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.4
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 / Settings / TestMode.php
nitropack / classes / WordPress / Settings Last commit date
AutoPurge.php 4 months ago BeaverBuilder.php 4 months ago CPTOptimization.php 4 months ago CacheWarmup.php 3 months ago CartCache.php 4 months ago Components.php 3 months ago EditorClearCache.php 4 months ago GeneratePreview.php 7 months ago HTMLCompression.php 3 months ago Logger.php 4 months ago OptimizationLevel.php 3 months ago Optimizations.php 4 months ago PurgeCache.php 4 months ago Shortcodes.php 4 months ago StockRefresh.php 2 months ago Subscription.php 4 months ago SystemReport.php 3 months ago TestMode.php 4 months ago
TestMode.php
165 lines
1 <?php
2 namespace NitroPack\WordPress\Settings;
3
4 use NitroPack\WordPress\NitroPack;
5
6 class TestMode {
7 private static $instance = NULL;
8 public function __construct() {
9 add_action( 'wp_ajax_nitropack_safemode_status', [ $this, 'nitropack_safemode_status' ] );
10 add_action( 'wp_ajax_nitropack_enable_safemode', [ $this, 'nitropack_enable_safemode' ] );
11 add_action( 'wp_ajax_nitropack_disable_safemode', [ $this, 'nitropack_disable_safemode' ] );
12 add_action( 'plugins_loaded', [ $this, 'nitropack_offer_safemode' ] );
13 }
14 public static function getInstance() {
15 if ( ! self::$instance ) {
16 self::$instance = new TestMode();
17 }
18
19 return self::$instance;
20 }
21 /* Offer test mode instead of disabling NitroPack from Plugins page */
22 public function nitropack_offer_safemode() {
23 global $pagenow;
24 if ( $pagenow == 'plugins.php' && ! $this->is_test_mode_enabled() ) {
25 add_action( 'admin_enqueue_scripts', function () {
26 wp_enqueue_script( 'np_safemode', NITROPACK_PLUGIN_DIR_URL . 'view/javascript/np_safemode.js', array( 'jquery' ) );
27 wp_enqueue_style( 'np_safemode', NITROPACK_PLUGIN_DIR_URL . 'view/stylesheet/safemode.min.css' );
28 } );
29 add_action( 'admin_footer', function () {
30 require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-safemode.php';
31 } );
32 }
33 }
34 /* Checks test mode in Settings page every visit */
35 public function nitropack_safemode_status( $dontExit = false ) {
36 nitropack_verify_ajax_nonce( $_REQUEST );
37 if ( null !== $nitro = get_nitropack_sdk() ) {
38 try {
39 $isEnabled = $nitro->getApi()->isSafeModeEnabled();
40 } catch (\Exception $e) {
41 if ( ! $dontExit ) {
42 NitroPack::getInstance()->getLogger()->error( 'Test mode cannot be ' . ( $isEnabled ? 'enabled' : 'disabled' ) );
43 nitropack_json_and_exit( array(
44 "type" => "error",
45 "message" => nitropack_admin_toast_msgs( 'success' )
46 ) );
47 }
48 return NULL;
49 }
50
51 if ( ! $dontExit ) {
52 nitropack_json_and_exit( array(
53 "type" => "success",
54 "isEnabled" => $isEnabled,
55 ) );
56 }
57 return $isEnabled;
58 }
59
60 if ( ! $dontExit ) {
61 NitroPack::getInstance()->getLogger()->error( 'There was an SDK error while fetching status of safe mode' );
62 nitropack_json_and_exit( array(
63 "type" => "error",
64 "message" => __( 'Error! There was an SDK error while fetching status of safe mode!', 'nitropack' )
65 ) );
66 }
67 return NULL;
68 }
69
70 /**
71 * Check if the user has test mode enabled
72 *
73 * @return bool
74 */
75 public function is_test_mode_enabled(): bool {
76 try {
77 $nitro = get_nitropack_sdk();
78 if ( ! $nitro ) {
79 // Return the default value (not enabled) in case we can't get the SDK.
80 return false;
81 }
82
83 if ( isset( $nitro->getConfig()->SafeMode ) ) {
84 return (bool) $nitro->getConfig()->SafeMode;
85 }
86
87 nitropack_fetch_config();
88 return (bool) $nitro->getApi()->isSafeModeEnabled();
89 } catch (\Exception $e) {
90 NitroPack::getInstance()->getLogger()->error( 'There was an SDK error while fetching status of safe mode: ' . $e );
91 // Return the default value (not enabled) in case of error.
92 return false;
93 }
94 }
95
96 public function nitropack_enable_safemode() {
97 nitropack_verify_ajax_nonce( $_REQUEST );
98 if ( null !== $nitro = get_nitropack_sdk() ) {
99 try {
100 $nitro->enableSafeMode();
101 } catch (\Exception $e) {
102 NitroPack::getInstance()->getLogger()->error( 'Test mode cannot be enabled. Error: ' . $e );
103 }
104
105 NitroPack::getInstance()->getLogger()->notice( 'Test mode is enabled' );
106 nitropack_json_and_exit( array(
107 "type" => "success",
108 "message" => nitropack_admin_toast_msgs( 'success' )
109
110 ) );
111 }
112
113 nitropack_json_and_exit( array(
114 "type" => "error",
115 "message" => nitropack_admin_toast_msgs( 'error' )
116 ) );
117 }
118
119 public function nitropack_disable_safemode() {
120 nitropack_verify_ajax_nonce( $_REQUEST );
121
122
123 if ( null !== $nitro = get_nitropack_sdk() ) {
124 try {
125 $nitro->disableSafeMode();
126 } catch (\Exception $e) {
127 NitroPack::getInstance()->getLogger()->error( 'Test mode cannot be disabled. Error: ' . $e );
128 }
129
130 NitroPack::getInstance()->getLogger()->notice( 'Test mode is disabled' );
131 nitropack_json_and_exit( array(
132 "type" => "success",
133 "message" => nitropack_admin_toast_msgs( 'success' )
134 ) );
135 }
136 nitropack_json_and_exit( array(
137 "type" => "error",
138 "message" => nitropack_admin_toast_msgs( 'error' )
139 ) );
140 }
141 public function render() {
142 ?>
143 <div class="nitro-option" id="test-mode-widget">
144 <div class="nitro-option-main">
145 <div class="text-box" id="safemode-status-slider">
146 <h6><?php esc_html_e( 'Test Mode', 'nitropack' ); ?></h6>
147 <p><?php esc_html_e( 'Test NitroPack\'s features without affecting your visitors\' experience', 'nitropack' ); ?>.
148 <a href="https://support.nitropack.io/en/articles/8390292-test-mode" class="text-blue"
149 target="_blank"><?php esc_html_e( 'Learn more', 'nitropack' ); ?></a>
150 </p>
151 </div>
152 <?php $components = new Components();
153 $components->render_toggle( 'safemode-status', $this->is_test_mode_enabled() );
154 ?>
155 </div>
156 <div class="msg-container hidden" id="loading-safemode-status">
157 <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'view/images/loading.svg'; ?>" alt="loading"
158 class="icon">
159 <?php esc_html_e( 'Loading test mode status', 'nitropack' ); ?>
160 </div>
161 <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-test-mode.php'; ?>
162 </div>
163 <?php
164 }
165 }