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
49 changes: 49 additions & 0 deletions admin/importers/class-convertkit-admin-importer-newsletter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* ConvertKit Admin Importer Newsletter class.
*
* @package ConvertKit
* @author ConvertKit
*/

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

/**
* Holds the shortcode name for Newsletter forms.
*
* @since 3.1.6
*
* @var string
*/
public $shortcode_name = 'newsletter_form';

/**
* Holds the block name for Newsletter forms.
*
* @since 3.1.6
*
* @var string
*/
public $block_name = 'tnp/minimal';

/**
* Returns an array of the Newsletter Default Form, if the shortcode
* or block is used.
*
* @since 3.1.6
*
* @return array
*/
public function get_forms() {

return $this->get_form_ids_in_posts();

}

}
71 changes: 54 additions & 17 deletions admin/importers/class-convertkit-admin-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ abstract class ConvertKit_Admin_Importer {
*
* @since 3.1.0
*
* @var string
* @var bool|string
*/
public $shortcode_id_attribute = '';
public $shortcode_id_attribute = false;

/**
* Holds the block name for the third party Form plugin.
Expand All @@ -46,9 +46,9 @@ abstract class ConvertKit_Admin_Importer {
*
* @since 3.1.6
*
* @var string
* @var bool|string
*/
public $block_id_attribute = '';
public $block_id_attribute = false;

/**
* Returns an array of third party form IDs and titles.
Expand Down Expand Up @@ -171,13 +171,20 @@ public function replace_shortcodes_in_posts( $third_party_form_id, $form_id ) {
*/
public function replace_shortcodes_in_content( $content, $third_party_form_id, $form_id ) {

$pattern = '/\[' // Start regex with an opening square bracket.
// If there's no shortcode ID attribute, match shortcodes with or without any attribute.
if ( ! $this->shortcode_id_attribute ) {
$pattern = '/\[' // Start regex with an opening square bracket.
. preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars.
. '[^\]]*?' // Match any characters that are not a closing square bracket, non-greedy.
. '\b' . preg_quote( $this->shortcode_id_attribute, '/' ) // Match the id attribute word boundary and escape as needed.
. '\s*=\s*' // Match optional whitespace around an equals sign.
. '(?:"' . preg_quote( (string) $third_party_form_id, '/' ) . '"|' . preg_quote( (string) $third_party_form_id, '/' ) . ')' // Match the form ID, quoted or unquoted.
. '[^\]]*?\]/i'; // Match any other characters (non-greedy) up to the closing square bracket, case-insensitive.
} else {
$pattern = '/\[' // Start regex with an opening square bracket.
. preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars.
. '[^\]]*?' // Match any characters that are not a closing square bracket, non-greedy.
. '\b' . preg_quote( $this->shortcode_id_attribute, '/' ) // Match the id attribute word boundary and escape as needed.
. '\s*=\s*' // Match optional whitespace around an equals sign.
. '(?:"' . preg_quote( (string) $third_party_form_id, '/' ) . '"|' . preg_quote( (string) $third_party_form_id, '/' ) . ')' // Match the form ID, quoted or unquoted.
. '[^\]]*?\]/i'; // Match any other characters (non-greedy) up to the closing square bracket, case-insensitive.
}

return preg_replace(
$pattern,
Expand All @@ -204,6 +211,14 @@ public function get_form_ids_in_posts() {
return array();
}

// If the shortcode or block ID attribute is not set, the third party Plugin doesn't use IDs
// and only has one form.
if ( ! $this->shortcode_id_attribute && ! $this->block_id_attribute ) {
return array(
__( 'Default Form', 'convertkit' ),
);
}

// Iterate through Posts, extracting the Form IDs from the third party form shortcodes.
$form_ids = array();
foreach ( $post_ids as $post_id ) {
Expand All @@ -227,6 +242,24 @@ public function get_form_ids_in_posts() {
*/
public function get_form_ids_from_content( $content ) {

// If there's no shortcode ID attribute, match shortcodes with or without any attribute and treat any match as a single "form".
if ( ! $this->shortcode_id_attribute ) {
$pattern = '/\[' // Start regex with an opening square bracket.
. preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars.
. '(?:\s+[^\]]*)?' // Optionally match any attributes (key/value pairs), non-greedy.
. '[^\]]*?\]/i'; // Match up to closing bracket, case-insensitive.

preg_match_all( $pattern, $content, $matches );

// If we matched at least one occurrence, just return an array with a single 0 (default/non-ID form).
if ( ! empty( $matches[0] ) ) {
return array( 0 );
}

return array();
}

// Legacy: Extract where attribute is required.
$pattern = '/\[' // Start regex with an opening square bracket.
. preg_quote( $this->shortcode_name, '/' ) // Match the shortcode name, escaping any regex special chars.
. '(?:\s+[^\]]*)?' // Optionally match any attributes (key/value pairs), non-greedy.
Expand Down Expand Up @@ -365,14 +398,18 @@ private function recursively_convert_blocks( $blocks, $third_party_form_id, $for
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;
// If the block ID attribute is not set, the third party Plugin doesn't use IDs,
// so there's no need to check the $third_party_form_id matches the block attribute.
if ( $this->block_id_attribute ) {
// 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.
Expand Down
59 changes: 49 additions & 10 deletions admin/section/class-convertkit-admin-section-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ public function register_notices( $notices ) {
return array_merge(
$notices,
array(
'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ),
'import_configuration_invalid_file_type' => __( '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_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' ),
'import_configuration_upload_error' => __( 'An error occured uploading the configuration file.', 'convertkit' ),
'import_configuration_invalid_file_type' => __( '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_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' ),
'migrate_newsletter_configuration_success' => __( 'Newsletter forms migrated successfully.', 'convertkit' ),
)
);

Expand All @@ -81,6 +82,7 @@ private function maybe_perform_actions() {
$this->maybe_migrate_aweber_configuration();
$this->maybe_migrate_mc4wp_configuration();
$this->maybe_migrate_mailpoet_configuration();
$this->maybe_migrate_newsletter_configuration();

}

Expand Down Expand Up @@ -428,6 +430,42 @@ private function maybe_migrate_mailpoet_configuration() {

}

/**
* 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' );

}

/**
* Outputs the Debug Log and System Info view.
*
Expand All @@ -450,9 +488,10 @@ 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();
$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';
Expand Down
Loading
Loading