class-dom-document-helper.php
1 year ago
class-html-processing-helper.php
4 months ago
class-social-links-helper.php
1 year ago
class-styles-helper.php
4 months ago
class-table-wrapper-helper.php
11 months ago
class-dom-document-helper.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | namespace Automattic\WooCommerce\EmailEditor\Integrations\Utils; |
| 10 | |
| 11 | /** |
| 12 | * This class should guarantee that our work with the DOMDocument is unified and safe. |
| 13 | */ |
| 14 | class Dom_Document_Helper { |
| 15 | /** |
| 16 | * Instance of the DOMDocument. |
| 17 | * |
| 18 | * @var \DOMDocument |
| 19 | */ |
| 20 | private \DOMDocument $dom; |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @param string $html_content The HTML content to load. |
| 26 | */ |
| 27 | public function __construct( string $html_content ) { |
| 28 | $this->load_html( $html_content ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Loads the given HTML content into the DOMDocument. |
| 33 | * |
| 34 | * @param string $html_content The HTML content to load. |
| 35 | */ |
| 36 | private function load_html( string $html_content ): void { |
| 37 | libxml_use_internal_errors( true ); |
| 38 | $this->dom = new \DOMDocument(); |
| 39 | if ( ! empty( $html_content ) ) { |
| 40 | // prefixing the content with the XML declaration to force the input encoding to UTF-8. |
| 41 | $this->dom->loadHTML( '<?xml encoding="UTF-8">' . $html_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); |
| 42 | } |
| 43 | libxml_clear_errors(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Searches for the first appearance of the given tag name. |
| 48 | * |
| 49 | * @param string $tag_name The tag name to search for. |
| 50 | */ |
| 51 | public function find_element( string $tag_name ): ?\DOMElement { |
| 52 | $elements = $this->dom->getElementsByTagName( $tag_name ); |
| 53 | return $elements->item( 0 ) ? $elements->item( 0 ) : null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the value of the given attribute from the given element. |
| 58 | * |
| 59 | * @param \DOMElement $element The element to get the attribute value from. |
| 60 | * @param string $attribute The attribute to get the value from. |
| 61 | */ |
| 62 | public function get_attribute_value( \DOMElement $element, string $attribute ): string { |
| 63 | return $element->hasAttribute( $attribute ) ? $element->getAttribute( $attribute ) : ''; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Searches for the first appearance of the given tag name and returns the value of specified attribute. |
| 68 | * |
| 69 | * @param string $tag_name The tag name to search for. |
| 70 | * @param string $attribute The attribute to get the value from. |
| 71 | */ |
| 72 | public function get_attribute_value_by_tag_name( string $tag_name, string $attribute ): ?string { |
| 73 | $element = $this->find_element( $tag_name ); |
| 74 | if ( ! $element ) { |
| 75 | return null; |
| 76 | } |
| 77 | return $this->get_attribute_value( $element, $attribute ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the outer HTML of the given element. |
| 82 | * |
| 83 | * @param \DOMElement $element The element to get the outer HTML from. |
| 84 | */ |
| 85 | public function get_outer_html( \DOMElement $element ): string { |
| 86 | return (string) $this->dom->saveHTML( $element ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns the inner HTML of the given element. |
| 91 | * |
| 92 | * @param \DOMElement $element The element to get the inner HTML from. |
| 93 | */ |
| 94 | public function get_element_inner_html( \DOMElement $element ): string { |
| 95 | $inner_html = ''; |
| 96 | $children = $element->childNodes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 97 | foreach ( $children as $child ) { |
| 98 | $inner_html .= $this->dom->saveHTML( $child ); |
| 99 | } |
| 100 | return $inner_html; |
| 101 | } |
| 102 | } |
| 103 |