[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Thu Mar 28 08:20:01 2024 Cross-referenced by PHPXref