[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-recovery-mode-email-service.php (source)

   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 `error_get_last()`.
  44       * @param array $extension {
  45       *     The extension that caused the error.
  46       *
  47       *     @type string $slug The extension slug. The plugin or theme's directory.
  48       *     @type string $type The extension type. Either 'plugin' or 'theme'.
  49       * }
  50       * @return true|WP_Error True if email sent, WP_Error otherwise.
  51       */
  52  	public function maybe_send_recovery_mode_email( $rate_limit, $error, $extension ) {
  53  
  54          $last_sent = get_option( self::RATE_LIMIT_OPTION );
  55  
  56          if ( ! $last_sent || time() > $last_sent + $rate_limit ) {
  57              if ( ! update_option( self::RATE_LIMIT_OPTION, time() ) ) {
  58                  return new WP_Error( 'storage_error', __( 'Could not update the email last sent time.' ) );
  59              }
  60  
  61              $sent = $this->send_recovery_mode_email( $rate_limit, $error, $extension );
  62  
  63              if ( $sent ) {
  64                  return true;
  65              }
  66  
  67              return new WP_Error(
  68                  'email_failed',
  69                  sprintf(
  70                      /* translators: %s: mail() */
  71                      __( 'The email could not be sent. Possible reason: your host may have disabled the %s function.' ),
  72                      'mail()'
  73                  )
  74              );
  75          }
  76  
  77          $err_message = sprintf(
  78              /* translators: 1: Last sent as a human time diff, 2: Wait time as a human time diff. */
  79              __( 'A recovery link was already sent %1$s ago. Please wait another %2$s before requesting a new email.' ),
  80              human_time_diff( $last_sent ),
  81              human_time_diff( $last_sent + $rate_limit )
  82          );
  83  
  84          return new WP_Error( 'email_sent_already', $err_message );
  85      }
  86  
  87      /**
  88       * Clears the rate limit, allowing a new recovery mode email to be sent immediately.
  89       *
  90       * @since 5.2.0
  91       *
  92       * @return bool True on success, false on failure.
  93       */
  94  	public function clear_rate_limit() {
  95          return delete_option( self::RATE_LIMIT_OPTION );
  96      }
  97  
  98      /**
  99       * Sends the Recovery Mode email to the site admin email address.
 100       *
 101       * @since 5.2.0
 102       *
 103       * @param int   $rate_limit Number of seconds before another email can be sent.
 104       * @param array $error      Error details from `error_get_last()`.
 105       * @param array $extension {
 106       *     The extension that caused the error.
 107       *
 108       *     @type string $slug The extension slug. The directory of the plugin or theme.
 109       *     @type string $type The extension type. Either 'plugin' or 'theme'.
 110       * }
 111       * @return bool Whether the email was sent successfully.
 112       */
 113  	private function send_recovery_mode_email( $rate_limit, $error, $extension ) {
 114  
 115          $url      = $this->link_service->generate_url();
 116          $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
 117  
 118          $switched_locale = false;
 119  
 120          // The switch_to_locale() function is loaded before it can actually be used.
 121          if ( function_exists( 'switch_to_locale' ) && isset( $GLOBALS['wp_locale_switcher'] ) ) {
 122              $switched_locale = switch_to_locale( get_locale() );
 123          }
 124  
 125          if ( $extension ) {
 126              $cause   = $this->get_cause( $extension );
 127              $details = wp_strip_all_tags( wp_get_extension_error_description( $error ) );
 128  
 129              if ( $details ) {
 130                  $header  = __( 'Error Details' );
 131                  $details = "\n\n" . $header . "\n" . str_pad( '', strlen( $header ), '=' ) . "\n" . $details;
 132              }
 133          } else {
 134              $cause   = '';
 135              $details = '';
 136          }
 137  
 138          /**
 139           * Filters the support message sent with the the fatal error protection email.
 140           *
 141           * @since 5.2.0
 142           *
 143           * @param string $message The Message to include in the email.
 144           */
 145          $support = apply_filters( 'recovery_email_support_info', __( 'Please contact your host for assistance with investigating this issue further.' ) );
 146  
 147          /**
 148           * Filters the debug information included in the fatal error protection email.
 149           *
 150           * @since 5.3.0
 151           *
 152           * @param array $message An associative array of debug information.
 153           */
 154          $debug = apply_filters( 'recovery_email_debug_info', $this->get_debug( $extension ) );
 155  
 156          /* translators: Do not translate LINK, EXPIRES, CAUSE, DETAILS, SITEURL, PAGEURL, SUPPORT. DEBUG: those are placeholders. */
 157          $message = __(
 158              'Howdy!
 159  
 160  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.
 161  ###CAUSE###
 162  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.
 163  
 164  ###SUPPORT###
 165  
 166  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.
 167  
 168  ###LINK###
 169  
 170  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.
 171  
 172  When seeking help with this issue, you may be asked for some of the following information:
 173  ###DEBUG###
 174  
 175  ###DETAILS###'
 176          );
 177          $message = str_replace(
 178              array(
 179                  '###LINK###',
 180                  '###EXPIRES###',
 181                  '###CAUSE###',
 182                  '###DETAILS###',
 183                  '###SITEURL###',
 184                  '###PAGEURL###',
 185                  '###SUPPORT###',
 186                  '###DEBUG###',
 187              ),
 188              array(
 189                  $url,
 190                  human_time_diff( time() + $rate_limit ),
 191                  $cause ? "\n{$cause}\n" : "\n",
 192                  $details,
 193                  home_url( '/' ),
 194                  home_url( $_SERVER['REQUEST_URI'] ),
 195                  $support,
 196                  implode( "\r\n", $debug ),
 197              ),
 198              $message
 199          );
 200  
 201          $email = array(
 202              'to'          => $this->get_recovery_mode_email_address(),
 203              /* translators: %s: Site title. */
 204              'subject'     => __( '[%s] Your Site is Experiencing a Technical Issue' ),
 205              'message'     => $message,
 206              'headers'     => '',
 207              'attachments' => '',
 208          );
 209  
 210          /**
 211           * Filters the contents of the Recovery Mode email.
 212           *
 213           * @since 5.2.0
 214           * @since 5.6.0 The `$email` argument includes the `attachments` key.
 215           *
 216           * @param array  $email {
 217           *     Used to build a call to wp_mail().
 218           *
 219           *     @type string|array $to          Array or comma-separated list of email addresses to send message.
 220           *     @type string       $subject     Email subject
 221           *     @type string       $message     Message contents
 222           *     @type string|array $headers     Optional. Additional headers.
 223           *     @type string|array $attachments Optional. Files to attach.
 224           * }
 225           * @param string $url   URL to enter recovery mode.
 226           */
 227          $email = apply_filters( 'recovery_mode_email', $email, $url );
 228  
 229          $sent = wp_mail(
 230              $email['to'],
 231              wp_specialchars_decode( sprintf( $email['subject'], $blogname ) ),
 232              $email['message'],
 233              $email['headers'],
 234              $email['attachments']
 235          );
 236  
 237          if ( $switched_locale ) {
 238              restore_previous_locale();
 239          }
 240  
 241          return $sent;
 242      }
 243  
 244      /**
 245       * Gets the email address to send the recovery mode link to.
 246       *
 247       * @since 5.2.0
 248       *
 249       * @return string Email address to send recovery mode link to.
 250       */
 251  	private function get_recovery_mode_email_address() {
 252          if ( defined( 'RECOVERY_MODE_EMAIL' ) && is_email( RECOVERY_MODE_EMAIL ) ) {
 253              return RECOVERY_MODE_EMAIL;
 254          }
 255  
 256          return get_option( 'admin_email' );
 257      }
 258  
 259      /**
 260       * Gets the description indicating the possible cause for the error.
 261       *
 262       * @since 5.2.0
 263       *
 264       * @param array $extension {
 265       *     The extension that caused the error.
 266       *
 267       *     @type string $slug The extension slug. The directory of the plugin or theme.
 268       *     @type string $type The extension type. Either 'plugin' or 'theme'.
 269       * }
 270       * @return string Message about which extension caused the error.
 271       */
 272  	private function get_cause( $extension ) {
 273  
 274          if ( 'plugin' === $extension['type'] ) {
 275              $plugin = $this->get_plugin( $extension );
 276  
 277              if ( false === $plugin ) {
 278                  $name = $extension['slug'];
 279              } else {
 280                  $name = $plugin['Name'];
 281              }
 282  
 283              /* translators: %s: Plugin name. */
 284              $cause = sprintf( __( 'In this case, WordPress caught an error with one of your plugins, %s.' ), $name );
 285          } else {
 286              $theme = wp_get_theme( $extension['slug'] );
 287              $name  = $theme->exists() ? $theme->display( 'Name' ) : $extension['slug'];
 288  
 289              /* translators: %s: Theme name. */
 290              $cause = sprintf( __( 'In this case, WordPress caught an error with your theme, %s.' ), $name );
 291          }
 292  
 293          return $cause;
 294      }
 295  
 296      /**
 297       * Return the details for a single plugin based on the extension data from an error.
 298       *
 299       * @since 5.3.0
 300       *
 301       * @param array $extension {
 302       *     The extension that caused the error.
 303       *
 304       *     @type string $slug The extension slug. The directory of the plugin or theme.
 305       *     @type string $type The extension type. Either 'plugin' or 'theme'.
 306       * }
 307       * @return array|false A plugin array {@see get_plugins()} or `false` if no plugin was found.
 308       */
 309  	private function get_plugin( $extension ) {
 310          if ( ! function_exists( 'get_plugins' ) ) {
 311              require_once ABSPATH . 'wp-admin/includes/plugin.php';
 312          }
 313  
 314          $plugins = get_plugins();
 315  
 316          // Assume plugin main file name first since it is a common convention.
 317          if ( isset( $plugins[ "{$extension['slug']}/{$extension['slug']}.php" ] ) ) {
 318              return $plugins[ "{$extension['slug']}/{$extension['slug']}.php" ];
 319          } else {
 320              foreach ( $plugins as $file => $plugin_data ) {
 321                  if ( 0 === strpos( $file, "{$extension['slug']}/" ) || $file === $extension['slug'] ) {
 322                      return $plugin_data;
 323                  }
 324              }
 325          }
 326  
 327          return false;
 328      }
 329  
 330      /**
 331       * Return debug information in an easy to manipulate format.
 332       *
 333       * @since 5.3.0
 334       *
 335       * @param array $extension {
 336       *     The extension that caused the error.
 337       *
 338       *     @type string $slug The extension slug. The directory of the plugin or theme.
 339       *     @type string $type The extension type. Either 'plugin' or 'theme'.
 340       * }
 341       * @return array An associative array of debug information.
 342       */
 343  	private function get_debug( $extension ) {
 344          $theme      = wp_get_theme();
 345          $wp_version = get_bloginfo( 'version' );
 346  
 347          if ( $extension ) {
 348              $plugin = $this->get_plugin( $extension );
 349          } else {
 350              $plugin = null;
 351          }
 352  
 353          $debug = array(
 354              'wp'    => sprintf(
 355                  /* translators: %s: Current WordPress version number. */
 356                  __( 'WordPress version %s' ),
 357                  $wp_version
 358              ),
 359              'theme' => sprintf(
 360                  /* translators: 1: Current active theme name. 2: Current active theme version. */
 361                  __( 'Active theme: %1$s (version %2$s)' ),
 362                  $theme->get( 'Name' ),
 363                  $theme->get( 'Version' )
 364              ),
 365          );
 366  
 367          if ( null !== $plugin ) {
 368              $debug['plugin'] = sprintf(
 369                  /* translators: 1: The failing plugins name. 2: The failing plugins version. */
 370                  __( 'Current plugin: %1$s (version %2$s)' ),
 371                  $plugin['Name'],
 372                  $plugin['Version']
 373              );
 374          }
 375  
 376          $debug['php'] = sprintf(
 377              /* translators: %s: The currently used PHP version. */
 378              __( 'PHP version %s' ),
 379              PHP_VERSION
 380          );
 381  
 382          return $debug;
 383      }
 384  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1