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
4 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
4 months ago
Subscription.php
4 months ago
SystemReport.php
4 months ago
TestMode.php
4 months ago
HTMLCompression.php
123 lines
| 1 | <?php |
| 2 | |
| 3 | namespace NitroPack\WordPress\Settings; |
| 4 | use NitroPack\WordPress\NitroPack; |
| 5 | use NitroPack\HttpClient\HttpClient; |
| 6 | |
| 7 | class HTMLCompression { |
| 8 | private static $instance = null; |
| 9 | public $option_name; |
| 10 | /** |
| 11 | * Get the singleton instance of the HTMLCompression class |
| 12 | * |
| 13 | * @return HTMLCompression |
| 14 | */ |
| 15 | public static function getInstance() { |
| 16 | if ( self::$instance === null ) { |
| 17 | self::$instance = new HTMLCompression(); |
| 18 | } |
| 19 | return self::$instance; |
| 20 | } |
| 21 | |
| 22 | public function __construct() { |
| 23 | add_action( 'wp_ajax_nitropack_test_compression_ajax', [ $this, 'nitropack_test_compression_ajax' ] ); |
| 24 | add_action( 'wp_ajax_nitropack_set_compression_ajax', [ $this, 'nitropack_set_compression_ajax' ] ); |
| 25 | |
| 26 | $this->option_name = 'nitropack-enableCompression'; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * AJAX handler when toggling (saving) the compression option |
| 31 | * @return void |
| 32 | */ |
| 33 | public function nitropack_set_compression_ajax() { |
| 34 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 35 | $option = (int) ! empty( $_POST["data"]["compressionStatus"] ); |
| 36 | $updated = update_option( $this->option_name, $option ); |
| 37 | |
| 38 | if ( $updated ) { |
| 39 | NitroPack::getInstance()->getLogger()->notice( 'HTML Compression is ' . ( $option === 1 ? 'enabled' : 'disabled' ) ); |
| 40 | nitropack_json_and_exit( array( "type" => "success", "message" => nitropack_admin_toast_msgs( 'success' ), "hasCompression" => $option ) ); |
| 41 | } else { |
| 42 | NitroPack::getInstance()->getLogger()->error( 'HTML Compression cannot be ' . ( $option === 1 ? 'enabled' : 'disabled' ) ); |
| 43 | nitropack_json_and_exit( array( |
| 44 | "type" => "error", |
| 45 | "message" => nitropack_admin_toast_msgs( 'error' ) |
| 46 | ) ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * AJAX handler when testing the compression on page load. |
| 52 | * Most servers have compression enabled by default - br or gzip. |
| 53 | * If not, we force enable NitroPack GZIP compression. |
| 54 | * @return void |
| 55 | */ |
| 56 | public function nitropack_test_compression_ajax() { |
| 57 | nitropack_verify_ajax_nonce( $_REQUEST ); |
| 58 | $hasCompression = true; |
| 59 | try { |
| 60 | $hostingsWithCompression = [ \NitroPack\Integration\Hosting\Flywheel::detect(), \NitroPack\Integration\Hosting\WPEngine::detect() ]; |
| 61 | if ( $hostingsWithCompression ) { |
| 62 | $hasCompression = true; |
| 63 | update_option( $this->option_name, 0 ); |
| 64 | nitropack_json_and_exit( array( "type" => "success", "hasCompression" => $hasCompression ) ); |
| 65 | } else { |
| 66 | /* Reset setting each time when testing */ |
| 67 | update_option( $this->option_name, 0 ); |
| 68 | |
| 69 | require_once plugin_dir_path( NITROPACK_FILE ) . nitropack_trailingslashit( 'nitropack-sdk' ) . 'autoload.php'; |
| 70 | $http = new HTTPClient( get_site_url() ); |
| 71 | $http->setHeader( "X-NitroPack-Request", 1 ); |
| 72 | $http->timeout = 25; |
| 73 | $http->fetch(); |
| 74 | $headers = $http->getHeaders(); |
| 75 | if ( $http->status_code !== 200 ) { |
| 76 | NitroPack::getInstance()->getLogger()->error( 'Compression test failed with status code: ' . $http->status_code ); |
| 77 | nitropack_json_and_exit( array( "type" => "error", "message" => nitropack_admin_toast_msgs( 'error' ), "status_code" => $http->status_code ) ); |
| 78 | } |
| 79 | /* Check for content-encoding header - br, gzip, deflate, zstd, etc. Most servers support these and have it enabled. */ |
| 80 | if ( ! empty( $headers["content-encoding"] ) ) { |
| 81 | $hasCompression = true; |
| 82 | nitropack_json_and_exit( array( "type" => "success", "hasCompression" => $hasCompression ) ); |
| 83 | } else { |
| 84 | /* If not found, we enable NitroPack GZIP compression */ |
| 85 | $hasCompression = false; |
| 86 | update_option( $this->option_name, 1 ); |
| 87 | nitropack_json_and_exit( array( "type" => "success", "hasCompression" => $hasCompression ) ); |
| 88 | } |
| 89 | } |
| 90 | } catch (\Exception $e) { |
| 91 | nitropack_json_and_exit( array( "type" => "error", "message" => nitropack_admin_toast_msgs( 'error' ) ) ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public function render() { |
| 96 | $enableCompression = get_option( $this->option_name ); |
| 97 | ?> |
| 98 | <div class="nitro-option" id="compression-widget"> |
| 99 | <div class="nitro-option-main"> |
| 100 | <div class="text-box"> |
| 101 | <h6><span id="detected-compression"><?php esc_html_e( 'HTML Compression', 'nitropack' ); ?> |
| 102 | </span></h6> |
| 103 | <p> |
| 104 | <?php esc_html_e( 'Compressing the structure of your HTML, ensures faster page rendering and an optimized browsing experience for your users.', 'nitropack' ); ?> |
| 105 | <a href="https://support.nitropack.io/en/articles/8390333-nitropack-plugin-settings-in-wordpress#h_29b7ab4836" |
| 106 | class="text-blue" target="_blank"><?php esc_html_e( 'Learn more', 'nitropack' ); ?></a> |
| 107 | </p> |
| 108 | </div> |
| 109 | <?php $components = new Components(); |
| 110 | $components->render_toggle( 'compression-status', $enableCompression, [ 'disabled' => true ] ); |
| 111 | ?> |
| 112 | </div> |
| 113 | <div class="mt-4 text-primary"> |
| 114 | <a href="javascript:void(0);" id="compression-test-btn" |
| 115 | class="text-primary"><?php esc_html_e( 'Run compression test', 'nitropack' ); ?></a> |
| 116 | <div class="flex items-start msg-container hidden"> |
| 117 | <span class="msg"></span> |
| 118 | </div> |
| 119 | </div> |
| 120 | </div> |
| 121 | <?php |
| 122 | } |
| 123 | } |