PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 4.1.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v4.1.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 / SiteHealth.php
wp-mail-smtp / src Last commit date
Admin 1 year ago Compatibility 1 year ago Helpers 1 year ago Providers 1 year ago Queue 1 year ago Reports 1 year ago Tasks 1 year ago UsageTracking 1 year ago AbstractConnection.php 1 year ago Conflicts.php 1 year ago Connect.php 1 year ago Connection.php 1 year ago ConnectionInterface.php 1 year ago ConnectionsManager.php 1 year ago Core.php 1 year ago DBRepair.php 1 year ago Debug.php 1 year ago Geo.php 1 year ago MailCatcher.php 1 year ago MailCatcherInterface.php 1 year ago MailCatcherTrait.php 1 year ago MailCatcherV6.php 1 year ago Migration.php 1 year ago MigrationAbstract.php 1 year ago Migrations.php 1 year ago OptimizedEmailSending.php 1 year ago Options.php 1 year ago Processor.php 1 year ago SiteHealth.php 1 year ago Upgrade.php 1 year ago Uploads.php 1 year ago WP.php 1 year ago WPMailArgs.php 1 year ago WPMailInitiator.php 1 year ago
SiteHealth.php
429 lines
1 <?php
2
3 namespace WPMailSMTP;
4
5 use WPMailSMTP\Admin\Area;
6 use WPMailSMTP\Admin\DomainChecker;
7
8 /**
9 * Class SiteHealth adds the plugin status and information to the WP Site Health admin page.
10 *
11 * @since 1.9.0
12 */
13 class SiteHealth {
14
15 /**
16 * String of a badge color.
17 * Options: blue, green, red, orange, purple and gray.
18 *
19 * @see https://make.wordpress.org/core/2019/04/25/site-health-check-in-5-2/
20 *
21 * @since 1.9.0
22 */
23 const BADGE_COLOR = 'blue';
24
25 /**
26 * Debug info plugin slug.
27 * This should be a plugin unique string, which will be used in the WP Site Health page,
28 * for the "info" tab and will present the plugin info section.
29 *
30 * @since 1.9.0
31 */
32 const DEBUG_INFO_SLUG = 'wp_mail_smtp';
33
34 /**
35 * Translatable string for the plugin label.
36 *
37 * @since 1.9.0
38 *
39 * @return string
40 */
41 public function get_label() {
42
43 return esc_html__( 'WP Mail SMTP', 'wp-mail-smtp' );
44 }
45
46 /**
47 * Initialize the site heath functionality.
48 *
49 * @since 1.9.0
50 */
51 public function init() {
52
53 // Enqueue site health page scripts and styles.
54 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
55
56 add_filter( 'site_status_tests', array( $this, 'register_site_status_tests' ) );
57 add_filter( 'debug_information', array( $this, 'register_debug_information' ) );
58
59 // Register async test hooks.
60 add_action( 'wp_ajax_health-check-email-domain_check_test', array( $this, 'email_domain_check_test' ) );
61 }
62
63 /**
64 * Enqueue site health page scripts and styles.
65 *
66 * @since 2.8.0
67 *
68 * @param string $hook Current hook.
69 */
70 public function enqueue_assets( $hook ) {
71
72 if ( $hook !== 'site-health.php' ) {
73 return;
74 }
75
76 wp_enqueue_style(
77 'wp-mail-smtp-site-health',
78 \wp_mail_smtp()->assets_url . '/css/admin-site-health.min.css',
79 false,
80 WPMS_PLUGIN_VER
81 );
82 }
83
84 /**
85 * Register plugin WP site health tests.
86 * This will be displayed in the "Status" tab of the WP Site Health page.
87 *
88 * @since 1.9.0
89 *
90 * @param array $tests The array with all WP site health tests.
91 *
92 * @return array
93 */
94 public function register_site_status_tests( $tests ) {
95
96 $tests['direct']['wp_mail_smtp_mailer_setup_complete'] = array(
97 'label' => esc_html__( 'Is WP Mail SMTP mailer setup complete?', 'wp-mail-smtp' ),
98 'test' => array( $this, 'mailer_setup_complete_test' ),
99 );
100
101 $tests['direct']['wp_mail_smtp_db_tables_exist'] = array(
102 'label' => esc_html__( 'Do WP Mail SMTP DB tables exist?', 'wp-mail-smtp' ),
103 'test' => [ $this, 'db_tables_test' ],
104 );
105
106 $tests['async']['wp_mail_smtp_email_domain_check'] = array(
107 'label' => esc_html__( 'Is email domain configured properly?', 'wp-mail-smtp' ),
108 'test' => 'email_domain_check_test',
109 );
110
111 return $tests;
112 }
113
114 /**
115 * Register plugin WP Site Health debug information.
116 * This will be displayed in the "Info" tab of the WP Site Health page.
117 *
118 * @since 1.9.0
119 *
120 * @param array $debug_info Array of existing debug information.
121 *
122 * @return array
123 */
124 public function register_debug_information( $debug_info ) {
125
126 $debug_notices = Debug::get();
127 $db_tables = $this->get_db_tables( 'existing' );
128
129 $debug_info[ self::DEBUG_INFO_SLUG ] = [
130 'label' => $this->get_label(),
131 'fields' => [
132 'version' => [
133 'label' => esc_html__( 'Version', 'wp-mail-smtp' ),
134 'value' => WPMS_PLUGIN_VER,
135 ],
136 'license_key_type' => [
137 'label' => esc_html__( 'License key type', 'wp-mail-smtp' ),
138 'value' => wp_mail_smtp()->get_license_type(),
139 ],
140 'debug' => [
141 'label' => esc_html__( 'Debug', 'wp-mail-smtp' ),
142 'value' => ! empty( $debug_notices ) ? implode( '; ', $debug_notices ) : esc_html__( 'No debug notices found.', 'wp-mail-smtp' ),
143 ],
144 'db_tables' => [
145 'label' => esc_html__( 'DB tables', 'wp-mail-smtp' ),
146 'value' => ! empty( $db_tables ) ?
147 implode( ', ', $db_tables ) : esc_html__( 'No DB tables found.', 'wp-mail-smtp' ),
148 'private' => true,
149 ],
150 ],
151 ];
152
153 // Install date.
154 $activated = get_option( 'wp_mail_smtp_activated', [] );
155 if ( ! empty( $activated['lite'] ) ) {
156 $date = $activated['lite'] + ( get_option( 'gmt_offset' ) * 3600 );
157
158 $debug_info[ self::DEBUG_INFO_SLUG ]['fields']['lite_install_date'] = [
159 'label' => esc_html__( 'Lite install date', 'wp-mail-smtp' ),
160 'value' => date_i18n( esc_html__( 'M j, Y @ g:ia' ), $date ),
161 ];
162 }
163
164 return $debug_info;
165 }
166
167 /**
168 * Perform the WP site health test for checking, if the mailer setup is complete.
169 *
170 * @since 1.9.0
171 */
172 public function mailer_setup_complete_test() {
173
174 $mailer = Options::init()->get( 'mail', 'mailer' );
175 $mailer_complete = false;
176 $mailer_title = esc_html__( 'None selected', 'wp-mail-smtp' );
177
178 if ( ! empty( $mailer ) ) {
179 $mailer_object = wp_mail_smtp()
180 ->get_providers()
181 ->get_mailer(
182 $mailer,
183 wp_mail_smtp()->get_processor()->get_phpmailer()
184 );
185
186 $mailer_complete = ! empty( $mailer_object ) ? $mailer_object->is_mailer_complete() : false;
187
188 $mailer_title = wp_mail_smtp()->get_providers()->get_options( $mailer )->get_title();
189 }
190
191 // The default mailer should be considered as a non-complete mailer.
192 if ( $mailer === 'mail' ) {
193 $mailer_complete = false;
194 }
195
196 $mailer_text = sprintf(
197 '%s: <strong>%s</strong>',
198 esc_html__( 'Current mailer', 'wp-mail-smtp' ),
199 esc_html( $mailer_title )
200 );
201
202 $result = array(
203 'label' => esc_html__( 'WP Mail SMTP mailer setup is complete', 'wp-mail-smtp' ),
204 'status' => 'good',
205 'badge' => array(
206 'label' => $this->get_label(),
207 'color' => self::BADGE_COLOR,
208 ),
209 'description' => sprintf(
210 '<p>%s</p><p>%s</p>',
211 $mailer_text,
212 esc_html__( 'The WP Mail SMTP plugin mailer setup is complete. You can send a test email, to make sure it\'s working properly.', 'wp-mail-smtp' )
213 ),
214 'actions' => sprintf(
215 '<p><a href="%s">%s</a></p>',
216 esc_url( add_query_arg( 'tab', 'test', wp_mail_smtp()->get_admin()->get_admin_page_url( Area::SLUG . '-tools' ) ) ),
217 esc_html__( 'Test email sending', 'wp-mail-smtp' )
218 ),
219 'test' => 'wp_mail_smtp_mailer_setup_complete',
220 );
221
222 if ( $mailer === 'mail' ) {
223 $mailer_text .= sprintf( /* translators: %s - explanation why default mailer is not a valid mailer option. */
224 '<p>%s</p>',
225 esc_html__( 'You currently have the default mailer selected, which means that you haven’t set up SMTP yet.', 'wp-mail-smtp' )
226 );
227 }
228
229 if ( $mailer_complete === false ) {
230 $result['label'] = esc_html__( 'WP Mail SMTP mailer setup is incomplete', 'wp-mail-smtp' );
231 $result['status'] = 'recommended';
232 $result['badge']['color'] = 'orange';
233 $result['description'] = sprintf(
234 '<p>%s</p><p>%s</p>',
235 $mailer_text,
236 esc_html__( 'The WP Mail SMTP plugin mailer setup is incomplete. Please click on the link below to access plugin settings and configure the mailer.', 'wp-mail-smtp' )
237 );
238 $result['actions'] = sprintf(
239 '<p><a href="%s">%s</a></p>',
240 esc_url( wp_mail_smtp()->get_admin()->get_admin_page_url() ),
241 esc_html__( 'Configure mailer', 'wp-mail-smtp' )
242 );
243 }
244
245 return $result;
246 }
247
248 /**
249 * Perform the test for checking if all custom plugin DB tables exist.
250 *
251 * @since 2.1.2
252 *
253 * @return array
254 */
255 public function db_tables_test() {
256
257 $result = array(
258 'label' => esc_html__( 'WP Mail SMTP DB tables are created', 'wp-mail-smtp' ),
259 'status' => 'good',
260 'badge' => array(
261 'label' => $this->get_label(),
262 'color' => self::BADGE_COLOR,
263 ),
264 'description' => esc_html__( 'WP Mail SMTP is using custom database tables for some of its features. In order to work properly, the custom tables should be created, and it looks like they exist in your database.', 'wp-mail-smtp' ),
265 'actions' => '',
266 'test' => 'wp_mail_smtp_db_tables_exist',
267 );
268
269 $missing_tables = $this->get_db_tables( 'missing' );
270
271 if ( ! empty( $missing_tables ) ) {
272 $missing_tables_create_link = wp_nonce_url(
273 add_query_arg(
274 [
275 'create-missing-db-tables' => 1,
276 ],
277 wp_mail_smtp()->get_admin()->get_admin_page_url( Area::SLUG )
278 ),
279 Area::SLUG . '-create-missing-db-tables'
280 );
281
282 $result['label'] = esc_html__( 'WP Mail SMTP DB tables check has failed', 'wp-mail-smtp' );
283 $result['status'] = 'critical';
284 $result['badge']['color'] = 'red';
285 $result['description'] = sprintf(
286 '<p>%s</p><p>%s</p>',
287 sprintf( /* translators: %s - the list of missing tables separated by comma. */
288 esc_html( _n( 'Missing table: %s', 'Missing tables: %s', count( $missing_tables ), 'wp-mail-smtp' ) ),
289 esc_html( implode( ', ', $missing_tables ) )
290 ),
291 wp_kses(
292 sprintf( /* translators: %1$s - Settings Page URL; %2$s - The aria label; %3$s - The text that will appear on the link. */
293 __( 'WP Mail SMTP is using custom database tables for some of its features. In order to work properly, the custom tables should be created, and it seems they are missing. Please try to <a href="%1$s" target="_self" aria-label="%2$s" rel="noopener noreferrer">%3$s</a>. If this issue persists, please contact our support.', 'wp-mail-smtp' ),
294 esc_url( $missing_tables_create_link ),
295 esc_attr__( 'Go to WP Mail SMTP settings page.', 'wp-mail-smtp' ),
296 esc_attr__( 'create the missing DB tables by clicking on this link', 'wp-mail-smtp' )
297 ),
298 [
299 'a' => [
300 'href' => [],
301 'rel' => [],
302 'target' => [],
303 'aria-label' => [],
304 ],
305 ]
306 )
307 );
308 }
309
310 return $result;
311 }
312
313 /**
314 * Perform the test (async) for checking if email domain configured properly.
315 *
316 * @since 2.8.0
317 */
318 public function email_domain_check_test() {
319
320 check_ajax_referer( 'health-check-site-status' );
321
322 if ( ! current_user_can( 'view_site_health_checks' ) ) {
323 wp_send_json_error();
324 }
325
326 $options = Options::init();
327 $mailer = $options->get( 'mail', 'mailer' );
328 $email = $options->get( 'mail', 'from_email' );
329 $domain = '';
330
331 $email_domain_text = sprintf(
332 '%1$s: <strong>%2$s</strong>',
333 esc_html__( 'Current from email domain', 'wp-mail-smtp' ),
334 esc_html( WP::get_email_domain( $email ) )
335 );
336
337 $result = array(
338 'label' => esc_html__( 'Email domain is configured correctly', 'wp-mail-smtp' ),
339 'status' => 'good',
340 'badge' => array(
341 'label' => $this->get_label(),
342 'color' => self::BADGE_COLOR,
343 ),
344 'description' => sprintf(
345 '<p>%1$s</p><p>%2$s</p>',
346 $email_domain_text,
347 esc_html__( 'All checks for your email domain were successful. It looks like everything is configured correctly.', 'wp-mail-smtp' )
348 ),
349 'actions' => sprintf(
350 '<p><a href="%1$s">%2$s</a></p>',
351 esc_url( add_query_arg( 'tab', 'test', wp_mail_smtp()->get_admin()->get_admin_page_url( Area::SLUG . '-tools' ) ) ),
352 esc_html__( 'Send a Test Email', 'wp-mail-smtp' )
353 ),
354 'test' => 'wp_mail_smtp_email_domain_check',
355 );
356
357 // Add the optional sending domain parameter.
358 if ( in_array( $mailer, [ 'mailgun', 'sendinblue', 'sendgrid' ], true ) ) {
359 $domain = $options->get( $mailer, 'domain' );
360 }
361
362 $domain_checker = new DomainChecker( $mailer, $email, $domain );
363
364 if ( ! $domain_checker->no_issues() ) {
365 $result['label'] = esc_html__( 'Email domain issues detected', 'wp-mail-smtp' );
366 $result['status'] = 'recommended';
367 $result['description'] = sprintf(
368 '<p>%1$s</p> %2$s',
369 $email_domain_text,
370 $domain_checker->get_results_html()
371 );
372 $result['actions'] = sprintf(
373 '<p><a href="%1$s">%2$s</a></p>',
374 esc_url( wp_mail_smtp()->get_admin()->get_admin_page_url() ),
375 esc_html__( 'Configure mailer', 'wp-mail-smtp' )
376 );
377 }
378
379 wp_send_json_success( $result );
380 }
381
382 /**
383 * Get the missing tables from the database.
384 *
385 * @since 3.6.0
386 *
387 * @return array
388 */
389 public function get_missing_db_tables() {
390
391 return $this->get_db_tables( 'missing' );
392 }
393
394 /**
395 * Check DB:
396 * - if any required plugin DB table is missing,
397 * - which of the required plugin DB tables exist.
398 *
399 * @since 2.1.2
400 *
401 * @param string $check Which type of tables to return: 'missing' or 'existing'.
402 *
403 * @return array Missing or existing tables.
404 */
405 private function get_db_tables( $check = 'missing' ) {
406
407 global $wpdb;
408
409 $tables = wp_mail_smtp()->get_custom_db_tables();
410
411 $missing_tables = [];
412 $existing_tables = [];
413
414 foreach ( $tables as $table ) {
415
416 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
417 $db_result = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );
418
419 if ( is_null( $db_result ) || strtolower( $db_result ) !== strtolower( $table ) ) {
420 $missing_tables[] = $table;
421 } else {
422 $existing_tables[] = $table;
423 }
424 }
425
426 return ( $check === 'existing' ) ? $existing_tables : $missing_tables;
427 }
428 }
429