|
1 <?php |
|
2 /** |
|
3 * Error Protection API: WP_Recovery_Mode_Email_Link class |
|
4 * |
|
5 * @package WordPress |
|
6 * @since 5.2.0 |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Core class used to send an email with a link to begin Recovery Mode. |
|
11 * |
|
12 * @since 5.2.0 |
|
13 */ |
|
14 final class WP_Recovery_Mode_Email_Service { |
|
15 |
|
16 const RATE_LIMIT_OPTION = 'recovery_mode_email_last_sent'; |
|
17 |
|
18 /** |
|
19 * Service to generate recovery mode URLs. |
|
20 * |
|
21 * @since 5.2.0 |
|
22 * @var WP_Recovery_Mode_Link_Service |
|
23 */ |
|
24 private $link_service; |
|
25 |
|
26 /** |
|
27 * WP_Recovery_Mode_Email_Service constructor. |
|
28 * |
|
29 * @since 5.2.0 |
|
30 * |
|
31 * @param WP_Recovery_Mode_Link_Service $link_service |
|
32 */ |
|
33 public function __construct( WP_Recovery_Mode_Link_Service $link_service ) { |
|
34 $this->link_service = $link_service; |
|
35 } |
|
36 |
|
37 /** |
|
38 * Sends the recovery mode email if the rate limit has not been sent. |
|
39 * |
|
40 * @since 5.2.0 |
|
41 * |
|
42 * @param int $rate_limit Number of seconds before another email can be sent. |
|
43 * @param array $error Error details from {@see error_get_last()} |
|
44 * @param array $extension The extension that caused the error. { |
|
45 * @type string $slug The extension slug. The plugin or theme's directory. |
|
46 * @type string $type The extension type. Either 'plugin' or 'theme'. |
|
47 * } |
|
48 * @return true|WP_Error True if email sent, WP_Error otherwise. |
|
49 */ |
|
50 public function maybe_send_recovery_mode_email( $rate_limit, $error, $extension ) { |
|
51 |
|
52 $last_sent = get_option( self::RATE_LIMIT_OPTION ); |
|
53 |
|
54 if ( ! $last_sent || time() > $last_sent + $rate_limit ) { |
|
55 if ( ! update_option( self::RATE_LIMIT_OPTION, time() ) ) { |
|
56 return new WP_Error( 'storage_error', __( 'Could not update the email last sent time.' ) ); |
|
57 } |
|
58 |
|
59 $sent = $this->send_recovery_mode_email( $rate_limit, $error, $extension ); |
|
60 |
|
61 if ( $sent ) { |
|
62 return true; |
|
63 } |
|
64 |
|
65 return new WP_Error( 'email_failed', __( 'The email could not be sent. Possible reason: your host may have disabled the mail() function.' ) ); |
|
66 } |
|
67 |
|
68 $err_message = sprintf( |
|
69 /* translators: 1. Last sent as a human time diff 2. Wait time as a human time diff. */ |
|
70 __( 'A recovery link was already sent %1$s ago. Please wait another %2$s before requesting a new email.' ), |
|
71 human_time_diff( $last_sent ), |
|
72 human_time_diff( $last_sent + $rate_limit ) |
|
73 ); |
|
74 |
|
75 return new WP_Error( 'email_sent_already', $err_message ); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Clears the rate limit, allowing a new recovery mode email to be sent immediately. |
|
80 * |
|
81 * @since 5.2.0 |
|
82 * |
|
83 * @return bool True on success, false on failure. |
|
84 */ |
|
85 public function clear_rate_limit() { |
|
86 return delete_option( self::RATE_LIMIT_OPTION ); |
|
87 } |
|
88 |
|
89 /** |
|
90 * Sends the Recovery Mode email to the site admin email address. |
|
91 * |
|
92 * @since 5.2.0 |
|
93 * |
|
94 * @param int $rate_limit Number of seconds before another email can be sent. |
|
95 * @param array $error Error details from {@see error_get_last()} |
|
96 * @param array $extension Extension that caused the error. |
|
97 * |
|
98 * @return bool Whether the email was sent successfully. |
|
99 */ |
|
100 private function send_recovery_mode_email( $rate_limit, $error, $extension ) { |
|
101 |
|
102 $url = $this->link_service->generate_url(); |
|
103 $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
|
104 |
|
105 $switched_locale = false; |
|
106 |
|
107 // The switch_to_locale() function is loaded before it can actually be used. |
|
108 if ( function_exists( 'switch_to_locale' ) && isset( $GLOBALS['wp_locale_switcher'] ) ) { |
|
109 $switched_locale = switch_to_locale( get_locale() ); |
|
110 } |
|
111 |
|
112 if ( $extension ) { |
|
113 $cause = $this->get_cause( $extension ); |
|
114 $details = wp_strip_all_tags( wp_get_extension_error_description( $error ) ); |
|
115 |
|
116 if ( $details ) { |
|
117 $header = __( 'Error Details' ); |
|
118 $details = "\n\n" . $header . "\n" . str_pad( '', strlen( $header ), '=' ) . "\n" . $details; |
|
119 } |
|
120 } else { |
|
121 $cause = ''; |
|
122 $details = ''; |
|
123 } |
|
124 |
|
125 /** |
|
126 * Filters the support message sent with the the fatal error protection email. |
|
127 * |
|
128 * @since 5.2.0 |
|
129 * |
|
130 * @param $message string The Message to include in the email. |
|
131 */ |
|
132 $support = apply_filters( 'recovery_email_support_info', __( 'Please contact your host for assistance with investigating this issue further.' ) ); |
|
133 |
|
134 /* translators: Do not translate LINK, EXPIRES, CAUSE, DETAILS, SITEURL, PAGEURL, SUPPORT: those are placeholders. */ |
|
135 $message = __( |
|
136 'Howdy! |
|
137 |
|
138 Since WordPress 5.2 there is a built-in feature that detects when a plugin or theme causes a fatal error on your site, and notifies you with this automated email. |
|
139 ###CAUSE### |
|
140 First, visit your website (###SITEURL###) and check for any visible issues. Next, visit the page where the error was caught (###PAGEURL###) and check for any visible issues. |
|
141 |
|
142 ###SUPPORT### |
|
143 |
|
144 If your site appears broken and you can\'t access your dashboard normally, WordPress now has a special "recovery mode". This lets you safely login to your dashboard and investigate further. |
|
145 |
|
146 ###LINK### |
|
147 |
|
148 To keep your site safe, this link will expire in ###EXPIRES###. Don\'t worry about that, though: a new link will be emailed to you if the error occurs again after it expires. |
|
149 |
|
150 ###DETAILS###' |
|
151 ); |
|
152 $message = str_replace( |
|
153 array( |
|
154 '###LINK###', |
|
155 '###EXPIRES###', |
|
156 '###CAUSE###', |
|
157 '###DETAILS###', |
|
158 '###SITEURL###', |
|
159 '###PAGEURL###', |
|
160 '###SUPPORT###', |
|
161 ), |
|
162 array( |
|
163 $url, |
|
164 human_time_diff( time() + $rate_limit ), |
|
165 $cause ? "\n{$cause}\n" : "\n", |
|
166 $details, |
|
167 home_url( '/' ), |
|
168 home_url( $_SERVER['REQUEST_URI'] ), |
|
169 $support, |
|
170 ), |
|
171 $message |
|
172 ); |
|
173 |
|
174 $email = array( |
|
175 'to' => $this->get_recovery_mode_email_address(), |
|
176 /* translators: %s: site name */ |
|
177 'subject' => __( '[%s] Your Site is Experiencing a Technical Issue' ), |
|
178 'message' => $message, |
|
179 'headers' => '', |
|
180 ); |
|
181 |
|
182 /** |
|
183 * Filter the contents of the Recovery Mode email. |
|
184 * |
|
185 * @since 5.2.0 |
|
186 * |
|
187 * @param array $email Used to build wp_mail(). |
|
188 * @param string $url URL to enter recovery mode. |
|
189 */ |
|
190 $email = apply_filters( 'recovery_mode_email', $email, $url ); |
|
191 |
|
192 $sent = wp_mail( |
|
193 $email['to'], |
|
194 wp_specialchars_decode( sprintf( $email['subject'], $blogname ) ), |
|
195 $email['message'], |
|
196 $email['headers'] |
|
197 ); |
|
198 |
|
199 if ( $switched_locale ) { |
|
200 restore_previous_locale(); |
|
201 } |
|
202 |
|
203 return $sent; |
|
204 } |
|
205 |
|
206 /** |
|
207 * Gets the email address to send the recovery mode link to. |
|
208 * |
|
209 * @since 5.2.0 |
|
210 * |
|
211 * @return string Email address to send recovery mode link to. |
|
212 */ |
|
213 private function get_recovery_mode_email_address() { |
|
214 if ( defined( 'RECOVERY_MODE_EMAIL' ) && is_email( RECOVERY_MODE_EMAIL ) ) { |
|
215 return RECOVERY_MODE_EMAIL; |
|
216 } |
|
217 |
|
218 return get_option( 'admin_email' ); |
|
219 } |
|
220 |
|
221 /** |
|
222 * Gets the description indicating the possible cause for the error. |
|
223 * |
|
224 * @since 5.2.0 |
|
225 * |
|
226 * @param array $extension The extension that caused the error. |
|
227 * @return string Message about which extension caused the error. |
|
228 */ |
|
229 private function get_cause( $extension ) { |
|
230 |
|
231 if ( 'plugin' === $extension['type'] ) { |
|
232 if ( ! function_exists( 'get_plugins' ) ) { |
|
233 require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
234 } |
|
235 |
|
236 $plugins = get_plugins(); |
|
237 |
|
238 $name = ''; |
|
239 |
|
240 // Assume plugin main file name first since it is a common convention. |
|
241 if ( isset( $plugins[ "{$extension['slug']}/{$extension['slug']}.php" ] ) ) { |
|
242 $name = $plugins[ "{$extension['slug']}/{$extension['slug']}.php" ]['Name']; |
|
243 } else { |
|
244 foreach ( $plugins as $file => $plugin_data ) { |
|
245 if ( 0 === strpos( $file, "{$extension['slug']}/" ) || $file === $extension['slug'] ) { |
|
246 $name = $plugin_data['Name']; |
|
247 break; |
|
248 } |
|
249 } |
|
250 } |
|
251 |
|
252 if ( empty( $name ) ) { |
|
253 $name = $extension['slug']; |
|
254 } |
|
255 |
|
256 /* translators: %s: plugin name */ |
|
257 $cause = sprintf( __( 'In this case, WordPress caught an error with one of your plugins, %s.' ), $name ); |
|
258 } else { |
|
259 $theme = wp_get_theme( $extension['slug'] ); |
|
260 $name = $theme->exists() ? $theme->display( 'Name' ) : $extension['slug']; |
|
261 |
|
262 /* translators: %s: theme name */ |
|
263 $cause = sprintf( __( 'In this case, WordPress caught an error with your theme, %s.' ), $name ); |
|
264 } |
|
265 |
|
266 return $cause; |
|
267 } |
|
268 } |