diff --git a/includes/class-convertkit-output.php b/includes/class-convertkit-output.php index 80e090eb..500a6156 100644 --- a/includes/class-convertkit-output.php +++ b/includes/class-convertkit-output.php @@ -391,19 +391,22 @@ public function append_form_to_content( $content ) { */ private function inject_form_after_element( $content, $tag, $index, $form ) { + // Define the meta tag. + $meta_tag = ''; + // Wrap content in , and tags now, so we can inject the UTF-8 Content-Type meta tag. - $content = '' . $content . ''; + $modified_content = '' . $content . ''; // Forcibly tell DOMDocument that this HTML uses the UTF-8 charset. // isn't enough, as DOMDocument still interprets the HTML as ISO-8859, which breaks character encoding // Use of mb_convert_encoding() with HTML-ENTITIES is deprecated in PHP 8.2, so we have to use this method. // If we don't, special characters render incorrectly. - $content = str_replace( '', '' . "\n" . '', $content ); + $modified_content = str_replace( '', '' . "\n" . $meta_tag, $modified_content ); // Load Page / Post content into DOMDocument. libxml_use_internal_errors( true ); $html = new DOMDocument(); - $html->loadHTML( $content, LIBXML_HTML_NODEFDTD ); + $html->loadHTML( $modified_content, LIBXML_HTML_NODEFDTD ); // Find the element to append the form to. // item() is a zero based index. @@ -411,7 +414,7 @@ private function inject_form_after_element( $content, $tag, $index, $form ) { // If the element could not be found, either the number of elements by tag name is less // than the requested position the form be inserted in, or no element exists. - // Append the form to the content and return. + // Append the form to the original content and return. if ( is_null( $element_node ) ) { return $content . $form; } @@ -424,19 +427,20 @@ private function inject_form_after_element( $content, $tag, $index, $form ) { $element_node->parentNode->insertBefore( $html->importNode( $form_node->documentElement, true ), $element_node->nextSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase // Fetch HTML string. - $content = $html->saveHTML(); + $modified_content = $html->saveHTML(); // Remove some HTML tags that DOMDocument adds, returning the output. // We do this instead of using LIBXML_HTML_NOIMPLIED in loadHTML(), because Legacy Forms are not always contained in // a single root / outer element, which is required for LIBXML_HTML_NOIMPLIED to correctly work. - $content = str_replace( '', '', $content ); - $content = str_replace( '', '', $content ); - $content = str_replace( '', '', $content ); - $content = str_replace( '', '', $content ); - $content = str_replace( '', '', $content ); - $content = str_replace( '', '', $content ); - - return $content; + $modified_content = str_replace( '', '', $modified_content ); + $modified_content = str_replace( '', '', $modified_content ); + $modified_content = str_replace( '', '', $modified_content ); + $modified_content = str_replace( '', '', $modified_content ); + $modified_content = str_replace( '', '', $modified_content ); + $modified_content = str_replace( '', '', $modified_content ); + $modified_content = str_replace( $meta_tag, '', $modified_content ); + + return $modified_content; } diff --git a/tests/acceptance/forms/post-types/CPTFormCest.php b/tests/acceptance/forms/post-types/CPTFormCest.php index 0a5b968c..e857ab33 100644 --- a/tests/acceptance/forms/post-types/CPTFormCest.php +++ b/tests/acceptance/forms/post-types/CPTFormCest.php @@ -317,6 +317,9 @@ public function testAddNewCPTUsingDefaultFormAfterParagraphElement(AcceptanceTes // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -356,6 +359,9 @@ public function testAddNewCPTUsingDefaultFormAfterHeadingElement(AcceptanceTeste // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -395,6 +401,9 @@ public function testAddNewCPTUsingDefaultFormAfterImageElement(AcceptanceTester // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -435,6 +444,9 @@ public function testAddNewCPTUsingDefaultFormAfterOutOfBoundsElement(AcceptanceT // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** diff --git a/tests/acceptance/forms/post-types/PageFormCest.php b/tests/acceptance/forms/post-types/PageFormCest.php index 48e461c2..9642b923 100644 --- a/tests/acceptance/forms/post-types/PageFormCest.php +++ b/tests/acceptance/forms/post-types/PageFormCest.php @@ -241,6 +241,9 @@ public function testAddNewPageUsingDefaultFormAfterParagraphElement(AcceptanceTe // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -280,6 +283,9 @@ public function testAddNewPageUsingDefaultFormAfterHeadingElement(AcceptanceTest // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -319,6 +325,12 @@ public function testAddNewPageUsingDefaultFormAfterImageElement(AcceptanceTester // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -359,6 +371,9 @@ public function testAddNewPageUsingDefaultFormAfterOutOfBoundsElement(Acceptance // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** diff --git a/tests/acceptance/forms/post-types/PostFormCest.php b/tests/acceptance/forms/post-types/PostFormCest.php index 073f556a..f710ec1d 100644 --- a/tests/acceptance/forms/post-types/PostFormCest.php +++ b/tests/acceptance/forms/post-types/PostFormCest.php @@ -240,6 +240,9 @@ public function testAddNewPostUsingDefaultFormAfterParagraphElement(AcceptanceTe // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -279,6 +282,9 @@ public function testAddNewPostUsingDefaultFormAfterHeadingElement(AcceptanceTest // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -318,6 +324,9 @@ public function testAddNewPostUsingDefaultFormAfterImageElement(AcceptanceTester // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /** @@ -358,6 +367,9 @@ public function testAddNewPostUsingDefaultFormAfterOutOfBoundsElement(Acceptance // Confirm character encoding is not broken due to using DOMDocument. $I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium'); + + // Confirm no meta tag exists within the content. + $I->dontSeeInSource(''); } /**