Sin descripción

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

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. 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. * @global string $pagenow
  62. *
  63. * @param int $ttl Number of seconds the link should be valid for.
  64. */
  65. public function handle_begin_link( $ttl ) {
  66. if ( ! isset( $GLOBALS['pagenow'] ) || 'wp-login.php' !== $GLOBALS['pagenow'] ) {
  67. return;
  68. }
  69. if ( ! isset( $_GET['action'], $_GET['rm_token'], $_GET['rm_key'] ) || self::LOGIN_ACTION_ENTER !== $_GET['action'] ) {
  70. return;
  71. }
  72. if ( ! function_exists( 'wp_generate_password' ) ) {
  73. require_once ABSPATH . WPINC . '/pluggable.php';
  74. }
  75. $validated = $this->key_service->validate_recovery_mode_key( $_GET['rm_token'], $_GET['rm_key'], $ttl );
  76. if ( is_wp_error( $validated ) ) {
  77. wp_die( $validated, '' );
  78. }
  79. $this->cookie_service->set_cookie();
  80. $url = add_query_arg( 'action', self::LOGIN_ACTION_ENTERED, wp_login_url() );
  81. wp_redirect( $url );
  82. die;
  83. }
  84. /**
  85. * Gets a URL to begin recovery mode.
  86. *
  87. * @since 5.2.0
  88. *
  89. * @param string $token Recovery Mode token created by {@see generate_recovery_mode_token()}.
  90. * @param string $key Recovery Mode key created by {@see generate_and_store_recovery_mode_key()}.
  91. * @return string Recovery mode begin URL.
  92. */
  93. private function get_recovery_mode_begin_url( $token, $key ) {
  94. $url = add_query_arg(
  95. array(
  96. 'action' => self::LOGIN_ACTION_ENTER,
  97. 'rm_token' => $token,
  98. 'rm_key' => $key,
  99. ),
  100. wp_login_url()
  101. );
  102. /**
  103. * Filters the URL to begin recovery mode.
  104. *
  105. * @since 5.2.0
  106. *
  107. * @param string $url The generated recovery mode begin URL.
  108. * @param string $token The token used to identify the key.
  109. * @param string $key The recovery mode key.
  110. */
  111. return apply_filters( 'recovery_mode_begin_url', $url, $token, $key );
  112. }
  113. }