configuration = $configuration; } /** * Apply transformations to the provided DOM document. * * @param Document $document DOM document to apply the transformations to. * @param ErrorCollection $errors Collection of errors that are collected during transformation. * @return void */ public function transform( Document $document, ErrorCollection $errors ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable // @todo How should we handle an existing schema.org script? $schema_org_meta_script = $document->xpath->query( self::SCHEMA_ORG_XPATH )->item( 0 ); if ( $schema_org_meta_script ) { return; } $metadata = $this->configuration->get( AmpSchemaOrgMetadataConfiguration::METADATA ); if ( ! $metadata ) { return; } $script = $document->createElement( Tag::SCRIPT ); $script->setAttribute( Attribute::TYPE, Attribute::TYPE_LD_JSON ); $json = wp_json_encode( $metadata, JSON_UNESCAPED_UNICODE ); $script->appendChild( $document->createTextNode( $json ) ); $document->head->appendChild( $script ); } }