client = $client; $this->results = $results; } public function register_hooks(): void { add_action( 'admin_post_two_ua_release_guard_handoff', array( $this, 'open' ) ); add_filter( 'allowed_redirect_hosts', array( $this, 'allow_2ua_host' ) ); } public function open(): void { if ( ! Capabilities::can_manage() ) { wp_die( esc_html__( 'You are not allowed to open this 2UA result.', '2ua-seo-release-guard' ) ); } check_admin_referer( 'two_ua_release_guard_handoff' ); $run_id = $this->request_value( 'run_public_id' ); $purpose = $this->request_value( 'purpose' ); if ( ! $this->valid_request( $run_id, $purpose ) ) { wp_die( esc_html__( 'The 2UA result link is invalid.', '2ua-seo-release-guard' ) ); } try { $handoff = $this->client->handoff( $run_id, $purpose ); } catch ( ApiException $exception ) { $this->results->mark_error( $exception->error_code() ); wp_safe_redirect( admin_url( 'options-general.php?page=two-ua-seo-release-guard' ) ); exit; } wp_safe_redirect( $handoff->url ); exit; } /** @param list $hosts */ public function allow_2ua_host( array $hosts ): array { $hosts[] = '2-ua.com'; return array_values( array_unique( $hosts ) ); } private function valid_request( string $run_id, string $purpose ): bool { $summary = $this->results->last(); return $summary instanceof RunSummary && hash_equals( $summary->state->public_id, $run_id ) && in_array( $purpose, self::PURPOSES, true ); } private function request_value( string $key ): string { $value = filter_input( INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS ); return is_string( $value ) ? $value : ''; } }