| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Dashboard\User_Interface\Setup; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 7 |
use Yoast\WP\SEO\Dashboard\Infrastructure\Integrations\Site_Kit; |
| 8 |
use Yoast\WP\SEO\Helpers\Current_Page_Helper; |
| 9 |
use Yoast\WP\SEO\Helpers\Redirect_Helper; |
| 10 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Setup url Interceptor class. |
| 14 |
*/ |
| 15 |
class Setup_Url_Interceptor implements Integration_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The page url where this check lives. |
| 19 |
*/ |
| 20 |
public const PAGE = 'wpseo_page_site_kit_set_up'; |
| 21 |
|
| 22 |
/** |
| 23 |
* The name of the transient. |
| 24 |
*/ |
| 25 |
public const SITE_KIT_SETUP_TRANSIENT = 'wpseo_site_kit_set_up_transient'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Holds the Current_Page_Helper. |
| 29 |
* |
| 30 |
* @var Current_Page_Helper $current_page_helper |
| 31 |
*/ |
| 32 |
private $current_page_helper; |
| 33 |
|
| 34 |
/** |
| 35 |
* Holds the redirect helper. |
| 36 |
* |
| 37 |
* @var Redirect_Helper $redirect_helper |
| 38 |
*/ |
| 39 |
private $redirect_helper; |
| 40 |
|
| 41 |
/** |
| 42 |
* The Site Kit configuration object. |
| 43 |
* |
| 44 |
* @var Site_Kit $site_kit_configuration |
| 45 |
*/ |
| 46 |
private $site_kit_configuration; |
| 47 |
|
| 48 |
/** |
| 49 |
* The constructor. |
| 50 |
* |
| 51 |
* @param Current_Page_Helper $current_page_helper The current page helper. |
| 52 |
* @param Site_Kit $site_kit_configuration The Site Kit configuration object. |
| 53 |
* @param Redirect_Helper $redirect_helper The redirect helper to abstract away the actual redirecting. |
| 54 |
*/ |
| 55 |
public function __construct( Current_Page_Helper $current_page_helper, Site_Kit $site_kit_configuration, Redirect_Helper $redirect_helper ) { |
| 56 |
$this->current_page_helper = $current_page_helper; |
| 57 |
$this->redirect_helper = $redirect_helper; |
| 58 |
|
| 59 |
$this->site_kit_configuration = $site_kit_configuration; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* The conditions for this integration to load. |
| 64 |
* |
| 65 |
* @return string[] The conditionals. |
| 66 |
*/ |
| 67 |
public static function get_conditionals() { |
| 68 |
return [ Admin_Conditional::class ]; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Registers the interceptor code to the admin_init function. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function register_hooks() { |
| 77 |
\add_filter( 'admin_menu', [ $this, 'add_redirect_page' ] ); |
| 78 |
\add_action( 'admin_init', [ $this, 'intercept_site_kit_setup_url_redirect' ], 1 ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Adds a dummy page. |
| 83 |
* |
| 84 |
* We need to register this in between page. |
| 85 |
* |
| 86 |
* @param array<array<string>> $pages The pages. |
| 87 |
* |
| 88 |
* @return array<array<string>> The pages. |
| 89 |
*/ |
| 90 |
public function add_redirect_page( $pages ) { |
| 91 |
\add_submenu_page( |
| 92 |
'options.php', |
| 93 |
'', |
| 94 |
'', |
| 95 |
'wpseo_manage_options', |
| 96 |
self::PAGE, |
| 97 |
); |
| 98 |
|
| 99 |
return $pages; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Checks if we are trying to reach a site kit setup url and sets the needed transient in between. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function intercept_site_kit_setup_url_redirect() { |
| 108 |
$allowed_setup_links = [ |
| 109 |
$this->site_kit_configuration->get_install_url(), |
| 110 |
$this->site_kit_configuration->get_activate_url(), |
| 111 |
$this->site_kit_configuration->get_setup_url(), |
| 112 |
$this->site_kit_configuration->get_update_url(), |
| 113 |
]; |
| 114 |
|
| 115 |
// Are we on the in-between page? |
| 116 |
if ( $this->current_page_helper->get_current_yoast_seo_page() === self::PAGE ) { |
| 117 |
// Check if parameter is there and is valid. |
| 118 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 119 |
if ( isset( $_GET['redirect_setup_url'] ) && \in_array( \wp_unslash( $_GET['redirect_setup_url'] ), $allowed_setup_links, true ) ) { |
| 120 |
// We overwrite the transient for each time this redirect is hit to keep refreshing the time. |
| 121 |
\set_transient( self::SITE_KIT_SETUP_TRANSIENT, 1, ( \MINUTE_IN_SECONDS * 15 ) ); |
| 122 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Only allowed pre verified links can end up here. |
| 123 |
$redirect_url = \wp_unslash( $_GET['redirect_setup_url'] ); |
| 124 |
$this->redirect_helper->do_safe_redirect( $redirect_url, 302, 'Yoast SEO' ); |
| 125 |
} |
| 126 |
else { |
| 127 |
$this->redirect_helper->do_safe_redirect( \self_admin_url( 'admin.php?page=wpseo_dashboard' ), 302, 'Yoast SEO' ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|