DebugEvents
6 days ago
EmailSendingErrors
6 days ago
Pages
6 days ago
Recommendations
6 days ago
AdminBarMenu.php
6 days ago
Area.php
6 days ago
ConnectionSettings.php
6 days ago
DashboardWidget.php
6 days ago
DomainChecker.php
6 days ago
Education.php
6 days ago
FlyoutMenu.php
6 days ago
Notifications.php
6 days ago
PageAbstract.php
6 days ago
PageInterface.php
6 days ago
ParentPageAbstract.php
6 days ago
PluginsInstallSkin.php
6 days ago
Review.php
6 days ago
SetupWizard.php
6 days ago
WooCommerceActiveLayerEducation.php
6 days ago
DomainChecker.php
204 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP\Admin; |
| 4 | |
| 5 | use WPMailSMTP\Helpers\Helpers; |
| 6 | |
| 7 | /** |
| 8 | * Class for interacting with the Domain Checker API. |
| 9 | * |
| 10 | * @since 2.6.0 |
| 11 | */ |
| 12 | class DomainChecker { |
| 13 | |
| 14 | /** |
| 15 | * The domain checker API endpoint. |
| 16 | * |
| 17 | * @since 2.6.0 |
| 18 | */ |
| 19 | const ENDPOINT = 'https://connect.wpmailsmtp.com/domain-check/'; |
| 20 | |
| 21 | /** |
| 22 | * The API results. |
| 23 | * |
| 24 | * @since 2.6.0 |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | private $results; |
| 29 | |
| 30 | /** |
| 31 | * The plugin mailer slug. |
| 32 | * |
| 33 | * @since 2.7.0 |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $mailer; |
| 38 | |
| 39 | /** |
| 40 | * Verify the domain for the provided mailer and email address and save the API results. |
| 41 | * |
| 42 | * @since 2.6.0 |
| 43 | * |
| 44 | * @param string $mailer The plugin mailer. |
| 45 | * @param string $email The email address from which the domain will be extracted. |
| 46 | * @param string $sending_domain The optional sending domain to check the domain records for. |
| 47 | */ |
| 48 | public function __construct( $mailer, $email, $sending_domain = '' ) { |
| 49 | |
| 50 | $this->mailer = $mailer; |
| 51 | |
| 52 | $params = [ |
| 53 | 'mailer' => $mailer, |
| 54 | 'email' => base64_encode( $email ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 55 | 'domain' => $sending_domain, |
| 56 | ]; |
| 57 | |
| 58 | $response = wp_remote_get( |
| 59 | add_query_arg( $params, self::ENDPOINT ), |
| 60 | [ |
| 61 | 'user-agent' => Helpers::get_default_user_agent(), |
| 62 | ] |
| 63 | ); |
| 64 | |
| 65 | if ( is_wp_error( $response ) ) { |
| 66 | $this->results = [ |
| 67 | 'success' => false, |
| 68 | 'message' => method_exists( $response, 'get_error_message' ) ? |
| 69 | $response->get_error_message() : |
| 70 | esc_html__( 'Something went wrong. Please try again later.', 'wp-mail-smtp' ), |
| 71 | 'checks' => [], |
| 72 | ]; |
| 73 | } else { |
| 74 | $this->results = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Simple getter for the API results. |
| 80 | * |
| 81 | * @since 2.6.0 |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | public function get_results() { |
| 86 | return $this->results; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if the domain checker has found any errors. |
| 91 | * |
| 92 | * @since 2.6.0 |
| 93 | * |
| 94 | * @return bool |
| 95 | */ |
| 96 | public function has_errors() { |
| 97 | |
| 98 | if ( empty( $this->results['success'] ) ) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | if ( empty( $this->results['checks'] ) ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | $has_error = false; |
| 107 | |
| 108 | foreach ( $this->results['checks'] as $check ) { |
| 109 | if ( $check['state'] === 'error' ) { |
| 110 | $has_error = true; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return $has_error; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if the domain checker has not found any errors or warnings. |
| 120 | * |
| 121 | * @since 2.6.0 |
| 122 | * |
| 123 | * @return bool |
| 124 | */ |
| 125 | public function no_issues() { |
| 126 | |
| 127 | if ( empty( $this->results['success'] ) ) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | $no_issues = true; |
| 132 | |
| 133 | foreach ( $this->results['checks'] as $check ) { |
| 134 | if ( in_array( $check['state'], [ 'error', 'warning' ], true ) ) { |
| 135 | $no_issues = false; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return $no_issues; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Check if the domain checker support mailer. |
| 145 | * |
| 146 | * @since 2.7.0 |
| 147 | * |
| 148 | * @return bool |
| 149 | */ |
| 150 | public function is_supported_mailer() { |
| 151 | |
| 152 | return ! in_array( $this->mailer, [ 'mail', 'pepipostapi' ], true ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the domain checker results html. |
| 157 | * |
| 158 | * @since 2.8.0 |
| 159 | * |
| 160 | * @return string |
| 161 | */ |
| 162 | public function get_results_html() { |
| 163 | |
| 164 | $results = $this->get_results(); |
| 165 | $allowed_html = [ |
| 166 | 'b' => [], |
| 167 | 'i' => [], |
| 168 | 'a' => [ |
| 169 | 'href' => [], |
| 170 | 'target' => [], |
| 171 | 'rel' => [], |
| 172 | ], |
| 173 | ]; |
| 174 | |
| 175 | ob_start(); |
| 176 | ?> |
| 177 | <div id="wp-mail-smtp-domain-check-details"> |
| 178 | <h2><?php esc_html_e( 'Domain Check Results', 'wp-mail-smtp' ); ?></h2> |
| 179 | |
| 180 | <?php if ( empty( $results['success'] ) ) : ?> |
| 181 | <div class="notice-inline <?php echo $this->is_supported_mailer() ? 'notice-error' : 'notice-warning'; ?>"> |
| 182 | <p><?php echo wp_kses( $results['message'], $allowed_html ); ?></p> |
| 183 | </div> |
| 184 | <?php endif; ?> |
| 185 | |
| 186 | <?php if ( ! empty( $results['checks'] ) ) : ?> |
| 187 | <div class="wp-mail-smtp-domain-check-details-check-list"> |
| 188 | <?php foreach ( $results['checks'] as $check ) : ?> |
| 189 | <div class="wp-mail-smtp-domain-check-details-check-list-item"> |
| 190 | <img src="<?php echo esc_url( wp_mail_smtp()->assets_url . '/images/icons/' . esc_attr( $check['state'] ) . '.svg' ); ?>" class="wp-mail-smtp-domain-check-details-check-list-item-icon" alt="<?php printf( /* translators: %s - item state name. */ esc_attr__( '%s icon', 'wp-mail-smtp' ), esc_attr( $check['state'] ) ); ?>"> |
| 191 | <div class="wp-mail-smtp-domain-check-details-check-list-item-content"> |
| 192 | <h3><?php echo esc_html( $check['type'] ); ?></h3> |
| 193 | <p><?php echo wp_kses( $check['message'], $allowed_html ); ?></p> |
| 194 | </div> |
| 195 | </div> |
| 196 | <?php endforeach; ?> |
| 197 | </div> |
| 198 | <?php endif; ?> |
| 199 | </div> |
| 200 | <?php |
| 201 | return ob_get_clean(); |
| 202 | } |
| 203 | } |
| 204 |