mailpoet
/
vendor
/
woocommerce
/
email-editor
/
src
/
Integrations
/
Utils
/
class-dom-document-helper.php
class-dom-document-helper.php
1 year ago
class-html-processing-helper.php
6 months ago
class-social-links-helper.php
1 year ago
class-styles-helper.php
6 months ago
class-table-wrapper-helper.php
1 year ago
index.php
1 year ago
class-dom-document-helper.php
45 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 get_attribute_value( \DOMElement $element, string $attribute ): string { |
| 24 | return $element->hasAttribute( $attribute ) ? $element->getAttribute( $attribute ) : ''; |
| 25 | } |
| 26 | public function get_attribute_value_by_tag_name( string $tag_name, string $attribute ): ?string { |
| 27 | $element = $this->find_element( $tag_name ); |
| 28 | if ( ! $element ) { |
| 29 | return null; |
| 30 | } |
| 31 | return $this->get_attribute_value( $element, $attribute ); |
| 32 | } |
| 33 | public function get_outer_html( \DOMElement $element ): string { |
| 34 | return (string) $this->dom->saveHTML( $element ); |
| 35 | } |
| 36 | public function get_element_inner_html( \DOMElement $element ): string { |
| 37 | $inner_html = ''; |
| 38 | $children = $element->childNodes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 39 | foreach ( $children as $child ) { |
| 40 | $inner_html .= $this->dom->saveHTML( $child ); |
| 41 | } |
| 42 | return $inner_html; |
| 43 | } |
| 44 | } |
| 45 |