| 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 flow interceptor class. |
| 14 |
*/ |
| 15 |
class Setup_Flow_Interceptor implements Integration_Interface { |
| 16 |
|
| 17 |
/** |
| 18 |
* The page name of the Site Kit Setup finished page. |
| 19 |
*/ |
| 20 |
private const GOOGLE_SITE_KIT_SEARCH_CONSOLE_SETUP_FINISHED_PAGE = 'googlesitekit-splash'; |
| 21 |
private const GOOGLE_SITE_KIT_ANALYTICS_SETUP_FINISHED_PAGE = 'googlesitekit-dashboard'; |
| 22 |
|
| 23 |
/** |
| 24 |
* The Site Kit configuration object. |
| 25 |
* |
| 26 |
* @var Site_Kit $site_kit_configuration |
| 27 |
*/ |
| 28 |
private $site_kit_configuration; |
| 29 |
|
| 30 |
/** |
| 31 |
* Holds the Current_Page_Helper. |
| 32 |
* |
| 33 |
* @var Current_Page_Helper |
| 34 |
*/ |
| 35 |
private $current_page_helper; |
| 36 |
|
| 37 |
/** |
| 38 |
* Holds the redirect helper. |
| 39 |
* |
| 40 |
* @var Redirect_Helper $redirect_helper |
| 41 |
*/ |
| 42 |
private $redirect_helper; |
| 43 |
|
| 44 |
/** |
| 45 |
* The constructor. |
| 46 |
* |
| 47 |
* @param Current_Page_Helper $current_page_helper The current page helper. |
| 48 |
* @param Redirect_Helper $redirect_helper The redirect helper to abstract away the actual redirecting. |
| 49 |
* @param Site_Kit $site_kit_configuration The Site Kit configuration object. |
| 50 |
*/ |
| 51 |
public function __construct( Current_Page_Helper $current_page_helper, Redirect_Helper $redirect_helper, Site_Kit $site_kit_configuration ) { |
| 52 |
$this->current_page_helper = $current_page_helper; |
| 53 |
$this->redirect_helper = $redirect_helper; |
| 54 |
$this->site_kit_configuration = $site_kit_configuration; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Registers our redirect back to our dashboard all the way at the end of the admin init to make sure everything from the Site Kit callback can be finished. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function register_hooks() { |
| 63 |
\add_action( 'admin_init', [ $this, 'intercept_site_kit_setup_flow' ], 999 ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* The conditions for this integration to load. |
| 68 |
* |
| 69 |
* @return string[] The conditionals. |
| 70 |
*/ |
| 71 |
public static function get_conditionals() { |
| 72 |
return [ Admin_Conditional::class ]; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Checks if we should intercept the final page from the Site Kit flow. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function intercept_site_kit_setup_flow() { |
| 81 |
if ( \get_transient( Setup_Url_Interceptor::SITE_KIT_SETUP_TRANSIENT ) === '1' && $this->is_site_kit_setup_completed_page() ) { |
| 82 |
\delete_transient( Setup_Url_Interceptor::SITE_KIT_SETUP_TRANSIENT ); |
| 83 |
$this->redirect_helper->do_safe_redirect( \self_admin_url( 'admin.php?page=wpseo_dashboard&redirected_from_site_kit' ), 302, 'Yoast SEO' ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Checks if we are on the site kit setup completed page. |
| 89 |
* |
| 90 |
* @return bool |
| 91 |
*/ |
| 92 |
private function is_site_kit_setup_completed_page(): bool { |
| 93 |
$current_page = $this->current_page_helper->get_current_yoast_seo_page(); |
| 94 |
$on_search_console_setup_page = $current_page === self::GOOGLE_SITE_KIT_SEARCH_CONSOLE_SETUP_FINISHED_PAGE; |
| 95 |
$on_analytics_setup_page = $current_page === self::GOOGLE_SITE_KIT_ANALYTICS_SETUP_FINISHED_PAGE; |
| 96 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 97 |
$authentication_success_notification = isset( $_GET['notification'] ) && \sanitize_text_field( \wp_unslash( $_GET['notification'] ) ) === 'authentication_success'; |
| 98 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 99 |
$analytics_4_slug = isset( $_GET['slug'] ) && \sanitize_text_field( \wp_unslash( $_GET['slug'] ) ) === 'analytics-4'; |
| 100 |
|
| 101 |
/** |
| 102 |
* This checks two scenarios |
| 103 |
* 1. The user only wants Search Console. In this case just checking if you are on the thank-you page from Site Kit is enough. |
| 104 |
* 2. The user also wants analytics. So we need to check another page and also check if the analytics 4 connection is finalized. |
| 105 |
*/ |
| 106 |
return ( $on_search_console_setup_page && $authentication_success_notification ) || ( $on_analytics_setup_page && $authentication_success_notification && $analytics_4_slug && $this->site_kit_configuration->is_ga_connected() ); |
| 107 |
} |
| 108 |
} |
| 109 |
|