From f64c65248859eed7c868c56d3c2a565ded296d84 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 29 Jul 2026 21:51:54 +0800 Subject: [PATCH 1/5] Remove Legacy Forms from Settings Dropdowns --- .../class-convertkit-block-form-builder.php | 29 ++- .../blocks/class-convertkit-block-form.php | 30 ++- includes/class-convertkit-resource-forms.php | 71 ++++++- ...onvertkit-plugin-sidebar-post-settings.php | 34 +++- resources/backend/js/gutenberg.js | 81 ++++++++ .../PageBlockFormBuilderCest.php | 185 +++++++++--------- .../blocks-shortcodes/PageBlockFormCest.php | 153 ++++++++++----- .../forms/general/EditFormLinkCest.php | 47 ----- .../EndToEnd/forms/general/WidgetFormCest.php | 78 -------- .../forms/post-types/BlockEditorFormCest.php | 140 ++++++++----- .../post-types/ClassicEditorFormCest.php | 52 ----- .../general/other/SelectOptionOrderCest.php | 144 ++++++++++++++ .../other/ContactForm7FormCest.php | 29 --- .../integrations/other/ForminatorCest.php | 29 --- .../integrations/wlm/WishListMemberCest.php | 64 ------ tests/Integration/ResourceFormsTest.php | 85 ++++++++ views/backend/subscription-dropdown-field.php | 25 ++- 17 files changed, 750 insertions(+), 526 deletions(-) diff --git a/includes/blocks/class-convertkit-block-form-builder.php b/includes/blocks/class-convertkit-block-form-builder.php index 16833a0fd..3d0e83cfb 100644 --- a/includes/blocks/class-convertkit-block-form-builder.php +++ b/includes/blocks/class-convertkit-block-form-builder.php @@ -511,17 +511,27 @@ public function get_supports() { */ public function get_fields() { - // Get Kit Forms. - $forms = new ConvertKit_Resource_Forms( 'block_form_builder' ); - $forms_options = array(); + // Get Kit Forms. Non-legacy forms populate the sidebar dropdown; + // legacy forms are exposed separately as a fallback so the sidebar can + // keep displaying a previously-saved legacy form as the current + // selection without offering other legacy forms as new choices. + $forms = new ConvertKit_Resource_Forms( 'block_form_builder' ); + $forms_options = array(); + $forms_legacy_options = array(); if ( $forms->exist() ) { foreach ( $forms->get() as $form ) { - // Legacy forms don't include a `format` key, so define them as inline. - $forms_options[ $form['id'] ] = sprintf( + $label = sprintf( '%s [%s]', sanitize_text_field( $form['name'] ), + // Legacy forms don't include a `format` key, so define them as inline. ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) ); + + if ( ! empty( $form['format'] ) ) { + $forms_options[ $form['id'] ] = $label; + } else { + $forms_legacy_options[ $form['id'] ] = $label; + } } } @@ -569,10 +579,11 @@ public function get_fields() { ), ), 'form_id' => array( - 'label' => __( 'Form', 'convertkit' ), - 'type' => 'select', - 'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ), - 'values' => $forms_options, + 'label' => __( 'Form', 'convertkit' ), + 'type' => 'select', + 'description' => __( 'The Kit form to add the subscriber to. Useful if you want to send an incentive email.', 'convertkit' ), + 'values' => $forms_options, + 'legacy_values' => $forms_legacy_options, ), 'tag_id' => array( 'label' => __( 'Tag', 'convertkit' ), diff --git a/includes/blocks/class-convertkit-block-form.php b/includes/blocks/class-convertkit-block-form.php index f5e121ba8..59acd50cf 100644 --- a/includes/blocks/class-convertkit-block-form.php +++ b/includes/blocks/class-convertkit-block-form.php @@ -262,30 +262,42 @@ public function get_supports() { */ public function get_fields() { - // Get ConvertKit Forms. + // Get ConvertKit Forms. Non-legacy forms populate the sidebar dropdown; + // legacy forms are exposed separately as a fallback so the sidebar can + // keep displaying a previously-saved legacy form as the current + // selection without offering other legacy forms as new choices. $forms = array(); + $legacy_forms = array(); $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' ); if ( $convertkit_forms->exist() ) { foreach ( $convertkit_forms->get() as $form ) { - // Legacy forms don't include a `format` key, so define them as inline. - $forms[ absint( $form['id'] ) ] = sprintf( + $label = sprintf( '%s [%s]', sanitize_text_field( $form['name'] ), + // Legacy forms don't include a `format` key, so define them as inline. ( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) ); + + if ( ! empty( $form['format'] ) ) { + $forms[ absint( $form['id'] ) ] = $label; + } else { + $legacy_forms[ absint( $form['id'] ) ] = $label; + } } } return array( 'form' => array( - 'label' => __( 'Form', 'convertkit' ), - 'type' => 'resource', - 'resource' => 'forms', - 'values' => $forms, - 'data' => array( + 'label' => __( 'Form', 'convertkit' ), + 'type' => 'resource', + 'resource' => 'forms', + 'values' => $forms, + 'legacy_values' => $legacy_forms, + 'data' => array( // Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format // (modal, slide in, sticky bar) and output a message in the block editor for the preview to explain - // why some formats cannot be previewed. + // why some formats cannot be previewed. Includes legacy forms so the preview code can still find + // them when a saved block references a legacy form. 'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ), ), ), diff --git a/includes/class-convertkit-resource-forms.php b/includes/class-convertkit-resource-forms.php index 966361277..277636756 100644 --- a/includes/class-convertkit-resource-forms.php +++ b/includes/class-convertkit-resource-forms.php @@ -153,6 +153,42 @@ public function non_inline_exist() { } + /** + * Returns all non-legacy forms (any v4 format: inline, modal, slide in + * or sticky bar) based on the sort order. Legacy forms lack a `format` + * key and are therefore excluded. + * + * @since 3.3.7 + * + * @return bool|array + */ + public function get_non_legacy() { + + // If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated + // addon), or a WordPress site updates this Plugin before other ConvertKit Plugins, + // get_by() won't be available and will cause an E_ERROR, crashing the site. + // @see https://wordpress.org/support/topic/error-1795/. + if ( ! method_exists( $this, 'get_by' ) ) { // @phpstan-ignore-line Older WordPress Libraries won't have this function. + return false; + } + + return $this->get_by( 'format', array( 'inline', 'modal', 'slide in', 'sticky bar' ) ); + + } + + /** + * Returns whether any non-legacy forms exist in the options table. + * + * @since 3.3.7 + * + * @return bool + */ + public function non_legacy_exist() { + + return (bool) $this->get_non_legacy(); + + } + /** * Determines if the given Form ID is a legacy Form or Landing Page. * @@ -196,7 +232,7 @@ public function is_legacy( $id ) { public function get_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { return $this->get_select_field( - $this->get(), + $this->get_forms_for_select_field( $selected_option ), $name, $id, $css_classes, @@ -224,7 +260,7 @@ public function get_select_field_all( $name, $id, $css_classes, $selected_option public function output_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { $this->output_select_field( - $this->get(), + $this->get_forms_for_select_field( $selected_option ), $name, $id, $css_classes, @@ -236,6 +272,37 @@ public function output_select_field_all( $name, $id, $css_classes, $selected_opt } + /** + * Returns the array of forms to display in an "all forms" dropdown: every + * non-legacy form, plus the currently-selected legacy form (if any) so + * existing saved legacy assignments continue to render as selected in the + * UI without exposing other legacy forms as new selection options. + * + * @since 3.3.7 + * + * @param string|int $selected_option Currently-selected form ID. + * @return array + */ + private function get_forms_for_select_field( $selected_option ) { + + $forms = $this->get_non_legacy(); + if ( ! is_array( $forms ) ) { + $forms = array(); + } + + // If the currently-selected form is a legacy form, append it so the + // dropdown shows the saved value as selected. + if ( $selected_option && $this->is_legacy( $selected_option ) ) { + $legacy_form = $this->get_by_id( (int) $selected_option ); + if ( $legacy_form ) { + $forms[] = $legacy_form; + } + } + + return $forms; + + } + /** * Returns a