PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.0
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 / HTMLCompression.php
nitropack / classes / WordPress / Settings Last commit date
AutoPurge.php 5 months ago BeaverBuilder.php 5 months ago CPTOptimization.php 5 months ago CacheWarmup.php 5 months ago CartCache.php 5 months ago Components.php 5 months ago EditorClearCache.php 5 months ago GeneratePreview.php 8 months ago HTMLCompression.php 5 months ago Logger.php 5 months ago OptimizationLevel.php 5 months ago Optimizations.php 5 months ago PurgeCache.php 5 months ago Shortcodes.php 5 months ago StockRefresh.php 5 months ago Subscription.php 5 months ago SystemReport.php 5 months ago TestMode.php 5 months ago
HTMLCompression.php
119 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 if ( \NitroPack\Integration\Hosting\Flywheel::detect() ) { // Flywheel: Compression is enabled by default
61 $hasCompression = true;
62 update_option( $this->option_name, 0 );
63 nitropack_json_and_exit( array( "type" => "success", "hasCompression" => $hasCompression ) );
64 } else {
65 /* Reset setting each time when testing */
66 update_option( $this->option_name, 0 );
67
68 require_once plugin_dir_path( NITROPACK_FILE ) . nitropack_trailingslashit( 'nitropack-sdk' ) . 'autoload.php';
69 $http = new HTTPClient( get_site_url() );
70 $http->setHeader( "X-NitroPack-Request", 1 );
71 $http->timeout = 25;
72 $http->fetch();
73 $headers = $http->getHeaders();
74
75 /* Check for content-encoding header - br, gzip, deflate, zstd, etc. Most servers support these and have it enabled. */
76 if ( ! empty( $headers["content-encoding"] ) ) {
77 $hasCompression = true;
78 nitropack_json_and_exit( array( "type" => "success", "hasCompression" => $hasCompression ) );
79 } else {
80 /* If not found, we enable NitroPack GZIP compression */
81 $hasCompression = false;
82 update_option( $this->option_name, 1 );
83 nitropack_json_and_exit( array( "type" => "success", "hasCompression" => $hasCompression ) );
84 }
85 }
86 } catch (\Exception $e) {
87 nitropack_json_and_exit( array( "type" => "error", "message" => nitropack_admin_toast_msgs( 'error' ) ) );
88 }
89 }
90
91 public function render() {
92 $enableCompression = get_option( $this->option_name );
93 ?>
94 <div class="nitro-option" id="compression-widget">
95 <div class="nitro-option-main">
96 <div class="text-box">
97 <h6><span id="detected-compression"><?php esc_html_e( 'HTML Compression', 'nitropack' ); ?>
98 </span></h6>
99 <p>
100 <?php esc_html_e( 'Compressing the structure of your HTML, ensures faster page rendering and an optimized browsing experience for your users.', 'nitropack' ); ?>
101 <a href="https://support.nitropack.io/en/articles/8390333-nitropack-plugin-settings-in-wordpress#h_29b7ab4836"
102 class="text-blue" target="_blank"><?php esc_html_e( 'Learn more', 'nitropack' ); ?></a>
103 </p>
104 </div>
105 <?php $components = new Components();
106 $components->render_toggle( 'compression-status', $enableCompression, [ 'disabled' => true ] );
107 ?>
108 </div>
109 <div class="mt-4 text-primary">
110 <a href="javascript:void(0);" id="compression-test-btn"
111 class="text-primary"><?php esc_html_e( 'Run compression test', 'nitropack' ); ?></a>
112 <div class="flex items-start msg-container hidden">
113 <span class="msg"></span>
114 </div>
115 </div>
116 </div>
117 <?php
118 }
119 }