diff --git a/admin/importers/class-convertkit-admin-importer-mailpoet.php b/admin/importers/class-convertkit-admin-importer-mailpoet.php new file mode 100644 index 000000000..27dc10954 --- /dev/null +++ b/admin/importers/class-convertkit-admin-importer-mailpoet.php @@ -0,0 +1,82 @@ +get_results( + "SELECT id, name FROM {$wpdb->prefix}mailpoet_forms WHERE deleted_at IS NULL" + ); + + if ( empty( $results ) ) { + return array(); + } + + $forms = array(); + foreach ( $results as $form ) { + $forms[ $form->id ] = $form->name; + } + + return $forms; + + } + +} diff --git a/admin/section/class-convertkit-admin-section-tools.php b/admin/section/class-convertkit-admin-section-tools.php index b13956391..a37447258 100644 --- a/admin/section/class-convertkit-admin-section-tools.php +++ b/admin/section/class-convertkit-admin-section-tools.php @@ -59,6 +59,7 @@ public function register_notices( $notices ) { 'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ), 'migrate_aweber_configuration_success' => __( 'AWeber forms migrated successfully.', 'convertkit' ), 'migrate_mc4wp_configuration_success' => __( 'MC4WP forms migrated successfully.', 'convertkit' ), + 'migrate_mailpoet_configuration_success' => __( 'MailPoet forms migrated successfully.', 'convertkit' ), ) ); @@ -79,6 +80,7 @@ private function maybe_perform_actions() { $this->maybe_import_configuration(); $this->maybe_migrate_aweber_configuration(); $this->maybe_migrate_mc4wp_configuration(); + $this->maybe_migrate_mailpoet_configuration(); } @@ -390,6 +392,42 @@ private function maybe_migrate_mc4wp_configuration() { } + /** + * Replaces Mailpoet Form Shortcodes and Blocks with Kit Form Shortcodes and Blocks, if the user submitted the + * Mailpoet Migrate Configuration section. + * + * @since 3.1.6 + */ + private function maybe_migrate_mailpoet_configuration() { + + // Bail if nonce verification fails. + if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { + return; + } + + if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { + return; + } + + // Bail if no Mailpoet Form IDs were submitted. + if ( ! isset( $_REQUEST['_wp_convertkit_integration_mailpoet_settings'] ) ) { + return; + } + + // Initialise the importer. + $mailpoet = new ConvertKit_Admin_Importer_Mailpoet(); + + // Iterate through the Mailpoet Form IDs and replace the shortcodes with the Kit Form Shortcodes. + foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_mailpoet_settings'] ) ) as $mailpoet_form_id => $kit_form_id ) { + $mailpoet->replace_blocks_in_posts( (int) $mailpoet_form_id, (int) $kit_form_id ); + $mailpoet->replace_shortcodes_in_posts( (int) $mailpoet_form_id, (int) $kit_form_id ); + } + + // Redirect to Tools screen. + $this->redirect_with_success_notice( 'migrate_mailpoet_configuration_success' ); + + } + /** * Outputs the Debug Log and System Info view. * @@ -412,8 +450,9 @@ public function render() { $forms = new ConvertKit_Resource_Forms(); // Get Importers. - $aweber = new ConvertKit_Admin_Importer_AWeber(); - $mc4wp = new ConvertKit_Admin_Importer_MC4WP(); + $aweber = new ConvertKit_Admin_Importer_AWeber(); + $mc4wp = new ConvertKit_Admin_Importer_MC4WP(); + $mailpoet = new ConvertKit_Admin_Importer_Mailpoet(); // Output view. require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php'; diff --git a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMailPoetCest.php b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMailPoetCest.php new file mode 100644 index 000000000..781348f14 --- /dev/null +++ b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterMailPoetCest.php @@ -0,0 +1,292 @@ + Kit > Tools > Import sections for the MailPoet third party form plugin. + * + * @since 3.1.6 + */ +class PluginSettingsToolsImporterMailPoetCest +{ + /** + * Run common actions before running the test functions in this class. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + */ + public function _before(EndToEndTester $I) + { + // Activate Plugins. + $I->activateKitPlugin($I); + } + + /** + * Test that MailPoet Form Shortcodes are replaced with Kit Form Shortcodes when the Tools > MailPoet: Migrate Configuration is configured. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + */ + public function testMailPoetImportWithShortcodes(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create MailPoet Forms. + $mailPoetFormIDs = $this->_createMailPoetForms($I); + + // Insert MailPoet Form Shortcodes into Pages. + $pageIDs = $this->_createPagesWithMailPoetFormShortcodes($I, $mailPoetFormIDs); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Select the Kit Forms to replace the MailPoet Forms. + foreach ($mailPoetFormIDs as $mailPoetFormID) { + $I->selectOption('_wp_convertkit_integration_mailpoet_settings[' . $mailPoetFormID . ']', $_ENV['CONVERTKIT_API_FORM_ID']); + } + + // Click the Migrate button. + $I->click('Migrate'); + + // Confirm success message displays. + $I->waitForElementVisible('.notice-success'); + $I->see('MailPoet forms migrated successfully.'); + + // View the Pages, to confirm Kit Forms now display. + foreach ($pageIDs as $pageID) { + $I->amOnPage('?p=' . $pageID); + $I->seeElementInDOM('form[data-sv-form]'); + } + } + + /** + * Test that MailPoet Blocks are replaced with Kit Blocks when the Tools > MailPoet: Migrate Configuration is configured. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + */ + public function testMailPoetImportWithBlocks(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create MailPoet Forms. + $mailPoetFormIDs = $this->_createMailPoetForms($I); + + // Insert MailPoet Blocks into Pages. + $pageIDs = $this->_createPagesWithMailPoetBlocks($I, $mailPoetFormIDs); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Select the Kit Forms to replace the MailPoet Forms. + foreach ($mailPoetFormIDs as $mailPoetFormID) { + $I->selectOption('_wp_convertkit_integration_mailpoet_settings[' . $mailPoetFormID . ']', $_ENV['CONVERTKIT_API_FORM_ID']); + } + + // Click the Migrate button. + $I->click('Migrate'); + + // Confirm success message displays. + $I->waitForElementVisible('.notice-success'); + $I->see('MailPoet 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('!@£$%^&*()_+~!@£$%^&*()_+\\'); + } + } + + /** + * Test that the MailPoet: Migrate Configuration section is not displayed when no MailPoet Forms exist. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + */ + public function testMailPoetImportWhenNoMailPoetForms(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Confirm no MailPoet: Migrate Configuration section is displayed. + $I->dontSeeElementInDOM('#import-mailpoet'); + } + + /** + * Test that the MailPoet: Migrate Configuration section is not displayed when MailPoet Forms exist, + * but no Pages, Posts or Custom Posts contain MailPoet Form Shortcodes. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + */ + public function testMailPoetImportWhenNoMailPoetShortcodesInContent(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create MailPoet Forms. + $mailPoetFormIDs = $this->_createMailPoetForms($I); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Confirm no MailPoet: Migrate Configuration section is displayed, as there are no + // MailPoet Form Shortcodes in the content. + $I->dontSeeElementInDOM('#import-mailpoet'); + } + + /** + * Test that the MailPoet: Migrate Configuration section is not displayed when no Kit Forms exist. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + */ + public function testMailPoetImportWhenNoKitForms(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPluginCredentialsNoData($I); + $I->setupKitPluginResourcesNoData($I); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Confirm no MailPoet: Migrate Configuration section is displayed, as there are no + // MailPoet Form Shortcodes in the content. + $I->dontSeeElementInDOM('#import-mailpoet'); + } + + /** + * Create MailPoet Forms. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + * @return array + */ + private function _createMailPoetForms(EndToEndTester $I) + { + return array( + $I->haveInDatabase( + 'wp_mailpoet_forms', + [ + 'name' => 'MailPoet Form #1', + ] + ), + $I->haveInDatabase( + 'wp_mailpoet_forms', + [ + 'name' => 'MailPoet Form #2', + ] + ), + ); + } + + /** + * Create Pages with MailPoet Form Shortcodes. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + * @param array $mailPoetFormIDs MailPoet Form IDs. + * @return array + */ + private function _createPagesWithMailPoetFormShortcodes(EndToEndTester $I, $mailPoetFormIDs) + { + $pageIDs = array(); + + foreach ($mailPoetFormIDs as $mailPoetFormID) { + $pageIDs[] = $I->havePostInDatabase( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Page with MailPoet Form #' . $mailPoetFormID, + 'post_content' => '[mailpoet_form id="' . $mailPoetFormID . '"]', + 'meta_input' => [ + '_wp_convertkit_post_meta' => [ + 'form' => '0', + 'landing_page' => '', + 'tag' => '', + ], + ], + ] + ); + } + + return $pageIDs; + } + + /** + * Create Pages with MailPoet Blocks. + * + * @since 3.1.6 + * + * @param EndToEndTester $I Tester. + * @param array $mailPoetFormIDs MailPoet Form IDs. + * @return array + */ + private function _createPagesWithMailPoetBlocks(EndToEndTester $I, $mailPoetFormIDs) + { + $pageIDs = array(); + + foreach ($mailPoetFormIDs as $mailPoetFormID) { + $pageIDs[] = $I->havePostInDatabase( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Page with MailPoet Block #' . $mailPoetFormID, + 'post_content' => '
+
+
| + | + |
|---|---|
| + | + + | +
+ +
+