-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplyto.php
176 lines (159 loc) · 5.04 KB
/
replyto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Plugin Name: Reply-To for WP_Mail
* Description: Configure your "Reply-To:" for WP_Mail with validation and admin settings.
* Requires at least: 4.1
* Requires PHP: 5.6
* Version: 1.0.2
* Author: Javier Casares
* Author URI: https://www.javiercasares.com/
* License: GPL-2.0-or-later
* License URI: https://spdx.org/licenses/GPL-2.0-or-later.html
* Text Domain: replyto
* Domain Path: /languages
*
* @package replyto
*
* @version 1.0.0
*/
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
/**
* Configure the "Reply-To" header for emails sent by WP Mail.
*
* @param array $args Email arguments.
* @return array Modified email arguments.
*/
function wp_mail_replyto( $args ) {
// Get the "Reply-To" email from plugin settings.
$reply_to_email = get_option( 'wp_mail_replyto_email' );
// Define the new "Reply-To" if configured.
if ( ! empty( $reply_to_email ) ) {
$new_reply_to = sprintf(
/* translators: %s: email address */
__( 'Reply-To: <%s>', 'replyto' ),
sanitize_email( $reply_to_email )
);
}
// Initialize variables for "From" and existing "Reply-To".
$from = '';
$existing_reply_to = '';
// Check if there are existing headers.
if ( ! empty( $args['headers'] ) ) {
// Normalize headers to an array.
if ( ! is_array( $args['headers'] ) ) {
$args['headers'] = array_filter( explode( "\n", str_replace( "\r\n", "\n", $args['headers'] ) ) );
}
// Loop through headers to find "From" and "Reply-To".
foreach ( $args['headers'] as $header ) {
if ( stripos( $header, 'from:' ) === 0 ) {
$from = trim( substr( $header, strlen( 'from:' ) ) );
}
if ( stripos( $header, 'reply-to:' ) === 0 ) {
$existing_reply_to = trim( substr( $header, strlen( 'reply-to:' ) ) );
}
}
// Check if "Reply-To" exists and compare with "From".
if ( ! empty( $existing_reply_to ) && strtolower( $existing_reply_to ) === strtolower( $from ) ) {
// If they are the same, replace "Reply-To" with the new one.
$args['headers'] = array_filter(
$args['headers'],
function ( $header ) {
return stripos( $header, 'reply-to:' ) !== 0;
}
);
$args['headers'][] = $new_reply_to;
} elseif ( isset( $new_reply_to ) ) {
// If "Reply-To" does not exist and new_reply_to is set, add it.
$args['headers'][] = $new_reply_to;
}
} elseif ( isset( $new_reply_to ) ) {
// If there are no headers and new_reply_to is set, create headers with "Reply-To".
$args['headers'] = array( $new_reply_to );
}
return $args;
}
add_filter( 'wp_mail', 'wp_mail_replyto' );
/**
* Add a settings page to the WordPress admin menu.
*/
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' );
/**
* Register the plugin settings.
*/
function wp_mail_replyto_register_settings() {
register_setting(
'wp_mail_replyto_settings_group',
'wp_mail_replyto_email',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_email',
'default' => '',
)
);
add_settings_section(
'wp_mail_replyto_main_section',
esc_html__( 'Reply-To Configuration', 'replyto' ),
'wp_mail_replyto_main_section_callback',
'replyto'
);
add_settings_field(
'wp_mail_replyto_email_field',
esc_html__( 'Reply-To Email Address', 'replyto' ),
'wp_mail_replyto_email_field_callback',
'replyto',
'wp_mail_replyto_main_section'
);
}
add_action( 'admin_init', 'wp_mail_replyto_register_settings' );
/**
* Callback for the settings section description.
*/
function wp_mail_replyto_main_section_callback() {
echo '<p>' . esc_html__( 'Set the email address to be used in the "Reply-To" header of outgoing emails.', 'replyto' ) . '</p>';
}
/**
* Callback to render the email address field.
*/
function wp_mail_replyto_email_field_callback() {
$email = get_option( 'wp_mail_replyto_email', '' );
echo '<input type="email" id="wp_mail_replyto_email" name="wp_mail_replyto_email" value="' . esc_attr( $email ) . '" size="50" />';
echo '<p class="description">' . esc_html__( 'Enter the email address to be used as "Reply-To".', 'replyto' ) . '</p>';
}
/**
* Render the plugin settings page.
*/
function wp_mail_replyto_render_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
settings_errors( 'wp_mail_replyto_messages' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form action="options.php" method="post">
<?php
wp_nonce_field( 'update-options' );
settings_fields( 'wp_mail_replyto_settings_group' );
do_settings_sections( 'replyto' );
submit_button( esc_html__( 'Save Settings', 'replyto' ) );
?>
</form>
</div>
<?php
}
/**
* Load plugin textdomain for translations.
*/
function wp_mail_replyto_load_textdomain() {
load_plugin_textdomain( 'replyto', false, basename( __DIR__ ) . '/languages' );
}
add_action( 'plugins_loaded', 'wp_mail_replyto_load_textdomain' );