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 | } |