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 / OptimizationLevel.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
OptimizationLevel.php
330 lines
1 <?php
2 namespace NitroPack\WordPress\Settings;
3
4 use NitroPack\HttpClient\HttpClient;
5
6 class OptimizationLevel {
7 private static $instance = NULL;
8 private $levels = [
9 1 => "standard",
10 2 => "medium",
11 3 => "strong",
12 4 => "ludicrous",
13 5 => "custom"
14 ];
15 public $level_name = '';
16 public function __construct() {
17 add_action( 'wp_ajax_nitropack_set_optimization_mode', [ $this, 'nitropack_set_optimization_mode' ] );
18 }
19 public static function getInstance() {
20 if ( null === self::$instance ) {
21 self::$instance = new self();
22 }
23 return self::$instance;
24 }
25 /* Ajax handler to set optimization mode. Use our HTTPClient to send data.
26 * @return void
27 */
28 public function nitropack_set_optimization_mode() {
29 nitropack_verify_ajax_nonce( $_REQUEST );
30
31 $mode_name = ! empty( $_POST["mode_name"] ) ? sanitize_text_field( $_POST["mode_name"] ) : null;
32
33 $quickSetupSave = get_nitropack_integration_url( "optimization_preset" );
34 $quickSetupHTTP = new HttpClient( $quickSetupSave );
35 $quickSetupHTTP->setHeader( "Content-Type", "application/x-www-form-urlencoded" );
36 $quickSetupHTTP->setPostData( "optimization_preset=" . $mode_name );
37 $quickSetupHTTP->fetch( true, "POST" );
38 switch ( $quickSetupHTTP->getStatusCode() ) {
39 case 200:
40 \NitroPack\WordPress\NitroPack::getInstance()->getLogger()->notice( '[Preview] Optimization mode set to: ' . $mode_name );
41 nitropack_json_and_exit( array( "type" => "success", "message" => nitropack_admin_toast_msgs( 'success' ), "mode" => $mode_name ) );
42 break;
43 case 400:
44 \NitroPack\WordPress\NitroPack::getInstance()->getLogger()->error( '[Preview] Tried to set optimization mode to: ' . $mode_name );
45 nitropack_json_and_exit( array( "type" => "error", 'message' => json_decode( $quickSetupHTTP->getBody(), true )['error_message'] ) );
46 break;
47 case 503:
48 \NitroPack\WordPress\NitroPack::getInstance()->getLogger()->error( '[Preview] Tried to set optimization mode to: ' . $mode_name );
49 nitropack_json_and_exit( array( "type" => "error", "message" => json_decode( $quickSetupHTTP->getBody(), true )['error_message'] ) );
50 break;
51 default:
52 \NitroPack\WordPress\NitroPack::getInstance()->getLogger()->error( '[Preview] Tried to set optimization mode to: ' . $mode_name );
53 nitropack_json_and_exit( array( "type" => "error", "message" => "An unexpected error occurred. Please try again later." ) );
54 break;
55 }
56 }
57 /**
58 * Fetch user plan from NitroPack App
59 * @return string Plan name - Free, etc.
60 */
61 public function fetch_plan() {
62 $planDetailsUrl = get_nitropack_integration_url( "plan_details_json" );
63 $quickSetupHTTP = new HttpClient( $planDetailsUrl );
64 $quickSetupHTTP->timeout = 30;
65 $quickSetupHTTP->fetch();
66 $resp = $quickSetupHTTP->getStatusCode() == 200 ? json_decode( $quickSetupHTTP->getBody(), true ) : false;
67 $plan = $resp && isset( $resp['plan_title'] ) ? $resp['plan_title'] : null;
68 return $plan;
69 }
70 /**
71 * Fetch optimization modes data from NitroPack App
72 * @return array
73 */
74 public function optimization_modes() {
75 $quickSetupUrl = get_nitropack_integration_url( "optimization_preset" );
76 $quickSetupHTTP = new HttpClient( $quickSetupUrl );
77
78 $quickSetupHTTP->timeout = 30;
79 $quickSetupHTTP->fetch();
80 $resp = $quickSetupHTTP->getStatusCode() == 200 ? json_decode( $quickSetupHTTP->getBody(), true ) : false;
81 $modes = null;
82 if ( $resp && ! empty( $resp['optimization_options'] ) ) {
83 $modes = $resp;
84 } else {
85 $modes = $this->static_modes();
86 }
87 return $modes;
88 }
89 /**
90 * Static optimization modes data when NitroPack App is unreachable
91 * @return array
92 */
93 private function static_modes() {
94 $free_plan = $this->fetch_plan() === 'Free' ? true : false;
95 $modes = [
96 'optimization_selected' => 'medium',
97 'optimization_options' => [
98 'standard' => [
99 'human_readable_name' => 'Standard',
100 'description' => 'Standard optimization features enabled for your site. Ideal choice for maximum stability.',
101 'description_onboarding' => 'Applies basic optimizations.',
102 'is_available' => true,
103 ],
104 'medium' => [
105 'human_readable_name' => 'Medium',
106 'description' => 'Adds image lazy loading to standard optimizations. Uses built-in browser techniques for loading resources.',
107 'description_onboarding' => 'Implements moderate optimizations.',
108 'is_available' => true,
109 ],
110 'strong' => [
111 'human_readable_name' => 'Strong',
112 'description' => 'Includes smart resource loading on top of Medium optimizations. Balances speed boost with stability.',
113 'description_onboarding' => 'Strikes a great balance between stability and website speed.',
114 'is_available' => true,
115 ],
116 'ludicrous' => [
117 'human_readable_name' => 'Ludicrous',
118 'description' => 'Applies deferred JS and advanced resource loading for optimal performance and Core Web Vitals.',
119 'description_onboarding' => 'The most powerful optimization setting, aiming for best possible performance.',
120 'is_available' => $free_plan ? false : true,
121 ],
122 'custom' => [
123 'human_readable_name' => 'Custom',
124 'description' => 'Activated when manual setups are made. Ideal for advanced NitroPack optimizations.',
125 'description_onboarding' => 'Activated when manual setups are made. Ideal for advanced NitroPack optimizations.',
126 'is_available' => false,
127 ],
128 ],
129 ];
130 return $modes;
131 }
132 /**
133 * Fetch optimization name from NitroPack App
134 * @return array Optimization name (standard, medium, etc.)
135 */
136 public function fetch_optimization_name() {
137 if ( ! empty( $this->optimization_modes() ) ) {
138 $optimization_name = $this->optimization_modes()['optimization_selected'];
139 }
140
141 return $optimization_name;
142 }
143 /**
144 * Render optimization level setting HTML
145 */
146 public function render() {
147 $modes = $this->optimization_modes(); ?>
148 <div class="card card-optimization-mode">
149 <div class="card-header no-border mb-0">
150 <div class="flex items-center">
151 <h3 class="mb-0"><?php esc_html_e( 'Optimization mode', 'nitropack' ); ?></h3>
152 <span class="tooltip-icon" data-tooltip-target="tooltip-optimization">
153 <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'view/images/info.svg'; ?>">
154 </span>
155 <div id="tooltip-optimization" role="tooltip" class="tooltip-container hidden">
156 <?php esc_html_e( 'Select from our range of predefined optimization modes to boost your site\'s performance.', 'nitropack' );
157 ?>
158 <div class="tooltip-arrow" data-popper-arrow></div>
159 </div>
160 </div>
161 </div>
162 <?php ?>
163 <div class="tabs-wrapper">
164 <div class="tabs" id="optimization-modes">
165 <?php
166 $upgrade_msg = false;
167 $optimization_level_name = $modes['optimization_selected'];
168 foreach ( $modes['optimization_options'] as $mode_id => $mode ) :
169 $active = $optimization_level_name === $mode_id;
170 $css = [];
171 $css[] = $active ? 'active btn-primary' : 'btn-link';
172 $css[] = "mode-{$mode_id}";
173 if ( ! $mode['is_available'] ) {
174 $css[] = 'disabled';
175 }
176 if ( ! $mode['is_available'] && $mode_id === 'ludicrous' ) {
177 $upgrade_msg = true;
178 }
179 $css = implode( ' ', $css );
180 ?>
181 <a class="btn tab-link <?php echo $css; ?>" data-mode="<?php echo $mode_id; ?>"
182 data-modal-target="modal-optimization-mode"
183 data-modal-toggle="modal-optimization-mode"><?php echo $mode['human_readable_name']; ?>
184 <?php if ( ! $mode['is_available'] && $mode['human_readable_name'] === 'Ludicrous' ) { ?>
185 <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'view/images/lock.svg'; ?>" />
186 <?php } ?>
187 </a>
188 <?php endforeach; ?>
189 </div>
190
191 <?php if ( $upgrade_msg ) : ?>
192 <div class="upgrade-message bg-success">
193 <p class="">
194 <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
195 <g clip-path="url(#clip0_1039_2988)">
196 <path
197 d="M10 18.3334C14.6024 18.3334 18.3333 14.6024 18.3333 10C18.3333 5.39765 14.6024 1.66669 10 1.66669C5.39763 1.66669 1.66667 5.39765 1.66667 10C1.66667 14.6024 5.39763 18.3334 10 18.3334Z"
198 stroke="#33D2B5" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
199 <path d="M13.3333 10L10 6.66669L6.66667 10" stroke="#33D2B5" stroke-width="1.5"
200 stroke-linecap="round" stroke-linejoin="round" />
201 <path d="M10 13.3334V6.66669" stroke="#33D2B5" stroke-width="1.5" stroke-linecap="round"
202 stroke-linejoin="round" />
203 </g>
204 <defs>
205 <clipPath id="clip0_1039_2988">
206 <rect width="20" height="20" fill="white" />
207 </clipPath>
208 </defs>
209 </svg>
210 <?php _e( 'Ludicrous mode available on <b>Starter</b> Plan.', 'nitropack' ); ?> <a
211 href="https://app.nitropack.io/subscription/buy" class="upgrade-here"
212 target="_blank"><?php esc_html_e( 'Upgrade here', 'nitropack' ); ?></a>
213 </p>
214 </div>
215 <?php endif; ?>
216
217 <p><?php esc_html_e( 'Active Mode', 'nitropack' ); ?>: <span
218 class="active-mode"><?php echo esc_html( $optimization_level_name ); ?></span></p>
219
220 <div class="tab-content-wrapper">
221 <?php foreach ( $modes['optimization_options'] as $mode_id => $mode ) :
222 $active = ( $optimization_level_name === $mode_id ? true : false );
223 $css = [];
224 $css[] = ( $active ? '' : 'hidden' );
225 $css = implode( ' ', array_filter( $css ) );
226 ?>
227 <div class="tab-content <?php echo esc_attr( $css ); ?>" role="tabpanel"
228 data-tab="<?php echo $mode_id; ?>-tab">
229 <p class="text-secondary mt-2">
230 <?php echo esc_html( $mode['description'] ); ?>
231 </p>
232 </div>
233 <?php endforeach; ?>
234
235 <div class="hidden tab-content" role="tabpanel" data-tab="custom-tab">
236 <p class="text-secondary mt-2">
237 <?php esc_html_e( 'Activated when manual setups are made. Ideal for advanced NitroPack optimizations.', 'nitropack' ); ?>
238 </p>
239 </div>
240 </div>
241 </div>
242 <div class="card-footer">
243 <div class="flex flex-row">
244 <p class=""><?php esc_html_e( 'Which optimization mode to choose?', 'nitropack' ); ?></p>
245 <a class="text-primary btn-link ml-auto see-modes" data-modal-target="modal-modes"
246 data-modal-toggle="modal-modes"><?php esc_html_e( 'See modes comparison', 'nitropack' ); ?></a>
247 <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-modes.php'; ?>
248 </div>
249 </div>
250 <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-optimization-mode.php'; ?>
251 </div>
252 <?php }
253 public function preview_render() {
254 $optimization_modes = $this->optimization_modes();
255 unset( $optimization_modes['optimization_options']['custom'] );
256 ?>
257 <div class="optimization-modes">
258 <div class="flex flex-row">
259 <h2><?php esc_html_e( 'Optimization modes', 'nitropack' ); ?></h2>
260 <a class="text-primary btn-link ml-auto see-modes" data-modal-target="modal-modes"
261 data-modal-toggle="modal-modes"><?php esc_html_e( 'See modes comparison', 'nitropack' ); ?></a>
262 <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-modes.php'; ?>
263 </div>
264 <div class="modes-container">
265 <?php $chevrons = 1;
266 $active_mode_name = $optimization_modes['optimization_selected'];
267 foreach ( $optimization_modes['optimization_options'] as $key => $mode ) :
268 $active = ( $active_mode_name === $key ? true : false );
269 $css = [];
270 $css[] = $active ? 'active' : '';
271 $css[] = "mode-{$key}";
272 $css = implode( ' ', $css );
273 ?>
274 <div class="mode <?php echo $css; ?>" data-mode="<?php echo esc_attr( $key ); ?>">
275 <div class="header-text">
276 <div class="mode-header">
277 <div class="chevron-wrapper">
278 <?php if ( $mode['human_readable_name'] === 'Ludicrous' ) : ?>
279 <svg width="17" height="17" viewBox="0 0 17 17" fill="none" xmlns="http://www.w3.org/2000/svg">
280 <path
281 d="M9.63151 2.12634L2.96484 10.1263H8.96484L8.29818 15.4597L14.9648 7.45968H8.96484L9.63151 2.12634Z"
282 stroke="#776795" stroke-linecap="round" stroke-linejoin="round" />
283 </svg>
284 <?php else :
285 for ( $i = 0; $i < $chevrons; $i++ ) : ?>
286 <svg xmlns="http://www.w3.org/2000/svg" width="10" height="6" viewBox="0 0 10 6" fill="none">
287 <path d="M9.29688 4.79297L5.29688 0.792969L1.29688 4.79297" stroke="#776795"
288 stroke-linecap="round" stroke-linejoin="round" />
289 </svg>
290 <?php endfor;
291 endif; ?>
292 </div>
293 <h3><?php echo esc_html( $mode['human_readable_name'] ); ?></h3>
294 <?php if ( ! $mode['is_available'] && $mode['human_readable_name'] === 'Ludicrous' ) : ?>
295 <div class="ml-auto">
296 <?php echo '<span class="badge badge-success">' . esc_html__( 'Available on Starter', 'nitropack' ) . '</span>' ?>
297 </div>
298 <?php endif; ?>
299 </div>
300 <p><?php echo esc_html( $mode['description_onboarding'] ); ?></p>
301 </div>
302 <?php if ( $active ) : ?>
303
304 <a class="btn btn-secondary select-mode selected">
305 <svg class="icon" xmlns="http://www.w3.org/2000/svg" width="12" height="9" viewBox="0 0 12 9"
306 fill="none">
307 <path d="M11.4674 0.792969L4.13411 8.1263L0.800781 4.79297" stroke="#4600CC" stroke-linecap="round"
308 stroke-linejoin="round" />
309 </svg><?php esc_html_e( 'Active Mode', 'nitropack' ); ?></a>
310 <?php else :
311 if ( ! $mode['is_available'] ) {
312 ?>
313 <a class="btn btn-secondary text-center <?php echo esc_attr( $css ); ?>"
314 href="https://app.nitropack.io/subscription/buy" target="_blank"><img
315 src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'view/images/lock.svg'; ?>" />
316 <?php esc_html_e( 'Upgrade plan', 'nitropack' ); ?></a>
317 <?php } else { ?>
318 <a
319 class="btn btn-secondary select-mode <?php echo esc_attr( $css ); ?>"><?php esc_html_e( 'Select Mode', 'nitropack' ); ?></a>
320 <?php }
321 endif; ?>
322 </div>
323 <?php
324 $chevrons++;
325 endforeach; ?>
326
327 </div>
328 </div>
329 <?php }
330 }