diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index b82857644e8..90600181b80 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1536,10 +1536,10 @@ public static function filter_admin_bar_script_loader_tag( $tag, $handle ) { * @todo All of this might be better placed inside of a sanitizer. * @todo Consider removing any scripts that are not among the $script_handles. * - * @param DOMDocument $dom Document. - * @param string[] $script_handles AMP script handles for components identified during output buffering. + * @param AMP_DOM_Document $dom Document. + * @param string[] $script_handles AMP script handles for components identified during output buffering. */ - public static function ensure_required_markup( DOMDocument $dom, $script_handles = [] ) { + public static function ensure_required_markup( AMP_DOM_Document $dom, $script_handles = [] ) { /** * Elements. * @@ -1550,18 +1550,14 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles * @var DOMElement $noscript */ - $xpath = new DOMXPath( $dom ); - - $head = $dom->getElementsByTagName( 'head' )->item( 0 ); - // Ensure there is a schema.org script in the document. // @todo Consider applying the amp_schemaorg_metadata filter on the contents when a script is already present. - $schema_org_meta_script = $xpath->query( '//script[ @type = "application/ld+json" ][ contains( ./text(), "schema.org" ) ]' )->item( 0 ); + $schema_org_meta_script = $dom->xpath->query( '//script[ @type = "application/ld+json" ][ contains( ./text(), "schema.org" ) ]' )->item( 0 ); if ( ! $schema_org_meta_script ) { $script = $dom->createElement( 'script' ); $script->setAttribute( 'type', 'application/ld+json' ); $script->appendChild( $dom->createTextNode( wp_json_encode( amp_get_schemaorg_metadata(), JSON_UNESCAPED_UNICODE ) ) ); - $head->appendChild( $script ); + $dom->head->appendChild( $script ); } // Gather all links. @@ -1578,7 +1574,7 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles ), ], ]; - $link_elements = $head->getElementsByTagName( 'link' ); + $link_elements = $dom->head->getElementsByTagName( 'link' ); foreach ( $link_elements as $link ) { if ( $link->hasAttribute( 'rel' ) ) { $links[ $link->getAttribute( 'rel' ) ][] = $link; @@ -1596,18 +1592,18 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles 'href' => self::get_current_canonical_url(), ] ); - $head->appendChild( $rel_canonical ); + $dom->head->appendChild( $rel_canonical ); } // Store the last meta tag as the previous node to append to. - $meta_tags = $head->getElementsByTagName( 'meta' ); - $previous_node = $meta_tags->length > 0 ? $meta_tags->item( $meta_tags->length - 1 ) : $head->firstChild; + $meta_tags = $dom->head->getElementsByTagName( 'meta' ); + $previous_node = $meta_tags->length > 0 ? $meta_tags->item( $meta_tags->length - 1 ) : $dom->head->firstChild; // Handle the title. - $title = $head->getElementsByTagName( 'title' )->item( 0 ); + $title = $dom->head->getElementsByTagName( 'title' )->item( 0 ); if ( $title ) { $title->parentNode->removeChild( $title ); // So we can move it. - $head->insertBefore( $title, $previous_node->nextSibling ); + $dom->head->insertBefore( $title, $previous_node->nextSibling ); $previous_node = $title; } @@ -1623,7 +1619,7 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles $ordered_scripts = []; $head_scripts = []; $runtime_src = wp_scripts()->registered['amp-runtime']->src; - foreach ( $head->getElementsByTagName( 'script' ) as $script ) { // Note that prepare_response() already moved body scripts to head. + foreach ( $dom->head->getElementsByTagName( 'script' ) as $script ) { // Note that prepare_response() already moved body scripts to head. $head_scripts[] = $script; } foreach ( $head_scripts as $script ) { @@ -1719,7 +1715,7 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles if ( $link->parentNode ) { $link->parentNode->removeChild( $link ); // So we can move it. } - $head->insertBefore( $link, $previous_node->nextSibling ); + $dom->head->insertBefore( $link, $previous_node->nextSibling ); $previous_node = $link; } } @@ -1753,25 +1749,25 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles */ $ordered_scripts = array_merge( $ordered_scripts, $amp_scripts ); foreach ( $ordered_scripts as $ordered_script ) { - $head->insertBefore( $ordered_script, $previous_node->nextSibling ); + $dom->insertBefore( $ordered_script, $previous_node->nextSibling ); $previous_node = $ordered_script; } /* * "8. Specify any custom styles by using the