Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Categories: Forms: Change 'Default' value from -1 to 0 #742

Merged
merged 10 commits into from
Nov 25, 2024
52 changes: 52 additions & 0 deletions includes/class-convertkit-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function update() {
return;
}

/**
* 2.6.6: Migrate 'Default' Form value in _wp_convertkit_term_meta[form] from 0 to -1,
* to match Posts.
*/
if ( version_compare( $current_version, '2.6.6', '<' ) ) {
$this->migrate_term_default_form_settings();
}

/**
* 2.5.4: Migrate WishList Member to ConvertKit Form Mappings
*/
Expand Down Expand Up @@ -121,6 +129,50 @@ public function update() {

}

/**
* Change the Default value of 0 to -1 in wp_convertkit_term_meta[form], to
* match how the Default value is stored in Posts.
*
* @since 2.6.6
*/
private function migrate_term_default_form_settings() {

// Get all Terms that have ConvertKit settings defined.
$query = new WP_Term_Query(
array(
'taxonomy' => 'category',
'hide_empty' => false,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_wp_convertkit_term_meta',
'comparison' => 'EXISTS',
),
),
)
);

// Bail if no Terms exist.
if ( ! $query->terms ) {
return;
}

// Iterate through Terms, mapping settings.
foreach ( $query->terms as $term_id ) {
$term_settings = new ConvertKit_Term( $term_id );

// If the Form setting is Default i.e. it does not have a formchange it from 0 to -1.
if ( ! $term_settings->has_form() ) {
$term_settings->save(
array(
'form' => -1,
)
);
}
}

}

/**
* 2.5.4: Migrate WLM settings:
* - Prefix any WishList Member to ConvertKit Form ID mappings with `form:`,
Expand Down
20 changes: 19 additions & 1 deletion includes/class-convertkit-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public function has_form() {

}

/**
* Whether the Term is set to use the Plugin's Default Form Setting.
*
* @since 2.6.6
*
* @return bool
*/
public function uses_default_form() {

return ( $this->settings['form'] === -1 );

}

/**
* Returns the form position setting for the Term
* on the Term archive.
Expand Down Expand Up @@ -138,7 +151,12 @@ public function has_form_position() {
*/
public function save( $meta ) {

return update_term_meta( $this->term_id, self::TERM_META_KEY, $meta );
$result = update_term_meta( $this->term_id, self::TERM_META_KEY, array_merge( $this->get(), $meta ) );

// Reload meta in class, to reflect changes.
$this->settings = get_term_meta( $this->term_id, self::TERM_META_KEY, true );

return $result;

}

Expand Down
73 changes: 70 additions & 3 deletions tests/acceptance/forms/general/CategoryFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,14 @@ public function testAddNewPostUsingDefaultFormWithCategoryCreatedBefore1960(Acce
}

/**
* Tests that existing Category settings stored in the Term Meta key [] are
* automatically migrated when updating the Plugin to 2.4.9.1 or higher.
* Tests that existing Category settings stored in the Term Meta key `ck_default_form` are
* automatically migrated to the new key `_wp_convertkit_term_meta` when updating the Plugin to 2.4.9.1 or higher.
*
* @since 2.4.9.1
*
* @param AcceptanceTester $I Tester.
*/
public function testCategorySettingsMigratedOnUpgrade(AcceptanceTester $I)
public function testCategorySettingsMigratedToNewTermKeyOnUpgrade(AcceptanceTester $I)
{
// Create Category as if it were created / edited when the ConvertKit Plugin < 2.4.9.1
// was active.
Expand Down Expand Up @@ -555,6 +555,73 @@ public function testCategorySettingsMigratedOnUpgrade(AcceptanceTester $I)
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID']);
}

/**
* Tests that existing Category settings set to 'Default' have their values automatically migrated
* from 0 to -1, to match how Post settings are stored, when updating the Plugin to 2.6.6 or higher.
*
* @since 2.6.6
*
* @param AcceptanceTester $I Tester.
*/
public function testCategoryDefaultSettingsMigratedToNewValueOnUpgrade(AcceptanceTester $I)
{
// Create Category as if it were created / edited when the ConvertKit Plugin < 2.6.6
// was active.
$termID = $I->haveTermInDatabase(
'Kit 2.6.6 and earlier',
'category',
[
'meta' => [
'_wp_convertkit_term_meta' => array(
'form' => 0,
'form_position' => '',
),
],
]
);
$termID = $termID[0];

// Create Post, assigned to Category.
$postID = $I->havePostInDatabase(
[
'post_type' => 'post',
'post_title' => 'Kit: Default Form: Category Created before 2.6.6',
'tax_input' => [
[ 'category' => $termID ],
],
]
);

// Downgrade the Plugin version to simulate an upgrade.
$I->haveOptionInDatabase('convertkit_version', '2.6.5');

// Load admin screen.
$I->amOnAdminPage('index.php');

// Check Category settings structure has been updated to the new meta key.
$I->seeTermMetaInDatabase(
[
'term_id' => $termID,
'meta_key' => '_wp_convertkit_term_meta',
'meta_value' => [
'form' => -1,
'form_position' => '',
],
]
);

// Load the Post on the frontend site.
$I->amOnPage('/?p=' . $postID);

// Check that no PHP warnings or notices were output.
$I->checkNoWarningsAndNoticesOnScreen($I);

// Confirm that the default ConvertKit Form is output in the DOM.
// This confirms that there is only one script on the page for this form, which renders the form,
// and that the Category settings were correctly mapped.
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID']);
}

/**
* 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
Expand Down
4 changes: 2 additions & 2 deletions views/backend/term/fields-add.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
array(
'convertkit-select2',
),
'0',
'-1',
array(
'0' => esc_html__( 'Default', 'convertkit' ),
'-1' => esc_html__( 'Default', 'convertkit' ),
)
);
?>
Expand Down
2 changes: 1 addition & 1 deletion views/backend/term/fields-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
),
esc_attr( $convertkit_term->get_form() ),
array(
'0' => esc_html__( 'Default', 'convertkit' ),
'-1' => esc_html__( 'Default', 'convertkit' ),
)
);
?>
Expand Down
4 changes: 2 additions & 2 deletions wp-convertkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Plugin Name: Kit (formerly ConvertKit)
* Plugin URI: https://kit.com/
* Description: Display Kit (formerly ConvertKit) email subscription forms, landing pages, products, broadcasts and more.
* Version: 2.6.5
* Version: 2.6.6
* Author: Kit
* Author URI: https://kit.com/
* Text Domain: convertkit
Expand All @@ -25,7 +25,7 @@
define( 'CONVERTKIT_PLUGIN_FILE', plugin_basename( __FILE__ ) );
define( 'CONVERTKIT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CONVERTKIT_PLUGIN_PATH', __DIR__ );
define( 'CONVERTKIT_PLUGIN_VERSION', '2.6.5' );
define( 'CONVERTKIT_PLUGIN_VERSION', '2.6.6' );
define( 'CONVERTKIT_OAUTH_CLIENT_ID', 'HXZlOCj-K5r0ufuWCtyoyo3f688VmMAYSsKg1eGvw0Y' );
define( 'CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI', 'https://app.kit.com/wordpress/redirect' );

Expand Down