detector = $detector; $this->catalog_detector = $catalog_detector; } /** * `admin_post_agentmysite_connect` handler. */ public function handle(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to manage this plugin.', 'agentmysite' ), '', array( 'response' => 403 ) ); } check_admin_referer( self::ACTION ); $challenge = AgentMySite_Verifier_Store::start( get_current_user_id() ); $url = $this->connect_url( $challenge ); add_filter( 'allowed_redirect_hosts', array( self::class, 'allow_admin_host' ) ); wp_safe_redirect( $url ); $this->terminate(); } /** * Add our admin host to the hosts wp_safe_redirect() will follow. * * @param string[] $hosts Allowed hosts. * @return string[] */ public static function allow_admin_host( $hosts ): array { $hosts = is_array( $hosts ) ? $hosts : array(); $host = AgentMySite_Config::host_of( AgentMySite_Config::admin_origin() ); if ( '' !== $host && ! in_array( $host, $hosts, true ) ) { $hosts[] = $host; } return $hosts; } /** * This site's address as sent at connect and at exchange. */ public static function site_url(): string { return home_url(); } /** * The plugin settings page. */ public static function settings_url(): string { return admin_url( 'options-general.php' ) . '?' . self::query( array( 'page' => self::PAGE_SLUG ) ); } /** * Settings page URL carrying a fresh return nonce. */ public static function return_url(): string { return admin_url( 'options-general.php' ) . '?' . self::query( array( 'page' => self::PAGE_SLUG, 'ams_nonce' => wp_create_nonce( self::RETURN_NONCE_ACTION ), ) ); } /** * Our connect screen URL for a challenge. * * @param string $challenge PKCE challenge. */ public function connect_url( string $challenge ): string { $params = array( 'challenge' => $challenge, 'site_url' => self::site_url(), 'site_name' => self::site_name(), ); $sitemap = $this->detector()->detect()['url']; if ( null !== $sitemap ) { $params['sitemap_url'] = $sitemap; } $catalog = $this->catalog_detector()->detect(); if ( null !== $catalog ) { $params['catalog_url'] = $catalog; } $params['return'] = self::return_url(); return AgentMySite_Config::connect_url() . '?' . self::query( $params ); } /** * Stop after redirecting. Overridden in tests. */ protected function terminate(): void { exit; } /** * Plain-text site title, capped. */ private static function site_name(): string { $name = html_entity_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES, 'UTF-8' ); return mb_substr( trim( $name ), 0, self::SITE_NAME_MAX, 'UTF-8' ); } /** * RFC 3986 encoded query string. * * @param array $params Parameters. */ private static function query( array $params ): string { return http_build_query( $params, '', '&', PHP_QUERY_RFC3986 ); } /** * The sitemap detector. */ private function detector(): AgentMySite_Sitemap_Detector { if ( null === $this->detector ) { $this->detector = AgentMySite_Sitemap_Detector::from_wordpress(); } return $this->detector; } /** * The WooCommerce catalog detector. */ private function catalog_detector(): AgentMySite_Catalog_Detector { if ( null === $this->catalog_detector ) { $this->catalog_detector = AgentMySite_Catalog_Detector::from_wordpress(); } return $this->catalog_detector; } }