From 9415cb78eb1180c7b39fe3dc52189b44e1ff96f1 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 25 Oct 2024 11:09:18 +0800 Subject: [PATCH 1/3] Settings: Conditionally display Form Position and Element settings, only if Forms exist --- .../class-convertkit-settings-general.php | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/admin/section/class-convertkit-settings-general.php b/admin/section/class-convertkit-settings-general.php index ce7076e6..57f0c5e9 100644 --- a/admin/section/class-convertkit-settings-general.php +++ b/admin/section/class-convertkit-settings-general.php @@ -271,6 +271,9 @@ public function register_fields() { $this->name ); + // Initialize resource classes and perform a refresh if this hasn't yet been done. + $this->maybe_initialize_and_refresh_resources(); + foreach ( convertkit_get_supported_post_types() as $supported_post_type ) { // Get Post Type's Label. $post_type = get_post_type_object( $supported_post_type ); @@ -297,34 +300,39 @@ public function register_fields() { 'post_type_object' => $post_type, ) ); - add_settings_field( - $supported_post_type . '_form_position', - sprintf( - /* translators: Post Type Name, plural */ - __( 'Form Position (%s)', 'convertkit' ), - $post_type->label - ), - array( $this, 'default_form_position_callback' ), - $this->settings_key, - $this->name, - array( - 'label_for' => '_wp_convertkit_settings_' . $supported_post_type . '_form_position', - 'post_type' => $supported_post_type, - 'post_type_object' => $post_type, - ) - ); - add_settings_field( - $supported_post_type . '_form_position_element', - '', - array( $this, 'default_form_position_element_callback' ), - $this->settings_key, - $this->name, - array( - 'label_for' => '_wp_convertkit_settings_' . $supported_post_type . '_form_position_element', - 'post_type' => $supported_post_type, - 'post_type_object' => $post_type, - ) - ); + + // Show Form settings only if Forms exist. + if ( $this->forms->exist() ) { + add_settings_field( + $supported_post_type . '_form_position', + sprintf( + /* translators: Post Type Name, plural */ + __( 'Form Position (%s)', 'convertkit' ), + $post_type->label + ), + array( $this, 'default_form_position_callback' ), + $this->settings_key, + $this->name, + array( + 'label_for' => '_wp_convertkit_settings_' . $supported_post_type . '_form_position', + 'post_type' => $supported_post_type, + 'post_type_object' => $post_type, + ) + ); + + add_settings_field( + $supported_post_type . '_form_position_element', + '', + array( $this, 'default_form_position_element_callback' ), + $this->settings_key, + $this->name, + array( + 'label_for' => '_wp_convertkit_settings_' . $supported_post_type . '_form_position_element', + 'post_type' => $supported_post_type, + 'post_type_object' => $post_type, + ) + ); + } } add_settings_field( @@ -485,9 +493,6 @@ public function maybe_initialize_and_refresh_resources() { */ public function default_form_callback( $args ) { - // Initialize resource classes and perform a refresh if this hasn't yet been done. - $this->maybe_initialize_and_refresh_resources(); - // Bail if no Forms exist. if ( ! $this->forms->exist() ) { esc_html_e( 'No Forms exist in Kit.', 'convertkit' ); @@ -633,9 +638,6 @@ public function default_form_position_element_callback( $args ) { */ public function non_inline_form_callback( $args ) { - // Initialize resource classes and perform a refresh if this hasn't yet been done. - $this->maybe_initialize_and_refresh_resources(); - // Bail if no non-inline Forms exist. if ( ! $this->forms->non_inline_exist() ) { esc_html_e( 'No non-inline Forms exist in Kit.', 'convertkit' ); From 14062d9f1f66a7a33d6b895e78035296af0a5793 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 25 Oct 2024 16:56:17 +0800 Subject: [PATCH 2/3] Added test --- .../class-convertkit-settings-general.php | 20 +++++++---- .../general/PluginSettingsGeneralCest.php | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/admin/section/class-convertkit-settings-general.php b/admin/section/class-convertkit-settings-general.php index 57f0c5e9..44576676 100644 --- a/admin/section/class-convertkit-settings-general.php +++ b/admin/section/class-convertkit-settings-general.php @@ -263,6 +263,9 @@ public function enqueue_styles( $section ) { */ public function register_fields() { + // Initialize resource classes. + $this->maybe_initialize_and_refresh_resources(); + add_settings_field( 'account_name', __( 'Account Name', 'convertkit' ), @@ -272,8 +275,6 @@ public function register_fields() { ); // Initialize resource classes and perform a refresh if this hasn't yet been done. - $this->maybe_initialize_and_refresh_resources(); - foreach ( convertkit_get_supported_post_types() as $supported_post_type ) { // Get Post Type's Label. $post_type = get_post_type_object( $supported_post_type ); @@ -300,8 +301,7 @@ public function register_fields() { 'post_type_object' => $post_type, ) ); - - // Show Form settings only if Forms exist. + if ( $this->forms->exist() ) { add_settings_field( $supported_post_type . '_form_position', @@ -460,9 +460,17 @@ public function maybe_initialize_and_refresh_resources() { return; } - // Initialize and refresh resource classes, to ensure up to date resources are stored - // for when editing e.g. Pages. + // Initialize forms resource class. $this->forms = new ConvertKit_Resource_Forms( 'settings' ); + + // Don't refresh resources if we're not on the settings screen, as + // it's a resource intense process that can take several seconds. + // We don't want to block other parts of the admin UI. + if ( ! $this->on_settings_screen( $this->name ) ) { + return; + } + + // Refresh Forms. $this->forms->refresh(); // Also refresh Landing Pages, Tags and Posts. Whilst not displayed in the Plugin Settings, this ensures up to date diff --git a/tests/acceptance/general/PluginSettingsGeneralCest.php b/tests/acceptance/general/PluginSettingsGeneralCest.php index b15fa7fe..9ff9d59b 100644 --- a/tests/acceptance/general/PluginSettingsGeneralCest.php +++ b/tests/acceptance/general/PluginSettingsGeneralCest.php @@ -583,6 +583,42 @@ public function testEnableAndDisableCSSSetting(AcceptanceTester $I) $I->seeInSource(''); + + // Confirm no Form settings are displayed for Pages or Posts. + $I->dontSeeElementInDOM('#_wp_convertkit_settings_page_form'); + $I->dontSeeElementInDOM('#_wp_convertkit_settings_page_form_position'); + $I->dontSeeElementInDOM('#_wp_convertkit_settings_page_form_position_element'); + $I->dontSeeElementInDOM('#_wp_convertkit_settings_page_form_position_element_index'); + $I->dontSeeElementInDOM('#_wp_convertkit_settings_post_form'); + $I->dontSeeElementInDOM('#_wp_convertkit_settings_post_form_position'); + $I->dontSeeElementInDOM('#_wp_convertkit_settings_post_form_position_element'); + $I->dontSeeElementInDOM('#_wp_convertkit_settings_post_form_position_element_index'); + + // Confirm no Form settings are displayed for non-inline Forms. + $I->dontSeeElementInDOM('#_wp_convertkit_settings_non_inline_form'); + } + /** * 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 ff10ed4eea4b278dafce9d3f8fc14e80d187071b Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 25 Oct 2024 16:59:19 +0800 Subject: [PATCH 3/3] Tests: Fix version number --- tests/acceptance/general/PluginSettingsGeneralCest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/acceptance/general/PluginSettingsGeneralCest.php b/tests/acceptance/general/PluginSettingsGeneralCest.php index 9ff9d59b..38da9c8b 100644 --- a/tests/acceptance/general/PluginSettingsGeneralCest.php +++ b/tests/acceptance/general/PluginSettingsGeneralCest.php @@ -588,7 +588,7 @@ public function testEnableAndDisableCSSSetting(AcceptanceTester $I) * when using a Kit account with no resources, and that the applicable Form settings * fields do not display. * - * @since 2.6. + * @since 2.6.3 * * @param AcceptanceTester $I Tester. */