From 5145bebae6b3c0beb6f1b30c89ba7c9215a41d26 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 20 Nov 2024 16:03:04 +0800 Subject: [PATCH 1/9] =?UTF-8?q?Change=20Category=E2=80=99s=20=E2=80=98Defa?= =?UTF-8?q?ult=E2=80=99=20value=20to=20-1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This matches the ‘Default’ setting on Posts and --- includes/class-convertkit-setup.php | 52 +++++++++++++++++++++++++++++ includes/class-convertkit-term.php | 20 ++++++++++- views/backend/term/fields-add.php | 4 +-- views/backend/term/fields-edit.php | 2 +- 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/includes/class-convertkit-setup.php b/includes/class-convertkit-setup.php index 3451ad31..40c2705b 100644 --- a/includes/class-convertkit-setup.php +++ b/includes/class-convertkit-setup.php @@ -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 */ @@ -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 0, change it to -1. + if ( $term_settings->get_form() === 0 ) { + $term_settings->save( + array( + 'form' => -1, + ) + ); + } + } + + } + /** * 2.5.4: Migrate WLM settings: * - Prefix any WishList Member to ConvertKit Form ID mappings with `form:`, diff --git a/includes/class-convertkit-term.php b/includes/class-convertkit-term.php index 6f219350..b396066a 100644 --- a/includes/class-convertkit-term.php +++ b/includes/class-convertkit-term.php @@ -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. @@ -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( $term_id, self::TERM_META_KEY, true ); + + return $result; } diff --git a/views/backend/term/fields-add.php b/views/backend/term/fields-add.php index 940ebcac..21d58bb8 100644 --- a/views/backend/term/fields-add.php +++ b/views/backend/term/fields-add.php @@ -18,9 +18,9 @@ array( 'convertkit-select2', ), - '0', + '-1', array( - '0' => esc_html__( 'Default', 'convertkit' ), + '-1' => esc_html__( 'Default', 'convertkit' ), ) ); ?> diff --git a/views/backend/term/fields-edit.php b/views/backend/term/fields-edit.php index 94b30a8e..dde19fd0 100644 --- a/views/backend/term/fields-edit.php +++ b/views/backend/term/fields-edit.php @@ -22,7 +22,7 @@ ), esc_attr( $convertkit_term->get_form() ), array( - '0' => esc_html__( 'Default', 'convertkit' ), + '-1' => esc_html__( 'Default', 'convertkit' ), ) ); ?> From fa03df5b329341c8fe82bb485207a918494357c2 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 20 Nov 2024 16:34:52 +0800 Subject: [PATCH 2/9] Started tests --- .../forms/general/CategoryFormCest.php | 73 ++++++++++++++++++- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/tests/acceptance/forms/general/CategoryFormCest.php b/tests/acceptance/forms/general/CategoryFormCest.php index 157d09c9..9a24f186 100644 --- a/tests/acceptance/forms/general/CategoryFormCest.php +++ b/tests/acceptance/forms/general/CategoryFormCest.php @@ -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. @@ -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 From 59f56b4acc0cfa143bf8cba7aacefe0f95986db0 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 20 Nov 2024 16:39:43 +0800 Subject: [PATCH 3/9] Coding standards --- tests/acceptance/forms/general/CategoryFormCest.php | 4 ++-- wp-convertkit.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/acceptance/forms/general/CategoryFormCest.php b/tests/acceptance/forms/general/CategoryFormCest.php index 9a24f186..134e4711 100644 --- a/tests/acceptance/forms/general/CategoryFormCest.php +++ b/tests/acceptance/forms/general/CategoryFormCest.php @@ -573,9 +573,9 @@ public function testCategoryDefaultSettingsMigratedToNewValueOnUpgrade(Acceptanc [ 'meta' => [ '_wp_convertkit_term_meta' => array( - 'form' => 0, + 'form' => 0, 'form_position' => '', - ) + ), ], ] ); diff --git a/wp-convertkit.php b/wp-convertkit.php index 24ab686c..8962abc9 100644 --- a/wp-convertkit.php +++ b/wp-convertkit.php @@ -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 @@ -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' ); From 599f71c24e9d5e669eaaeb92e9b50fb1333e7286 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 20 Nov 2024 17:31:41 +0800 Subject: [PATCH 4/9] PHPStan compat. --- includes/class-convertkit-setup.php | 4 ++-- includes/class-convertkit-term.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-convertkit-setup.php b/includes/class-convertkit-setup.php index 40c2705b..49969336 100644 --- a/includes/class-convertkit-setup.php +++ b/includes/class-convertkit-setup.php @@ -161,8 +161,8 @@ private function migrate_term_default_form_settings() { foreach ( $query->terms as $term_id ) { $term_settings = new ConvertKit_Term( $term_id ); - // If the Form setting is 0, change it to -1. - if ( $term_settings->get_form() === 0 ) { + // 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, diff --git a/includes/class-convertkit-term.php b/includes/class-convertkit-term.php index b396066a..7c99bbaa 100644 --- a/includes/class-convertkit-term.php +++ b/includes/class-convertkit-term.php @@ -154,7 +154,7 @@ public function save( $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( $term_id, self::TERM_META_KEY, true ); + $this->settings = get_term_meta( $this->term_id, self::TERM_META_KEY, true ); return $result; From 78af4af6fdc7bf15c408838060b4f26cced93a96 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 20 Nov 2024 18:06:06 +0800 Subject: [PATCH 5/9] =?UTF-8?q?Add=20=E2=80=98None=E2=80=99=20support=20fo?= =?UTF-8?q?r=20Category=20Form?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-convertkit-output.php | 8 +++++++- includes/class-convertkit-term.php | 13 +++++++++++++ views/backend/term/fields-add.php | 4 ++++ views/backend/term/fields-edit.php | 4 ++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/includes/class-convertkit-output.php b/includes/class-convertkit-output.php index 69cb16bb..e6d4df15 100644 --- a/includes/class-convertkit-output.php +++ b/includes/class-convertkit-output.php @@ -652,9 +652,15 @@ private function get_post_form_id( $post_id ) { if ( $term_settings->has_form() ) { return $term_settings->get_form(); } + + // If the Term specifies that no Form should be used, return false. + if ( $term_settings->uses_no_form() ) { + return false; + } } - // If here, use the Plugin's Default Form. + // If here, all Terms were set to display the Default Form. + // Therefore use the Plugin's Default Form. return $this->settings->get_default_form( get_post_type( $post_id ) ); } diff --git a/includes/class-convertkit-term.php b/includes/class-convertkit-term.php index 7c99bbaa..57278919 100644 --- a/includes/class-convertkit-term.php +++ b/includes/class-convertkit-term.php @@ -113,6 +113,19 @@ public function uses_default_form() { } + /** + * Whether the Post's Form setting is set to 'None' + * + * @since 2.6.6 + * + * @return bool + */ + public function uses_no_form() { + + return ( $this->settings['form'] === 0 ); + + } + /** * Returns the form position setting for the Term * on the Term archive. diff --git a/views/backend/term/fields-add.php b/views/backend/term/fields-add.php index 21d58bb8..fb46ace4 100644 --- a/views/backend/term/fields-add.php +++ b/views/backend/term/fields-add.php @@ -21,6 +21,7 @@ '-1', array( '-1' => esc_html__( 'Default', 'convertkit' ), + '0' => esc_html__( 'None', 'convertkit' ), ) ); ?> @@ -32,6 +33,9 @@
+ + +
get_form() ), array( '-1' => esc_html__( 'Default', 'convertkit' ), + '0' => esc_html__( 'None', 'convertkit' ), ) ); ?> @@ -34,6 +35,9 @@
+ + +
Date: Wed, 20 Nov 2024 18:22:37 +0800 Subject: [PATCH 6/9] Added Tests --- .../forms/general/CategoryFormCest.php | 131 ++++++++++++++++++ views/backend/term/fields-add.php | 2 +- views/backend/term/fields-edit.php | 2 +- 3 files changed, 133 insertions(+), 2 deletions(-) diff --git a/tests/acceptance/forms/general/CategoryFormCest.php b/tests/acceptance/forms/general/CategoryFormCest.php index 134e4711..a07802f0 100644 --- a/tests/acceptance/forms/general/CategoryFormCest.php +++ b/tests/acceptance/forms/general/CategoryFormCest.php @@ -82,6 +82,67 @@ public function testAddCategoryWithValidFormSetting(AcceptanceTester $I) $I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID']); } + /** + * Test that no Form is displayed when the user: + * - Creates a Category in WordPress, selecting 'None' as the ConvertKit Form to display, + * - Creates a WordPress Post assigned to the created Category. + * + * @since 2.6.6 + * + * @param AcceptanceTester $I Tester. + */ + public function testAddCategoryWithNoFormSetting(AcceptanceTester $I) + { + // Navigate to Posts > Categories. + $I->amOnAdminPage('edit-tags.php?taxonomy=category'); + + // Confirm that settings have label[for] attributes. + $I->seeInSource('