Skip to content

Commit 5c4f94e

Browse files
authored
Merge pull request #102 from Kit/use-domdocument-body-html
Use DOMDocument for returning <body> HTML
2 parents ad41275 + f1253ce commit 5c4f94e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/class-convertkit-api-traits.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,9 @@ public function convert_script_type(\DOMNodeList $elements)
17731773
*/
17741774
public function strip_html_head_body_tags(string $markup)
17751775
{
1776+
// Mark as deprecated in 2.1.0.
1777+
_deprecated_function( __FUNCTION__, '2.1.0', 'get_body_html()' );
1778+
17761779
$markup = str_replace('<html>', '', $markup);
17771780
$markup = str_replace('</html>', '', $markup);
17781781
$markup = str_replace('<head>', '', $markup);
@@ -1784,6 +1787,28 @@ public function strip_html_head_body_tags(string $markup)
17841787
return $markup;
17851788
}
17861789

1790+
/**
1791+
* Returns the HTML within the DOMDocument's <body> tag as a string.
1792+
*
1793+
* @param \DOMDocument $dom DOM Document.
1794+
*
1795+
* @since 2.1.0
1796+
*
1797+
* @return string
1798+
*/
1799+
public function get_body_html(\DOMDocument $dom) {
1800+
1801+
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
1802+
1803+
$html = '';
1804+
foreach ( $body->childNodes as $child ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
1805+
$html .= $dom->saveHTML( $child );
1806+
}
1807+
1808+
return $html;
1809+
1810+
}
1811+
17871812
/**
17881813
* Adds total count and pagination parameters to the given array of existing API parameters.
17891814
*

src/class-convertkit-api-v4.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,10 +1251,8 @@ public function get_html( $url, $body_only = true ) {
12511251
return $html->saveHTML();
12521252
}
12531253

1254-
// Remove some HTML tags that DOMDocument adds, returning the output.
1255-
// We do this instead of using LIBXML_HTML_NOIMPLIED in loadHTML(), because Legacy Forms are not always contained in
1256-
// a single root / outer element, which is required for LIBXML_HTML_NOIMPLIED to correctly work.
1257-
return $this->strip_html_head_body_tags( $html->saveHTML() );
1254+
// Return the HTML within the DOMDocument's <body> tag as a string.
1255+
return $this->get_body_html( $html );
12581256

12591257
}
12601258

0 commit comments

Comments
 (0)