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

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