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