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
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* ConvertKit Admin Importer CampaignMonitor class.
*
* @package ConvertKit
* @author ConvertKit
*/

/**
* Import and migrate data from CampaignMonitor to Kit.
*
* @package ConvertKit
* @author ConvertKit
*/
class ConvertKit_Admin_Importer_CampaignMonitor extends ConvertKit_Admin_Importer {

/**
* Holds the programmatic name of the importer (lowercase, no spaces).
*
* @since 3.1.7
*
* @var string
*/
public $name = 'campaignmonitor';

/**
* Holds the title of the importer (for display in the importer list).
*
* @since 3.1.7
*
* @var string
*/
public $title = 'CampaignMonitor';

/**
* Holds the shortcode name for ActiveCampaign forms.
*
* @since 3.1.7
*
* @var string
*/
public $shortcode_name = 'cm_form';

/**
* Holds the ID attribute name for CampaignMonitor forms.
*
* @since 3.1.7
*
* @var string
*/
public $shortcode_id_attribute = 'form_id';

/**
* Constructor
*
* @since 3.1.7
*/
public function __construct() {

// Register this as an importer, if CampaignMonitor forms exist.
add_filter( 'convertkit_get_form_importers', array( $this, 'register' ) );

}

/**
* Returns an array of CampaignMonitor form IDs and titles.
*
* @since 3.1.7
*
* @return array
*/
public function get_forms() {

// Forms are cached in the Plugin Settings.
$settings = get_option( 'forms_for_campaign_monitor_forms' );

// Bail if the CampaignMonitor Plugin Settings are not set.
if ( ! $settings ) {
return array();
}

// Build array of forms.
$forms = array();
foreach ( $settings as $form_id => $form ) {
// $form is a Campaign Monitor forms\core\Form object. It'll be a __PHP_Incomplete_Class if the Campaign Monitor plugin is not active.
// To consistently access the protected form name property, we have to cast to an array.
$form = (array) $form;

// Access the protected form name property.
// When casting __PHP_Incomplete_Class to an array, protected properties are prefixed with \0*\0.
$forms[ $form_id ] = $form["\0*\0name"];
}

return $forms;

}

}
95 changes: 62 additions & 33 deletions admin/importers/class-convertkit-admin-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ public function import( $mappings ) {
continue;
}

$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 );
if ( $this->block_name ) {
$this->replace_blocks_in_posts( $third_party_form_id, (int) $kit_form_id );
}
if ( $this->shortcode_name ) {
$this->replace_shortcodes_in_posts( $third_party_form_id, (int) $kit_form_id );
}
}

}
Expand All @@ -141,24 +145,49 @@ public function get_forms_in_posts() {

global $wpdb;

// 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
OR post_content LIKE %s
)
",
'publish',
'%[' . $this->shortcode_name . '%',
'%<!-- wp:' . $this->block_name . '%'
// Build WHERE clauses and values.
$post_content_clauses = array();
$post_content_values = array();

if ( $this->shortcode_name ) {
$post_content_clauses[] = 'post_content LIKE %s';
$post_content_values[] = '%[' . $this->shortcode_name . '%';
}
if ( $this->block_name ) {
$post_content_clauses[] = 'post_content LIKE %s';
$post_content_values[] = '%<!-- wp:' . $this->block_name . '%';
}

// Bail early if nothing to search for.
if ( empty( $post_content_clauses ) ) {
return array();
}

// Prepare SQL using wpdb->prepare.
// call_user_func_array() is used so variable length arrays can be passed to prepare().
$query = call_user_func_array(
array( $wpdb, 'prepare' ),
array_merge(
array(
"
SELECT ID
FROM {$wpdb->posts}
WHERE post_status = %s
AND (
" . implode( ' OR ', $post_content_clauses ) . '
)
',
),
array_merge(
array( 'publish' ),
$post_content_values
)
)
);

// Run query.
$results = $wpdb->get_col( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

return $results ? $results : array();

}
Expand Down Expand Up @@ -194,8 +223,8 @@ public function has_forms_in_posts() {
*
* @since 3.1.0
*
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
* @param string|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 @@ -234,9 +263,9 @@ public function replace_shortcodes_in_posts( $third_party_form_id, $form_id ) {
*
* @since 3.1.0
*
* @param string $content Content containing third party Form Shortcodes.
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
* @param string $content Content containing third party Form Shortcodes.
* @param string|int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.

* @return string
*/
Expand Down Expand Up @@ -358,8 +387,8 @@ public function get_form_ids_from_content( $content ) {
*
* @since 3.1.6
*
* @param int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
* @param string|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 ) {

Expand All @@ -383,9 +412,9 @@ public function replace_blocks_in_posts( $third_party_form_id, $form_id ) {
*
* @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.
* @param int $post_id Post ID.
* @param string|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 ) {

Expand Down Expand Up @@ -424,9 +453,9 @@ public function replace_blocks_in_post( $post_id, $third_party_form_id, $form_id
*
* @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.
* @param array $blocks Blocks.
* @param string|int $third_party_form_id Third Party Form ID.
* @param int $form_id Kit Form ID.
*
* @return string
*/
Expand All @@ -446,9 +475,9 @@ public function replace_blocks_in_content( $blocks, $third_party_form_id, $form_
*
* @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.
* @param array $blocks Blocks.
* @param string|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 ) {
Expand Down
1 change: 1 addition & 0 deletions admin/section/class-convertkit-admin-section-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function register_notices( $notices ) {
'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_campaignmonitor_configuration_success' => __( 'Campaign Monitor forms migrated successfully.', 'convertkit' ),
'migrate_mc4wp_configuration_success' => __( 'MC4WP forms migrated successfully.', 'convertkit' ),
'migrate_mailpoet_configuration_success' => __( 'MailPoet forms migrated successfully.', 'convertkit' ),
'migrate_newsletter_configuration_success' => __( 'Newsletter forms migrated successfully.', 'convertkit' ),
Expand Down
1 change: 1 addition & 0 deletions includes/class-wp-convertkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private function initialize_admin() {
$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_campaignmonitor'] = new ConvertKit_Admin_Importer_CampaignMonitor();
$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();
Expand Down
Loading