Skip to content

Commit

Permalink
Block Visibility: Support non-inline Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
n7studios committed Nov 21, 2024
1 parent 2f0eaed commit 06e5762
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
DB_USER: root
DB_PASS: root
DB_HOST: localhost
INSTALL_PLUGINS: "admin-menu-editor autoptimize beaver-builder-lite-version contact-form-7 classic-editor custom-post-type-ui elementor forminator jetpack-boost woocommerce wordpress-seo wpforms-lite litespeed-cache wp-crontrol wp-super-cache w3-total-cache wp-fastest-cache wp-optimize sg-cachepress" # Don't include this repository's Plugin here.
INSTALL_PLUGINS: "admin-menu-editor autoptimize beaver-builder-lite-version block-visibility contact-form-7 classic-editor custom-post-type-ui elementor forminator jetpack-boost woocommerce wordpress-seo wpforms-lite litespeed-cache wp-crontrol wp-super-cache w3-total-cache wp-fastest-cache wp-optimize sg-cachepress" # Don't include this repository's Plugin here.
INSTALL_PLUGINS_URLS: "https://downloads.wordpress.org/plugin/convertkit-for-woocommerce.1.6.4.zip http://cktestplugins.wpengine.com/wp-content/uploads/2024/01/convertkit-action-filter-tests.zip" # URLs to specific third party Plugins
CONVERTKIT_API_KEY: ${{ secrets.CONVERTKIT_API_KEY }} # ConvertKit API Key, stored in the repository's Settings > Secrets
CONVERTKIT_API_SECRET: ${{ secrets.CONVERTKIT_API_SECRET }} # ConvertKit API Secret, stored in the repository's Settings > Secrets
Expand Down
7 changes: 7 additions & 0 deletions includes/blocks/class-convertkit-block-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ public function get_default_values() {
*/
public function render( $atts ) {

// Check if the Block Visibility Plugin permits displaying this block.
if ( ! $this->is_block_visible( $atts ) ) {
// Block should not be displayed due to Block Visibility Plugin conditions.
// Return a blank string now.
return '';
}

// Parse shortcode attributes, defining fallback defaults if required.
$atts = shortcode_atts(
$this->get_default_values(),
Expand Down
45 changes: 45 additions & 0 deletions includes/blocks/class-convertkit-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,49 @@ public function is_block_editor_request() {

}

/**
* If the Block Visiblity Plugin is active, run the block through its conditions now.
* We don't wait for Block Visibility to do this, as it performs this on the
* `render_block` filter, by which time the code in this method has fully executed,
* meaning any non-inline Forms will have had their scripts added to the
* `convertkit_output_scripts_footer` hook.
* As a result, the non-inline Form will always display, regardless of whether
* Block Visibility's conditions are met.
* We deliberately don't output non-inline Forms in their block, instead deferring
* to the `convertkit_output_scripts_footer` hook, to ensure the non-inline Forms
* styling are not constrained by the Theme's width, layout or other properties.
*
* @since 2.6.6
*
* @param array $atts Block Attributes.
* @return bool Display Block
*/
public function is_block_visible( $atts ) {

// Display the block if the Block Visibility Plugin isn't active.
if ( ! function_exists( '\BlockVisibility\Frontend\render_with_visibility' ) ) {
return true;
}

// Determine whether the block should display.
$display_block = \BlockVisibility\Frontend\render_with_visibility(
'block',
array(
'blockName' => 'convertkit-' . $this->get_name(),
'attrs' => $atts,
)
);

// If the content returned is a blank string, conditions on this block set
// by the user in the Block Visibility Plugin resulted in the block not displaying.
// Don't display it.
if ( empty( $display_block ) ) {
return false;
}

// If here, the block can be displayed.
return true;

}

}
49 changes: 49 additions & 0 deletions tests/acceptance/forms/blocks-shortcodes/PageBlockFormCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,55 @@ public function testFormBlockWithWPRocketPluginMinifyJS(AcceptanceTester $I)
$I->deactivateThirdPartyPlugin($I, 'wp-rocket');
}

/**
* Test that a non-inline Form is not displayed when specified in the Form Block and the
* Block Visibility Plugin sets the block to not display.
*
* @since 2.6.6
*
* @param AcceptanceTester $I Tester.
*/
public function testFormBlockWithNonInlineFormAndBlockVisibilityPlugin(AcceptanceTester $I)
{
// Setup Plugin and Resources.
$I->setupConvertKitPlugin($I);
$I->setupConvertKitPluginResources($I);

// Activate Block Visibility Plugin.
$I->activateThirdPartyPlugin($I, 'block-visibility');

// Create a Page as if it were create in Gutenberg with the Form block
// set to display a non-inline Form, and the Block Visibility Plugin
// set to hide the block.
$pageID = $I->havePostInDatabase(
[
'post_type' => 'page',
'post_title' => 'Kit: Page: Form: Block: Block Visibility',
'post_content' => '<!-- wp:convertkit/form {"form":"' . $_ENV['CONVERTKIT_API_FORM_FORMAT_STICKY_BAR_ID'] . '","blockVisibility":{"controlSets":[{"id":1,"enable":true,"controls":{"location":{"ruleSets":[{"enable":true,"rules":[{"field":"postType","operator":"any","value":["page"]}]}],"hideOnRuleSets":true}}}]}} /-->',
'meta_input' => [
// Configure ConvertKit Plugin to not display a default Form.
'_wp_convertkit_post_meta' => [
'form' => '0',
'landing_page' => '',
'tag' => '',
],
],
]
);

// Load Page.
$I->amOnPage('?p=' . $pageID);

// 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]');

// Deactivate Block Visibility Plugin.
$I->deactivateThirdPartyPlugin($I, 'block-visibility');
}

/**
* 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

0 comments on commit 06e5762

Please sign in to comment.