diff --git a/admin/importers/class-convertkit-admin-importer-activecampaign.php b/admin/importers/class-convertkit-admin-importer-activecampaign.php index f8fe7b023..4fb91fd8b 100644 --- a/admin/importers/class-convertkit-admin-importer-activecampaign.php +++ b/admin/importers/class-convertkit-admin-importer-activecampaign.php @@ -14,6 +14,24 @@ */ class ConvertKit_Admin_Importer_ActiveCampaign extends ConvertKit_Admin_Importer { + /** + * Holds the programmatic name of the importer (lowercase, no spaces). + * + * @since 3.1.7 + * + * @var string + */ + public $name = 'activecampaign'; + + /** + * Holds the title of the importer (for display in the importer list). + * + * @since 3.1.7 + * + * @var string + */ + public $title = 'ActiveCampaign'; + /** * Holds the shortcode name for MC4WP forms. * @@ -50,6 +68,18 @@ class ConvertKit_Admin_Importer_ActiveCampaign extends ConvertKit_Admin_Importer */ public $block_id_attribute = 'formId'; + /** + * Constructor + * + * @since 3.1.7 + */ + public function __construct() { + + // Register this as an importer, if ActiveCampaign forms exist. + add_filter( 'convertkit_get_form_importers', array( $this, 'register' ) ); + + } + /** * Returns an array of MC4WP form IDs and titles. * diff --git a/admin/importers/class-convertkit-admin-importer-aweber.php b/admin/importers/class-convertkit-admin-importer-aweber.php index 2df10614e..5ba1671d4 100644 --- a/admin/importers/class-convertkit-admin-importer-aweber.php +++ b/admin/importers/class-convertkit-admin-importer-aweber.php @@ -14,6 +14,24 @@ */ class ConvertKit_Admin_Importer_AWeber extends ConvertKit_Admin_Importer { + /** + * Holds the programmatic name of the importer (lowercase, no spaces). + * + * @since 3.1.7 + * + * @var string + */ + public $name = 'aweber'; + + /** + * Holds the title of the importer (for display in the importer list). + * + * @since 3.1.7 + * + * @var string + */ + public $title = 'AWeber'; + /** * Holds the shortcode name for AWeber forms. * @@ -50,6 +68,18 @@ class ConvertKit_Admin_Importer_AWeber extends ConvertKit_Admin_Importer { */ public $block_id_attribute = 'selectedShortCode'; + /** + * Constructor + * + * @since 3.1.7 + */ + public function __construct() { + + // Register this as an importer, if AWeber forms exist. + add_filter( 'convertkit_get_form_importers', array( $this, 'register' ) ); + + } + /** * Returns an array of AWeber form IDs and titles. * diff --git a/admin/importers/class-convertkit-admin-importer-mailpoet.php b/admin/importers/class-convertkit-admin-importer-mailpoet.php index 27dc10954..e80272d4b 100644 --- a/admin/importers/class-convertkit-admin-importer-mailpoet.php +++ b/admin/importers/class-convertkit-admin-importer-mailpoet.php @@ -14,6 +14,24 @@ */ class ConvertKit_Admin_Importer_Mailpoet extends ConvertKit_Admin_Importer { + /** + * Holds the programmatic name of the importer (lowercase, no spaces). + * + * @since 3.1.7 + * + * @var string + */ + public $name = 'mailpoet'; + + /** + * Holds the title of the importer (for display in the importer list). + * + * @since 3.1.7 + * + * @var string + */ + public $title = 'Mailpoet'; + /** * Holds the shortcode name for Mailpoet forms. * @@ -50,6 +68,18 @@ class ConvertKit_Admin_Importer_Mailpoet extends ConvertKit_Admin_Importer { */ public $block_id_attribute = 'formId'; + /** + * Constructor + * + * @since 3.1.7 + */ + public function __construct() { + + // Register this as an importer, if Mailpoet forms exist. + add_filter( 'convertkit_get_form_importers', array( $this, 'register' ) ); + + } + /** * Returns an array of Mailpoet form IDs and titles. * diff --git a/admin/importers/class-convertkit-admin-importer-mc4wp.php b/admin/importers/class-convertkit-admin-importer-mc4wp.php index ad9239bb7..f88380320 100644 --- a/admin/importers/class-convertkit-admin-importer-mc4wp.php +++ b/admin/importers/class-convertkit-admin-importer-mc4wp.php @@ -14,6 +14,24 @@ */ class ConvertKit_Admin_Importer_MC4WP extends ConvertKit_Admin_Importer { + /** + * Holds the programmatic name of the importer (lowercase, no spaces). + * + * @since 3.1.7 + * + * @var string + */ + public $name = 'mc4wp'; + + /** + * Holds the title of the importer (for display in the importer list). + * + * @since 3.1.7 + * + * @var string + */ + public $title = 'MC4WP'; + /** * Holds the shortcode name for MC4WP forms. * @@ -50,6 +68,18 @@ class ConvertKit_Admin_Importer_MC4WP extends ConvertKit_Admin_Importer { */ public $block_id_attribute = 'id'; + /** + * Constructor + * + * @since 3.1.7 + */ + public function __construct() { + + // Register this as an importer, if MC4WP forms exist. + add_filter( 'convertkit_get_form_importers', array( $this, 'register' ) ); + + } + /** * Returns an array of MC4WP form IDs and titles. * diff --git a/admin/importers/class-convertkit-admin-importer-newsletter.php b/admin/importers/class-convertkit-admin-importer-newsletter.php index a99d34e9e..caddf5e26 100644 --- a/admin/importers/class-convertkit-admin-importer-newsletter.php +++ b/admin/importers/class-convertkit-admin-importer-newsletter.php @@ -14,6 +14,24 @@ */ class ConvertKit_Admin_Importer_Newsletter extends ConvertKit_Admin_Importer { + /** + * Holds the programmatic name of the importer (lowercase, no spaces). + * + * @since 3.1.7 + * + * @var string + */ + public $name = 'newsletter'; + + /** + * Holds the title of the importer (for display in the importer list). + * + * @since 3.1.7 + * + * @var string + */ + public $title = 'Newsletter'; + /** * Holds the shortcode name for Newsletter forms. * @@ -32,6 +50,18 @@ class ConvertKit_Admin_Importer_Newsletter extends ConvertKit_Admin_Importer { */ public $block_name = 'tnp/minimal'; + /** + * Constructor + * + * @since 3.1.7 + */ + public function __construct() { + + // Register this as an importer, if Newsletter forms exist. + add_filter( 'convertkit_get_form_importers', array( $this, 'register' ) ); + + } + /** * Returns an array of the Newsletter Default Form, if the shortcode * or block is used. diff --git a/admin/importers/class-convertkit-admin-importer.php b/admin/importers/class-convertkit-admin-importer.php index f9abfc724..94877e72e 100644 --- a/admin/importers/class-convertkit-admin-importer.php +++ b/admin/importers/class-convertkit-admin-importer.php @@ -14,6 +14,24 @@ */ abstract class ConvertKit_Admin_Importer { + /** + * Holds the importer name. + * + * @since 3.1.7 + * + * @var string + */ + public $name = ''; + + /** + * Holds the importer title. + * + * @since 3.1.7 + * + * @var string + */ + public $title = ''; + /** * Holds the shortcode name for the third party Form plugin. * @@ -59,6 +77,54 @@ abstract class ConvertKit_Admin_Importer { */ abstract public function get_forms(); + /** + * Registers the importer if third party forms exist. + * + * @since 3.1.7 + * + * @param array $importers Importers. + * @return array + */ + public function register( $importers ) { + + // Bail if no third party forms exist in posts. + if ( ! $this->has_forms_in_posts() ) { + return $importers; + } + + // Bail if no third party forms exist for this importer. + if ( ! $this->has_forms() ) { + return $importers; + } + + // Add this importer to the list of importers. + $importers[ $this->name ] = array( + 'name' => $this->name, + 'title' => $this->title, + 'forms' => $this->get_forms(), + ); + + return $importers; + + } + + /** + * Replaces third party form shortcodes and blocks with Kit form shortcodes and blocks. + * + * @since 3.1.7 + * + * @param array $mappings Mappings. + */ + public function import( $mappings ) { + + // Iterate through the mappings, replacing the third party form shortcodes and blocks with the Kit form shortcodes and blocks. + foreach ( $mappings as $third_party_form_id => $kit_form_id ) { + $this->replace_blocks_in_posts( (int) $third_party_form_id, (int) $kit_form_id ); + $this->replace_shortcodes_in_posts( (int) $third_party_form_id, (int) $kit_form_id ); + } + + } + /** * Returns an array of post IDs that contain the third party form block or shortcode. * diff --git a/admin/section/class-convertkit-admin-section-tools.php b/admin/section/class-convertkit-admin-section-tools.php index 1c85274ff..d3985a31d 100644 --- a/admin/section/class-convertkit-admin-section-tools.php +++ b/admin/section/class-convertkit-admin-section-tools.php @@ -80,11 +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(); - $this->maybe_migrate_newsletter_configuration(); + $this->maybe_migrate_forms(); } @@ -330,7 +326,7 @@ private function maybe_import_configuration() { * * @since 3.1.7 */ - private function maybe_migrate_activecampaign_configuration() { + private function maybe_migrate_forms() { // Bail if nonce verification fails. if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { @@ -341,168 +337,31 @@ private function maybe_migrate_activecampaign_configuration() { 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. - * - * @since 3.1.5 - */ - private function maybe_migrate_aweber_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 AWeber Form IDs were submitted. - if ( ! isset( $_REQUEST['_wp_convertkit_integration_aweber_settings'] ) ) { - return; - } - - // Initialise the importer. - $aweber = new ConvertKit_Admin_Importer_AWeber(); - - // 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 ); - } - - // Redirect to Tools screen. - $this->redirect_with_success_notice( 'migrate_aweber_configuration_success' ); - - } - - /** - * Replaces MC4WP Form Shortcodes with Kit Form Shortcodes, if the user submitted the - * MC4WP Migrate Configuration section. - * - * @since 3.1.0 - */ - private function maybe_migrate_mc4wp_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 MC4WP Form IDs were submitted. - if ( ! isset( $_REQUEST['_wp_convertkit_integration_mc4wp_settings'] ) ) { - return; - } - - // Initialise the importer. - $mc4wp = new ConvertKit_Admin_Importer_MC4WP(); - - // 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 ); - } - - // Redirect to Tools screen. - $this->redirect_with_success_notice( 'migrate_mc4wp_configuration_success' ); - - } - - /** - * 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() { + // Get importers. + $importers = convertkit_get_form_importers(); - // Bail if nonce verification fails. - if ( ! isset( $_REQUEST['_convertkit_settings_tools_nonce'] ) ) { - return; - } + // Find the importer that was used for the form submission. + foreach ( $importers as $importer ) { + // Skip if this importer was not used. + if ( ! isset( $_REQUEST[ 'convertkit-import-' . $importer['name'] ] ) ) { + continue; + } - if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_convertkit_settings_tools_nonce'] ), 'convertkit-settings-tools' ) ) { - return; - } + // Skip if no mappings were submitted for the importer. + if ( ! isset( $_REQUEST[ '_wp_convertkit_integration_' . $importer['name'] . '_settings' ] ) ) { + continue; + } - // Bail if no Mailpoet Form IDs were submitted. - if ( ! isset( $_REQUEST['_wp_convertkit_integration_mailpoet_settings'] ) ) { - return; - } + // Sanitize mappings. + $mappings = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST[ '_wp_convertkit_integration_' . $importer['name'] . '_settings' ] ) ); - // Initialise the importer. - $mailpoet = new ConvertKit_Admin_Importer_Mailpoet(); + // Replace third party form shortcodes and blocks with Kit form shortcodes and blocks. + WP_ConvertKit()->get_class( 'admin_importer_' . $importer['name'] )->import( $mappings ); - // 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_' . $importer['name'] . '_configuration_success' ); } - // Redirect to Tools screen. - $this->redirect_with_success_notice( 'migrate_mailpoet_configuration_success' ); - - } - - /** - * Replaces Newsletter Form Blocks and Shortcodes with Kit Form Blocks and Shortcodes, if the user submitted the - * Newsletter Migrate Configuration section. - * - * @since 3.1.6 - */ - private function maybe_migrate_newsletter_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 Newsletter Form IDs were submitted. - if ( ! isset( $_REQUEST['_wp_convertkit_integration_newsletter_settings'] ) ) { - return; - } - - // Initialise the importer. - $newsletter = new ConvertKit_Admin_Importer_Newsletter(); - - // Iterate through the Newsletter Form IDs and replace the blocks and shortcodes with the Kit Form blocks and shortcodes. - foreach ( array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['_wp_convertkit_integration_newsletter_settings'] ) ) as $newsletter_form_id => $kit_form_id ) { - $newsletter->replace_blocks_in_posts( (int) $newsletter_form_id, (int) $kit_form_id ); - $newsletter->replace_shortcodes_in_posts( (int) $newsletter_form_id, (int) $kit_form_id ); - } - - // Redirect to Tools screen. - $this->redirect_with_success_notice( 'migrate_newsletter_configuration_success' ); - } /** @@ -526,12 +385,11 @@ public function render() { // Get Forms. $forms = new ConvertKit_Resource_Forms(); - // Get Importers. - $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(); + // Get Importers, if Kit Forms exist. + $importers = array(); + if ( $forms->exist() ) { + $importers = convertkit_get_form_importers(); + } // Output view. require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/tools.php'; diff --git a/includes/class-wp-convertkit.php b/includes/class-wp-convertkit.php index 9e095b9ce..53f163f34 100644 --- a/includes/class-wp-convertkit.php +++ b/includes/class-wp-convertkit.php @@ -83,6 +83,11 @@ private function initialize_admin() { $this->classes['admin_cache_plugins'] = new ConvertKit_Admin_Cache_Plugins(); $this->classes['admin_category'] = new ConvertKit_Admin_Category(); $this->classes['admin_landing_page'] = new ConvertKit_Admin_Landing_Page(); + $this->classes['admin_importer_activecampaign'] = new ConvertKit_Admin_Importer_ActiveCampaign(); + $this->classes['admin_importer_aweber'] = new ConvertKit_Admin_Importer_AWeber(); + $this->classes['admin_importer_mc4wp'] = new ConvertKit_Admin_Importer_MC4WP(); + $this->classes['admin_importer_mailpoet'] = new ConvertKit_Admin_Importer_Mailpoet(); + $this->classes['admin_importer_newsletter'] = new ConvertKit_Admin_Importer_Newsletter(); $this->classes['admin_post'] = new ConvertKit_Admin_Post(); $this->classes['admin_quick_edit'] = new ConvertKit_Admin_Quick_Edit(); $this->classes['admin_restrict_content'] = new ConvertKit_Admin_Restrict_Content(); diff --git a/includes/functions.php b/includes/functions.php index 6ad62e1e0..06750a515 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -258,6 +258,31 @@ function convertkit_get_pre_publish_actions() { } +/** + * Helper method to get registered importers that can replace third party + * form shortcodes and blocks with Kit form shortcodes and blocks. + * + * @since 3.1.7 + * + * @return array Importers. + */ +function convertkit_get_form_importers() { + + $importers = array(); + + /** + * Registers form importers for the ConvertKit Plugin. + * + * @since 3.1.7 + * + * @param array $importers Importers. + */ + $importers = apply_filters( 'convertkit_get_form_importers', $importers ); + + return $importers; + +} + /** * Helper method to return the Plugin Settings Link * diff --git a/views/backend/settings/tools.php b/views/backend/settings/tools.php index c16e735f1..728fe380a 100644 --- a/views/backend/settings/tools.php +++ b/views/backend/settings/tools.php @@ -102,251 +102,54 @@ has_forms_in_posts() && $activecampaign->has_forms() && $forms->exist() ) { + // Importers. + foreach ( $importers as $importer ) { ?> -
-
-
| - | - |
|---|---|
| - | - - | -
- -
-
-
-
| - | - |
|---|---|
| - | - - | -
- -
-
-
-
| - | - |
|---|---|
| - | - - | -
+
-
-
| - | - |
|---|---|
| - | - - | -
-
-