class-wp-recovery-mode-link-service.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Error Protection API: WP_Recovery_Mode_Link_Handler class
  4. *
  5. * @package WordPress
  6. * @since 5.2.0
  7. */
  8. /**
  9. * Core class used to generate and handle recovery mode links.
  10. *
  11. * @since 5.2.0
  12. */
  13. class WP_Recovery_Mode_Link_Service {
  14. const LOGIN_ACTION_ENTER = 'enter_recovery_mode';
  15. const LOGIN_ACTION_ENTERED = 'entered_recovery_mode';
  16. /**
  17. * Service to generate and validate recovery mode keys.
  18. *
  19. * @since 5.2.0
  20. * @var WP_Recovery_Mode_Key_Service
  21. */
  22. private $key_service;
  23. /**
  24. * Service to handle cookies.
  25. *
  26. * @since 5.2.0
  27. * @var WP_Recovery_Mode_Cookie_Service
  28. */
  29. private $cookie_service;
  30. /**
  31. * WP_Recovery_Mode_Link_Service constructor.
  32. *
  33. * @since 5.2.0
  34. *
  35. * @param WP_Recovery_Mode_Cookie_Service $cookie_service Service to handle setting the recovery mode cookie.
  36. * @param WP_Recovery_Mode_Key_Service $key_service Service to handle generating recovery mode keys.
  37. */
  38. public function __construct( WP_Recovery_Mode_Cookie_Service $cookie_service, WP_Recovery_Mode_Key_Service $key_service ) {
  39. $this->cookie_service = $cookie_service;
  40. $this->key_service = $key_service;
  41. }
  42. /**
  43. * Generates a URL to begin recovery mode.
  44. *
  45. * Only one recovery mode URL can may be valid at the same time.
  46. *
  47. * @since 5.2.0
  48. *
  49. * @return string Generated URL.
  50. */
  51. public function generate_url() {
  52. $token = $this->key_service->generate_recovery_mode_token();
  53. $key = $this->key_service->generate_and_store_recovery_mode_key( $token );
  54. return $this->get_recovery_mode_begin_url( $token, $key );
  55. }
  56. /**
  57. * Enters recovery mode when the user hits wp-login.php with a valid recovery mode link.
  58. *
  59. * @since 5.2.0
  60. *
  61. * @param int $ttl Number of seconds the link should be valid for.
  62. */
  63. public function handle_begin_link( $ttl ) {
  64. if ( ! isset( $GLOBALS['pagenow'] ) || 'wp-login.php' !== $GLOBALS['pagenow'] ) {
  65. return;
  66. }
  67. if ( ! isset( $_GET['action'], $_GET['rm_token'], $_GET['rm_key'] ) || self::LOGIN_ACTION_ENTER !== $_GET['action'] ) {
  68. return;
  69. }
  70. if ( ! function_exists( 'wp_generate_password' ) ) {
  71. require_once ABSPATH . WPINC . '/pluggable.php';
  72. }
  73. $validated = $this->key_service->validate_recovery_mode_key( $_GET['rm_token'], $_GET['rm_key'], $ttl );
  74. if ( is_wp_error( $validated ) ) {
  75. wp_die( $validated, '' );
  76. }
  77. $this->cookie_service->set_cookie();
  78. $url = add_query_arg( 'action', self::LOGIN_ACTION_ENTERED, wp_login_url() );
  79. wp_redirect( $url );
  80. die;
  81. }
  82. /**
  83. * Gets a URL to begin recovery mode.
  84. *
  85. * @since 5.2.0
  86. *
  87. * @param string $token Recovery Mode token created by {@see generate_recovery_mode_token()}.
  88. * @param string $key Recovery Mode key created by {@see generate_and_store_recovery_mode_key()}.
  89. * @return string Recovery mode begin URL.
  90. */
  91. private function get_recovery_mode_begin_url( $token, $key ) {
  92. $url = add_query_arg(
  93. array(
  94. 'action' => self::LOGIN_ACTION_ENTER,
  95. 'rm_token' => $token,
  96. 'rm_key' => $key,
  97. ),
  98. wp_login_url()
  99. );
  100. /**
  101. * Filter the URL to begin recovery mode.
  102. *
  103. * @since 5.2.0
  104. *
  105. * @param string $url The generated recovery mode begin URL.
  106. * @param string $token The token used to identify the key.
  107. * @param string $key The recovery mode key.
  108. */
  109. return apply_filters( 'recovery_mode_begin_url', $url, $token, $key );
  110. }
  111. }