PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / trunk
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization vtrunk
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 1 month ago CacheWarmup.php 1 month ago CartCache.php 4 months ago Components.php 1 month ago EditorClearCache.php 1 month ago GeneratePreview.php 7 months ago HTMLCompression.php 3 months ago Logger.php 4 months ago OptimizationLevel.php 3 days ago Optimizations.php 1 month ago PurgeCache.php 3 days ago Shortcodes.php 4 months ago StockRefresh.php 2 months ago Subscription.php 4 months ago SystemReport.php 4 months ago TestMode.php 1 month ago
OptimizationLevel.php
346 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 $optimization_options = [
96 'standard' => [
97 'human_readable_name' => 'Standard',
98 'description' => 'Standard optimization features enabled for your site. Ideal choice for maximum stability.',
99 'description_onboarding' => 'Applies basic optimizations.',
100 'is_available' => true,
101 ],
102 'medium' => [
103 'human_readable_name' => 'Medium',
104 'description' => 'Adds image lazy loading to standard optimizations. Uses built-in browser techniques for loading resources.',
105 'description_onboarding' => 'Implements moderate optimizations.',
106 'is_available' => true,
107 ],
108 'strong' => [
109 'human_readable_name' => 'Strong',
110 'description' => 'Includes smart resource loading on top of Medium optimizations. Balances speed boost with stability.',
111 'description_onboarding' => 'Strikes a great balance between stability and website speed.',
112 'is_available' => true,
113 ],
114 'ludicrous' => [
115 'human_readable_name' => 'Ludicrous',
116 'description' => 'Applies deferred JS and advanced resource loading for optimal performance and Core Web Vitals.',
117 'description_onboarding' => 'The most powerful optimization setting, aiming for best possible performance.',
118 'is_available' => $free_plan ? false : true,
119 ],
120 ];
121
122 if ( get_nitropack()->getDistribution() == "oneclick" ) {
123 $optimization_options['ludicrous_plus'] = [
124 'human_readable_name' => 'Ludicrous Plus',
125 'description' => 'Extends Ludicrous optimizations with additional features for maximum performance.',
126 'description_onboarding' => 'Extends Ludicrous optimizations with additional features for maximum performance.',
127 'is_available' => true,
128 ];
129 }
130
131 $optimization_options['custom'] = [
132 'human_readable_name' => 'Custom',
133 'description' => 'Activated when manual setups are made. Ideal for advanced NitroPack optimizations.',
134 'description_onboarding' => 'Activated when manual setups are made. Ideal for advanced NitroPack optimizations.',
135 'is_available' => false,
136 ];
137
138 return [
139 'optimization_selected' => 'medium',
140 'optimization_options' => $optimization_options,
141 ];
142 }
143 /**
144 * Fetch optimization name from NitroPack App
145 * @return string Optimization name (standard, medium, etc.)
146 */
147 public function fetch_optimization_name() {
148 if ( ! empty( $this->optimization_modes() ) ) {
149 $optimization_name = $this->optimization_modes()['optimization_selected'];
150 } else {
151 $optimization_name = 'N/A';
152 }
153
154 return $optimization_name;
155 }
156 /**
157 * Render optimization level setting HTML
158 */
159 public function render() {
160 $modes = $this->optimization_modes(); ?>
161 <div class="card card-optimization-mode">
162 <div class="card-header no-border mb-0">
163 <div class="flex items-center">
164 <h3 class="mb-0"><?php esc_html_e( 'Optimization mode', 'nitropack' ); ?></h3>
165 <span class="tooltip-icon" data-tooltip-target="tooltip-optimization">
166 <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'assets/img/info.svg'; ?>" alt="info">
167 </span>
168 <div id="tooltip-optimization" role="tooltip" class="tooltip-container hidden">
169 <?php esc_html_e( 'Select from our range of predefined optimization modes to boost your site\'s performance.', 'nitropack' );
170 ?>
171 <div class="tooltip-arrow" data-popper-arrow></div>
172 </div>
173 </div>
174 </div>
175 <?php ?>
176 <div class="tabs-wrapper">
177 <div class="tabs" id="optimization-modes">
178 <?php
179 $upgrade_msg = false;
180 $optimization_level_name = $modes['optimization_selected'];
181 foreach ( $modes['optimization_options'] as $mode_id => $mode ) :
182 $active = $optimization_level_name === $mode_id;
183 if ( $active ) {
184 $optimization_level_name = $mode['human_readable_name'];
185 }
186 $css = [];
187 $css[] = $active ? 'active btn-primary' : 'btn-link';
188 $css[] = "mode-{$mode_id}";
189 if ( ! $mode['is_available'] ) {
190 $css[] = 'disabled';
191 }
192 if ( ! $mode['is_available'] && $mode_id === 'ludicrous' ) {
193 $upgrade_msg = true;
194 }
195 $css = implode( ' ', $css );
196 ?>
197 <a class="btn tab-link <?php echo $css; ?>" data-mode="<?php echo $mode_id; ?>"
198 data-modal-target="modal-optimization-mode"
199 data-modal-toggle="modal-optimization-mode"><?php echo $mode['human_readable_name']; ?>
200 <?php if ( ! $mode['is_available'] && $mode['human_readable_name'] === 'Ludicrous' ) { ?>
201 <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'assets/img/lock.svg'; ?>" alt="lock" />
202 <?php } ?>
203 </a>
204 <?php endforeach; ?>
205 </div>
206
207 <?php if ( $upgrade_msg ) : ?>
208 <div class="upgrade-message bg-success">
209 <p class="">
210 <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
211 <g clip-path="url(#clip0_1039_2988)">
212 <path
213 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"
214 stroke="#33D2B5" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" />
215 <path d="M13.3333 10L10 6.66669L6.66667 10" stroke="#33D2B5" stroke-width="1.5"
216 stroke-linecap="round" stroke-linejoin="round" />
217 <path d="M10 13.3334V6.66669" stroke="#33D2B5" stroke-width="1.5" stroke-linecap="round"
218 stroke-linejoin="round" />
219 </g>
220 <defs>
221 <clipPath id="clip0_1039_2988">
222 <rect width="20" height="20" fill="white" />
223 </clipPath>
224 </defs>
225 </svg>
226 <?php _e( 'Ludicrous mode available on <b>Starter</b> Plan.', 'nitropack' ); ?> <a
227 href="https://app.nitropack.io/subscription/buy" class="upgrade-here"
228 target="_blank"><?php esc_html_e( 'Upgrade here', 'nitropack' ); ?></a>
229 </p>
230 </div>
231 <?php endif; ?>
232
233 <p><?php esc_html_e( 'Active Mode', 'nitropack' ); ?>: <span
234 class="active-mode"><?php echo esc_html( $optimization_level_name ); ?></span></p>
235
236 <div class="tab-content-wrapper">
237 <?php foreach ( $modes['optimization_options'] as $mode_id => $mode ) :
238 $active = ( $optimization_level_name === $mode_id ? true : false );
239 $css = [];
240 $css[] = ( $active ? '' : 'hidden' );
241 $css = implode( ' ', array_filter( $css ) );
242 ?>
243 <div class="tab-content <?php echo esc_attr( $css ); ?>" role="tabpanel"
244 data-tab="<?php echo $mode_id; ?>-tab">
245 <p class="text-secondary mt-2">
246 <?php echo esc_html( $mode['description'] ); ?>
247 </p>
248 </div>
249 <?php endforeach; ?>
250
251 <div class="hidden tab-content" role="tabpanel" data-tab="custom-tab">
252 <p class="text-secondary mt-2">
253 <?php esc_html_e( 'Activated when manual setups are made. Ideal for advanced NitroPack optimizations.', 'nitropack' ); ?>
254 </p>
255 </div>
256 </div>
257 </div>
258 <div class="card-footer">
259 <div class="flex flex-row">
260 <p class=""><?php esc_html_e( 'Which optimization mode to choose?', 'nitropack' ); ?></p>
261 <a class="text-primary btn-link ml-auto see-modes" data-modal-target="modal-modes"
262 data-modal-toggle="modal-modes"><?php esc_html_e( 'See modes comparison', 'nitropack' ); ?></a>
263 <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-modes.php'; ?>
264 </div>
265 </div>
266 <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-optimization-mode.php'; ?>
267 </div>
268 <?php }
269 public function preview_render() {
270 $optimization_modes = $this->optimization_modes();
271 unset( $optimization_modes['optimization_options']['custom'] );
272 ?>
273 <div class="optimization-modes">
274 <div class="flex flex-row">
275 <h2><?php esc_html_e( 'Optimization modes', 'nitropack' ); ?></h2>
276 <a class="text-primary btn-link ml-auto see-modes" data-modal-target="modal-modes"
277 data-modal-toggle="modal-modes"><?php esc_html_e( 'See modes comparison', 'nitropack' ); ?></a>
278 <?php require_once NITROPACK_PLUGIN_DIR . 'view/modals/modal-modes.php'; ?>
279 </div>
280 <div class="modes-container">
281 <?php $chevrons = 1;
282 $active_mode_name = $optimization_modes['optimization_selected'];
283 foreach ( $optimization_modes['optimization_options'] as $key => $mode ) :
284 $active = ( $active_mode_name === $key ? true : false );
285 $css = [];
286 $css[] = $active ? 'active' : '';
287 $css[] = "mode-{$key}";
288 $css = implode( ' ', $css );
289 ?>
290 <div class="mode <?php echo $css; ?>" data-mode="<?php echo esc_attr( $key ); ?>">
291 <div class="header-text">
292 <div class="mode-header">
293 <div class="chevron-wrapper">
294 <?php if ( $mode['human_readable_name'] === 'Ludicrous' ) : ?>
295 <svg width="17" height="17" viewBox="0 0 17 17" fill="none" xmlns="http://www.w3.org/2000/svg">
296 <path
297 d="M9.63151 2.12634L2.96484 10.1263H8.96484L8.29818 15.4597L14.9648 7.45968H8.96484L9.63151 2.12634Z"
298 stroke="#776795" stroke-linecap="round" stroke-linejoin="round" />
299 </svg>
300 <?php else :
301 for ( $i = 0; $i < $chevrons; $i++ ) : ?>
302 <svg xmlns="http://www.w3.org/2000/svg" width="10" height="6" viewBox="0 0 10 6" fill="none">
303 <path d="M9.29688 4.79297L5.29688 0.792969L1.29688 4.79297" stroke="#776795"
304 stroke-linecap="round" stroke-linejoin="round" />
305 </svg>
306 <?php endfor;
307 endif; ?>
308 </div>
309 <h3><?php echo esc_html( $mode['human_readable_name'] ); ?></h3>
310 <?php if ( ! $mode['is_available'] && $mode['human_readable_name'] === 'Ludicrous' ) : ?>
311 <div class="ml-auto">
312 <?php echo '<span class="badge badge-success">' . esc_html__( 'Available on Starter', 'nitropack' ) . '</span>' ?>
313 </div>
314 <?php endif; ?>
315 </div>
316 <p><?php echo esc_html( $mode['description_onboarding'] ); ?></p>
317 </div>
318 <?php if ( $active ) : ?>
319
320 <a class="btn btn-secondary select-mode selected">
321 <svg class="icon" xmlns="http://www.w3.org/2000/svg" width="12" height="9" viewBox="0 0 12 9"
322 fill="none">
323 <path d="M11.4674 0.792969L4.13411 8.1263L0.800781 4.79297" stroke="#4600CC" stroke-linecap="round"
324 stroke-linejoin="round" />
325 </svg><?php esc_html_e( 'Active Mode', 'nitropack' ); ?></a>
326 <?php else :
327 if ( ! $mode['is_available'] ) {
328 ?>
329 <a class="btn btn-secondary text-center <?php echo esc_attr( $css ); ?>"
330 href="https://app.nitropack.io/subscription/buy" target="_blank"><img
331 src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'assets/img/lock.svg'; ?>" alt="lock" />
332 <?php esc_html_e( 'Upgrade plan', 'nitropack' ); ?></a>
333 <?php } else { ?>
334 <a
335 class="btn btn-secondary select-mode <?php echo esc_attr( $css ); ?>"><?php esc_html_e( 'Select Mode', 'nitropack' ); ?></a>
336 <?php }
337 endif; ?>
338 </div>
339 <?php
340 $chevrons++;
341 endforeach; ?>
342
343 </div>
344 </div>
345 <?php }
346 }