'site-url' ] ); throw new Exception( __( 'Invalid site URL.', 'google-listings-and-ads' ) ); } // Retrieve the meta tag with verification token. try { $meta_tag = $this->get_token( $site_url ); } catch ( Exception $e ) { do_action( 'woocommerce_gla_site_verify_failure', [ 'step' => 'token' ] ); throw $e; } // Store the meta tag in the options table and mark as unverified. $site_verification_options = [ 'verified' => self::VERIFICATION_STATUS_UNVERIFIED, 'meta_tag' => $meta_tag, ]; $this->options->update( OptionsInterface::SITE_VERIFICATION, $site_verification_options ); // Attempt verification. try { $this->insert( $site_url ); $site_verification_options['verified'] = self::VERIFICATION_STATUS_VERIFIED; $this->options->update( OptionsInterface::SITE_VERIFICATION, $site_verification_options ); do_action( 'woocommerce_gla_site_verify_success', [] ); } catch ( Exception $e ) { do_action( 'woocommerce_gla_site_verify_failure', [ 'step' => 'meta-tag' ] ); throw $e; } } /** * Get the META token for site verification. * https://developers.google.com/site-verification/v1/webResource/getToken * * @param string $identifier The URL of the site to verify (including protocol). * * @return string The meta tag to be used for verification. * @throws ExceptionWithResponseData When unable to retrieve meta token. */ protected function get_token( string $identifier ): string { /** @var SiteVerificationService $service */ $service = $this->container->get( SiteVerificationService::class ); $post_body = new GetTokenRequest( [ 'verificationMethod' => self::VERIFICATION_METHOD, 'site' => new GetTokenRequestSite( [ 'type' => 'SITE', 'identifier' => $identifier, ] ), ] ); try { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $response = $service->webResource->getToken( $post_body ); } catch ( GoogleServiceException $e ) { do_action( 'woocommerce_gla_sv_client_exception', $e, __METHOD__ ); $errors = $this->get_exception_errors( $e ); throw new ExceptionWithResponseData( /* translators: %s Error message */ sprintf( __( 'Unable to retrieve site verification token: %s', 'google-listings-and-ads' ), reset( $errors ) ), $e->getCode(), null, [ 'errors' => $errors ] ); } return $response->getToken(); } /** * Instructs the Google Site Verification API to verify site ownership * using the META method. * * @param string $identifier The URL of the site to verify (including protocol). * * @throws ExceptionWithResponseData When unable to verify token. */ protected function insert( string $identifier ) { /** @var SiteVerificationService $service */ $service = $this->container->get( SiteVerificationService::class ); $post_body = new WebResource( [ 'site' => new WebResourceSite( [ 'type' => 'SITE', 'identifier' => $identifier, ] ), ] ); try { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $service->webResource->insert( self::VERIFICATION_METHOD, $post_body ); } catch ( GoogleServiceException $e ) { do_action( 'woocommerce_gla_sv_client_exception', $e, __METHOD__ ); $errors = $this->get_exception_errors( $e ); throw new ExceptionWithResponseData( /* translators: %s Error message */ sprintf( __( 'Unable to insert site verification: %s', 'google-listings-and-ads' ), reset( $errors ) ), $e->getCode(), null, [ 'errors' => $errors ] ); } } }