balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-recovery-mode-link-service.php (3401B)


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