Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions admin/importers/class-convertkit-admin-importer-aweber.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ class ConvertKit_Admin_Importer_AWeber extends ConvertKit_Admin_Importer {
*/
public $shortcode_id_attribute = 'formid';

/**
* Holds the block name for AWeber forms.
*
* @since 3.1.6
*
* @var string
*/
public $block_name = 'aweber-signupform-block/aweber-shortcode';

/**
* Holds the ID attribute name for AWeber forms.
*
* @since 3.1.6
*
* @var string
*/
public $block_id_attribute = 'selectedShortCode';

/**
* Returns an array of AWeber form IDs and titles.
*
Expand Down
18 changes: 18 additions & 0 deletions admin/importers/class-convertkit-admin-importer-mc4wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ class ConvertKit_Admin_Importer_MC4WP extends ConvertKit_Admin_Importer {
*/
public $shortcode_id_attribute = 'id';

/**
* Holds the block name for MC4WP forms.
*
* @since 3.1.6
*
* @var string
*/
public $block_name = 'mailchimp-for-wp/form';

/**
* Holds the ID attribute name for MC4WP forms.
*
* @since 3.1.6
*
* @var string
*/
public $block_id_attribute = 'id';

/**
* Returns an array of MC4WP form IDs and titles.
*
Expand Down
178 changes: 171 additions & 7 deletions admin/importers/class-convertkit-admin-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,32 @@ abstract class ConvertKit_Admin_Importer {
public $shortcode_name = '';

/**
* Holds the ID attribute name for the third party Form plugin.
* Holds the shortcode ID attribute name for the third party Form plugin.
*
* @since 3.1.0
*
* @var string
*/
public $shortcode_id_attribute = '';

/**
* Holds the block name for the third party Form plugin.
*
* @since 3.1.6
*
* @var string
*/
public $block_name = '';

/**
* Holds the block ID attribute name for the third party Form plugin.
*
* @since 3.1.6
*
* @var string
*/
public $block_id_attribute = '';

/**
* Returns an array of third party form IDs and titles.
*
Expand All @@ -42,7 +60,7 @@ abstract class ConvertKit_Admin_Importer {
abstract public function get_forms();

/**
* Returns an array of post IDs that contain the third partyform shortcode.
* Returns an array of post IDs that contain the third party form block or shortcode.
*
* @since 3.1.5
*
Expand All @@ -52,17 +70,21 @@ public function get_forms_in_posts() {

global $wpdb;

// Search post_content for the third party form shortcode and return array of post IDs.
// Search post_content for the third party form block or shortcode and return array of post IDs.
$results = $wpdb->get_col(
$wpdb->prepare(
"
SELECT ID
FROM {$wpdb->posts}
WHERE post_status = %s
AND post_content LIKE %s
AND (
post_content LIKE %s
OR post_content LIKE %s
)
",
'publish',
'%[' . $this->shortcode_name . '%'
'%[' . $this->shortcode_name . '%',
'%<!-- wp:' . $this->block_name . '%'
)
);

Expand Down Expand Up @@ -101,8 +123,8 @@ public function has_forms_in_posts() {
*
* @since 3.1.0
*
* @param int $third_party_form_id The ID of the third party form.
* @param int $form_id The ID of the Kit form.
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
*/
public function replace_shortcodes_in_posts( $third_party_form_id, $form_id ) {

Expand Down Expand Up @@ -227,4 +249,146 @@ public function get_form_ids_from_content( $content ) {

}

/**
* Replaces the third party form block with the Kit form block.
*
* @since 3.1.6
*
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
*/
public function replace_blocks_in_posts( $third_party_form_id, $form_id ) {

// Get Posts that contain the third party Form Block.
$posts = $this->get_forms_in_posts();

// Bail if no Posts contain the third party Form Block.
if ( empty( $posts ) ) {
return;
}

// Iterate through Posts and replace the third party Form Block with the Kit Form Block.
foreach ( $posts as $post_id ) {
$this->replace_blocks_in_post( $post_id, $third_party_form_id, $form_id );
}

}

/**
* Replaces the third party form block with the Kit form block in the given post.
*
* @since 3.1.6
*
* @param int $post_id Post ID.
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
*/
public function replace_blocks_in_post( $post_id, $third_party_form_id, $form_id ) {

// Get Post content.
$post_content = get_post_field( 'post_content', $post_id );

// Fetch Blocks from Content.
$blocks = parse_blocks( $post_content );

// If a single block was returned with blockName null, this content was not created using the block editor.
if ( count( $blocks ) === 1 && is_null( $blocks[0]['blockName'] ) ) {
return;
}

// Replace the third party Form Block with the Kit Form Block.
$post_content = $this->replace_blocks_in_content( $blocks, $third_party_form_id, $form_id );

// Double escape backslashes so that wp_update_post doesn't remove them.
// When content contains a single backslash (\), wp_update_post will strip it unless we double escape it (\\).
$post_content = str_replace( '\\', '\\\\', $post_content );

// Update the Post content.
wp_update_post(
array(
'ID' => $post_id,
'post_content' => $post_content,
),
false,
false // Don't fire after action hooks.
);

}

/**
* Replaces the third party form block with the Kit form block in the given string.
*
* @since 3.1.6
*
* @param array $blocks Blocks.
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
*
* @return string
*/
public function replace_blocks_in_content( $blocks, $third_party_form_id, $form_id ) {

// Recursively convert blocks.
$blocks = $this->recursively_convert_blocks( $blocks, $third_party_form_id, $form_id );

// Serialize blocks.
return serialize_blocks( $blocks );

}

/**
* Recursively walks through an array of blocks and innerBlocks,
* converting third party form blocks to Kit form blocks.
*
* @since 3.1.6
*
* @param array $blocks Blocks.
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
* @return array
*/
private function recursively_convert_blocks( $blocks, $third_party_form_id, $form_id ) {

foreach ( $blocks as $index => $block ) {
// If this block has inner blocks, walk through the inner blocks.
if ( ! empty( $block['innerBlocks'] ) ) {
$blocks[ $index ]['innerBlocks'] = $this->recursively_convert_blocks( $block['innerBlocks'], $third_party_form_id, $form_id );
}

// Skip if a null block name.
if ( is_null( $block['blockName'] ) ) {
continue;
}

// Skip if not a third party form block.
if ( strpos( $block['blockName'], $this->block_name ) === false ) {
continue;
}

// Skip if the attribute doesn't exist i.e. the block was not configured.
if ( ! array_key_exists( $this->block_id_attribute, $block['attrs'] ) ) {
continue;
}

// Skip if the third party form ID doesn't exist within the third party form block's attribute.
if ( stripos( $block['attrs'][ $this->block_id_attribute ], (string) $third_party_form_id ) === false ) {
continue;
}

// Replace third party form block with Kit form block.
$blocks[ $index ] = array(
'blockName' => 'convertkit/form',
'attrs' => array(
'form' => (string) $form_id,
),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
);
}

return $blocks;

}

}
2 changes: 2 additions & 0 deletions admin/section/class-convertkit-admin-section-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ private function maybe_migrate_aweber_configuration() {

// Iterate through the AWeber Form IDs and replace the shortcodes with the Kit Form Shortcodes.
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_aweber_settings'] ) ) as $aweber_form_id => $kit_form_id ) {
$aweber->replace_blocks_in_posts( (int) $aweber_form_id, (int) $kit_form_id );
$aweber->replace_shortcodes_in_posts( (int) $aweber_form_id, (int) $kit_form_id );
}

Expand Down Expand Up @@ -380,6 +381,7 @@ private function maybe_migrate_mc4wp_configuration() {

// Iterate through the MC4WP Form IDs and replace the shortcodes with the Kit Form Shortcodes.
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_mc4wp_settings'] ) ) as $mc4wp_form_id => $kit_form_id ) {
$mc4wp->replace_blocks_in_posts( (int) $mc4wp_form_id, (int) $kit_form_id );
$mc4wp->replace_shortcodes_in_posts( (int) $mc4wp_form_id, (int) $kit_form_id );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function _before(EndToEndTester $I)
*
* @param EndToEndTester $I Tester.
*/
public function testAWeberImport(EndToEndTester $I)
public function testAWeberImportWithShortcodes(EndToEndTester $I)
{
// Setup Plugin.
$I->setupKitPlugin($I);
Expand Down Expand Up @@ -65,6 +65,52 @@ public function testAWeberImport(EndToEndTester $I)
}
}

/**
* Test that AWeber Blocks are replaced with Kit Blocks when the Tools > AWeber: Migrate Configuration is configured.
*
* @since 3.1.6
*
* @param EndToEndTester $I Tester.
*/
public function testAWeberImportWithBlocks(EndToEndTester $I)
{
// Setup Plugin.
$I->setupKitPlugin($I);
$I->setupKitPluginResources($I);

// Create Aweber Forms.
$aweberFormIDs = $this->_createAWeberForms($I);

// Insert AWeber Blocks into Pages.
$pageIDs = $this->_createPagesWithAWeberBlocks($I, $aweberFormIDs);

// Navigate to the Tools screen.
$I->loadKitSettingsToolsScreen($I);

// Select the Kit Forms to replace the AWeber Forms.
foreach ($aweberFormIDs as $aweberFormID) {
$I->selectOption('_wp_convertkit_integration_aweber_settings[' . $aweberFormID . ']', $_ENV['CONVERTKIT_API_FORM_ID']);
}

// Click the Migrate button.
$I->click('Migrate');

// Confirm success message displays.
$I->waitForElementVisible('.notice-success');
$I->see('AWeber forms migrated successfully.');

// Test each Page.
foreach ($pageIDs as $pageID) {
$I->amOnPage('?p=' . $pageID);

// Check Kit Form block is displayed.
$I->seeElementInDOM('form[data-sv-form]');

// Confirm special characters have not been stripped.
$I->seeInSource('!@£$%^&amp;*()_+~!@£$%^&amp;*()_+\\');
}
}

/**
* Test that the AWeber: Migrate Configuration section is not displayed when no AWeber Forms exist.
*
Expand Down Expand Up @@ -167,6 +213,44 @@ private function _createPagesWithAWeberFormShortcodes(EndToEndTester $I, $aweber
'post_status' => 'publish',
'post_title' => 'Page with AWeber Form #' . $aweberFormID,
'post_content' => '[aweber formid="' . $aweberFormID . '"]',

// Configure Kit Plugin to not display a default Form, so we test against the Kit Form in the content.
'meta_input' => [
'_wp_convertkit_post_meta' => [
'form' => '0',
'landing_page' => '',
'tag' => '',
],
],
]
);
}

return $pageIDs;
}

/**
* Create Pages with AWeber Blocks.
*
* @since 3.1.6
*
* @param EndToEndTester $I Tester.
* @param array $aweberFormIDs AWeber Form IDs.
* @return array
*/
private function _createPagesWithAWeberBlocks(EndToEndTester $I, $aweberFormIDs)
{
$pageIDs = array();

foreach ($aweberFormIDs as $aweberFormID) {
$pageIDs[] = $I->havePostInDatabase(
[
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => 'Page with AWeber Block #' . $aweberFormID,
'post_content' => '<!-- wp:aweber-signupform-block/aweber-shortcode {"selectedShortCode":"6924484-' . $aweberFormID . '-webform"} -->
<div class="wp-block-aweber-signupform-block-aweber-shortcode">[aweber listid=6924484 formid=' . $aweberFormID . ' formtype=webform]</div>
<!-- /wp:aweber-signupform-block/aweber-shortcode --><!-- wp:html --><div class="wp-block-core-html">Some content with characters !@£$%^&amp;*()_+~!@£$%^&amp;*()_+\\\</div><!-- /wp:html -->',
'meta_input' => [
'_wp_convertkit_post_meta' => [
'form' => '0',
Expand Down
Loading