Skip to content

Commit 2093cfe

Browse files
committed
fix: sync role caps via activation and migration
1 parent 6add674 commit 2093cfe

4 files changed

Lines changed: 84 additions & 22 deletions

File tree

classes/class-blocks.php

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ public function __construct() {
7979
// https://github.com/nk-crew/lazy-blocks/issues/247 .
8080
add_filter( 'allowed_block_types_all', array( $this, 'allowed_block_types_all' ), 100, 2 );
8181

82-
// Custom post roles.
83-
add_action( 'admin_init', array( $this, 'add_role_caps' ) );
84-
8582
// Additional elements in blocks list table.
8683
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
8784
add_filter( 'disable_months_dropdown', array( $this, 'disable_months_dropdown' ), 10, 2 );
@@ -296,32 +293,67 @@ public function allowed_block_types_all( $allowed_block_types, $editor_context )
296293
}
297294

298295
/**
299-
* Add Roles
296+
* Get the Lazy Blocks role capability matrix.
297+
*
298+
* @return array
300299
*/
301-
public function add_role_caps() {
302-
global $wp_roles;
303-
304-
if ( isset( $wp_roles ) ) {
305-
$wp_roles->add_cap( 'administrator', 'edit_lazyblock' );
306-
$wp_roles->add_cap( 'administrator', 'edit_lazyblocks' );
307-
$wp_roles->add_cap( 'administrator', 'edit_other_lazyblocks' );
308-
$wp_roles->add_cap( 'administrator', 'publish_lazyblocks' );
309-
$wp_roles->add_cap( 'administrator', 'read_lazyblock' );
310-
$wp_roles->add_cap( 'administrator', 'read_private_lazyblocks' );
311-
$wp_roles->add_cap( 'administrator', 'delete_lazyblocks' );
312-
$wp_roles->add_cap( 'administrator', 'delete_lazyblock' );
300+
public function get_role_caps_matrix() {
301+
return array(
302+
'administrator' => array(
303+
'edit_lazyblock',
304+
'edit_lazyblocks',
305+
'edit_other_lazyblocks',
306+
'publish_lazyblocks',
307+
'read_lazyblock',
308+
'read_private_lazyblocks',
309+
'delete_lazyblocks',
310+
'delete_lazyblock',
311+
),
312+
'editor' => array(
313+
'read_lazyblock',
314+
'read_private_lazyblocks',
315+
),
316+
'author' => array(
317+
'read_lazyblock',
318+
'read_private_lazyblocks',
319+
),
320+
'contributor' => array(
321+
'read_lazyblock',
322+
'read_private_lazyblocks',
323+
),
324+
);
325+
}
313326

314-
$wp_roles->add_cap( 'editor', 'read_lazyblock' );
315-
$wp_roles->add_cap( 'editor', 'read_private_lazyblocks' );
327+
/**
328+
* Synchronize Lazy Blocks capabilities for built-in roles.
329+
*
330+
* @return void
331+
*/
332+
public function sync_role_caps() {
333+
foreach ( $this->get_role_caps_matrix() as $role_name => $caps ) {
334+
$role = get_role( $role_name );
316335

317-
$wp_roles->add_cap( 'author', 'read_lazyblock' );
318-
$wp_roles->add_cap( 'author', 'read_private_lazyblocks' );
336+
if ( ! $role ) {
337+
continue;
338+
}
319339

320-
$wp_roles->add_cap( 'contributor', 'read_lazyblock' );
321-
$wp_roles->add_cap( 'contributor', 'read_private_lazyblocks' );
340+
foreach ( $caps as $capability ) {
341+
$role->add_cap( $capability );
342+
}
322343
}
323344
}
324345

346+
/**
347+
* Add Roles
348+
*
349+
* @deprecated Use sync_role_caps().
350+
*
351+
* @return void
352+
*/
353+
public function add_role_caps() {
354+
$this->sync_role_caps();
355+
}
356+
325357
/**
326358
* Disable month dropdown.
327359
*

classes/class-migration.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function init() {
5757
*/
5858
public function get_migrations() {
5959
return array(
60+
array(
61+
'version' => '4.3.0',
62+
'cb' => array( $this, 'v_4_3_0_sync_role_caps' ),
63+
),
6064
array(
6165
'version' => '2.5.0',
6266
'cb' => array( $this, 'v_2_5_0' ),
@@ -68,6 +72,17 @@ public function get_migrations() {
6872
);
6973
}
7074

75+
/**
76+
* Synchronize Lazy Blocks capabilities during the 4.3.0 upgrade path.
77+
*
78+
* @return void
79+
*/
80+
public function v_4_3_0_sync_role_caps() {
81+
if ( function_exists( 'lazyblocks' ) && lazyblocks()->blocks() ) {
82+
lazyblocks()->blocks()->sync_role_caps();
83+
}
84+
}
85+
7186
/**
7287
* Convert old templates to new one.
7388
*/

lazy-blocks.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public function __construct() {
119119
* Activation Hook
120120
*/
121121
public function activation_hook() {
122+
if ( $this->blocks ) {
123+
$this->blocks->sync_role_caps();
124+
}
125+
122126
LazyBlocks_Dummy::add();
123127
}
124128

tests/phpunit/CapabilitySyncTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,15 @@ public function test_migration_syncs_role_caps_for_fresh_installs() {
195195
$this->assert_lazyblocks_capability_matrix_applied();
196196
$this->assertSame( LAZY_BLOCKS_VERSION, get_option( 'lzb_db_version' ) );
197197
}
198+
199+
/**
200+
* Fresh installs should receive capabilities during plugin activation.
201+
*/
202+
public function test_activation_hook_syncs_role_caps_for_fresh_installs() {
203+
$this->remove_lazyblocks_capabilities();
204+
205+
lazyblocks()->activation_hook();
206+
207+
$this->assert_lazyblocks_capability_matrix_applied();
208+
}
198209
}

0 commit comments

Comments
 (0)