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

fix: enforce display condition to return string in render_block filter #2192

Merged
merged 2 commits into from
Apr 22, 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: 5 additions & 3 deletions inc/plugins/class-block-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Block_Conditions {
*/
public function init() {
if ( get_option( 'themeisle_blocks_settings_block_conditions', true ) ) {
add_action( 'render_block', array( $this, 'render_blocks' ), 999, 2 );
add_filter( 'render_block', array( $this, 'render_blocks' ), 999, 2 );
add_action( 'wp_loaded', array( $this, 'add_attributes_to_blocks' ), 999 );
}
}
Expand All @@ -36,6 +36,8 @@ public function init() {
*
* @param string $block_content Content of block.
* @param array $block Block Attributes.
*
* @return string
*
* @since 1.7.0
* @access public
Expand All @@ -46,12 +48,12 @@ public function render_blocks( $block_content, $block ) {
$display = $this->evaluate_condition_collection( $block['attrs']['otterConditions'] );

if ( false === $display ) {
return;
return '';
}

$enhanced_content = $this->should_add_hide_css_class( $this->get_hide_css_condition( $block['attrs']['otterConditions'] ), $block_content );

if ( false !== $enhanced_content ) {
if ( false !== $enhanced_content && is_string( $enhanced_content ) ) {
return $enhanced_content;
}
}
Expand Down
49 changes: 49 additions & 0 deletions src/blocks/test/e2e/blocks/block-conditions.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* WordPress dependencies
*/
import { test, expect } from '@wordpress/e2e-test-utils-playwright';
import { tryLoginIn } from '../utils';

test.describe( 'Block Conditions', () => {
test.beforeEach( async({ admin, requestUtils, page }) => {
await tryLoginIn( page, 'admin', 'password' );
await admin.createNewPost();
});

test.afterEach( async({ page }) => {

/**
* Because some conditions require an user to be logged in, we need to log in the user after each test so that we do not break the next test.
*/
await tryLoginIn( page, 'admin', 'password' );
});

test( 'check logged out users', async({ editor, page, admin, requestUtils }) => {
await editor.insertBlock({
name: 'core/image',
attributes: {
url: 'https://mllj2j8xvfl0.i.optimole.com/cb:jC7e.37109/w:794/h:397/q:mauto/dpr:2.0/f:best/https://themeisle.com/blog/wp-content/uploads/2021/01/How-to-Change-Font-in-WordPress-Theme.png',
otterConditions: [
[
{
type: 'loggedInUser'
}
]
]
}
});

const postId = await editor.publishPost();

// Check the block for logged in users.
await page.goto( `/?p=${postId}` );
await expect( page.locator( '#wp--skip-link--target img' ) ).toBeVisible();

// Check the block for logged out users.
await page.getByRole( 'menuitem', { name: 'Howdy, admin' }).hover();
await page.waitForTimeout( 200 );
await page.getByRole( 'menuitem', { name: 'Log Out' }).click();
await page.goto( `/?p=${postId}` );
await expect( page.locator( '#wp--skip-link--target img' ) ).toBeHidden();
});
});
7 changes: 7 additions & 0 deletions src/blocks/test/e2e/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ export function deleteFile( filePath ) {
unlinkSync( filePath );
}
}

export async function tryLoginIn( page, username, password ) {
await page.goto( '/wp-login.php' );
await page.fill( 'input[name="log"]', username );
await page.fill( 'input[name="pwd"]', password );
await page.click( 'input[name="wp-submit"]' );
}
Loading