PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 3.3.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v3.3.0
0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / src / Connect.php
wp-mail-smtp / src Last commit date
Admin 4 years ago Compatibility 4 years ago Helpers 4 years ago Providers 4 years ago Reports 4 years ago Tasks 4 years ago UsageTracking 4 years ago Conflicts.php 4 years ago Connect.php 4 years ago Core.php 4 years ago Debug.php 4 years ago Geo.php 4 years ago MailCatcher.php 4 years ago MailCatcherInterface.php 4 years ago MailCatcherV6.php 4 years ago Migration.php 4 years ago MigrationAbstract.php 4 years ago Options.php 4 years ago Processor.php 4 years ago SiteHealth.php 4 years ago Upgrade.php 4 years ago Uploads.php 4 years ago WP.php 4 years ago
Connect.php
302 lines
1 <?php
2
3 namespace WPMailSMTP;
4
5 use WP_Error;
6 use WPMailSMTP\Admin\PluginsInstallSkin;
7 use WPMailSMTP\Admin\PluginsInstallUpgrader;
8
9 /**
10 * WP Mail SMTP Connect.
11 *
12 * WP Mail SMTP Connect is our service that makes it easy for non-techy users to
13 * upgrade to Pro version without having to manually install Pro plugin.
14 *
15 * @since 2.6.0
16 */
17 class Connect {
18
19 /**
20 * Hooks.
21 *
22 * @since 2.6.0
23 */
24 public function hooks() {
25
26 add_action( 'wp_mail_smtp_admin_area_enqueue_assets', [ $this, 'enqueue_scripts' ] );
27 add_action( 'wp_ajax_wp_mail_smtp_connect_url', [ $this, 'ajax_generate_url' ] );
28 add_action( 'wp_ajax_nopriv_wp_mail_smtp_connect_process', [ $this, 'process' ] );
29 }
30
31 /**
32 * Enqueue connect JS file to WP Mail SMTP admin area hook.
33 *
34 * @since 2.6.0
35 */
36 public function enqueue_scripts() {
37
38 wp_enqueue_script(
39 'wp-mail-smtp-connect',
40 wp_mail_smtp()->assets_url . '/js/connect' . WP::asset_min() . '.js',
41 [ 'jquery' ],
42 WPMS_PLUGIN_VER,
43 true
44 );
45
46 wp_localize_script(
47 'wp-mail-smtp-connect',
48 'wp_mail_smtp_connect',
49 [
50 'ajax_url' => admin_url( 'admin-ajax.php' ),
51 'plugin_url' => wp_mail_smtp()->plugin_url,
52 'nonce' => wp_create_nonce( 'wp-mail-smtp-connect' ),
53 'text' => [
54 'plugin_activate_btn' => esc_html__( 'Activate', 'wp-mail-smtp' ),
55 'almost_done' => esc_html__( 'Almost Done', 'wp-mail-smtp' ),
56 'oops' => esc_html__( 'Oops!', 'wp-mail-smtp' ),
57 'ok' => esc_html__( 'OK', 'wp-mail-smtp' ),
58 'server_error' => esc_html__( 'Unfortunately there was a server connection error.', 'wp-mail-smtp' ),
59 ],
60 ]
61 );
62 }
63
64 /**
65 * Generate and return WP Mail SMTP Connect URL.
66 *
67 * @since 2.6.0
68 *
69 * @param string $key The license key.
70 * @param string $oth The One-time hash.
71 * @param string $redirect The redirect URL.
72 *
73 * @return bool|string
74 */
75 public static function generate_url( $key, $oth, $redirect = '' ) {
76
77 if ( empty( $key ) || wp_mail_smtp()->is_pro() ) {
78 return false;
79 }
80
81 $redirect = ! empty( $redirect ) ? $redirect : wp_mail_smtp()->get_admin()->get_admin_page_url();
82
83 update_option( 'wp_mail_smtp_connect_token', $oth );
84 update_option( 'wp_mail_smtp_connect', $key );
85
86 return add_query_arg(
87 [
88 'key' => $key,
89 'oth' => $oth,
90 'endpoint' => admin_url( 'admin-ajax.php' ),
91 'version' => WPMS_PLUGIN_VER,
92 'siteurl' => admin_url(),
93 'homeurl' => home_url(),
94 'redirect' => rawurldecode( base64_encode( $redirect ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
95 'v' => 2,
96 ],
97 'https://upgrade.wpmailsmtp.com'
98 );
99 }
100
101 /**
102 * AJAX callback to generate and return the WP Mail SMTP Connect URL.
103 *
104 * @since 2.6.0
105 */
106 public function ajax_generate_url() { //phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
107
108 // Run a security check.
109 check_ajax_referer( 'wp-mail-smtp-connect', 'nonce' );
110
111 // Check for permissions.
112 if ( ! current_user_can( 'install_plugins' ) ) {
113 wp_send_json_error(
114 [
115 'message' => esc_html__( 'You are not allowed to install plugins.', 'wp-mail-smtp' ),
116 ]
117 );
118 }
119
120 $key = ! empty( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : '';
121
122 if ( empty( $key ) ) {
123 wp_send_json_error(
124 [
125 'message' => esc_html__( 'Please enter your license key to connect.', 'wp-mail-smtp' ),
126 ]
127 );
128 }
129
130 if ( wp_mail_smtp()->is_pro() ) {
131 wp_send_json_error(
132 [
133 'message' => esc_html__( 'Only the Lite version can be upgraded.', 'wp-mail-smtp' ),
134 ]
135 );
136 }
137
138 // Verify pro version is not installed.
139 $active = activate_plugin( 'wp-mail-smtp-pro/wp_mail_smtp.php', false, false, true );
140
141 if ( ! is_wp_error( $active ) ) {
142
143 // Deactivate Lite.
144 deactivate_plugins( plugin_basename( WPMS_PLUGIN_FILE ) );
145
146 wp_send_json_success(
147 [
148 'message' => esc_html__( 'WP Mail SMTP Pro was already installed, but was not active. We activated it for you.', 'wp-mail-smtp' ),
149 'reload' => true,
150 ]
151 );
152 }
153
154 $oth = hash( 'sha512', wp_rand() );
155 $url = self::generate_url( $key, $oth );
156
157 if ( empty( $url ) ) {
158 wp_send_json_error(
159 [
160 'message' => esc_html__( 'There was an error while generating an upgrade URL. Please try again.', 'wp-mail-smtp' ),
161 ]
162 );
163 }
164
165 wp_send_json_success(
166 [
167 'url' => $url,
168 'back_url' => add_query_arg(
169 [
170 'action' => 'wp_mail_smtp_connect',
171 'oth' => $oth,
172 ],
173 admin_url( 'admin-ajax.php' )
174 ),
175 ]
176 );
177 }
178
179 /**
180 * AJAX callback to process WP Mail SMTP Connect.
181 *
182 * @since 2.6.0
183 */
184 public function process() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded
185
186 $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' );
187
188 // Verify params present (oth & download link).
189 $post_oth = ! empty( $_REQUEST['oth'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['oth'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
190 $post_url = ! empty( $_REQUEST['file'] ) ? esc_url_raw( wp_unslash( $_REQUEST['file'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
191
192 if ( empty( $post_oth ) || empty( $post_url ) ) {
193 wp_send_json_error( $error );
194 }
195
196 // Verify oth.
197 $oth = get_option( 'wp_mail_smtp_connect_token' );
198
199 if ( empty( $oth ) || ! hash_equals( $oth, $post_oth ) ) {
200 wp_send_json_error( $error );
201 }
202
203 // Delete so cannot replay.
204 delete_option( 'wp_mail_smtp_connect_token' );
205
206 // Set the current screen to avoid undefined notices.
207 set_current_screen( 'toplevel_page_wp-mail-smtp' );
208
209 // Prepare variables.
210 $url = esc_url_raw( wp_mail_smtp()->get_admin()->get_admin_page_url() );
211
212 // Verify pro not activated.
213 if ( wp_mail_smtp()->is_pro() ) {
214 wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wp-mail-smtp' ) );
215 }
216
217 // Verify pro not installed.
218 $active = activate_plugin( 'wp-mail-smtp-pro/wp_mail_smtp.php', $url, false, true );
219
220 if ( ! is_wp_error( $active ) ) {
221 deactivate_plugins( plugin_basename( WPMS_PLUGIN_FILE ) );
222 wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wp-mail-smtp' ) );
223 }
224
225 $creds = request_filesystem_credentials( $url, '', false, false, null );
226
227 // Check for file system permissions.
228 $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' );
229
230 if ( false === $creds || ! WP_Filesystem( $creds ) ) {
231 wp_send_json_error( $perm_error );
232 }
233
234 /*
235 * We do not need any extra credentials if we have gotten this far, so let's install the plugin.
236 */
237
238 // Do not allow WordPress to search/download translations, as this will break JS output.
239 remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
240
241 // Create the plugin upgrader with our custom skin.
242 $installer = new PluginsInstallUpgrader( new PluginsInstallSkin() );
243
244 // Error check.
245 if ( ! method_exists( $installer, 'install' ) ) {
246 wp_send_json_error( $error );
247 }
248
249 // Check license key.
250 $key = get_option( 'wp_mail_smtp_connect', false );
251 delete_option( 'wp_mail_smtp_connect' );
252
253 if ( empty( $key ) ) {
254 wp_send_json_error(
255 new WP_Error(
256 '403',
257 esc_html__( 'There was an error while installing an upgrade. Please try again.', 'wp-mail-smtp' )
258 )
259 );
260 }
261
262 $installer->install( $post_url );
263
264 // Flush the cache and return the newly installed plugin basename.
265 wp_cache_flush();
266
267 $plugin_basename = $installer->plugin_info();
268
269 if ( $plugin_basename ) {
270
271 // Deactivate the lite version first.
272 deactivate_plugins( plugin_basename( WPMS_PLUGIN_FILE ) );
273
274 // Activate the plugin silently.
275 $activated = activate_plugin( $plugin_basename, '', false, true );
276
277 if ( ! is_wp_error( $activated ) ) {
278
279 // Save the license data, since it was verified on the connect page.
280 $options = Options::init();
281 $all_opt = $options->get_all_raw();
282
283 $all_opt['license']['key'] = $key;
284 $all_opt['license']['type'] = 'pro';
285 $all_opt['license']['is_expired'] = false;
286 $all_opt['license']['is_disabled'] = false;
287 $all_opt['license']['is_invalid'] = false;
288
289 $options->set( $all_opt, false, true );
290
291 wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wp-mail-smtp' ) );
292 } else {
293 // Reactivate the lite plugin if pro activation failed.
294 activate_plugin( plugin_basename( WPMS_PLUGIN_FILE ), '', false, true );
295 wp_send_json_error( esc_html__( 'Pro version installed but needs to be activated on the Plugins page.', 'wp-mail-smtp' ) );
296 }
297 }
298
299 wp_send_json_error( $error );
300 }
301 }
302