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 / GeneratePreview.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
GeneratePreview.php
125 lines
1 <?php
2 namespace NitroPack\WordPress\Settings;
3
4 use NitroPack\WordPress\Settings\OptimizationLevel;
5
6 /** Handles generation of "Preview Site" (onboarding process => /admin.php?page=nitropack&onboarding=1) for different optimization levels
7 * Passes automatically onboarding for all current users when they visit the nitropack page
8 * Enables caching for logged-in users in preview mode
9 * AJAX handlers to check if preview cache is ready and to finalize onboarding
10 */
11 class GeneratePreview {
12 private static $instance = NULL;
13 public function __construct() {
14 add_action( 'admin_init', [ $this, 'pass_onboarding_for_current_users' ] );
15 add_filter( 'nitropack_passes_cookie_requirements', [ $this, 'enable_cache_for_logged_in_preview_users' ] );
16 add_action( 'wp_ajax_nitropack_generate_homepage_preview', [ $this, 'nitropack_generate_homepage_preview' ] );
17 add_action( 'wp_ajax_nitropack_is_homepage_preview_cached', [ $this, 'nitropack_is_homepage_preview_cached' ] );
18 add_action( 'wp_ajax_nitropack_passed_onboarding', [ $this, 'nitropack_passed_onboarding' ] );
19
20 }
21 public static function getInstance() {
22 if ( ! self::$instance ) {
23 self::$instance = new GeneratePreview();
24 }
25 return self::$instance;
26 }
27 /**
28 * Pass onboarding for current users when they visit the nitropack page
29 * and display onboarding for inital users when connect NitroPack
30 * @return void
31 */
32 public function pass_onboarding_for_current_users() {
33 if ( ! get_nitropack()->isConnected() )
34 return;
35
36 $onboarding = get_option( 'nitropack-onboardingPassed' );
37 if ( ! empty( $_GET['page'] ) && $_GET['page'] === 'nitropack' && ! empty( $_GET['onboarding'] ) && $onboarding !== '1' ) {
38 update_option( 'nitropack-onboardingPassed', 0 );
39 } else if ( ! empty( $_GET['page'] ) && $_GET['page'] === 'nitropack' ) {
40 update_option( 'nitropack-onboardingPassed', 1 );
41 }
42 }
43 /**
44 * Enable caching for logged-in users in preview mode
45 * @param boolean $passes
46 * @return boolean
47 */
48 public function enable_cache_for_logged_in_preview_users( $passes ): bool {
49 $isUserLoggedIn = nitropack_is_logged_in();
50 if ( $isUserLoggedIn && ! empty( $_GET['previewmode'] ) ) {
51 return true;
52 }
53 return $passes;
54 }
55
56 /**
57 * Get homepage preview URL with selected optimization level.
58 * Sort $_GET alphabetically to ensure consistent URL for caching (first previewmode, then testnitro)
59 * @return string
60 */
61 public function get_homepage_preview_url( $mode_name = null ): string {
62 $siteConfig = nitropack_get_site_config();
63
64 if ( ! $mode_name ) {
65 $optimization_level_class = new OptimizationLevel();
66 $mode_name = $optimization_level_class->fetch_optimization_name();
67 }
68 $home_url_preview = $siteConfig["home_url"] . "/?previewmode=" . $mode_name . "&testnitro=1";
69 return $home_url_preview;
70 }
71
72 /**
73 * Check if preview cache is ready (called repeatedly up to 60 sec in preview-site.php)
74 * @return void AJAX response
75 */
76 public function nitropack_is_homepage_preview_cached(): void {
77 nitropack_verify_ajax_nonce( $_REQUEST );
78
79 $mode_name = isset( $_POST['mode_name'] ) ? sanitize_text_field( $_POST['mode_name'] ) : NULL;
80 $home_url_preview = $this->get_homepage_preview_url( $mode_name );
81
82 $siteConfig = nitropack_get_site_config();
83
84 if ( null !== $nitro = get_nitropack_sdk( $siteConfig["siteId"], $siteConfig["siteSecret"], $home_url_preview ) ) {
85 /* IMPORTANT: Remove the AJAX check which is used in isAJAXRequest() (nitropack/nitropack-sdk/NitroPack/SDK/NitroPack.php).
86 * If not done, it will return 403 to our API servers and cache will not be fetched.
87 */
88 if ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
89 unset( $_SERVER['HTTP_X_REQUESTED_WITH'] );
90 }
91 $hasLocalCache = $nitro->hasLocalCache( false );
92
93 /* If local cache doesn't exist, then check remote cache and force to generate one. */
94 if ( ! $hasLocalCache ) {
95 try {
96 $nitro->hasRemoteCache( 'default', false );
97 } catch (\Exception $e) {
98 nitropack_json_and_exit( [ "preview" => 0 ] );
99 }
100 }
101
102 nitropack_json_and_exit( [ "preview" => $hasLocalCache ? 1 : 0 ] );
103
104 } else {
105 nitropack_json_and_exit( [ "preview" => 0 ] );
106 }
107 }
108
109 /**
110 * Finalize onboarding by updating the option and disabling safemode
111 * @return void
112 */
113 public function nitropack_passed_onboarding(): void {
114 nitropack_verify_ajax_nonce( $_REQUEST );
115
116 update_option( 'nitropack-onboardingPassed', 1 );
117
118 if ( null !== $nitro = get_nitropack_sdk() ) {
119 $nitro->disableSafeMode();
120 }
121 $nitro_url = admin_url( "admin.php?page=nitropack" );
122
123 nitropack_json_and_exit( [ "status" => 1, "redirect" => $nitro_url ] );
124 }
125 }