mailpoet
/
vendor
/
woocommerce
/
email-editor
/
src
/
Integrations
/
Utils
/
class-dom-document-helper.php
class-dom-document-helper.php
2 weeks ago
class-html-processing-helper.php
2 weeks ago
class-social-links-helper.php
11 months ago
class-styles-helper.php
6 months ago
class-table-wrapper-helper.php
11 months ago
index.php
11 months ago
class-dom-document-helper.php
73 lines
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Integrations\Utils; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | class Dom_Document_Helper { |
| 6 | private \DOMDocument $dom; |
| 7 | public function __construct( string $html_content ) { |
| 8 | $this->load_html( $html_content ); |
| 9 | } |
| 10 | private function load_html( string $html_content ): void { |
| 11 | libxml_use_internal_errors( true ); |
| 12 | $this->dom = new \DOMDocument(); |
| 13 | if ( ! empty( $html_content ) ) { |
| 14 | // prefixing the content with the XML declaration to force the input encoding to UTF-8. |
| 15 | $this->dom->loadHTML( '<?xml encoding="UTF-8">' . $html_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); |
| 16 | } |
| 17 | libxml_clear_errors(); |
| 18 | } |
| 19 | public function find_element( string $tag_name ): ?\DOMElement { |
| 20 | $elements = $this->dom->getElementsByTagName( $tag_name ); |
| 21 | return $elements->item( 0 ) ? $elements->item( 0 ) : null; |
| 22 | } |
| 23 | public function find_elements( string $tag_name ): array { |
| 24 | $elements = array(); |
| 25 | foreach ( $this->dom->getElementsByTagName( $tag_name ) as $element ) { |
| 26 | if ( $element instanceof \DOMElement ) { |
| 27 | $elements[] = $element; |
| 28 | } |
| 29 | } |
| 30 | return $elements; |
| 31 | } |
| 32 | public function get_attribute_value( \DOMElement $element, string $attribute ): string { |
| 33 | return $element->hasAttribute( $attribute ) ? $element->getAttribute( $attribute ) : ''; |
| 34 | } |
| 35 | public function get_attribute_value_by_tag_name( string $tag_name, string $attribute ): ?string { |
| 36 | $element = $this->find_element( $tag_name ); |
| 37 | if ( ! $element ) { |
| 38 | return null; |
| 39 | } |
| 40 | return $this->get_attribute_value( $element, $attribute ); |
| 41 | } |
| 42 | public function get_outer_html( \DOMElement $element ): string { |
| 43 | return (string) $this->dom->saveHTML( $element ); |
| 44 | } |
| 45 | public function remove_element( \DOMElement $element ): void { |
| 46 | $parent = $element->parentNode; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 47 | if ( $parent instanceof \DOMNode ) { |
| 48 | $parent->removeChild( $element ); |
| 49 | } |
| 50 | } |
| 51 | public function get_root_html(): string { |
| 52 | $html = ''; |
| 53 | foreach ( $this->dom->childNodes as $child ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 54 | // Skip the `<?xml encoding="UTF-8">` processing instruction that load_html() prepends to |
| 55 | // force UTF-8; serializing it back would corrupt callers (e.g. strip_tags treats the |
| 56 | // unterminated `<?` as a tag and swallows the rest of the string). |
| 57 | if ( XML_PI_NODE === $child->nodeType ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 58 | continue; |
| 59 | } |
| 60 | $html .= (string) $this->dom->saveHTML( $child ); |
| 61 | } |
| 62 | return $html; |
| 63 | } |
| 64 | public function get_element_inner_html( \DOMElement $element ): string { |
| 65 | $inner_html = ''; |
| 66 | $children = $element->childNodes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 67 | foreach ( $children as $child ) { |
| 68 | $inner_html .= $this->dom->saveHTML( $child ); |
| 69 | } |
| 70 | return $inner_html; |
| 71 | } |
| 72 | } |
| 73 |