PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.3
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.3
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 / CacheWarmup.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 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
CacheWarmup.php
267 lines
1 <?php
2
3 namespace NitroPack\WordPress\Settings;
4 use NitroPack\WordPress\NitroPack;
5
6 class CacheWarmup {
7 private static $instance = NULL;
8 public function __construct() {
9 add_action( 'wp_ajax_nitropack_skip_cache_warmup', [ $this, 'nitropack_skip_cache_warmup' ] );
10 add_action( 'wp_ajax_nitropack_enable_warmup', [ $this, 'nitropack_enable_warmup' ] );
11 add_action( 'wp_ajax_nitropack_disable_warmup', [ $this, 'nitropack_disable_warmup' ] );
12 add_action( 'wp_ajax_nitropack_warmup_stats', [ $this, 'nitropack_warmup_stats' ] );
13 add_action( 'wp_ajax_nitropack_estimate_warmup', [ $this, 'nitropack_estimate_warmup' ] );
14 add_action( 'wp_ajax_nitropack_run_warmup', [ $this, 'nitropack_run_warmup' ] );
15 }
16 public static function getInstance() {
17 if ( null === self::$instance ) {
18 self::$instance = new self();
19 }
20 return self::$instance;
21 }
22
23 /* Dismiss cache warmup notice in the final third step when Onboarding */
24 public function nitropack_skip_cache_warmup() {
25 nitropack_verify_ajax_nonce( $_REQUEST );
26
27 $nitropack_notices = get_option( 'nitropack-dismissed-notices', array() );
28 $nitropack_notices['skip_cache_warmup'] = 1;
29 update_option( 'nitropack-dismissed-notices', $nitropack_notices );
30
31 nitropack_json_and_exit( array(
32 "type" => "success",
33 "message" => nitropack_admin_toast_msgs( 'success', esc_html__( 'Cache warmup skipped.', 'nitropack' ) )
34 ) );
35 }
36 /**
37 * AJAX handler to enable cache warmup
38 * @return void
39 */
40 public function nitropack_enable_warmup() {
41 nitropack_verify_ajax_nonce( $_REQUEST );
42 $prepend = '';
43 if ( nitropack_is_wp_cli() )
44 $prepend = '[CLI] ';
45 if ( null !== $nitro = get_nitropack_sdk() ) {
46 try {
47 $nitro->getApi()->enableWarmup();
48 $nitro->getApi()->setWarmupHomepage( get_home_url() );
49 $nitro->getApi()->runWarmup();
50 } catch (\Exception $e) {
51 NitroPack::getInstance()->getLogger()->error( $prepend . 'Cache warmup cannot be enabled. Error: ' . $e->getTraceAsString() );
52 }
53 NitroPack::getInstance()->getLogger()->notice( $prepend . 'Cache warmup is enabled' );
54 nitropack_json_and_exit( array(
55 "type" => "success",
56 "message" => __( 'Cache warmup has been enabled successfully!', 'nitropack' )
57 ) );
58 }
59 NitroPack::getInstance()->getLogger()->notice( $prepend . 'Cache warmup cannot be enabled' );
60 nitropack_json_and_exit( array(
61 "type" => "error",
62 "message" => __( 'There was an error while enabling the cache warmup!', 'nitropack' )
63 ) );
64 }
65 /**
66 * AJAX handler to disable cache warmup
67 * @return void
68 */
69 public function nitropack_disable_warmup() {
70 nitropack_verify_ajax_nonce( $_REQUEST );
71 $prepend = '';
72 if ( nitropack_is_wp_cli() )
73 $prepend = '[CLI] ';
74 if ( null !== $nitro = get_nitropack_sdk() ) {
75 try {
76 $nitro->getApi()->disableWarmup();
77 $nitro->getApi()->resetWarmup();
78 delete_option( 'nitropack-warmup-sitemap' );
79 } catch (\Exception $e) {
80 NitroPack::getInstance()->getLogger()->error( $prepend . 'Cache warmup cannot be disabled. Error: ' . $e->getTraceAsString() );
81 }
82 NitroPack::getInstance()->getLogger()->notice( $prepend . 'Cache warmup is disabled' );
83 nitropack_json_and_exit( array(
84 "type" => "success",
85 "message" => __( 'Cache warmup has been disabled successfully!', 'nitropack' )
86 ) );
87 }
88 nitropack_json_and_exit( array(
89 "type" => "error",
90 "message" => __( 'There was an error while disabling the cache warmup!', 'nitropack' )
91 ) );
92 }
93
94 /**
95 * AJAX handler to run cache warmup
96 * @return void
97 */
98 public function nitropack_run_warmup() {
99 nitropack_verify_ajax_nonce( $_REQUEST );
100
101 NitroPack::getInstance()->getLogger()->notice( 'Running warmup' );
102
103 if ( null !== $nitro = get_nitropack_sdk() ) {
104 try {
105 $nitro->getApi()->runWarmup();
106 NitroPack::getInstance()->getLogger()->notice( 'Cache warmup has been started' );
107 nitropack_json_and_exit( array(
108 "type" => "success",
109 "message" => __( 'Success! Cache warmup has been started successfully!', 'nitropack' )
110 ) );
111 } catch (\Exception $e) {
112 NitroPack::getInstance()->getLogger()->error( 'There was an error while starting the cache warmup. Error: ' . $e->getTraceAsString() );
113 }
114 }
115
116 nitropack_json_and_exit( array(
117 "type" => "error",
118 "message" => __( 'Error! There was an error while starting the cache warmup!', 'nitropack' )
119 ) );
120 }
121
122 /**
123 * AJAX handler to estimate cache warmup
124 * @return void
125 */
126 public function nitropack_estimate_warmup() {
127 nitropack_verify_ajax_nonce( $_REQUEST );
128 if ( null !== $nitro = get_nitropack_sdk() ) {
129 try {
130 if ( ! session_id() ) {
131 session_start();
132 }
133 $id = ! empty( $_POST["estId"] ) ? preg_replace( "/[^a-fA-F0-9]/", "", (string) $_POST["estId"] ) : NULL;
134 if ( $id !== NULL && ( ! is_string( $id ) || $id != $_SESSION["nitroEstimateId"] ) ) {
135 nitropack_json_and_exit( array(
136 "type" => "error",
137 "message" => __( 'Invalid estimation ID!', 'nitropack' )
138 ) );
139 }
140
141 $sitemapUrls = nitropack_active_sitemap_plugins() ? nitropack_get_site_maps() : NULL;
142 $configuredSitemap = false;
143
144 if ( $sitemapUrls === NULL ) {
145
146 $defaultSitemap = get_default_sitemap();
147 if ( $defaultSitemap ) {
148 $nitro->getApi()->setWarmupSitemap( $defaultSitemap );
149 $configuredSitemap = true;
150 }
151
152 delete_option( 'nitropack-warmup-sitemap' );
153 } else {
154
155 $warmupSitemap = evaluate_warmup_sitemap( $sitemapUrls );
156 if ( $warmupSitemap ) {
157 $nitro->getApi()->setWarmupSitemap( $warmupSitemap );
158 $configuredSitemap = true;
159 }
160 }
161
162 if ( ! $configuredSitemap ) {
163 $nitro->getApi()->setWarmupSitemap( NULL );
164 }
165
166 $nitro->getApi()->setWarmupHomepage( get_home_url() );
167
168 $optimizationsEstimate = $nitro->getApi()->estimateWarmup( $id );
169
170 if ( $id === NULL ) {
171 $_SESSION["nitroEstimateId"] = $optimizationsEstimate; // When id is NULL, $optimizationsEstimate holds the ID for the newly started estimate
172 }
173 } catch (\Exception $e) {
174 }
175 $json_data = array(
176 "type" => "success",
177 "res" => $optimizationsEstimate,
178 "sitemap_indication" => get_option( 'nitropack-warmup-sitemap', false ),
179 "message" => __( 'Warmup estimation failed.', 'nitropack' ),
180 );
181 if ( is_int( $optimizationsEstimate ) && $optimizationsEstimate > 0 ) {
182 $json_data["message"] = __( 'Cache warmup has been estimated successfully.', 'nitropack' );
183 } else if ( $optimizationsEstimate === 0 ) {
184 $json_data["message"] = __( 'We could not find any links for warming up on your home page.', 'nitropack' );
185 } else {
186 $json_data["message"] = __( 'Warmup estimation failed. Please try again or contact support if the issue persists', 'nitropack' );
187 }
188 nitropack_json_and_exit( $json_data );
189 }
190
191 nitropack_json_and_exit( array(
192 "type" => "error",
193 "message" => __( 'Warmup estimation failed.', 'nitropack' )
194 ) );
195 }
196 /**
197 * AJAX handler to get cache warmup stats
198 * @return void
199 */
200 public function nitropack_warmup_stats() {
201 nitropack_verify_ajax_nonce( $_REQUEST );
202 if ( null !== $nitro = get_nitropack_sdk() ) {
203 try {
204 $stats = $nitro->getApi()->getWarmupStats();
205 } catch (\Exception $e) {
206 nitropack_json_and_exit( array(
207 "type" => "error",
208 "message" => __( 'Error! There was an error while fetching warmup stats!', 'nitropack' )
209 ) );
210 }
211
212 nitropack_json_and_exit( array(
213 "type" => "success",
214 "stats" => $stats
215 ) );
216 }
217
218
219 nitropack_json_and_exit( array(
220 "type" => "error",
221 "message" => __( 'Error! There was an error while fetching warmup stats!', 'nitropack' )
222 ) );
223 }
224 /* Render cache warmup setting widget */
225 public function render() {
226 $nitro = get_nitropack_sdk();
227 try {
228 $cache_warmup_stats = $nitro->getApi()->getWarmupStats();
229 } catch ( \Exception $e ) {
230 $cache_warmup_stats = [ 'status' => 0 ];
231 }
232 ?>
233 <div class="nitro-option" id="cache-warmup-widget">
234 <div class="nitro-option-main">
235 <div class="text-box" id="warmup-status-slider">
236
237 <?php $sitemap = get_option( 'nitropack-warmup-sitemap', false );
238 $toolTipDisplayState = $sitemap ? '' : 'hidden'; ?>
239
240 <h6><?php esc_html_e( 'Cache warmup', 'nitropack' ); ?> <span
241 class="badge badge-primary ml-2"><?php esc_html_e( 'Recommended', 'nitropack' ); ?></span>
242 <span class="tooltip-icon <?php echo $toolTipDisplayState; ?>" data-tooltip-target="tooltip-sitemap">
243 <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'view/images/info.svg'; ?>">
244 </span>
245 </h6>
246 <div id="tooltip-sitemap" role="tooltip" class="tooltip-container hidden">
247 <?php echo $sitemap; ?>
248 <div class="tooltip-arrow" data-popper-arrow></div>
249 </div>
250 <p><?php esc_html_e( 'Automatically pre-caches your website\'s page content', 'nitropack' ); ?>.
251 <a href="https://support.nitropack.io/en/articles/8390320-cache-warmup" class="text-blue"
252 target="_blank"><?php esc_html_e( 'Learn more', 'nitropack' ); ?></a>
253 </p>
254 </div>
255 <?php $components = new Components();
256 $components->render_toggle( 'warmup-status', $cache_warmup_stats['status'] );
257 ?>
258 </div>
259 <div class="msg-container hidden" id="loading-warmup-status">
260 <img src="<?php echo plugin_dir_url( NITROPACK_FILE ) . 'view/images/loading.svg'; ?>" alt="loading"
261 class="icon">
262 <span class="msg"><?php esc_html_e( 'Loading cache warmup status', 'nitropack' ); ?></span>
263 </div>
264 </div>
265 <?php }
266 }
267