Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix character encoding when inserting Form after specific HTML element #745

Merged
9 changes: 9 additions & 0 deletions includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ public function append_form_to_content( $content ) {
*/
private function inject_form_after_element( $content, $tag, $index, $form ) {

// Wrap content in <html>, <head> and <body> tags now, so we can inject the UTF-8 Content-Type meta tag.
$content = '<html><head></head><body>' . $content . '</body></html>';

// Forcibly tell DOMDocument that this HTML uses the UTF-8 charset.
// <meta charset="utf-8"> 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( '<head>', '<head>' . "\n" . '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">', $content );

// Load Page / Post content into DOMDocument.
libxml_use_internal_errors( true );
$html = new DOMDocument();
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Helper/Acceptance/WPGutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public function addGutenbergPageToDatabase($I, $postType = 'page', $title = 'Gut
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Item #2</p>
<p>Item #2: Adhaésionés altéram improbis mi pariendarum sit stulti triarium</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->

Expand Down
12 changes: 12 additions & 0 deletions tests/acceptance/forms/post-types/CPTFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ public function testAddNewCPTUsingDefaultFormAfterParagraphElement(AcceptanceTes

// Confirm that one ConvertKit Form is output in the DOM after the third paragraph.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'p', 3);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -350,6 +353,9 @@ public function testAddNewCPTUsingDefaultFormAfterHeadingElement(AcceptanceTeste

// Confirm that one ConvertKit Form is output in the DOM after the second <h2> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'h2', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -386,6 +392,9 @@ public function testAddNewCPTUsingDefaultFormAfterImageElement(AcceptanceTester

// Confirm that one ConvertKit Form is output in the DOM after the second <img> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'img', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -423,6 +432,9 @@ public function testAddNewCPTUsingDefaultFormAfterOutOfBoundsElement(AcceptanceT
// Confirm that one ConvertKit Form is output in the DOM after the content, as
// the number of paragraphs is less than the position.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_content');

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down
48 changes: 48 additions & 0 deletions tests/acceptance/forms/post-types/PageFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,48 @@ public function testAddNewPageUsingDefaultFormAfterParagraphElement(AcceptanceTe

// Confirm that one ConvertKit Form is output in the DOM after the third paragraph.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'p', 3);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
* Test that the Default Form specified in the Plugin Settings works when
* creating and viewing a new WordPress Page, and its position is set
* to after the 2nd <h2> element.
*
* @since 2.6.6
*
* @param AcceptanceTester $I Tester.
*/
public function testAddNewPageUsingDefaultFormAfterHeadingElement(AcceptanceTester $I)
{
// Setup ConvertKit plugin with Default Form for Pages set to be output after the 2nd <h2> of content.
$I->setupConvertKitPlugin(
$I,
[
'page_form' => $_ENV['CONVERTKIT_API_FORM_ID'],
'page_form_position' => 'after_element',
'page_form_position_element' => 'h2',
'page_form_position_element_index' => 2,
]
);
$I->setupConvertKitPluginResources($I);

// Setup Page with placeholder content.
$pageID = $I->addGutenbergPageToDatabase($I, 'page', 'Kit: Page: Form: Default: After 2nd H2 Element');

// View the Page on the frontend site.
$I->amOnPage('?p=' . $pageID);

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Confirm that one ConvertKit Form is output in the DOM after the second <h2> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'h2', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -274,6 +316,9 @@ public function testAddNewPageUsingDefaultFormAfterImageElement(AcceptanceTester

// Confirm that one ConvertKit Form is output in the DOM after the second <img> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'img', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -311,6 +356,9 @@ public function testAddNewPageUsingDefaultFormAfterOutOfBoundsElement(Acceptance
// Confirm that one ConvertKit Form is output in the DOM after the content, as
// the number of paragraphs is less than the position.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_content');

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/acceptance/forms/post-types/PostFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ public function testAddNewPostUsingDefaultFormAfterParagraphElement(AcceptanceTe

// Confirm that one ConvertKit Form is output in the DOM after the third paragraph.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'p', 3);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -273,6 +276,9 @@ public function testAddNewPostUsingDefaultFormAfterHeadingElement(AcceptanceTest

// Confirm that one ConvertKit Form is output in the DOM after the second <h2> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'h2', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -309,6 +315,9 @@ public function testAddNewPostUsingDefaultFormAfterImageElement(AcceptanceTester

// Confirm that one ConvertKit Form is output in the DOM after the second <img> element.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_element', 'img', 2);

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down Expand Up @@ -346,6 +355,9 @@ public function testAddNewPostUsingDefaultFormAfterOutOfBoundsElement(Acceptance
// Confirm that one ConvertKit Form is output in the DOM after the content, as
// the number of paragraphs is less than the position.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID'], 'after_content');

// Confirm character encoding is not broken due to using DOMDocument.
$I->seeInSource('Adhaésionés altéram improbis mi pariendarum sit stulti triarium');
}

/**
Expand Down