args['canonical_url']; $query = $this->dom->xpath->query( '//link[@rel="canonical"]', $this->dom->head ); // Remove any duplicate items first. if ( $query instanceof DOMNodeList && $query->length > 1 ) { for ( $i = 1; $i < $query->length; $i++ ) { $node = $query->item( $i ); if ( $node ) { $this->dom->head->removeChild( $node ); } } } /** * DOMElement * * @var DOMElement|DOMNode $rel_canonical */ $rel_canonical = $query instanceof DOMNodeList ? $query->item( 0 ) : null; if ( ! $rel_canonical instanceof DOMElement ) { $rel_canonical = $this->dom->createElement( Tag::LINK ); if ( $rel_canonical instanceof DOMElement ) { $rel_canonical->setAttribute( Attribute::REL, Attribute::REL_CANONICAL ); $this->dom->head->appendChild( $rel_canonical ); } } if ( $rel_canonical instanceof DOMElement ) { // Ensure link[rel=canonical] has a non-empty href attribute. if ( empty( $rel_canonical->getAttribute( Attribute::HREF ) ) ) { $rel_canonical->setAttribute( Attribute::HREF, (string) $canonical_url ); } } } }