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: Add None Option #743

Merged
merged 5 commits into from
Nov 25, 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
8 changes: 7 additions & 1 deletion includes/class-convertkit-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );

}
Expand Down
13 changes: 13 additions & 0 deletions includes/class-convertkit-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
135 changes: 135 additions & 0 deletions tests/acceptance/forms/general/CategoryFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testAddCategoryWithValidFormSetting(AcceptanceTester $I)
'#wp-convertkit-form',
[
'Default',
'None',
]
);

Expand Down Expand Up @@ -82,6 +83,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 testAddCategoryWithNoneFormSetting(AcceptanceTester $I)
{
// Navigate to Posts > Categories.
$I->amOnAdminPage('edit-tags.php?taxonomy=category');

// Confirm that settings have label[for] attributes.
$I->seeInSource('<label for="wp-convertkit-form">');

// Create Category.
$I->fillField('tag-name', 'Kit: Create Category: None');
$I->fillSelect2Field($I, '#select2-wp-convertkit-form-container', 'None');

// Check the order of the Form resources are alphabetical, with the Default option prepending the Forms.
$I->checkSelectFormOptionOrder(
$I,
'#wp-convertkit-form',
[
'Default',
'None',
]
);

// Save.
$I->click('Add New Category');

// Confirm Category saved.
$I->waitForElementVisible('.notice-success');

// Get the Category ID from the table.
$termID = (int) str_replace( 'tag-', '', $I->grabAttributeFrom('#the-list tr:first-child', 'id') );

// Create Post, assigned to ConvertKit Category.
$postID = $I->havePostInDatabase(
[
'post_type' => 'post',
'post_title' => 'Kit: Inherit None Form from Add Category',
'tax_input' => [
[ 'category' => (int) $termID ],
],
]
);

// 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 no ConvertKit Form is displayed.
$I->dontSeeElementInDOM('form[data-sv-form]');
}

/**
* Test that the expected Form is displayed when the user:
* - Edits an existing Category in WordPress, selecting the ConvertKit Form to display,
Expand Down Expand Up @@ -123,6 +185,7 @@ public function testEditCategoryWithValidFormSetting(AcceptanceTester $I)
'#wp-convertkit-form',
[
'Default',
'None',
]
);

Expand Down Expand Up @@ -152,6 +215,76 @@ public function testEditCategoryWithValidFormSetting(AcceptanceTester $I)
$I->seeFormOutput($I, $_ENV['CONVERTKIT_API_FORM_ID']);
}

/**
* Test that no Form is displayed when the user:
* - Edits an existing Category in WordPress, selecting the 'None' ConvertKit Form option,
* - Creates a WordPress Post assigned to the edited Category.
*
* @since 2.6.6
*
* @param AcceptanceTester $I Tester.
*/
public function testEditCategoryWithNoneFormSetting(AcceptanceTester $I)
{
// Create Category.
$termID = $I->haveTermInDatabase( 'Kit: Edit Category: None', 'category' );
$termID = $termID[0];

// Create Post, assigned to ConvertKit Category.
$postID = $I->havePostInDatabase(
[
'post_type' => 'post',
'post_title' => 'Kit: Inherit No Form from Edit Category',
'tax_input' => [
[ 'category' => (int) $termID ],
],
]
);

// Edit the Term, defining a Form.
$I->amOnAdminPage('term.php?taxonomy=category&tag_ID=' . $termID);

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

// Confirm that settings have label[for] attributes.
$I->seeInSource('<label for="wp-convertkit-form">');

// Check the order of the Form resources are alphabetical, with the Default option prepending the Forms.
$I->checkSelectFormOptionOrder(
$I,
'#wp-convertkit-form',
[
'Default',
'None',
]
);

// Change Form to None.
$I->fillSelect2Field($I, '#select2-wp-convertkit-form-container', 'None');

// Click Update.
$I->click('Update');

// Wait for the page to load.
$I->waitForElementVisible('#wpfooter');

// Check that the update succeeded.
$I->seeElementInDOM('div.notice-success');

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

// 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 no ConvertKit Form is displayed.
$I->dontSeeElementInDOM('form[data-sv-form]');
}

/**
* Test that the expected Form is displayed on the Category archive above the Posts
* when the user:
Expand Down Expand Up @@ -310,6 +443,7 @@ public function testEditCategoryWithFormPositionBefore(AcceptanceTester $I)
'#wp-convertkit-form',
[
'Default',
'None',
]
);

Expand Down Expand Up @@ -390,6 +524,7 @@ public function testEditCategoryWithFormPositionAfter(AcceptanceTester $I)
'#wp-convertkit-form',
[
'Default',
'None',
]
);

Expand Down
2 changes: 2 additions & 0 deletions tests/acceptance/general/RefreshResourcesButtonCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ public function testRefreshResourcesOnAddCategory(AcceptanceTester $I)
'#wp-convertkit-form',
[
'Default',
'None',
]
);

Expand Down Expand Up @@ -339,6 +340,7 @@ public function testRefreshResourcesOnEditCategory(AcceptanceTester $I)
'#wp-convertkit-form',
[
'Default',
'None',
]
);

Expand Down
4 changes: 4 additions & 0 deletions views/backend/term/fields-add.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'-1',
array(
'-1' => esc_html__( 'Default', 'convertkit' ),
'0' => esc_html__( 'None', 'convertkit' ),
)
);
?>
Expand All @@ -32,6 +33,9 @@
<code><?php esc_html_e( 'Default', 'convertkit' ); ?></code>
<?php esc_html_e( ': Display a form based on the Post\'s settings.', 'convertkit' ); ?>
<br />
<code><?php esc_html_e( 'None', 'convertkit' ); ?></code>
<?php esc_html_e( ': do not display a form.', 'convertkit' ); ?>
<br />
<?php
printf(
/* translators: Taxonomy Name */
Expand Down
4 changes: 4 additions & 0 deletions views/backend/term/fields-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
esc_attr( $convertkit_term->get_form() ),
array(
'-1' => esc_html__( 'Default', 'convertkit' ),
'0' => esc_html__( 'None', 'convertkit' ),
)
);
?>
Expand All @@ -34,6 +35,9 @@
<code><?php esc_html_e( 'Default', 'convertkit' ); ?></code>
<?php esc_html_e( ': Display a form based on the Post\'s settings.', 'convertkit' ); ?>
<br />
<code><?php esc_html_e( 'None', 'convertkit' ); ?></code>
<?php esc_html_e( ': do not display a form.', 'convertkit' ); ?>
<br />
<?php
printf(
/* translators: Taxonomy Name */
Expand Down