Skip to content

Commit

Permalink
fix: enforce display condition to return string in render_block filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Soare-Robert-Daniel committed Apr 22, 2024
1 parent cf729b1 commit 8cf9764
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
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
42 changes: 42 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,42 @@
/**
* 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( '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();

await page.goto( `/?p=${postId}` );

await expect( page.locator( '#wp--skip-link--target img' ) ).toBeVisible();

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

0 comments on commit 8cf9764

Please sign in to comment.