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 / Connect.php
nitropack / classes / WordPress Last commit date
AdvancedCache 3 days ago Notifications 3 days ago Settings 3 days ago Admin.php 1 month ago CLI.php 3 days ago Config.php 1 year ago ConflictingPlugins.php 10 months ago Connect.php 3 days ago Cron.php 1 year ago Invalidations.php 2 months ago NitroPack.php 3 days ago Settings.php 4 months ago
Connect.php
205 lines
1 <?php
2
3 namespace NitroPack\WordPress;
4 use NitroPack\WordPress\AdvancedCache\AdvancedCache;
5
6 /**
7 * Here we handle the connection and disconnection of the plugin to NitroPack.
8 */
9 class Connect {
10 private $nitropack;
11 public function __construct() {
12 add_action( 'wp_ajax_nitropack_connect', [ $this, 'nitropack_connect' ] );
13 add_action( 'wp_ajax_nitropack_disconnect', [ $this, 'nitropack_disconnect' ] );
14 }
15 public function nitropack_connect() {
16 nitropack_verify_ajax_nonce( $_REQUEST );
17 $siteId = ! empty( $_POST["siteId"] ) ? $_POST["siteId"] : "";
18 $siteSecret = ! empty( $_POST["siteSecret"] ) ? $_POST["siteSecret"] : "";
19 $this->nitropack_verify_connect( $siteId, $siteSecret );
20 }
21 public function nitropack_verify_connect( string $siteId, string $siteSecret ) {
22 $blogId = get_current_blog_id();
23 $multisite_reason = ' ';
24 if ( $blogId ) {
25 $multisite_reason .= is_main_site() ? "in main site" : "in multisite: $blogId";
26 }
27 $this->nitropack = NitroPack::getInstance();
28
29 $this->nitropack->getLogger()->notice( 'Verifying connection to NitroPack API' . $multisite_reason );
30
31 if ( ! nitropack_check_func_availability( 'stream_socket_client' ) ) {
32
33 $this->nitropack->getLogger()->error( 'stream_socket_client function is not allowed by your host.' );
34
35 nitropack_json_and_exit( array( "status" => "error", "message" => "stream_socket_client function is not allowed by your host. <a href=\"https://support.nitropack.io/hc/en-us/articles/360020898137\" target=\"_blank\" rel=\"noreferrer noopener\">Read more</a>" ) );
36 }
37
38 if ( ! nitropack_check_func_availability( 'stream_context_create' ) ) {
39 // <a href=\"https://support.nitropack.io/hc/en-us/articles/360020898137\" target=\"_blank\" rel=\"noreferrer noopener\">Read more</a>
40 // ^ Similar article needed on website for stream_context_create function
41 $this->nitropack->getLogger()->error( 'stream_context_create function is not allowed by your host.' );
42 nitropack_json_and_exit( array( "status" => "error", "message" => "stream_context_create function is not allowed by your host." ) );
43 }
44
45 if ( empty( $siteId ) || empty( $siteSecret ) ) {
46
47 $this->nitropack->getLogger()->error( 'Invalid API key or API secret key value' );
48
49 nitropack_json_and_exit( array( "status" => "error", "message" => __( 'Invalid API key or API secret key value', 'nitropack' ) ) );
50 }
51
52 //remove tags and whitespaces
53 $siteId = trim( esc_attr( $siteId ) );
54 $siteSecret = trim( esc_attr( $siteSecret ) );
55
56 if ( ! nitropack_validate_site_id( $siteId ) || ! nitropack_validate_site_secret( $siteSecret ) ) {
57
58 $this->nitropack->getLogger()->error( 'Invalid API key or API secret key value' . $multisite_reason );
59
60 nitropack_json_and_exit( array( "status" => "error", "message" => __( 'Invalid API key or API secret key value', 'nitropack' ) ) );
61 }
62
63 try {
64
65 if ( null !== $nitro = get_nitropack_sdk( $siteId, $siteSecret, NULL, true ) ) {
66 if ( ! $nitro->checkHealthStatus() ) {
67
68 $this->nitropack->getLogger()->error( 'Error when trying to communicate with NitroPack\'s servers. Current health status: ' . $nitro->getHealthStatus() );
69
70 nitropack_json_and_exit( array(
71 "status" => "error",
72 "message" => __( 'Error when trying to communicate with NitroPack\'s servers. Please try again in a few minutes. If the issue persists, please', 'nitropack' ) . " <a href='https://support." . NITROPACK_HOST . "/hc/en-us' target='_blank'>contact us</a>."
73 ) );
74 }
75
76 $preventParing = apply_filters( 'nitropack_prevent_connect', nitropack_prevent_connecting( $nitro ) );
77 if ( $preventParing ) {
78
79 $this->nitropack->getLogger()->error( 'It looks like another site ' . $preventParing['remote'] . ' is already connected using these credentials. Either disconnect it or register a new site in your NitroPack dashboard.' );
80
81 nitropack_json_and_exit( array(
82 "status" => "error",
83 "message" => "It looks like another site <strong>({$preventParing['remote']})</strong> is already connected using these credentials. Either disconnect it or register a new site in your NitroPack dashboard.<br/>
84 <a href='https://support.nitropack.io/hc/en-us/articles/4405254569745' target='_blank' rel='noreferrer noopener'>Read more</a>"
85 ) );
86 }
87 $token = nitropack_generate_webhook_token( $siteId );
88 get_nitropack()->settings->set_required_settings( $token );
89
90 nitropack_setup_webhooks( $nitro, $token );
91
92 // _icl_current_language is WPML cookie, it is added here for compatibility with this module
93 $customVariationCookies = array( "np_wc_currency", "np_wc_currency_language", "_icl_current_language" );
94 $variationCookies = $nitro->getApi()->getVariationCookies();
95 foreach ( $variationCookies as $cookie ) {
96 $index = array_search( $cookie["name"], $customVariationCookies );
97 if ( $index !== false ) {
98 array_splice( $customVariationCookies, $index, 1 );
99 }
100 }
101
102 foreach ( $customVariationCookies as $cookieName ) {
103 $nitro->getApi()->setVariationCookie( $cookieName );
104 }
105
106 $nitro->fetchConfig(); // Reload the variation cookies
107
108 get_nitropack()->updateCurrentBlogConfig( $siteId, $siteSecret, $blogId );
109 $advanced_cache = new AdvancedCache();
110 $advanced_cache->install_advanced_cache();
111
112 try {
113 do_action( 'nitropack_integration_purge_all' );
114 } catch (\Exception $e) {
115 // Exception while signaling our 3rd party integration addons to purge their cache
116 }
117
118 nitropack_event( "connect", $nitro );
119 nitropack_event( "enable_extension", $nitro );
120
121 // Optimize front page
122 $siteConfig = nitropack_get_site_config();
123 if ( $siteConfig ) {
124 $nitro->getApi()->runWarmup( [ $siteConfig['home_url'] ], true ); // force run a warmup on the home page
125 }
126
127 $this->nitropack->getLogger()->notice( 'NitroPack connected' . $multisite_reason );
128
129 $onboarding = get_option( 'nitropack-onboardingPassed' );
130 $url = $onboarding === '1' ? get_admin_url( $blogId, "admin.php?page=nitropack" ) : get_admin_url( $blogId, "admin.php?page=nitropack&onboarding=1" );
131 nitropack_json_and_exit( array(
132 "status" => "success",
133 "url" => $url,
134 "message" => __( "Connected", "nitropack" )
135 ) );
136 }
137 } catch (\NitroPack\SDK\WebhookException $e) {
138
139 $this->nitropack->getLogger()->error( $e );
140
141 nitropack_json_and_exit( array( "status" => "error", "message" => $e ) );
142 } catch (\NitroPack\SDK\StorageException $e) {
143
144 $this->nitropack->getLogger()->error( 'Permission Error: ' . $e );
145
146 nitropack_json_and_exit( array( "status" => "error", "message" => __( 'Permission Error: ', 'nitropack' ) . $e ) );
147 } catch (\NitroPack\SDK\EmptyConfigException $e) {
148
149 $this->nitropack->getLogger()->error( 'Error while fetching remote config: ' . $e );
150
151 nitropack_json_and_exit( array( "status" => "error", "message" => __( 'Error while fetching remote config: ', 'nitropack' ) . $e ) );
152 } catch (\NitroPack\SocketOpenException $e) {
153
154 $this->nitropack->getLogger()->error( 'Can\'t establish connection with NitroPack\'s servers. ' . $e );
155
156 nitropack_json_and_exit( array( "status" => "error", "message" => __( 'Can\'t establish connection with NitroPack\'s servers', 'nitropack' ) ) );
157 } catch (\Exception $e) {
158
159 $this->nitropack->getLogger()->error( 'Incorrect API credentials. Please make sure that you copied them correctly and try again. ' . $e );
160
161 nitropack_json_and_exit( array( "status" => "error", "message" => __( 'Incorrect API credentials. Please make sure that you copied them correctly and try again.', 'nitropack' ) ) );
162 }
163
164 $this->nitropack->getLogger()->error( 'Error verifying connection to NitroPack' . $multisite_reason );
165
166 nitropack_json_and_exit( array( "status" => "error" ) );
167 }
168 public function nitropack_disconnect() {
169 nitropack_verify_ajax_nonce( $_REQUEST );
170 $blogId = get_current_blog_id();
171 $multisite_reason = ' ';
172 if ( $blogId ) {
173 $multisite_reason .= is_main_site() ? "in main site" : "in multisite: $blogId";
174 }
175 $this->nitropack = NitroPack::getInstance();
176 $this->nitropack->getLogger()->notice( 'NitroPack disconnecting... ' . $multisite_reason );
177
178 //Uninstall advanced cache
179 $advanced_cache = new AdvancedCache();
180 $advanced_cache->uninstall_advanced_cache();
181
182 try {
183 nitropack_event( "disconnect" );
184 if ( null !== $nitro = get_nitropack_sdk() ) {
185 nitropack_reset_webhooks( $nitro );
186 }
187 } catch (\Exception $e) {
188 $this->nitropack->getLogger()->error( 'NitroPack cannot be disconnected. Error: ' . $e );
189 nitropack_json_and_exit( array( "status" => "error", "message" => $e ) );
190 }
191
192 get_nitropack()->unsetCurrentBlogConfig();
193
194 $hostingNoticeFile = nitropack_get_hosting_notice_file();
195 if ( file_exists( $hostingNoticeFile ) ) {
196 if ( WP_DEBUG ) {
197 unlink( $hostingNoticeFile );
198 } else {
199 @unlink( $hostingNoticeFile );
200 }
201 }
202 $this->nitropack->getLogger()->notice( 'NitroPack disconnected.' . $multisite_reason );
203 nitropack_json_and_exit( array( "status" => "success", "message" => __( "Disconnected", "nitropack" ) ) );
204 }
205 }