[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Fri Oct 25 08:20:01 2019 Cross-referenced by PHPXref 0.7