Abilities
5 days ago
Admin
5 days ago
Compatibility
5 days ago
Helpers
5 days ago
Integrations
5 days ago
Providers
5 days ago
Queue
5 days ago
Reports
5 days ago
Tasks
5 days ago
TestEmail
5 days ago
UsageTracking
5 days ago
WPCLI
5 days ago
AbstractConnection.php
5 days ago
Conflicts.php
5 days ago
Connect.php
5 days ago
Connection.php
5 days ago
ConnectionInterface.php
5 days ago
ConnectionsManager.php
5 days ago
Core.php
5 days ago
DBRepair.php
5 days ago
Debug.php
5 days ago
EmailSendingDebug.php
5 days ago
Geo.php
5 days ago
MailCatcher.php
5 days ago
MailCatcherInterface.php
5 days ago
MailCatcherTrait.php
5 days ago
MailCatcherV6.php
5 days ago
Migration.php
5 days ago
MigrationAbstract.php
5 days ago
Migrations.php
5 days ago
OptimizedEmailSending.php
5 days ago
Options.php
5 days ago
Processor.php
5 days ago
SiteHealth.php
5 days ago
Upgrade.php
5 days ago
Uploads.php
5 days ago
WP.php
5 days ago
WPMailArgs.php
5 days ago
WPMailInitiator.php
5 days ago
Connect.php
309 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | use Plugin_Upgrader; |
| 6 | use WP_Error; |
| 7 | use WPMailSMTP\Admin\PluginsInstallSkin; |
| 8 | use WPMailSMTP\Helpers\Helpers; |
| 9 | |
| 10 | /** |
| 11 | * WP Mail SMTP Connect. |
| 12 | * |
| 13 | * WP Mail SMTP Connect is our service that makes it easy for non-techy users to |
| 14 | * upgrade to Pro version without having to manually install Pro plugin. |
| 15 | * |
| 16 | * @since 2.6.0 |
| 17 | */ |
| 18 | class Connect { |
| 19 | |
| 20 | /** |
| 21 | * Hooks. |
| 22 | * |
| 23 | * @since 2.6.0 |
| 24 | */ |
| 25 | public function hooks() { |
| 26 | |
| 27 | add_action( 'wp_mail_smtp_admin_area_enqueue_assets', [ $this, 'enqueue_scripts' ] ); |
| 28 | add_action( 'wp_ajax_wp_mail_smtp_connect_url', [ $this, 'ajax_generate_url' ] ); |
| 29 | add_action( 'wp_ajax_nopriv_wp_mail_smtp_connect_process', [ $this, 'process' ] ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Enqueue connect JS file to WP Mail SMTP admin area hook. |
| 34 | * |
| 35 | * @since 2.6.0 |
| 36 | */ |
| 37 | public function enqueue_scripts() { |
| 38 | |
| 39 | wp_enqueue_script( |
| 40 | 'wp-mail-smtp-connect', |
| 41 | wp_mail_smtp()->assets_url . '/js/connect' . WP::asset_min() . '.js', |
| 42 | [ 'jquery' ], |
| 43 | WPMS_PLUGIN_VER, |
| 44 | true |
| 45 | ); |
| 46 | |
| 47 | wp_localize_script( |
| 48 | 'wp-mail-smtp-connect', |
| 49 | 'wp_mail_smtp_connect', |
| 50 | [ |
| 51 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 52 | 'plugin_url' => wp_mail_smtp()->plugin_url, |
| 53 | 'nonce' => wp_create_nonce( 'wp-mail-smtp-connect' ), |
| 54 | 'text' => [ |
| 55 | 'plugin_activate_btn' => esc_html__( 'Activate', 'wp-mail-smtp' ), |
| 56 | 'almost_done' => esc_html__( 'Almost Done', 'wp-mail-smtp' ), |
| 57 | 'oops' => esc_html__( 'Oops!', 'wp-mail-smtp' ), |
| 58 | 'ok' => esc_html__( 'OK', 'wp-mail-smtp' ), |
| 59 | 'server_error' => esc_html__( 'Unfortunately there was a server connection error.', 'wp-mail-smtp' ), |
| 60 | ], |
| 61 | ] |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Generate and return WP Mail SMTP Connect URL. |
| 67 | * |
| 68 | * @since 2.6.0 |
| 69 | * |
| 70 | * @param string $key The license key. |
| 71 | * @param string $oth The One-time hash. |
| 72 | * @param string $redirect The redirect URL. |
| 73 | * |
| 74 | * @return bool|string |
| 75 | */ |
| 76 | public static function generate_url( $key, $oth = '', $redirect = '' ) { |
| 77 | |
| 78 | if ( empty( $key ) || wp_mail_smtp()->is_pro() ) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | $oth = ! empty( $oth ) ? $oth : hash( 'sha512', wp_rand() ); |
| 83 | $hashed_oth = hash_hmac( 'sha512', $oth, wp_salt() ); |
| 84 | |
| 85 | $redirect = ! empty( $redirect ) ? $redirect : wp_mail_smtp()->get_admin()->get_admin_page_url(); |
| 86 | |
| 87 | update_option( 'wp_mail_smtp_connect_token', $oth ); |
| 88 | update_option( 'wp_mail_smtp_connect', $key ); |
| 89 | |
| 90 | return add_query_arg( |
| 91 | [ |
| 92 | 'key' => $key, |
| 93 | 'oth' => $hashed_oth, |
| 94 | 'endpoint' => admin_url( 'admin-ajax.php' ), |
| 95 | 'version' => WPMS_PLUGIN_VER, |
| 96 | 'siteurl' => admin_url(), |
| 97 | 'homeurl' => site_url(), |
| 98 | 'redirect' => rawurldecode( base64_encode( $redirect ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 99 | 'v' => 2, |
| 100 | ], |
| 101 | 'https://upgrade.wpmailsmtp.com' |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * AJAX callback to generate and return the WP Mail SMTP Connect URL. |
| 107 | * |
| 108 | * @since 2.6.0 |
| 109 | */ |
| 110 | public function ajax_generate_url() { //phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh |
| 111 | |
| 112 | // Run a security check. |
| 113 | check_ajax_referer( 'wp-mail-smtp-connect', 'nonce' ); |
| 114 | |
| 115 | // Check for permissions. |
| 116 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 117 | wp_send_json_error( |
| 118 | [ |
| 119 | 'message' => esc_html__( 'You are not allowed to install plugins.', 'wp-mail-smtp' ), |
| 120 | ] |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | $key = ! empty( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; |
| 125 | |
| 126 | if ( empty( $key ) ) { |
| 127 | wp_send_json_error( |
| 128 | [ |
| 129 | 'message' => esc_html__( 'Please enter your license key to connect.', 'wp-mail-smtp' ), |
| 130 | ] |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | if ( wp_mail_smtp()->is_pro() ) { |
| 135 | wp_send_json_error( |
| 136 | [ |
| 137 | 'message' => esc_html__( 'Only the Lite version can be upgraded.', 'wp-mail-smtp' ), |
| 138 | ] |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | // Verify pro version is not installed. |
| 143 | $active = activate_plugin( 'wp-mail-smtp-pro/wp_mail_smtp.php', false, false, true ); |
| 144 | |
| 145 | if ( ! is_wp_error( $active ) ) { |
| 146 | |
| 147 | // Deactivate Lite. |
| 148 | deactivate_plugins( plugin_basename( WPMS_PLUGIN_FILE ) ); |
| 149 | |
| 150 | wp_send_json_success( |
| 151 | [ |
| 152 | 'message' => esc_html__( 'WP Mail SMTP Pro was already installed, but was not active. We activated it for you.', 'wp-mail-smtp' ), |
| 153 | 'reload' => true, |
| 154 | ] |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | $url = self::generate_url( $key ); |
| 159 | |
| 160 | if ( empty( $url ) ) { |
| 161 | wp_send_json_error( |
| 162 | [ |
| 163 | 'message' => esc_html__( 'There was an error while generating an upgrade URL. Please try again.', 'wp-mail-smtp' ), |
| 164 | ] |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | wp_send_json_success( [ 'url' => $url ] ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * AJAX callback to process WP Mail SMTP Connect. |
| 173 | * |
| 174 | * @since 2.6.0 |
| 175 | */ |
| 176 | public function process() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded |
| 177 | |
| 178 | $error = esc_html__( 'There was an error while installing an upgrade. Please download the plugin from wpmailsmtp.com and install it manually.', 'wp-mail-smtp' ); |
| 179 | |
| 180 | // Verify params present (oth & download link). |
| 181 | $post_oth = ! empty( $_REQUEST['oth'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['oth'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
| 182 | $post_url = ! empty( $_REQUEST['file'] ) ? esc_url_raw( wp_unslash( $_REQUEST['file'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
| 183 | |
| 184 | if ( empty( $post_oth ) || empty( $post_url ) ) { |
| 185 | wp_send_json_error( $error ); |
| 186 | } |
| 187 | |
| 188 | // Verify oth. |
| 189 | $oth = get_option( 'wp_mail_smtp_connect_token' ); |
| 190 | |
| 191 | if ( empty( $oth ) ) { |
| 192 | wp_send_json_error( $error ); |
| 193 | } |
| 194 | |
| 195 | if ( hash_hmac( 'sha512', $oth, wp_salt() ) !== $post_oth ) { |
| 196 | wp_send_json_error( $error ); |
| 197 | } |
| 198 | |
| 199 | // Delete so cannot replay. |
| 200 | delete_option( 'wp_mail_smtp_connect_token' ); |
| 201 | |
| 202 | // Set the current screen to avoid undefined notices. |
| 203 | set_current_screen( 'toplevel_page_wp-mail-smtp' ); |
| 204 | |
| 205 | // Prepare variables. |
| 206 | $url = esc_url_raw( wp_mail_smtp()->get_admin()->get_admin_page_url() ); |
| 207 | |
| 208 | // Verify pro not activated. |
| 209 | if ( wp_mail_smtp()->is_pro() ) { |
| 210 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wp-mail-smtp' ) ); |
| 211 | } |
| 212 | |
| 213 | // Verify pro not installed. |
| 214 | $active = activate_plugin( 'wp-mail-smtp-pro/wp_mail_smtp.php', $url, false, true ); |
| 215 | |
| 216 | if ( ! is_wp_error( $active ) ) { |
| 217 | deactivate_plugins( plugin_basename( WPMS_PLUGIN_FILE ) ); |
| 218 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wp-mail-smtp' ) ); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * The `request_filesystem_credentials` function will output a credentials form in case of failure. |
| 223 | * We don't want that, since it will break AJAX response. So just hide output with a buffer. |
| 224 | */ |
| 225 | ob_start(); |
| 226 | // phpcs:ignore WPForms.Formatting.EmptyLineAfterAssigmentVariables.AddEmptyLine |
| 227 | $creds = request_filesystem_credentials( $url, '', false, false, null ); |
| 228 | ob_end_clean(); |
| 229 | |
| 230 | // Check for file system permissions. |
| 231 | $perm_error = esc_html__( 'There was an error while installing an upgrade. Please check file system permissions and try again. Also, you can download the plugin from wpmailsmtp.com and install it manually.', 'wp-mail-smtp' ); |
| 232 | |
| 233 | if ( false === $creds || ! WP_Filesystem( $creds ) ) { |
| 234 | wp_send_json_error( $perm_error ); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
| 239 | */ |
| 240 | |
| 241 | // Do not allow WordPress to search/download translations, as this will break JS output. |
| 242 | remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
| 243 | |
| 244 | // Import the plugin upgrader. |
| 245 | Helpers::include_plugin_upgrader(); |
| 246 | |
| 247 | // Create the plugin upgrader with our custom skin. |
| 248 | $installer = new Plugin_Upgrader( new PluginsInstallSkin() ); |
| 249 | |
| 250 | // Error check. |
| 251 | if ( ! method_exists( $installer, 'install' ) ) { |
| 252 | wp_send_json_error( $error ); |
| 253 | } |
| 254 | |
| 255 | // Check license key. |
| 256 | $key = get_option( 'wp_mail_smtp_connect', false ); |
| 257 | delete_option( 'wp_mail_smtp_connect' ); |
| 258 | |
| 259 | if ( empty( $key ) ) { |
| 260 | wp_send_json_error( |
| 261 | new WP_Error( |
| 262 | '403', |
| 263 | esc_html__( 'There was an error while installing an upgrade. Please try again.', 'wp-mail-smtp' ) |
| 264 | ) |
| 265 | ); |
| 266 | } |
| 267 | |
| 268 | $installer->install( $post_url ); |
| 269 | |
| 270 | // Flush the cache and return the newly installed plugin basename. |
| 271 | wp_cache_flush(); |
| 272 | |
| 273 | $plugin_basename = $installer->plugin_info(); |
| 274 | |
| 275 | if ( $plugin_basename ) { |
| 276 | |
| 277 | // Deactivate the lite version first. |
| 278 | deactivate_plugins( plugin_basename( WPMS_PLUGIN_FILE ) ); |
| 279 | |
| 280 | // Activate the plugin silently. |
| 281 | $activated = activate_plugin( $plugin_basename, '', false, true ); |
| 282 | |
| 283 | if ( ! is_wp_error( $activated ) ) { |
| 284 | |
| 285 | // Save the license data, since it was verified on the connect page. |
| 286 | $options = Options::init(); |
| 287 | $all_opt = $options->get_all_raw(); |
| 288 | |
| 289 | $all_opt['license']['key'] = $key; |
| 290 | $all_opt['license']['type'] = 'pro'; |
| 291 | $all_opt['license']['is_expired'] = false; |
| 292 | $all_opt['license']['is_disabled'] = false; |
| 293 | $all_opt['license']['is_invalid'] = false; |
| 294 | $all_opt['license']['is_limit_reached'] = false; |
| 295 | |
| 296 | $options->set( $all_opt, false, true ); |
| 297 | |
| 298 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wp-mail-smtp' ) ); |
| 299 | } else { |
| 300 | // Reactivate the lite plugin if pro activation failed. |
| 301 | activate_plugin( plugin_basename( WPMS_PLUGIN_FILE ), '', false, true ); |
| 302 | wp_send_json_error( esc_html__( 'Pro version installed but needs to be activated on the Plugins page.', 'wp-mail-smtp' ) ); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | wp_send_json_error( $error ); |
| 307 | } |
| 308 | } |
| 309 |