diff --git a/inc/plugins/class-block-conditions.php b/inc/plugins/class-block-conditions.php index b735e64ec..421407c56 100644 --- a/inc/plugins/class-block-conditions.php +++ b/inc/plugins/class-block-conditions.php @@ -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 ); } } @@ -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 @@ -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; } } diff --git a/src/blocks/test/e2e/blocks/block-conditions.spec.js b/src/blocks/test/e2e/blocks/block-conditions.spec.js new file mode 100644 index 000000000..00709ad0e --- /dev/null +++ b/src/blocks/test/e2e/blocks/block-conditions.spec.js @@ -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(); + }); +}); diff --git a/src/blocks/test/e2e/utils.js b/src/blocks/test/e2e/utils.js index e052e47c2..a45223741 100644 --- a/src/blocks/test/e2e/utils.js +++ b/src/blocks/test/e2e/utils.js @@ -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"]' ); +}