diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-button.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-button.php index 2f6fd1e883..420cfd720c 100644 --- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-button.php +++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-button.php @@ -86,14 +86,14 @@ class Button extends Abstract_Block_Renderer { } $dom_helper = new Dom_Document_Helper( $parsed_block['innerHTML'] ); - $block_classname = $dom_helper->getAttributeValueByTagName( 'div', 'class' ) ?? ''; - $button_link = $dom_helper->findElement( 'a' ); + $block_classname = $dom_helper->get_attribute_value_by_tag_name( 'div', 'class' ) ?? ''; + $button_link = $dom_helper->find_element( 'a' ); if ( ! $button_link ) { return ''; } - $button_text = $dom_helper->getElementInnerHTML( $button_link ) ? $dom_helper->getElementInnerHTML( $button_link ) : ''; + $button_text = $dom_helper->get_element_inner_html( $button_link ) ? $dom_helper->get_element_inner_html( $button_link ) : ''; $button_url = $button_link->getAttribute( 'href' ) ? $button_link->getAttribute( 'href' ) : '#'; $block_attributes = wp_parse_args( diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php index 9556990bca..80e7af888b 100644 --- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php +++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-column.php @@ -56,7 +56,7 @@ class Column extends Abstract_Block_Renderer { * @param Settings_Controller $settings_controller Settings controller. */ private function get_block_wrapper( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string { - $original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->getAttributeValueByTagName( 'div', 'class' ) ?? ''; + $original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->get_attribute_value_by_tag_name( 'div', 'class' ) ?? ''; $block_attributes = wp_parse_args( $parsed_block['attrs'] ?? array(), array( diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-columns.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-columns.php index 65a76132c6..e9c2ebc248 100644 --- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-columns.php +++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-columns.php @@ -45,7 +45,7 @@ class Columns extends Abstract_Block_Renderer { * @param Settings_Controller $settings_controller Settings controller. */ private function getBlockWrapper( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string { - $original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->getAttributeValueByTagName( 'div', 'class' ) ?? ''; + $original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->get_attribute_value_by_tag_name( 'div', 'class' ) ?? ''; $block_attributes = wp_parse_args( $parsed_block['attrs'] ?? array(), array( diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-group.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-group.php index 2c9e65f459..e1ef8c860d 100644 --- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-group.php +++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-group.php @@ -47,7 +47,7 @@ class Group extends Abstract_Block_Renderer { * @param Settings_Controller $settings_controller Settings controller. */ private function get_block_wrapper( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string { - $original_classname = ( new Dom_Document_Helper( $block_content ) )->getAttributeValueByTagName( 'div', 'class' ) ?? ''; + $original_classname = ( new Dom_Document_Helper( $block_content ) )->get_attribute_value_by_tag_name( 'div', 'class' ) ?? ''; $block_attributes = wp_parse_args( $parsed_block['attrs'] ?? array(), array( diff --git a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php index 74367c78da..54ce278bb3 100644 --- a/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php +++ b/packages/php/email-editor/src/Integrations/Core/Renderer/Blocks/class-image.php @@ -320,21 +320,21 @@ class Image extends Abstract_Block_Renderer { $dom_helper = new Dom_Document_Helper( $block_content ); - $figure_tag = $dom_helper->findElement( 'figure' ); + $figure_tag = $dom_helper->find_element( 'figure' ); if ( ! $figure_tag ) { return null; } - $img_tag = $dom_helper->findElement( 'img' ); + $img_tag = $dom_helper->find_element( 'img' ); if ( ! $img_tag ) { return null; } - $image_src = $dom_helper->getAttributeValue( $img_tag, 'src' ); - $image_class = $dom_helper->getAttributeValue( $img_tag, 'class' ); - $image_html = $dom_helper->getOuterHtml( $img_tag ); - $figcaption = $dom_helper->findElement( 'figcaption' ); - $figcaption_html = $figcaption ? $dom_helper->getOuterHtml( $figcaption ) : ''; + $image_src = $dom_helper->get_attribute_value( $img_tag, 'src' ); + $image_class = $dom_helper->get_attribute_value( $img_tag, 'class' ); + $image_html = $dom_helper->get_outer_html( $img_tag ); + $figcaption = $dom_helper->find_element( 'figcaption' ); + $figcaption_html = $figcaption ? $dom_helper->get_outer_html( $figcaption ) : ''; $figcaption_html = str_replace( array( '' ), array( '' ), $figcaption_html ); return array( diff --git a/packages/php/email-editor/src/Integrations/Utils/class-dom-document-helper.php b/packages/php/email-editor/src/Integrations/Utils/class-dom-document-helper.php index 5fd8aa9802..47271d8a3f 100644 --- a/packages/php/email-editor/src/Integrations/Utils/class-dom-document-helper.php +++ b/packages/php/email-editor/src/Integrations/Utils/class-dom-document-helper.php @@ -1,62 +1,105 @@ -loadHtml( $htmlContent ); + /** + * Constructor. + * + * @param string $html_content The HTML content to load. + */ + public function __construct( string $html_content ) { + $this->load_html( $html_content ); } - private function loadHtml( string $htmlContent ): void { + /** + * Loads the given HTML content into the DOMDocument. + * + * @param string $html_content The HTML content to load. + */ + private function load_html( string $html_content ): void { libxml_use_internal_errors( true ); $this->dom = new \DOMDocument(); - if ( ! empty( $htmlContent ) ) { - // prefixing the content with the XML declaration to force the input encoding to UTF-8 - $this->dom->loadHTML( '' . $htmlContent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); + if ( ! empty( $html_content ) ) { + // prefixing the content with the XML declaration to force the input encoding to UTF-8. + $this->dom->loadHTML( '' . $html_content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); } libxml_clear_errors(); } - public function findElement( string $tagName ): ?\DOMElement { - $elements = $this->dom->getElementsByTagName( $tagName ); - return $elements->item( 0 ) ?: null; + /** + * Searches for the first appearance of the given tag name. + * + * @param string $tag_name The tag name to search for. + */ + public function find_element( string $tag_name ): ?\DOMElement { + $elements = $this->dom->getElementsByTagName( $tag_name ); + return $elements->item( 0 ) ? $elements->item( 0 ) : null; } - public function getAttributeValue( \DOMElement $element, string $attribute ): string { + /** + * Returns the value of the given attribute from the given element. + * + * @param \DOMElement $element The element to get the attribute value from. + * @param string $attribute The attribute to get the value from. + */ + public function get_attribute_value( \DOMElement $element, string $attribute ): string { return $element->hasAttribute( $attribute ) ? $element->getAttribute( $attribute ) : ''; } /** * Searches for the first appearance of the given tag name and returns the value of specified attribute. + * + * @param string $tag_name The tag name to search for. + * @param string $attribute The attribute to get the value from. */ - public function getAttributeValueByTagName( string $tagName, string $attribute ): ?string { - $element = $this->findElement( $tagName ); + public function get_attribute_value_by_tag_name( string $tag_name, string $attribute ): ?string { + $element = $this->find_element( $tag_name ); if ( ! $element ) { return null; } - return $this->getAttributeValue( $element, $attribute ); + return $this->get_attribute_value( $element, $attribute ); } - public function getOuterHtml( \DOMElement $element ): string { + /** + * Returns the outer HTML of the given element. + * + * @param \DOMElement $element The element to get the outer HTML from. + */ + public function get_outer_html( \DOMElement $element ): string { return (string) $this->dom->saveHTML( $element ); } - public function getElementInnerHTML( \DOMElement $element ): string { - $innerHTML = ''; - $children = $element->childNodes; + /** + * Returns the inner HTML of the given element. + * + * @param \DOMElement $element The element to get the inner HTML from. + */ + public function get_element_inner_html( \DOMElement $element ): string { + $inner_html = ''; + $children = $element->childNodes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase foreach ( $children as $child ) { if ( ! $child instanceof \DOMNode ) { continue; } - $innerHTML .= $this->dom->saveHTML( $child ); + $inner_html .= $this->dom->saveHTML( $child ); } - return $innerHTML; + return $inner_html; } } diff --git a/packages/php/email-editor/tests/unit/Integrations/Utils/Dom_Document_Helper_Test.php b/packages/php/email-editor/tests/unit/Integrations/Utils/Dom_Document_Helper_Test.php index a4b875b27e..498cbb8d16 100644 --- a/packages/php/email-editor/tests/unit/Integrations/Utils/Dom_Document_Helper_Test.php +++ b/packages/php/email-editor/tests/unit/Integrations/Utils/Dom_Document_Helper_Test.php @@ -6,8 +6,8 @@ class Dom_Document_Helper_Test extends \MailPoetUnitTest { public function testItFindsElement(): void { $html = '

Some text

'; $domDocumentHelper = new Dom_Document_Helper($html); - $element = $domDocumentHelper->findElement('p'); - $empty = $domDocumentHelper->findElement('span'); + $element = $domDocumentHelper->find_element('p'); + $empty = $domDocumentHelper->find_element('span'); $this->assertInstanceOf(\DOMElement::class, $element); $this->assertEquals('p', $element->tagName); $this->assertNull($empty); @@ -16,30 +16,30 @@ class Dom_Document_Helper_Test extends \MailPoetUnitTest { public function testItGetsAttributeValue(): void { $html = '

Some text

'; $domDocumentHelper = new Dom_Document_Helper($html); - $element = $domDocumentHelper->findElement('p'); + $element = $domDocumentHelper->find_element('p'); $this->assertInstanceOf(\DOMElement::class, $element); - $this->assertEquals('some-class', $domDocumentHelper->getAttributeValue($element, 'class')); + $this->assertEquals('some-class', $domDocumentHelper->get_attribute_value($element, 'class')); } public function testItGetsOuterHtml(): void { $html = '
Some text
'; $domDocumentHelper = new Dom_Document_Helper($html); - $element = $domDocumentHelper->findElement('span'); + $element = $domDocumentHelper->find_element('span'); $this->assertInstanceOf(\DOMElement::class, $element); - $this->assertEquals('Some text', $domDocumentHelper->getOuterHtml($element)); + $this->assertEquals('Some text', $domDocumentHelper->get_outer_html($element)); // testings encoding of special characters $html = '
'; $domDocumentHelper = new Dom_Document_Helper($html); - $element = $domDocumentHelper->findElement('img'); + $element = $domDocumentHelper->find_element('img'); $this->assertInstanceOf(\DOMElement::class, $element); - $this->assertEquals('', $domDocumentHelper->getOuterHtml($element)); + $this->assertEquals('', $domDocumentHelper->get_outer_html($element)); } public function testItGetsAttributeValueByTagName(): void { $html = '

Some text

'; $domDocumentHelper = new Dom_Document_Helper($html); - $this->assertEquals('some-class', $domDocumentHelper->getAttributeValueByTagName('p', 'class')); - $this->assertNull($domDocumentHelper->getAttributeValueByTagName('span', 'class')); + $this->assertEquals('some-class', $domDocumentHelper->get_attribute_value_by_tag_name('p', 'class')); + $this->assertNull($domDocumentHelper->get_attribute_value_by_tag_name('span', 'class')); } }