diff --git a/admin/importers/class-convertkit-admin-importer-activecampaign.php b/admin/importers/class-convertkit-admin-importer-activecampaign.php new file mode 100644 index 000000000..f8fe7b023 --- /dev/null +++ b/admin/importers/class-convertkit-admin-importer-activecampaign.php @@ -0,0 +1,85 @@ + __( 'The uploaded configuration file isn\'t valid.', 'convertkit' ), 'import_configuration_empty' => __( 'The uploaded configuration file contains no settings.', 'convertkit' ), 'import_configuration_success' => __( 'Configuration imported successfully.', 'convertkit' ), + 'migrate_activecampaign_configuration_success' => __( 'ActiveCampaign forms migrated 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_download_system_info(); $this->maybe_export_configuration(); $this->maybe_import_configuration(); + $this->maybe_migrate_activecampaign_configuration(); $this->maybe_migrate_aweber_configuration(); $this->maybe_migrate_mc4wp_configuration(); $this->maybe_migrate_mailpoet_configuration(); @@ -322,6 +324,43 @@ private function maybe_import_configuration() { } + /** + * Replaces ActiveCampaign Form Shortcodes and Blocks with Kit Form Shortcodes and Blocks, if the user submitted the + * ActiveCampaign Migrate Configuration section. + * + * @since 3.1.7 + */ + private function maybe_migrate_activecampaign_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 ActiveCampaign Form IDs were submitted. + if ( ! isset( $_REQUEST['_wp_convertkit_integration_activecampaign_settings'] ) ) { + return; + } + + // Initialise the importer. + $activecampaign = new ConvertKit_Admin_Importer_ActiveCampaign(); + + // Iterate through the ActiveCampaign Form IDs, replacing the shortcodes with the Kit Form Shortcodes + // and blocks with the Kit Form Blocks. + foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_activecampaign_settings'] ) ) as $activecampaign_form_id => $kit_form_id ) { + $activecampaign->replace_blocks_in_posts( (int) $activecampaign_form_id, (int) $kit_form_id ); + $activecampaign->replace_shortcodes_in_posts( (int) $activecampaign_form_id, (int) $kit_form_id ); + } + + // Redirect to Tools screen. + $this->redirect_with_success_notice( 'migrate_activecampaign_configuration_success' ); + + } + /** * Replaces AWeber Form Shortcodes with Kit Form Shortcodes, if the user submitted the * AWeber Migrate Configuration section. @@ -488,10 +527,11 @@ public function render() { $forms = new ConvertKit_Resource_Forms(); // Get Importers. - $aweber = new ConvertKit_Admin_Importer_AWeber(); - $mc4wp = new ConvertKit_Admin_Importer_MC4WP(); - $mailpoet = new ConvertKit_Admin_Importer_Mailpoet(); - $newsletter = new ConvertKit_Admin_Importer_Newsletter(); + $activecampaign = new ConvertKit_Admin_Importer_ActiveCampaign(); + $aweber = new ConvertKit_Admin_Importer_AWeber(); + $mc4wp = new ConvertKit_Admin_Importer_MC4WP(); + $mailpoet = new ConvertKit_Admin_Importer_Mailpoet(); + $newsletter = new ConvertKit_Admin_Importer_Newsletter(); // Output view. require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php'; diff --git a/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterActiveCampaignCest.php b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterActiveCampaignCest.php new file mode 100644 index 000000000..88cb63345 --- /dev/null +++ b/tests/EndToEnd/general/plugin-screens/PluginSettingsToolsImporterActiveCampaignCest.php @@ -0,0 +1,296 @@ + Kit > Tools > Import sections for ActiveCampaign. + * + * @since 3.1.7 + */ +class PluginSettingsToolsImporterActiveCampaignCest +{ + /** + * Run common actions before running the test functions in this class. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + */ + public function _before(EndToEndTester $I) + { + // Activate Plugins. + $I->activateKitPlugin($I); + } + + /** + * Test that ActiveCampaign Form Shortcodes are replaced with Kit Form Shortcodes when the Tools > ActiveCampaign: Migrate Configuration is configured. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + */ + public function testActiveCampaignImportWithShortcodes(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create ActiveCampaign Forms. + $activecampaignFormIDs = $this->_createActiveCampaignForms($I); + + // Insert ActiveCampaign Form Shortcodes into Pages. + $pageIDs = $this->_createPagesWithActiveCampaignFormShortcodes($I, $activecampaignFormIDs); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Select the Kit Forms to replace the ActiveCampaign Forms. + foreach ($activecampaignFormIDs as $activecampaignFormID) { + $I->selectOption('_wp_convertkit_integration_activecampaign_settings[' . $activecampaignFormID . ']', $_ENV['CONVERTKIT_API_FORM_ID']); + } + + // Click the Migrate button. + $I->click('Migrate'); + + // Confirm success message displays. + $I->waitForElementVisible('.notice-success'); + $I->see('ActiveCampaign 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 ActiveCampaign Blocks are replaced with Kit Blocks when the Tools > ActiveCampaign: Migrate Configuration is configured. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + */ + public function testActiveCampaignImportWithBlocks(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create ActiveCampaign Forms. + $activecampaignFormIDs = $this->_createActiveCampaignForms($I); + + // Insert ActiveCampaign Blocks into Pages. + $pageIDs = $this->_createPagesWithActiveCampaignBlocks($I, $activecampaignFormIDs); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Select the Kit Forms to replace the ActiveCampaign Forms. + foreach ($activecampaignFormIDs as $activecampaignFormID) { + $I->selectOption('_wp_convertkit_integration_activecampaign_settings[' . $activecampaignFormID . ']', $_ENV['CONVERTKIT_API_FORM_ID']); + } + + // Click the Migrate button. + $I->click('Migrate'); + + // Confirm success message displays. + $I->waitForElementVisible('.notice-success'); + $I->see('ActiveCampaign 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 ActiveCampaign: Migrate Configuration section is not displayed when no ActiveCampaign Forms exist. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + */ + public function testActiveCampaignImportWhenNoActiveCampaignForms(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Confirm no ActiveCampaign: Migrate Configuration section is displayed. + $I->dontSeeElementInDOM('#import-activecampaign'); + } + + /** + * Test that the ActiveCampaign: Migrate Configuration section is not displayed when ActiveCampaign Forms exist, + * but no Pages, Posts or Custom Posts contain ActiveCampaign Form Shortcodes. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + */ + public function testActiveCampaignImportWhenNoActiveCampaignShortcodesInContent(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPlugin($I); + $I->setupKitPluginResources($I); + + // Create ActiveCampaign Forms. + $this->_createActiveCampaignForms($I); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Confirm no ActiveCampaign: Migrate Configuration section is displayed, as there are no + // ActiveCampaign Form Shortcodes in the content. + $I->dontSeeElementInDOM('#import-activecampaign'); + } + + /** + * Test that the ActiveCampaign: Migrate Configuration section is not displayed when no Kit Forms exist. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + */ + public function testActiveCampaignImportWhenNoKitForms(EndToEndTester $I) + { + // Setup Plugin. + $I->setupKitPluginCredentialsNoData($I); + $I->setupKitPluginResourcesNoData($I); + + // Navigate to the Tools screen. + $I->loadKitSettingsToolsScreen($I); + + // Confirm no ActiveCampaign: Migrate Configuration section is displayed, as there are no + // ActiveCampaign Form Shortcodes in the content. + $I->dontSeeElementInDOM('#import-activecampaign'); + } + + /** + * Create ActiveCampaign Forms. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + * @return array + */ + private function _createActiveCampaignForms(EndToEndTester $I) + { + // Enable Defer and Delay JavaScript. + $I->haveOptionInDatabase( + 'settings_activecampaign', + [ + 'forms' => [ + 1 => [ + 'id' => '1', + 'name' => 'ActiveCampaign Form #1', + ], + 2 => [ + 'id' => '2', + 'name' => 'ActiveCampaign Form #2', + ], + ], + ] + ); + + // Return Form IDs. + return [ 1, 2 ]; + } + + /** + * Create Pages with ActiveCampaign Form Shortcodes. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + * @param array $activecampaignFormIDs ActiveCampaign Form IDs. + * @return array + */ + private function _createPagesWithActiveCampaignFormShortcodes(EndToEndTester $I, $activecampaignFormIDs) + { + $pageIDs = array(); + + foreach ($activecampaignFormIDs as $activecampaignFormID) { + $pageIDs[] = $I->havePostInDatabase( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Page with ActiveCampaign Form #' . $activecampaignFormID, + 'post_content' => '[activecampaign form="' . $activecampaignFormID . '"]', + 'meta_input' => [ + '_wp_convertkit_post_meta' => [ + 'form' => '0', + 'landing_page' => '', + 'tag' => '', + ], + ], + ] + ); + } + + return $pageIDs; + } + + /** + * Create Pages with ActiveCampaign Blocks. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + * @param array $activecampaignFormIDs ActiveCampaign Form IDs. + * @return array + */ + private function _createPagesWithActiveCampaignBlocks(EndToEndTester $I, $activecampaignFormIDs) + { + $pageIDs = array(); + + foreach ($activecampaignFormIDs as $activecampaignFormID) { + $pageIDs[] = $I->havePostInDatabase( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Page with ActiveCampaign Block #' . $activecampaignFormID, + 'post_content' => '
Some content with characters !@£$%^&*()_+~!@£$%^&*()_+\\\
', + + // 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; + } + + /** + * Deactivate and reset Plugin(s) after each test, if the test passes. + * We don't use _after, as this would provide a screenshot of the Plugin + * deactivation and not the true test error. + * + * @since 3.1.7 + * + * @param EndToEndTester $I Tester. + */ + public function _passed(EndToEndTester $I) + { + $I->deactivateKitPlugin($I); + $I->dontHaveOptionInDatabase('settings_activecampaign'); + $I->resetKitPlugin($I); + } +} diff --git a/tests/Integration/ImporterTest.php b/tests/Integration/ImporterTest.php index 580d6dca3..9d39348ab 100644 --- a/tests/Integration/ImporterTest.php +++ b/tests/Integration/ImporterTest.php @@ -56,6 +56,164 @@ public function tearDown(): void parent::tearDown(); } + /** + * Test that the get_form_ids_from_content() method returns ActiveCampaign form shortcode Form IDs + * ignoring any other shortcodes. + * + * @since 3.1.7 + */ + public function testActiveCampaignGetFormIDsFromContent() + { + // Initialize the class we want to test. + $this->importer = new \ConvertKit_Admin_Importer_ActiveCampaign(); + + // Confirm initialization didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $this->importer); + + // Define the content to test. + $content = '[activecampaign form="10"] some content [activecampaign form="11"] some other content [aweber formid="12"] different shortcode to ignore'; + + // Extract form IDs from content. + $form_ids = $this->importer->get_form_ids_from_content( $content ); + + // Assert the correct number of form IDs are returned. + $this->assertEquals( 2, count( $form_ids ) ); + $this->assertEquals( 10, $form_ids[0] ); + $this->assertEquals( 11, $form_ids[1] ); + } + + /** + * Test that the replace_shortcodes_in_content() method replaces the ActiveCampaign form shortcode with the Kit form shortcode. + * + * @since 3.1.7 + */ + public function testActiveCampaignReplaceShortcodesInContent() + { + // Initialize the class we want to test. + $this->importer = new \ConvertKit_Admin_Importer_ActiveCampaign(); + + // Confirm initialization didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $this->importer); + + // Define the shortcodes to test. + $shortcodes = [ + '[activecampaign form="1"]', + '[activecampaign form=1]', + '[activecampaign form="1" css="1"]', + '[activecampaign form=1 css=1]', + '[activecampaign css="1" form="1"]', + '[activecampaign css=1 form=1]', + ]; + + // Test each shortcode is replaced with the Kit form shortcode. + foreach ( $shortcodes as $shortcode ) { + $this->assertEquals( + '[convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"]', + $this->importer->replace_shortcodes_in_content( $shortcode, 1, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + + // Prepend and append some content. + $content = 'Some content before the shortcode: ' . $shortcode . ' Some content after the shortcode.'; + $this->assertEquals( + 'Some content before the shortcode: [convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"] Some content after the shortcode.', + $this->importer->replace_shortcodes_in_content( $content, 1, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + + // Prepend and append some content and duplicate the shortcode. + $content = 'Some content before the shortcode: ' . $shortcode . ' Some content after the shortcode: ' . $shortcode; + $this->assertEquals( + 'Some content before the shortcode: [convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"] Some content after the shortcode: [convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"]', + $this->importer->replace_shortcodes_in_content( $content, 1, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + } + } + + /** + * Test that the replace_shortcodes_in_content() method ignores non-ActiveCampaign shortcodes. + * + * @since 3.1.7 + */ + public function testActiveCampaignReplaceShortcodesInContentIgnoringOtherShortcodes() + { + // Initialize the class we want to test. + $this->importer = new \ConvertKit_Admin_Importer_ActiveCampaign(); + + // Confirm initialization didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $this->importer); + + // Define the shortcodes to test. + $shortcodes = [ + '[convertkit_form id="' . $_ENV['CONVERTKIT_API_FORM_ID'] . '"]', + '[a_random_shortcode]', + ]; + + // Test each shortcode is ignored. + foreach ( $shortcodes as $shortcode ) { + $this->assertEquals( + $shortcode, + $this->importer->replace_shortcodes_in_content( $shortcode, 10, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + } + } + + /** + * Test that the replace_blocks_in_post() method replaces the ActiveCampaign form block with the Kit form block, + * and special characters are not stripped when the Post is saved. + * + * @since 3.1.7 + */ + public function testActiveCampaignReplaceBlocksInPost() + { + // Initialize the class we want to test. + $this->importer = new \ConvertKit_Admin_Importer_ActiveCampaign(); + + // Confirm initialization didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $this->importer); + + // Create a Post with an ActiveCampaign form block and HTML block, as if the user already created this post. + $postID = $this->factory->post->create( + [ + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'ActiveCampaign: Replace Blocks in Post', + 'post_content' => str_replace( '\\', '\\\\', '' . $this->html_block ), + ] + ); + + // Replace the blocks in the post. + $this->importer->replace_blocks_in_post( $postID, 1, $_ENV['CONVERTKIT_API_FORM_ID'] ); + + // Test the block is replaced with the Kit form block, and special characters are not stripped. + $this->assertEquals( + '' . $this->html_block, + get_post_field( 'post_content', $postID ) + ); + } + + /** + * Test that the replace_blocks_in_content() method replaces the ActiveCampaign form block with the Kit form block, + * and special characters are not stripped. + * + * @since 3.1.7 + */ + public function testActiveCampaignReplaceBlocksInContent() + { + // Initialize the class we want to test. + $this->importer = new \ConvertKit_Admin_Importer_ActiveCampaign(); + + // Confirm initialization didn't result in an error. + $this->assertNotInstanceOf(\WP_Error::class, $this->importer); + + // Define the blocks to test. + $content = '' . $this->html_block; + + // Test the block is replaced with the Kit form block. + $this->assertEquals( + '' . $this->html_block, + $this->importer->replace_blocks_in_content( parse_blocks( $content ), 1, $_ENV['CONVERTKIT_API_FORM_ID'] ) + ); + } + /** * Test that the get_form_ids_from_content() method returns AWeber form shortcode Form IDs * ignoring any other shortcodes. diff --git a/views/backend/settings/tools.php b/views/backend/settings/tools.php index a6e05be82..c16e735f1 100644 --- a/views/backend/settings/tools.php +++ b/views/backend/settings/tools.php @@ -102,6 +102,61 @@ has_forms_in_posts() && $activecampaign->has_forms() && $forms->exist() ) { + ?> +
+

+ +

+
+

+ + + + + + + + + + get_forms() as $activecampaign_form_id => $activecampaign_form_title ) { + ?> + + + + + + +
+ +
+ +

+ +

+
+ has_forms_in_posts() && $aweber->has_forms() && $forms->exist() ) { ?> diff --git a/wp-convertkit.php b/wp-convertkit.php index cd24b4865..e3c69424d 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -117,6 +117,7 @@ require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-admin-setup-wizard.php'; require_once CONVERTKIT_PLUGIN_PATH . '/admin/class-convertkit-wp-list-table.php'; require_once CONVERTKIT_PLUGIN_PATH . '/admin/importers/class-convertkit-admin-importer.php'; +require_once CONVERTKIT_PLUGIN_PATH . '/admin/importers/class-convertkit-admin-importer-activecampaign.php'; require_once CONVERTKIT_PLUGIN_PATH . '/admin/importers/class-convertkit-admin-importer-aweber.php'; require_once CONVERTKIT_PLUGIN_PATH . '/admin/importers/class-convertkit-admin-importer-mc4wp.php'; require_once CONVERTKIT_PLUGIN_PATH . '/admin/importers/class-convertkit-admin-importer-mailpoet.php';