This commit is contained in:
Javier Casares 2026-06-09 12:50:30 +00:00
commit 48cef88d49

View file

@ -301,6 +301,9 @@ class Settings_Page {
add_action( 'admin_post_robotstxt_smtp_save_network_settings', array( $this, 'handle_save_network_settings' ) );
add_action( 'admin_post_robotstxt_smtp_reset_data', array( $this, 'handle_reset_data' ) );
add_action( 'admin_post_robotstxt_smtp_reset_data_network', array( $this, 'handle_reset_data_network' ) );
add_action( 'admin_post_robotstxt_smtp_clear_password', array( $this, 'handle_clear_password' ) );
add_action( 'admin_notices', array( $this, 'display_clear_password_notice' ) );
add_action( 'network_admin_notices', array( $this, 'display_clear_password_notice' ) );
add_action( 'admin_notices', array( $this, 'display_test_email_notice' ) );
add_action( 'network_admin_notices', array( $this, 'display_test_email_notice' ) );
add_action( 'admin_notices', array( $this, 'display_reset_data_notice' ) );
@ -1388,6 +1391,11 @@ class Settings_Page {
}
}
$dkim_selector_raw = filter_input( INPUT_GET, 'dkim_selector', FILTER_UNSAFE_RAW );
$dkim_selector = ( is_string( $dkim_selector_raw ) && preg_match( '/^[a-zA-Z0-9_\-]{1,63}$/', $dkim_selector_raw ) )
? sanitize_key( $dkim_selector_raw )
: '';
$tools = array(
'mx' => array(
'title' => esc_html__( 'Automatic MX Lookup', 'robotstxt-smtp' ),
@ -1430,16 +1438,17 @@ class Settings_Page {
$result = $this->get_tool_result_for_display( $scope, $tool_key, $tool_definition['callback'], $run_now );
$has_result = null !== $result;
$refresh_url = add_query_arg(
array(
'page' => self::TOOLS_PAGE_SLUG,
'robotstxt_smtp_tool' => $tool_key,
'robotstxt_smtp_refresh' => '1',
),
$base_url
$refresh_args = array(
'page' => self::TOOLS_PAGE_SLUG,
'robotstxt_smtp_tool' => $tool_key,
'robotstxt_smtp_refresh' => '1',
);
if ( 'authentication' === $tool_key && '' !== $dkim_selector ) {
$refresh_args['dkim_selector'] = $dkim_selector;
}
$refresh_url = add_query_arg( $refresh_args, $base_url );
$refresh_url = wp_nonce_url( $refresh_url, 'robotstxt_smtp_refresh_tool_' . $tool_key );
?>
?>
<div class="card robotstxt-smtp-tool-card" data-tool="<?php echo esc_attr( $tool_key ); ?>">
<h2><?php echo esc_html( $tool_definition['title'] ); ?></h2>
<p><?php echo esc_html( $tool_definition['description'] ); ?></p>
@ -1469,6 +1478,36 @@ class Settings_Page {
<div class="robotstxt-smtp-tool-result">
<?php echo isset( $result['html'] ) && is_string( $result['html'] ) ? wp_kses_post( $result['html'] ) : ''; ?>
</div>
<?php if ( 'authentication' === $tool_key ) : ?>
<?php
// Render DKIM selector form here — outside wp_kses_post() which strips form elements.
$dkim_form_url = $this->get_admin_url_for_scope( $scope, 'admin.php' );
?>
<form method="get" action="<?php echo esc_url( $dkim_form_url ); ?>" style="margin-top: 12px; padding-top: 12px; border-top: 1px solid #dcdcde;">
<input type="hidden" name="page" value="<?php echo esc_attr( self::SCOPE_NETWORK === $scope ? self::NETWORK_MENU_SLUG . '-tools' : self::TOOLS_PAGE_SLUG ); ?>" />
<input type="hidden" name="robotstxt_smtp_tool" value="authentication" />
<input type="hidden" name="robotstxt_smtp_refresh" value="1" />
<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'robotstxt_smtp_refresh_tool_authentication' ) ); ?>" />
<label for="robotstxt_smtp_dkim_selector_input" style="display:inline-block; margin-right:6px;">
<?php esc_html_e( 'Try a specific selector:', 'robotstxt-smtp' ); ?>
</label>
<input
type="text"
name="dkim_selector"
id="robotstxt_smtp_dkim_selector_input"
value="<?php echo esc_attr( $dkim_selector ); ?>"
placeholder="<?php esc_attr_e( 'e.g. google, selector1, mail', 'robotstxt-smtp' ); ?>"
maxlength="63"
pattern="[a-zA-Z0-9_\-]+"
style="width: 200px;"
/>
<button type="submit" class="button button-secondary" style="margin-left: 4px;">
<?php esc_html_e( 'Try selector', 'robotstxt-smtp' ); ?>
</button>
</form>
<?php endif; ?>
<?php else : ?>
<p class="robotstxt-smtp-tool-empty">
<?php esc_html_e( 'There are no results yet. Click the button below to run this check.', 'robotstxt-smtp' ); ?>
@ -2429,6 +2468,28 @@ class Settings_Page {
$mx_analysis = $this->analyze_mx_records( $sender_domain, $normalized_smtp_host );
}
// Read and validate a custom DKIM selector from the request.
$custom_selector_raw = filter_input( INPUT_GET, 'dkim_selector', FILTER_UNSAFE_RAW );
$custom_selector = ( is_string( $custom_selector_raw ) && preg_match( '/^[a-zA-Z0-9_\-]{1,63}$/', $custom_selector_raw ) )
? sanitize_key( $custom_selector_raw )
: '';
// Build the list of MX hostnames for provider detection.
$mx_hosts = array();
foreach ( $mx_analysis['records'] as $mx_record ) {
$normalized = $this->normalize_hostname( $mx_record['display_target'] );
if ( '' !== $normalized ) {
$mx_hosts[] = $normalized;
}
}
// Determine which selectors to try.
if ( '' !== $custom_selector ) {
$dkim_selectors = array( $custom_selector );
} else {
$dkim_selectors = $this->get_known_dkim_selectors_for_mx( $mx_hosts );
}
ob_start();
if ( '' === $sender_domain ) :
@ -2439,6 +2500,7 @@ class Settings_Page {
<?php
else :
$auth_analysis = $this->analyze_domain_authentication( $sender_domain, $normalized_smtp_host, $mx_analysis, $host_ips );
$dkim_result = $this->try_dkim_selectors( $sender_domain, $dkim_selectors );
if ( ! empty( $auth_analysis['errors'] ) ) :
?>
@ -2490,24 +2552,44 @@ class Settings_Page {
<h3><?php esc_html_e( 'DKIM', 'robotstxt-smtp' ); ?></h3>
<?php if ( ! empty( $auth_analysis['dkim']['errors'] ) ) : ?>
<?php if ( ! empty( $dkim_result['errors'] ) ) : ?>
<div class="notice notice-error inline">
<ul>
<?php foreach ( $auth_analysis['dkim']['errors'] as $error ) : ?>
<?php foreach ( $dkim_result['errors'] as $error ) : ?>
<li><?php echo esc_html( $error ); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php elseif ( true === $auth_analysis['dkim']['found'] ) : ?>
<?php elseif ( true === $dkim_result['found'] ) : ?>
<div class="notice notice-success inline">
<p><?php esc_html_e( 'A DKIM record was found for the default selector.', 'robotstxt-smtp' ); ?></p>
<?php if ( '' !== $auth_analysis['dkim']['record'] ) : ?>
<p><code><?php echo esc_html( $auth_analysis['dkim']['record'] ); ?></code></p>
<p>
<?php
printf(
/* translators: %s: DKIM selector name that was matched. */
esc_html__( 'A DKIM record was found for the selector "%s".', 'robotstxt-smtp' ),
esc_html( $dkim_result['selector'] )
);
?>
</p>
<?php if ( '' !== $dkim_result['record'] ) : ?>
<p><code><?php echo esc_html( $dkim_result['record'] ); ?></code></p>
<?php endif; ?>
</div>
<?php else : ?>
<div class="notice notice-warning inline">
<p><?php esc_html_e( 'No DKIM record was found for the default selector. Confirm the selector provided by your email service and publish the corresponding TXT record.', 'robotstxt-smtp' ); ?></p>
<?php if ( '' === $custom_selector && count( $dkim_result['tried'] ) > 0 ) : ?>
<p>
<?php
printf(
/* translators: %s: Comma-separated list of DKIM selectors that were tried. */
esc_html__( 'No DKIM record was found after trying the following selectors: %s. Enter the selector provided by your email service below and try again.', 'robotstxt-smtp' ),
esc_html( implode( ', ', $dkim_result['tried'] ) )
);
?>
</p>
<?php else : ?>
<p><?php esc_html_e( 'No DKIM record was found for the specified selector. Confirm the selector provided by your email service and publish the corresponding TXT record.', 'robotstxt-smtp' ); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
@ -3490,6 +3572,89 @@ class Settings_Page {
);
}
/**
* Handles the request to clear the stored SMTP password.
*
* Triggered by the "Clear password" form on the settings page.
* Works in both site and network scope.
*
* @since 2.2.1
*
* @return void
*/
public function handle_clear_password(): void {
// Link uses GET (wp_nonce_url), so nonce arrives in INPUT_GET.
$nonce_raw = filter_input( INPUT_GET, '_wpnonce', FILTER_UNSAFE_RAW );
if ( ! is_string( $nonce_raw ) || ! wp_verify_nonce( wp_unslash( $nonce_raw ), 'robotstxt_smtp_clear_password' ) ) {
wp_die( esc_html__( 'The link you followed has expired.', 'robotstxt-smtp' ) );
}
$scope_raw = filter_input( INPUT_GET, 'robotstxt_smtp_scope', FILTER_UNSAFE_RAW );
$scope = ( is_string( $scope_raw ) && self::SCOPE_NETWORK === sanitize_key( wp_unslash( $scope_raw ) ) )
? self::SCOPE_NETWORK
: self::SCOPE_SITE;
$this->ensure_capability( $scope );
if ( self::SCOPE_NETWORK === $scope ) {
$settings = get_site_option( self::NETWORK_OPTION_NAME, array() );
$settings = is_array( $settings ) ? $settings : array();
$settings['password'] = '';
// site options have no registered sanitize_callback, safe to update directly.
update_site_option( self::NETWORK_OPTION_NAME, $settings );
$page_url = network_admin_url( 'admin.php' );
$page_slug = self::NETWORK_PAGE_SLUG;
} else {
$settings = get_option( self::OPTION_NAME, array() );
$settings = is_array( $settings ) ? $settings : array();
$settings['password'] = '';
// Temporarily remove the registered sanitize_callback so that password=''
// is stored as-is. Without this, sanitize_options() treats an empty
// password as "keep the existing value" and the clear has no effect.
remove_filter( 'sanitize_option_' . self::OPTION_NAME, array( $this, 'sanitize_options' ) );
update_option( self::OPTION_NAME, $settings );
add_filter( 'sanitize_option_' . self::OPTION_NAME, array( $this, 'sanitize_options' ) );
$page_url = admin_url( 'admin.php' );
$page_slug = self::PAGE_SLUG;
}
wp_safe_redirect(
add_query_arg(
array(
'page' => $page_slug,
'robotstxt_smtp_pw_cleared' => '1',
),
$page_url
)
);
exit;
}
/**
* Displays a success notice after the password has been cleared.
*
* @since 2.2.1
*
* @return void
*/
public function display_clear_password_notice(): void {
$pw_cleared_raw = filter_input( INPUT_GET, 'robotstxt_smtp_pw_cleared', FILTER_UNSAFE_RAW );
if ( '1' !== $pw_cleared_raw ) {
return;
}
$screen = get_current_screen();
if ( null === $screen || false === strpos( $screen->id, 'robotstxt-smtp' ) ) {
return;
}
?>
<div class="notice notice-success is-dismissible">
<p><?php esc_html_e( 'The SMTP password has been cleared.', 'robotstxt-smtp' ); ?></p>
</div>
<?php
}
/**
* Handles the request to clear all stored logs.
*
@ -3752,21 +3917,43 @@ class Settings_Page {
* @return void
*/
public function render_password_field(): void {
$settings = $this->get_settings();
$has_password = '' !== $settings['password'];
$placeholder_msg = $has_password ? __( 'Leave empty to keep the current password.', 'robotstxt-smtp' ) : '';
$settings = $this->get_settings();
$has_password = '' !== $settings['password'];
$placeholder = $has_password
? __( 'Leave empty to keep the current password.', 'robotstxt-smtp' )
: __( 'No password set.', 'robotstxt-smtp' );
$scope = $this->get_current_admin_scope();
?>
<input
name="<?php echo esc_attr( $this->get_settings_field_name( 'password' ) ); ?>"
type="password"
id="robotstxt_smtp_password"
class="regular-text"
value=""
autocomplete="new-password"
placeholder="<?php echo esc_attr( $placeholder_msg ); ?>"
/>
<p class="description"><?php esc_html_e( 'Enter the password or app-specific password for the SMTP user. Leave empty to retain the existing value.', 'robotstxt-smtp' ); ?></p>
<?php
<input
name="<?php echo esc_attr( $this->get_settings_field_name( 'password' ) ); ?>"
type="password"
id="robotstxt_smtp_password"
class="regular-text"
value=""
autocomplete="new-password"
placeholder="<?php echo esc_attr( $placeholder ); ?>"
/>
<?php if ( $has_password ) : ?>
<?php
$clear_url = wp_nonce_url(
add_query_arg(
array(
'action' => 'robotstxt_smtp_clear_password',
'robotstxt_smtp_scope' => $scope,
),
admin_url( 'admin-post.php' )
),
'robotstxt_smtp_clear_password'
);
?>
<a href="<?php echo esc_url( $clear_url ); ?>" class="button button-secondary" style="margin-left: 6px;">
<?php esc_html_e( 'Clear password', 'robotstxt-smtp' ); ?>
</a>
<?php endif; ?>
<p class="description">
<?php esc_html_e( 'Enter the password or app-specific password for the SMTP user. Leave empty to retain the existing value.', 'robotstxt-smtp' ); ?>
</p>
<?php
}
/**
@ -4122,6 +4309,8 @@ class Settings_Page {
'invalid_from_email',
__( 'The "From Email" address is not valid. Please provide a valid email address.', 'robotstxt-smtp' )
);
} else {
$clean['from_email'] = '';
}
}
@ -4139,6 +4328,8 @@ class Settings_Page {
'invalid_reply_to_email',
__( 'The "Reply-To Email" address is not valid. Please provide a valid email address.', 'robotstxt-smtp' )
);
} else {
$clean['reply_to_email'] = '';
}
}
@ -4826,6 +5017,29 @@ class Settings_Page {
return $result;
}
// Check for DNSBL infrastructure error codes returned in the 127.255.0.0/16 range.
// These are not real blacklist hits — they are error responses from the DNSBL provider:
// 127.255.255.254 — query was made via a public/open DNS resolver (e.g. Cloudflare, Google).
// Spamhaus and others block public-resolver queries since 2022.
// 127.255.255.255 — excessive querying from this resolver.
// Treat both as non-listed with an informational error message.
foreach ( $a_records as $record ) {
$rec_ip = isset( $record['ip'] ) && is_string( $record['ip'] ) ? $record['ip'] : '';
if ( '127.255.255.254' === $rec_ip ) {
$removal_url = $provider['removal_url'] ?? '';
$result['error'] = sprintf(
/* translators: %s: URL to the provider's web lookup tool. */
__( 'This DNSBL no longer accepts queries from public DNS resolvers. Use the provider\'s web tool to check manually: %s', 'robotstxt-smtp' ),
$removal_url
);
return $result;
}
if ( '127.255.255.255' === $rec_ip ) {
$result['error'] = __( 'The DNSBL rate limit was exceeded from the server\'s DNS resolver. Try again later or use the provider\'s web lookup tool.', 'robotstxt-smtp' );
return $result;
}
}
$result['listed'] = true;
foreach ( $a_records as $record ) {
@ -5283,13 +5497,16 @@ class Settings_Page {
}
/**
* Looks up a DKIM record using the default selector.
* Looks up a DKIM record for the specified selector.
*
* @param string $domain Domain to inspect.
* @since 2.2.1
*
* @param string $domain Domain to inspect.
* @param string $selector DKIM selector to query. Defaults to 'default'.
*
* @return array{found: bool, errors: list<string>, record: string}
*/
private function analyze_dkim_record( string $domain ): array {
private function analyze_dkim_record( string $domain, string $selector = 'default' ): array {
$result = array(
'found' => false,
'errors' => array(),
@ -5302,7 +5519,7 @@ class Settings_Page {
return $result;
}
$selector_domain = 'default._domainkey.' . $domain;
$selector_domain = $selector . '._domainkey.' . $domain;
$records = $this->safe_dns_lookup( $selector_domain, DNS_TXT, 2 );
if ( false === $records ) {
@ -5329,6 +5546,83 @@ class Settings_Page {
return $result;
}
/**
* Returns an ordered list of DKIM selectors to try based on MX hostname patterns.
*
* @since 2.2.1
*
* @param array<int, string> $mx_hosts Lowercase MX hostnames.
* @return array<int, string> Selectors to try in order, always ending with 'default'.
*/
private function get_known_dkim_selectors_for_mx( array $mx_hosts ): array {
// Each entry: [regex, selectors]. First pattern match wins.
$patterns = array(
array( '/\.google\.com$/i', array( 'google', 'google2' ) ),
array( '/\.outlook\.com$/i', array( 'selector1', 'selector2' ) ),
array( '/\.mail\.protection\.outlook\.com$/i', array( 'selector1', 'selector2' ) ),
array( '/\.zoho\.com$/i', array( 'zoho', 'zmail' ) ),
array( '/\.protonmail\.ch$/i', array( 'protonmail', 'protonmail2', 'protonmail3' ) ),
array( '/\.icloud\.com$/i', array( 'sig1' ) ),
array( '/\.aruba\.it$/i', array( 'a1' ) ),
array( '/\.mcsv\.net$/i', array( 'k1', 'k2', 'k3' ) ),
);
foreach ( $mx_hosts as $host ) {
foreach ( $patterns as $entry ) {
if ( preg_match( $entry[0], $host ) ) {
$selectors = $entry[1];
$selectors[] = 'default';
return $selectors;
}
}
}
return array( 'default' );
}
/**
* Tries DKIM selectors in order and returns the first found record.
*
* @since 2.2.1
*
* @param string $domain Domain to check.
* @param array<int, string> $selectors Selectors to try in order.
* @return array{found: bool, selector: string, record: string, errors: list<string>, tried: list<string>}
*/
private function try_dkim_selectors( string $domain, array $selectors ): array {
$result = array(
'found' => false,
'selector' => '',
'record' => '',
'errors' => array(),
'tried' => array(),
);
if ( ! function_exists( 'dns_get_record' ) ) {
$result['errors'][] = __( 'DNS lookups are not available because the DNS functions are disabled on this server.', 'robotstxt-smtp' );
return $result;
}
foreach ( $selectors as $selector ) {
$result['tried'][] = $selector;
$lookup = $this->analyze_dkim_record( $domain, $selector );
if ( ! empty( $lookup['errors'] ) ) {
$result['errors'] = $lookup['errors'];
return $result;
}
if ( true === $lookup['found'] ) {
$result['found'] = true;
$result['selector'] = $selector;
$result['record'] = $lookup['record'];
return $result;
}
}
return $result;
}
/**
* Looks up the DMARC policy for the sender domain.
*