array( 'email' => $old_email, 'name' => $old_name, 'enabled' => true, ), 'authentication' => array( 'email' => '', 'name' => '', 'enabled' => false, ), 'comments' => array( 'email' => '', 'name' => '', 'enabled' => false, ), 'users' => array( 'email' => '', 'name' => '', 'enabled' => false, ), 'system' => array( 'email' => '', 'name' => '', 'enabled' => false, ), ); // Add WooCommerce context only if WooCommerce is active. if ( class_exists( 'WooCommerce' ) ) { $contexts['woocommerce'] = array( 'email' => '', 'name' => '', 'enabled' => false, ); } // Save new structure. update_option( 'wp_mail_replyto_contexts', $contexts ); // Keep old options for rollback safety (will be removed on uninstall). } // Mark migration as complete. update_option( 'wp_mail_replyto_migration_v200', true ); } /** * Detects email context based on WordPress backtrace. * * Analyzes the call stack to determine what type of email is being sent. * Uses DEBUG_BACKTRACE_IGNORE_ARGS with a 20-frame limit for production safety. * The detected value is cached in a static variable; pass $reset = true to clear * the cache between emails (called automatically by wp_mail_replyto_reset_context). * * @since 1.3.0 * * @param bool $reset If true, clears the static cache and returns without detecting. * @return string Context: 'authentication', 'comments', 'users', 'system', 'woocommerce', or 'default'. */ function wp_mail_replyto_detect_context( $reset = false ) { static $context = null; if ( $reset ) { $context = null; return ''; } // Use cached value if available (for performance within the same email). if ( null !== $context ) { return $context; } // Get backtrace with minimal overhead. // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Not debug code: debug_backtrace() is essential for production context detection. It analyzes the call stack to identify which WordPress function is sending the email (e.g., retrieve_password, wp_notify_postauthor). This is the core functionality of the plugin, not debugging. The function is optimized with DEBUG_BACKTRACE_IGNORE_ARGS and limited to 20 frames for performance. $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 20 ); $detected = 'default'; foreach ( $backtrace as $trace ) { if ( empty( $trace['function'] ) ) { continue; } $function = $trace['function']; $class = isset( $trace['class'] ) ? $trace['class'] : ''; // Authentication context. if ( in_array( $function, array( 'retrieve_password', 'reset_password', 'wp_password_change_notification', ), true ) ) { $detected = 'authentication'; break; } // Comments context. if ( in_array( $function, array( 'wp_notify_postauthor', 'wp_notify_moderator', 'wp_new_comment_notify_postauthor', 'wp_new_comment_notify_moderator', ), true ) ) { $detected = 'comments'; break; } // Users context. if ( in_array( $function, array( 'wp_new_user_notification', 'wp_send_new_user_notifications', 'register_new_user', ), true ) ) { $detected = 'users'; break; } // System context. if ( 'WP_Automatic_Updater' === $class || 'WP_Recovery_Mode' === $class || in_array( $function, array( 'wp_maybe_auto_update', 'send_core_update_notification_email', ), true ) ) { $detected = 'system'; break; } // WooCommerce context. if ( ! empty( $class ) && false !== strpos( $class, 'WC_Email' ) ) { $detected = 'woocommerce'; break; } } /** * Filters the detected email context. * * Allows overriding the context before the Reply-To address is resolved. * Return a custom context key to apply a different Reply-To configuration, * or 'default' to use the fallback. * * @since 2.0.0 * * @param string $detected Detected context: 'authentication', 'comments', 'users', * 'system', 'woocommerce', or 'default'. */ $context = (string) apply_filters( 'wp_mail_replyto_context', $detected ); return $context; } /** * Resets the context cache after email is sent. * * Ensures each email detection starts fresh when multiple emails are sent * within the same PHP process (e.g., WP-Cron, REST batch). * * @since 1.3.0 * * @param array{to: string|string[], subject: string, message: string, headers: string|string[], attachments: string[]} $args Email arguments. * @return array{to: string|string[], subject: string, message: string, headers: string|string[], attachments: string[]} Unmodified email arguments. */ function wp_mail_replyto_reset_context( $args ) { wp_mail_replyto_detect_context( true ); return $args; } add_filter( 'wp_mail', 'wp_mail_replyto_reset_context', 999 ); /** * Modifies the "Reply-To" header in emails sent using wp_mail(). * * Detects email context and applies appropriate Reply-To based on configuration. * Falls back to default context if specific context is not configured. * Removes any existing Reply-To header before injecting the configured value. * * @since 1.3.0 Updated to support context-based Reply-To. * * @param array{to: string|string[], subject: string, message: string, headers: string|string[], attachments: string[]} $args The email arguments passed to wp_mail(). * @return array{to: string|string[], subject: string, message: string, headers: string|string[], attachments: string[]} Modified email arguments with adjusted "Reply-To" header. */ function wp_mail_replyto( $args ) { // Detect email context. $detected_context = wp_mail_replyto_detect_context(); // Get all contexts configuration. $contexts_raw = get_option( 'wp_mail_replyto_contexts', array() ); $contexts = is_array( $contexts_raw ) ? $contexts_raw : array(); // Normalize the detected context entry. $detected_ctx = isset( $contexts[ $detected_context ] ) && is_array( $contexts[ $detected_context ] ) ? $contexts[ $detected_context ] : array(); // Normalize the default context entry. $default_ctx = isset( $contexts['default'] ) && is_array( $contexts['default'] ) ? $contexts['default'] : array(); // Fallback chain: specific context -> default -> legacy. $reply_to_email = ''; $reply_to_name = ''; $detected_email = isset( $detected_ctx['email'] ) && is_string( $detected_ctx['email'] ) ? $detected_ctx['email'] : ''; $detected_enabled = ! empty( $detected_ctx['enabled'] ); if ( $detected_enabled && ! empty( $detected_email ) ) { // Specific context is configured and enabled. $reply_to_email = $detected_email; $reply_to_name = isset( $detected_ctx['name'] ) && is_string( $detected_ctx['name'] ) ? $detected_ctx['name'] : ''; } else { $default_email = isset( $default_ctx['email'] ) && is_string( $default_ctx['email'] ) ? $default_ctx['email'] : ''; if ( ! empty( $default_email ) ) { // Fall back to default context. $reply_to_email = $default_email; $reply_to_name = isset( $default_ctx['name'] ) && is_string( $default_ctx['name'] ) ? $default_ctx['name'] : ''; } else { // Final fallback: legacy single option (for migration period). $legacy_email = get_option( 'wp_mail_replyto_email', '' ); $legacy_name = get_option( 'wp_mail_replyto_name', '' ); $reply_to_email = is_string( $legacy_email ) ? $legacy_email : ''; $reply_to_name = is_string( $legacy_name ) ? $legacy_name : ''; } } // Explicit header injection prevention - defense in depth. if ( ! empty( $reply_to_email ) ) { $reply_to_email = str_replace( array( "\r", "\n", '%0a', '%0d', "\0" ), '', $reply_to_email ); } if ( ! empty( $reply_to_name ) ) { $reply_to_name = str_replace( array( "\r", "\n", '%0a', '%0d', "\0" ), '', $reply_to_name ); } // Construct the new "Reply-To" header if a valid email address is set. $new_reply_to = ''; if ( ! empty( $reply_to_email ) && is_email( $reply_to_email ) ) { $safe_email = sanitize_email( $reply_to_email ); if ( ! empty( $reply_to_name ) ) { $display_name = sanitize_text_field( $reply_to_name ); // Strip any surrounding double-quotes the user may have typed manually. $display_name = trim( $display_name, '"' ); // RFC 5322 specials that require a quoted-string: ( ) < > [ ] : ; @ backslash comma period and double-quote. if ( preg_match( '/[()<>\[\]:;@\\\\,."\\r\\n]/', $display_name ) ) { $safe_name = str_replace( array( '\\', '"' ), array( '\\\\', '\\"' ), $display_name ); $new_reply_to = 'Reply-To: "' . $safe_name . '" <' . $safe_email . '>'; } else { // Simple phrase: no quoting needed. $new_reply_to = 'Reply-To: ' . $display_name . ' <' . $safe_email . '>'; } } else { // Email only. $new_reply_to = 'Reply-To: <' . $safe_email . '>'; } } /** * Filters the Reply-To header string before injection. * * Return an empty string to suppress Reply-To injection for this email. * * @since 2.0.0 * * @param string $new_reply_to The constructed Reply-To header (e.g. 'Reply-To: '). * @param array{to: string|string[], subject: string, message: string, headers: string|string[], attachments: string[]} $args The wp_mail arguments. */ $new_reply_to = (string) apply_filters( 'wp_mail_replyto_header', $new_reply_to, $args ); // Check if email headers exist. if ( ! empty( $args['headers'] ) ) { // Normalize headers into an array if they are not already. if ( ! is_array( $args['headers'] ) ) { $args['headers'] = array_filter( explode( "\n", str_replace( array( "\r\n", "\r" ), "\n", $args['headers'] ) ) ); } if ( ! empty( $new_reply_to ) ) { // Remove any existing Reply-To header unconditionally (RFC 5322 allows only one). $args['headers'] = array_values( array_filter( $args['headers'], function ( $header ) { return stripos( $header, 'reply-to:' ) !== 0; } ) ); $args['headers'][] = $new_reply_to; } } elseif ( ! empty( $new_reply_to ) ) { // If no headers exist at all, create a new headers array with the "Reply-To". $args['headers'] = array( $new_reply_to ); } // Return the modified email arguments. return $args; } add_filter( 'wp_mail', 'wp_mail_replyto' ); /** * Registers a settings page for the plugin in the WordPress admin menu. * * Adds a new page under the "Settings" menu where administrators can configure * the "Reply-To" email address used in outgoing emails. * * @since 1.3.0 * * @return void */ function wp_mail_replyto_add_settings_page() { add_options_page( esc_html__( 'WP Mail Reply-To Settings', 'replyto' ), esc_html__( 'Reply-To', 'replyto' ), 'manage_options', 'replyto', 'wp_mail_replyto_render_settings_page' ); } add_action( 'admin_menu', 'wp_mail_replyto_add_settings_page' ); /** * Validates email address with strict RFC 5322 format checking. * * Performs additional validation beyond WordPress's is_email() function * to ensure strict compliance with email standards. * * @since 1.1.0 * * @param string $email The email address to validate. * @return bool True if email is valid, false otherwise. */ function wp_mail_replyto_validate_email_strict( $email ) { // Basic WordPress email validation. if ( ! is_email( $email ) ) { return false; } // Ensure no angle brackets in the email address itself. if ( false !== strpos( $email, '<' ) || false !== strpos( $email, '>' ) ) { return false; } // Additional validation for special characters that could cause issues. if ( preg_match( '/[\r\n\0]/', $email ) ) { return false; } return true; } /** * Registers the plugin settings, section, and field in the WordPress Settings API. * * This function defines the context-based configuration structure. * * @since 1.3.0 Updated to support multiple contexts. * * @return void */ function wp_mail_replyto_register_settings() { // Register the contexts setting. register_setting( 'wp_mail_replyto_settings_group', 'wp_mail_replyto_contexts', array( 'type' => 'array', 'sanitize_callback' => 'wp_mail_replyto_sanitize_contexts', 'default' => array(), ) ); // The legacy v1.0.x options (wp_mail_replyto_email, wp_mail_replyto_name) // are intentionally NOT registered here: they are not part of the settings // form, and options.php calls update_option() with null for every registered // option missing from the POST body. That wiped the legacy values on every // save and triggered a sanitize_email(null) deprecation on PHP 8.1+. The // options are read directly during migration, which needs no registration. // Uninstall behaviour opt-in: data is preserved unless the user checks this. register_setting( 'wp_mail_replyto_settings_group', 'wp_mail_replyto_delete_on_uninstall', array( 'type' => 'boolean', 'sanitize_callback' => 'wp_mail_replyto_sanitize_delete_on_uninstall', 'default' => false, ) ); } add_action( 'admin_init', 'wp_mail_replyto_register_settings' ); /** * Sanitizes the uninstall data-removal opt-in. * * Accepts the hidden/checkbox form values ('0'/'1') and booleans from * programmatic update_option() calls. Anything else is treated as false. * * @since 2.1.0 * * @param mixed $value Raw input value. * @return bool True when data removal was explicitly requested. */ function wp_mail_replyto_sanitize_delete_on_uninstall( $value ) { return ( '1' === $value || true === $value ); } /** * Sanitizes and validates the contexts configuration. * * Validates all context email addresses and names, performs DNS validation, * provides appropriate error messages, and logs changes. * * @since 1.3.0 * * @param array $input The input contexts array. * @return array The sanitized contexts array. */ function wp_mail_replyto_sanitize_contexts( $input ) { $sanitized = array(); // Previously stored configuration, used to preserve contexts whose tab // was not rendered (WooCommerce while inactive) and for change logging. $old_contexts_raw = get_option( 'wp_mail_replyto_contexts', array() ); $old_contexts = is_array( $old_contexts_raw ) ? $old_contexts_raw : array(); // Define available contexts. WooCommerce is always part of the sanitize // set so its stored values survive saves made while WooCommerce is // inactive; the tab itself is only rendered when WooCommerce is active. $available_contexts = array( 'default' => __( 'Default', 'replyto' ), 'authentication' => __( 'Authentication', 'replyto' ), 'comments' => __( 'Comments', 'replyto' ), 'users' => __( 'Users', 'replyto' ), 'system' => __( 'System', 'replyto' ), 'woocommerce' => __( 'WooCommerce', 'replyto' ), ); foreach ( $available_contexts as $context_key => $context_label ) { // Context absent from the input means its tab was not rendered: // preserve the stored entry (normalized) instead of wiping it. if ( ! isset( $input[ $context_key ] ) ) { $stored = isset( $old_contexts[ $context_key ] ) && is_array( $old_contexts[ $context_key ] ) ? $old_contexts[ $context_key ] : array(); $sanitized[ $context_key ] = array( 'email' => isset( $stored['email'] ) && is_string( $stored['email'] ) ? $stored['email'] : '', 'name' => isset( $stored['name'] ) && is_string( $stored['name'] ) ? $stored['name'] : '', 'enabled' => ! empty( $stored['enabled'] ), ); continue; } // Get input for this context. $ctx_input = is_array( $input[ $context_key ] ) ? $input[ $context_key ] : array(); $email = isset( $ctx_input['email'] ) && is_string( $ctx_input['email'] ) ? sanitize_email( $ctx_input['email'] ) : ''; $name = isset( $ctx_input['name'] ) && is_string( $ctx_input['name'] ) ? sanitize_text_field( $ctx_input['name'] ) : ''; $enabled_raw = isset( $ctx_input['enabled'] ) ? $ctx_input['enabled'] : ''; $enabled = '1' === $enabled_raw || true === $enabled_raw; // Header injection prevention. if ( ! empty( $email ) ) { $email = str_replace( array( "\r", "\n", '%0a', '%0d', "\0" ), '', $email ); } if ( ! empty( $name ) ) { $name = str_replace( array( "\r", "\n", '%0a', '%0d', "\0" ), '', $name ); } // Validate email if provided. if ( ! empty( $email ) && ! wp_mail_replyto_validate_email_strict( $email ) ) { add_settings_error( 'wp_mail_replyto_messages', 'wp_mail_replyto_invalid_email_' . $context_key, /* translators: %s: context name */ sprintf( esc_html__( 'Invalid email address for %s context.', 'replyto' ), esc_html( $context_label ) ), 'error' ); $email = ''; } // Check domain existence (optional validation with warning only). if ( ! empty( $email ) && function_exists( 'checkdnsrr' ) ) { $domain_str = strrchr( $email, '@' ); if ( false !== $domain_str ) { $domain = substr( $domain_str, 1 ); if ( ! empty( $domain ) && ! checkdnsrr( $domain, 'MX' ) && ! checkdnsrr( $domain, 'A' ) ) { add_settings_error( 'wp_mail_replyto_messages', 'wp_mail_replyto_domain_warning_' . $context_key, /* translators: %s: context name */ sprintf( esc_html__( 'Warning: The email domain for %s context does not appear to have valid DNS records.', 'replyto' ), esc_html( $context_label ) ), 'warning' ); } } } // Limit name length (multibyte-aware). if ( function_exists( 'mb_strlen' ) ) { if ( mb_strlen( $name, 'UTF-8' ) > 255 ) { $name = mb_substr( $name, 0, 255, 'UTF-8' ); add_settings_error( 'wp_mail_replyto_messages', 'wp_mail_replyto_name_long_' . $context_key, /* translators: %s: context name */ sprintf( esc_html__( 'Name for %s context was truncated to 255 characters.', 'replyto' ), esc_html( $context_label ) ), 'warning' ); } } elseif ( strlen( $name ) > 255 ) { $name = substr( $name, 0, 255 ); add_settings_error( 'wp_mail_replyto_messages', 'wp_mail_replyto_name_long_' . $context_key, /* translators: %s: context name */ sprintf( esc_html__( 'Name for %s context was truncated to 255 characters.', 'replyto' ), esc_html( $context_label ) ), 'warning' ); } // Default context is always enabled if it has an email. if ( 'default' === $context_key && ! empty( $email ) ) { $enabled = true; } // Store sanitized values. $sanitized[ $context_key ] = array( 'email' => $email, 'name' => $name, 'enabled' => $enabled, ); } // Log changes if in debug mode. if ( $old_contexts !== $sanitized ) { $user = wp_get_current_user(); $remote_addr = isset( $_SERVER['REMOTE_ADDR'] ) && is_string( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : 'unknown'; if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Intentional: error_log() is used for security audit logging, not debugging. Only active when WP_DEBUG_LOG is enabled. Records context configuration changes with user info and IP for security compliance and troubleshooting. This is a production feature for administrators who enable logging in wp-config.php. error_log( sprintf( '[Reply-To Plugin] Contexts configuration updated by user %s (ID: %d) from IP: %s', $user->user_login, $user->ID, $remote_addr ) ); } add_settings_error( 'wp_mail_replyto_messages', 'wp_mail_replyto_updated', esc_html__( 'Reply-To configuration updated successfully.', 'replyto' ), 'success' ); } return $sanitized; } /** * Enqueues admin styles for the settings page. * * @since 1.3.0 * * @param string $hook The current admin page hook. * @return void */ function wp_mail_replyto_admin_styles( $hook ) { if ( 'settings_page_replyto' !== $hook ) { return; } wp_register_style( 'replyto-admin', false, array(), WP_MAIL_REPLYTO_VERSION ); wp_enqueue_style( 'replyto-admin' ); wp_add_inline_style( 'replyto-admin', '.replyto-context-description { margin: 10px 0 20px; padding: 10px; background: #f0f6fc; border-left: 4px solid #2271b1; } .replyto-field-group { margin-bottom: 20px; } .replyto-field-group label { display: block; font-weight: 600; margin-bottom: 5px; } .replyto-field-group input[type="email"], .replyto-field-group input[type="text"] { width: 100%; max-width: 400px; } .replyto-field-group .description { margin-top: 5px; color: #646970; } .replyto-toggle-wrapper { margin-bottom: 20px; padding: 15px; background: #fff9e5; border-left: 4px solid #dba617; } .replyto-tab-content { margin-top: 20px; } .replyto-status-legend { font-size: 12px; color: #646970; margin: 10px 0 0 0; padding: 0; } .replyto-status-legend span { margin-right: 15px; }' ); } add_action( 'admin_enqueue_scripts', 'wp_mail_replyto_admin_styles' ); /** * Renders the plugin's settings page with tab-based interface. * * Uses WordPress native nav-tab-wrapper for consistent admin UI. * * @since 1.3.0 Completely rewritten with tabs interface. * * @return void */ function wp_mail_replyto_render_settings_page() { // Check if the current user has the required capability. if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'replyto' ) ); } // Get current contexts configuration. $contexts_raw = get_option( 'wp_mail_replyto_contexts', array() ); $contexts = is_array( $contexts_raw ) ? $contexts_raw : array(); // Uninstall behaviour opt-in (data is preserved by default). $delete_on_uninstall = wp_mail_replyto_sanitize_delete_on_uninstall( get_option( 'wp_mail_replyto_delete_on_uninstall', false ) ); // Get active tab from URL, default to 'default'. // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Tab parameter is for display only, not data modification. $active_tab = 'default'; if ( isset( $_GET['tab'] ) && is_string( $_GET['tab'] ) ) { $active_tab = sanitize_key( wp_unslash( $_GET['tab'] ) ); } // phpcs:enable // Define contexts with descriptions. $context_config = array( 'default' => array( 'label' => __( 'Default', 'replyto' ), 'description' => __( 'Default Reply-To used for all emails that do not match a specific context. This is the fallback when other contexts are not configured.', 'replyto' ), 'examples' => __( 'Newsletter confirmations, general notifications, and any emails not covered by other contexts.', 'replyto' ), ), 'authentication' => array( 'label' => __( 'Authentication & Security', 'replyto' ), 'description' => __( 'Reply-To for password resets, password changes, and email address changes.', 'replyto' ), 'examples' => __( 'Password reset requests, password change confirmations, email address verifications.', 'replyto' ), ), 'comments' => array( 'label' => __( 'Comments & Moderation', 'replyto' ), 'description' => __( 'Reply-To for comment notifications and moderation alerts.', 'replyto' ), 'examples' => __( 'New comment notifications, comment moderation alerts, comment replies.', 'replyto' ), ), 'users' => array( 'label' => __( 'Users & Registration', 'replyto' ), 'description' => __( 'Reply-To for new user registrations, role changes, and user management emails.', 'replyto' ), 'examples' => __( 'New user welcome emails, user role changes, account activations.', 'replyto' ), ), 'system' => array( 'label' => __( 'System & Updates', 'replyto' ), 'description' => __( 'Reply-To for automatic updates, system alerts, and critical site health notifications.', 'replyto' ), 'examples' => __( 'WordPress core updates, plugin updates, theme updates, recovery mode, fatal error notifications.', 'replyto' ), ), ); // Add WooCommerce context only if WooCommerce is active. if ( class_exists( 'WooCommerce' ) ) { $context_config['woocommerce'] = array( 'label' => __( 'WooCommerce', 'replyto' ), 'description' => __( 'Reply-To for WooCommerce order notifications, invoices, and customer communications.', 'replyto' ), 'examples' => __( 'Order confirmations, shipping notifications, invoices, customer notes.', 'replyto' ), ); } // Validate active tab exists. if ( ! isset( $context_config[ $active_tab ] ) ) { $active_tab = 'default'; } // Display settings errors, if any. settings_errors( 'wp_mail_replyto_messages' ); ?>

🟢 🔴



$config_item ) { if ( $key === $active_tab ) { continue; } $ctx_data = isset( $contexts[ $key ] ) && is_array( $contexts[ $key ] ) ? $contexts[ $key ] : array(); $ctx_email = isset( $ctx_data['email'] ) && is_string( $ctx_data['email'] ) ? $ctx_data['email'] : ''; $ctx_name = isset( $ctx_data['name'] ) && is_string( $ctx_data['name'] ) ? $ctx_data['name'] : ''; $ctx_enabled = ! empty( $ctx_data['enabled'] ); ?>