-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplyto.php
More file actions
199 lines (177 loc) · 6.47 KB
/
replyto.php
File metadata and controls
199 lines (177 loc) · 6.47 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?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.3
* 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!' );
/**
* Modifies the "Reply-To" header in emails sent using wp_mail().
*
* Retrieves the "Reply-To" email address from the plugin settings, validates it,
* and injects it into the email headers if appropriate.
*
* @param array $args The email arguments passed to wp_mail().
* @return array Modified email arguments with adjusted "Reply-To" header.
*/
function wp_mail_replyto( $args ) {
// Retrieve the "Reply-To" email address from plugin settings.
$reply_to_email = get_option( 'wp_mail_replyto_email' );
// Construct the new "Reply-To" header if a valid email address is set.
if ( ! empty( $reply_to_email ) && is_email( $reply_to_email ) ) {
$new_reply_to = 'Reply-To: <' . sanitize_email( $reply_to_email ) . '>';
}
// Initialize variables to track the "From" and existing "Reply-To" headers.
$from = '';
$existing_reply_to = '';
// 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( "\r\n", "\n", $args['headers'] ) ) );
}
// Loop through the headers to locate "From" and "Reply-To" values.
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:' ) ) );
}
}
// If "Reply-To" exists and matches the "From" address, replace it with the new one.
if ( ! empty( $existing_reply_to ) && strtolower( $existing_reply_to ) === strtolower( $from ) ) {
// Remove the existing "Reply-To" header.
$args['headers'] = array_filter(
$args['headers'],
function ( $header ) {
return stripos( $header, 'reply-to:' ) !== 0;
}
);
// Add the new "Reply-To" header.
$args['headers'][] = $new_reply_to;
} elseif ( isset( $new_reply_to ) ) {
// If no conflicting "Reply-To" is found, append the new one.
$args['headers'][] = $new_reply_to;
}
} elseif ( isset( $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.
*/
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' );
/**
* Registers the plugin settings, section, and field in the WordPress Settings API.
*
* This function defines:
* - A setting for the "Reply-To" email address with sanitization.
* - A settings section with a description callback.
* - A field for entering the "Reply-To" email address.
*/
function wp_mail_replyto_register_settings() {
// Register the "Reply-To" email setting with sanitization.
register_setting(
'wp_mail_replyto_settings_group',
'wp_mail_replyto_email',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_email',
'default' => '',
)
);
// Add a section to the settings page for organizing fields.
add_settings_section(
'wp_mail_replyto_main_section',
esc_html__( 'Reply-To Configuration', 'replyto' ),
'wp_mail_replyto_main_section_callback',
'replyto'
);
// Add the input field for the "Reply-To" email address.
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' );
/**
* Outputs the description for the main settings section.
*
* This callback is used by the Settings API to display a description
* under the "Reply-To Configuration" section on the plugin settings page.
*/
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>';
}
/**
* Renders the input field for the "Reply-To" email address.
*
* This callback is used by the Settings API to display the input field
* where administrators can enter the email address to be used in the "Reply-To" header.
*/
function wp_mail_replyto_email_field_callback() {
// Get the current "Reply-To" email address from the plugin settings.
$email = get_option( 'wp_mail_replyto_email', '' );
// Output the input field HTML.
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>';
}
/**
* Renders the plugin's settings page in the WordPress admin area.
*
* This function outputs the HTML structure and form elements for the plugin settings page.
* It includes the registered settings fields and sections for configuring the "Reply-To" email.
*/
function wp_mail_replyto_render_settings_page() {
// Check if the current user has the required capability.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Display settings errors, if any.
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
// Output security fields and registered settings sections.
settings_fields( 'wp_mail_replyto_settings_group' );
do_settings_sections( 'replyto' );
submit_button( esc_html__( 'Save Settings', 'replyto' ) );
?>
</form>
</div>
<?php
}