TestMode.php
158 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' ) { |
| 25 | $smStatus = get_option( 'nitropack-safeModeStatus', "-1" ); |
| 26 | if ( $smStatus === "0" ) { |
| 27 | add_action( 'admin_enqueue_scripts', function () { |
| 28 | wp_enqueue_script( 'np_safemode', NITROPACK_PLUGIN_DIR_URL . 'view/javascript/np_safemode.js', array( 'jquery' ) ); |
| 29 | wp_enqueue_style( 'np_safemode', NITROPACK_PLUGIN_DIR_URL . 'view/stylesheet/safemode.min.css' ); |
| 30 | } ); |
| 31 | add_action( 'admin_footer', function () { |
| 32 | require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-safemode.php'; |
| 33 | } ); |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | /* Checks test mode in Settings page every visit */ |
| 38 | public function nitropack_safemode_status( $dontExit = false ) { |
| 39 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 40 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 41 | try { |
| 42 | $isEnabled = $nitro->getApi()->isSafeModeEnabled(); |
| 43 | } catch (\Exception $e) { |
| 44 | if ( ! $dontExit ) { |
| 45 | NitroPack::getInstance()->getLogger()->error( 'Test mode cannot be ' . ( $isEnabled ? 'enabled' : 'disabled' ) ); |
| 46 | nitropack_json_and_exit( array( |
| 47 | "type" => "error", |
| 48 | "message" => nitropack_admin_toast_msgs( 'success' ) |
| 49 | ) ); |
| 50 | } |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | $this->nitropack_cache_safemode_status( $isEnabled ); |
| 55 | if ( ! $dontExit ) { |
| 56 | nitropack_json_and_exit( array( |
| 57 | "type" => "success", |
| 58 | "isEnabled" => $isEnabled, |
| 59 | ) ); |
| 60 | } |
| 61 | return $isEnabled; |
| 62 | } |
| 63 | |
| 64 | $this->nitropack_cache_safemode_status(); |
| 65 | if ( ! $dontExit ) { |
| 66 | NitroPack::getInstance()->getLogger()->error( 'There was an SDK error while fetching status of safe mode' ); |
| 67 | nitropack_json_and_exit( array( |
| 68 | "type" => "error", |
| 69 | "message" => __( 'Error! There was an SDK error while fetching status of safe mode!', 'nitropack' ) |
| 70 | ) ); |
| 71 | } |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | public function nitropack_cache_safemode_status( $operation = null ) { |
| 76 | $sm = "-1"; |
| 77 | if ( is_bool( $operation ) ) { |
| 78 | $sm = $operation ? '1' : '0'; |
| 79 | } |
| 80 | return update_option( 'nitropack-safeModeStatus', $sm ); |
| 81 | } |
| 82 | |
| 83 | public function nitropack_enable_safemode() { |
| 84 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 85 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 86 | try { |
| 87 | $nitro->enableSafeMode(); |
| 88 | } catch (\Exception $e) { |
| 89 | NitroPack::getInstance()->getLogger()->error( 'Test mode cannot be enabled. Error: ' . $e ); |
| 90 | } |
| 91 | |
| 92 | $this->nitropack_cache_safemode_status( true ); |
| 93 | NitroPack::getInstance()->getLogger()->notice( 'Test mode is enabled' ); |
| 94 | nitropack_json_and_exit( array( |
| 95 | "type" => "success", |
| 96 | "message" => nitropack_admin_toast_msgs( 'success' ) |
| 97 | |
| 98 | ) ); |
| 99 | } |
| 100 | |
| 101 | nitropack_json_and_exit( array( |
| 102 | "type" => "error", |
| 103 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 104 | ) ); |
| 105 | } |
| 106 | |
| 107 | public function nitropack_disable_safemode() { |
| 108 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 109 | |
| 110 | |
| 111 | if ( null !== $nitro = get_nitropack_sdk() ) { |
| 112 | try { |
| 113 | $nitro->disableSafeMode(); |
| 114 | } catch (\Exception $e) { |
| 115 | NitroPack::getInstance()->getLogger()->error( 'Test mode cannot be disabled. Error: ' . $e ); |
| 116 | } |
| 117 | |
| 118 | $this->nitropack_cache_safemode_status( false ); |
| 119 | NitroPack::getInstance()->getLogger()->notice( 'Test mode is disabled' ); |
| 120 | nitropack_json_and_exit( array( |
| 121 | "type" => "success", |
| 122 | "message" => nitropack_admin_toast_msgs( 'success' ) |
| 123 | ) ); |
| 124 | } |
| 125 | nitropack_json_and_exit( array( |
| 126 | "type" => "error", |
| 127 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 128 | ) ); |
| 129 | } |
| 130 | public function render() { |
| 131 | $test_mode_option = get_option( 'nitropack-safeModeStatus' ); |
| 132 | ?> |
| 133 | <div class="nitro-option" id="test-mode-widget"> |
| 134 | <div class="nitro-option-main"> |
| 135 | <div class="text-box" id="safemode-status-slider"> |
| 136 | <h6><?php esc_html_e( 'Test Mode', 'nitropack' ); ?></h6> |
| 137 | <p><?php esc_html_e( 'Test NitroPack\'s features without affecting your visitors\' experience', 'nitropack' ); ?>. |
| 138 | <a href="https://support.nitropack.io/en/articles/8390292-test-mode" class="text-blue" |
| 139 | target="_blank"><?php esc_html_e( 'Learn more', 'nitropack' ); ?></a> |
| 140 | </p> |
| 141 | </div> |
| 142 | |
| 143 | <label class="inline-flex items-center cursor-pointer ml-auto"> |
| 144 | <input type="checkbox" class="sr-only peer" id="safemode-status" <?php echo (int) $test_mode_option === 1 ? "checked" : ""; ?>> |
| 145 | |
| 146 | <div class="toggle"></div> |
| 147 | </label> |
| 148 | </div> |
| 149 | <div class="msg-container hidden" id="loading-safemode-status"> |
| 150 | <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'view/images/loading.svg'; ?>" alt="loading" |
| 151 | class="icon"> |
| 152 | <?php esc_html_e( 'Loading test mode status', 'nitropack' ); ?> |
| 153 | </div> |
| 154 | <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-test-mode.php'; ?> |
| 155 | </div> |
| 156 | <?php |
| 157 | } |
| 158 | } |