@@ -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 *
0 commit comments