Skip to content

Commit 1ec7751

Browse files
committed
fix: sync role caps via activation and migration
1 parent e09b93e commit 1ec7751

4 files changed

Lines changed: 28 additions & 36 deletions

File tree

classes/class-blocks.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ class LazyBlocks_Blocks {
2828
*/
2929
private static $default_icon = null;
3030

31-
/**
32-
* Role capability sync option name.
33-
*
34-
* @var string
35-
*/
36-
const ROLE_CAPS_SYNC_OPTION = 'lzb_role_caps_version';
37-
38-
/**
39-
* Role capability sync version.
40-
*
41-
* @var string
42-
*/
43-
const ROLE_CAPS_SYNC_VERSION = '1';
44-
4531
/**
4632
* Cache hash for current request (includes controls and filter callbacks).
4733
*
@@ -357,26 +343,10 @@ public function sync_role_caps() {
357343
}
358344
}
359345

360-
/**
361-
* Synchronize Lazy Blocks capabilities once per sync version.
362-
*
363-
* @return bool
364-
*/
365-
public function maybe_sync_role_caps() {
366-
if ( self::ROLE_CAPS_SYNC_VERSION === get_option( self::ROLE_CAPS_SYNC_OPTION ) ) {
367-
return false;
368-
}
369-
370-
$this->sync_role_caps();
371-
update_option( self::ROLE_CAPS_SYNC_OPTION, self::ROLE_CAPS_SYNC_VERSION );
372-
373-
return true;
374-
}
375-
376346
/**
377347
* Add Roles
378348
*
379-
* @deprecated Use maybe_sync_role_caps() or sync_role_caps().
349+
* @deprecated Use sync_role_caps().
380350
*
381351
* @return void
382352
*/

classes/class-migration.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ public function __construct() {
3535
* Init.
3636
*/
3737
public function init() {
38-
if ( function_exists( 'lazyblocks' ) && lazyblocks()->blocks() ) {
39-
lazyblocks()->blocks()->maybe_sync_role_caps();
40-
}
41-
4238
// Migration code added after `$this->initial_version` plugin version.
4339
$saved_version = get_option( 'lzb_db_version', $this->initial_version );
4440
$current_version = LAZY_BLOCKS_VERSION;
@@ -61,6 +57,10 @@ public function init() {
6157
*/
6258
public function get_migrations() {
6359
return array(
60+
array(
61+
'version' => '4.3.0',
62+
'cb' => array( $this, 'v_4_3_0_sync_role_caps' ),
63+
),
6464
array(
6565
'version' => '2.5.0',
6666
'cb' => array( $this, 'v_2_5_0' ),
@@ -72,6 +72,17 @@ public function get_migrations() {
7272
);
7373
}
7474

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+
7586
/**
7687
* Convert old templates to new one.
7788
*/

lazy-blocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function __construct() {
120120
*/
121121
public function activation_hook() {
122122
if ( $this->blocks ) {
123-
$this->blocks->maybe_sync_role_caps();
123+
$this->blocks->sync_role_caps();
124124
}
125125

126126
LazyBlocks_Dummy::add();

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)