Skip to content
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
76 changes: 54 additions & 22 deletions classes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ public function __construct() {
// https://github.com/nk-crew/lazy-blocks/issues/247 .
add_filter( 'allowed_block_types_all', array( $this, 'allowed_block_types_all' ), 100, 2 );

// Custom post roles.
add_action( 'admin_init', array( $this, 'add_role_caps' ) );

// Additional elements in blocks list table.
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
add_filter( 'disable_months_dropdown', array( $this, 'disable_months_dropdown' ), 10, 2 );
Expand Down Expand Up @@ -296,32 +293,67 @@ public function allowed_block_types_all( $allowed_block_types, $editor_context )
}

/**
* Add Roles
* Get the Lazy Blocks role capability matrix.
*
* @return array
*/
public function add_role_caps() {
global $wp_roles;

if ( isset( $wp_roles ) ) {
$wp_roles->add_cap( 'administrator', 'edit_lazyblock' );
$wp_roles->add_cap( 'administrator', 'edit_lazyblocks' );
$wp_roles->add_cap( 'administrator', 'edit_other_lazyblocks' );
$wp_roles->add_cap( 'administrator', 'publish_lazyblocks' );
$wp_roles->add_cap( 'administrator', 'read_lazyblock' );
$wp_roles->add_cap( 'administrator', 'read_private_lazyblocks' );
$wp_roles->add_cap( 'administrator', 'delete_lazyblocks' );
$wp_roles->add_cap( 'administrator', 'delete_lazyblock' );
public function get_role_caps_matrix() {
return array(
'administrator' => array(
'edit_lazyblock',
'edit_lazyblocks',
'edit_other_lazyblocks',
'publish_lazyblocks',
'read_lazyblock',
'read_private_lazyblocks',
'delete_lazyblocks',
'delete_lazyblock',
),
'editor' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
'author' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
'contributor' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
);
}

$wp_roles->add_cap( 'editor', 'read_lazyblock' );
$wp_roles->add_cap( 'editor', 'read_private_lazyblocks' );
/**
* Synchronize Lazy Blocks capabilities for built-in roles.
*
* @return void
*/
public function sync_role_caps() {
foreach ( $this->get_role_caps_matrix() as $role_name => $caps ) {
$role = get_role( $role_name );

$wp_roles->add_cap( 'author', 'read_lazyblock' );
$wp_roles->add_cap( 'author', 'read_private_lazyblocks' );
if ( ! $role ) {
continue;
}

$wp_roles->add_cap( 'contributor', 'read_lazyblock' );
$wp_roles->add_cap( 'contributor', 'read_private_lazyblocks' );
foreach ( $caps as $capability ) {
$role->add_cap( $capability );
}
}
}

/**
* Add Roles
*
* @deprecated Use sync_role_caps().
*
* @return void
*/
public function add_role_caps() {
$this->sync_role_caps();
}

/**
* Disable month dropdown.
*
Expand Down
15 changes: 15 additions & 0 deletions classes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function init() {
*/
public function get_migrations() {
return array(
array(
'version' => '4.3.0',
'cb' => array( $this, 'v_4_3_0_sync_role_caps' ),
),
array(
'version' => '2.5.0',
'cb' => array( $this, 'v_2_5_0' ),
Expand All @@ -68,6 +72,17 @@ public function get_migrations() {
);
}

/**
* Synchronize Lazy Blocks capabilities during the 4.3.0 upgrade path.
*
* @return void
*/
public function v_4_3_0_sync_role_caps() {
if ( function_exists( 'lazyblocks' ) && lazyblocks()->blocks() ) {
lazyblocks()->blocks()->sync_role_caps();
}
}

/**
* Convert old templates to new one.
*/
Expand Down
4 changes: 4 additions & 0 deletions lazy-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public function __construct() {
* Activation Hook
*/
public function activation_hook() {
if ( $this->blocks ) {
$this->blocks->sync_role_caps();
}

LazyBlocks_Dummy::add();
}

Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/config/global-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ async function globalSetup(config) {
await requestUtils.setupRest();

// Reset the test environment before running the tests.
await requestUtils.activatePlugin('lazy-blocks');

await Promise.all([
requestUtils.activateTheme('empty-theme'),
// Disable this test plugin as it's conflicting with some of the tests.
Expand Down
24 changes: 21 additions & 3 deletions tests/e2e/specs/editor-block-attribute-useBlockProps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,25 @@ test.describe('editor block attribute useBlockProps', () => {
});

const expectSelector =
'figure.wp-block-lazyblock-test.test-custom-class[data-test="hello"][style="background-color: red; color: blue;"]:text("Hello There")';
'figure.wp-block-lazyblock-test.test-custom-class[data-test="hello"]';

await admin.createNewPost();

await editor.insertBlock({
name: 'lazyblock/test',
});

const editorBlock = editor.canvas
.locator(expectSelector)
.filter({ hasText: 'Hello There' });

// Editor render.
await expect(editor.canvas.locator(expectSelector)).toBeVisible();
await expect(editorBlock).toBeVisible();
await expect(editorBlock).toHaveCSS(
'background-color',
'rgb(255, 0, 0)'
);
await expect(editorBlock).toHaveCSS('color', 'rgb(0, 0, 255)');

// Publish.
await page
Expand All @@ -165,7 +174,16 @@ test.describe('editor block attribute useBlockProps', () => {

await frontendPage.waitForLoadState('domcontentloaded');

const frontendBlock = frontendPage
.locator(expectSelector)
.filter({ hasText: 'Hello There' });

// Frontend render.
await expect(frontendPage.locator(expectSelector)).toBeVisible();
await expect(frontendBlock).toBeVisible();
await expect(frontendBlock).toHaveCSS(
'background-color',
'rgb(255, 0, 0)'
);
await expect(frontendBlock).toHaveCSS('color', 'rgb(0, 0, 255)');
});
});
2 changes: 1 addition & 1 deletion tests/e2e/specs/editor-block-base-controls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test.describe('editor block with Base control', () => {

await admin.visitAdminPage('edit.php?post_type=lazyblocks');

await page.getByLabel('“Test Base Block” (Edit)').click();
await page.locator(`#post-${blockID} .row-title`).click();

await page.waitForTimeout(500);

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/specs/editor-block-repeater-control.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test.describe('editor block with Repeater control', () => {

await admin.visitAdminPage('edit.php?post_type=lazyblocks');

await page.getByLabel('“Test Repeater Block” (Edit)').click();
await page.locator(`#post-${blockID} .row-title`).click();

await page.waitForTimeout(500);

Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/specs/editor-block-with-frame.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test.describe('editor block with frame and content controls', () => {
admin,
requestUtils,
}) => {
await createBlock({
const blockID = await createBlock({
requestUtils,
title: 'Block with frame',
slug: 'test',
Expand All @@ -28,7 +28,7 @@ test.describe('editor block with frame and content controls', () => {
// Create control.
await admin.visitAdminPage('edit.php?post_type=lazyblocks');

await page.getByLabel('“Block with frame” (Edit)').click();
await page.locator(`#post-${blockID} .row-title`).click();

await createControl({
page,
Expand Down Expand Up @@ -63,7 +63,7 @@ test.describe('editor block with frame and content controls', () => {
admin,
requestUtils,
}) => {
await createBlock({
const blockID = await createBlock({
requestUtils,
title: 'Block with frame',
slug: 'test',
Expand All @@ -74,7 +74,7 @@ test.describe('editor block with frame and content controls', () => {
// Create control.
await admin.visitAdminPage('edit.php?post_type=lazyblocks');

await page.getByLabel('“Block with frame” (Edit)').click();
await page.locator(`#post-${blockID} .row-title`).click();

await createControl({
page,
Expand Down Expand Up @@ -109,7 +109,7 @@ test.describe('editor block with frame and content controls', () => {
admin,
requestUtils,
}) => {
await createBlock({
const blockID = await createBlock({
requestUtils,
title: 'Block with frame',
slug: 'test',
Expand All @@ -120,7 +120,7 @@ test.describe('editor block with frame and content controls', () => {
// Create control.
await admin.visitAdminPage('edit.php?post_type=lazyblocks');

await page.getByLabel('“Block with frame” (Edit)').click();
await page.locator(`#post-${blockID} .row-title`).click();

await createControl({
page,
Expand Down
Loading
Loading