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

Settings: Only display Form Settings if Forms exist #731

Merged
merged 3 commits into from
Oct 28, 2024
Merged
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
82 changes: 46 additions & 36 deletions admin/section/class-convertkit-settings-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand All @@ -271,6 +274,7 @@ public function register_fields() {
$this->name
);

// Initialize resource classes and perform a refresh if this hasn't yet been done.
foreach ( convertkit_get_supported_post_types() as $supported_post_type ) {
// Get Post Type's Label.
$post_type = get_post_type_object( $supported_post_type );
Expand All @@ -297,34 +301,38 @@ 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,
)
);

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(
Expand Down Expand Up @@ -452,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
Expand Down Expand Up @@ -485,9 +501,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' );
Expand Down Expand Up @@ -633,9 +646,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' );
Expand Down
36 changes: 36 additions & 0 deletions tests/acceptance/general/PluginSettingsGeneralCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,42 @@ public function testEnableAndDisableCSSSetting(AcceptanceTester $I)
$I->seeInSource('<link rel="stylesheet" id="convertkit-button-css" href="' . $_ENV['TEST_SITE_WP_URL'] . '/wp-content/plugins/convertkit/resources/frontend/css/button.css');
}

/**
* Test that no PHP errors or notices are displayed on the Plugin's Setting screen
* when using a Kit account with no resources, and that the applicable Form settings
* fields do not display.
*
* @since 2.6.3
*
* @param AcceptanceTester $I Tester.
*/
public function testSettingsScreenWhenNoResources(AcceptanceTester $I)
{
// Setup Plugin using account that has no resources or data.
$I->setupConvertKitPluginCredentialsNoData($I);

// Go to the Plugin's Settings Screen.
$I->loadConvertKitSettingsGeneralScreen($I);

// Confirm 'No Forms exist in Kit' message displays.
$I->see('No Forms exist in Kit.');
$I->see('Click here to create your first form');
$I->seeInSource('<a href="https://app.kit.com/forms/new/?utm_source=wordpress&amp;utm_term=en_US&amp;utm_content=convertkit" target="_blank">');

// 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
Expand Down