array_values( $disabled ), ) ); } /** * Filter allowed block types for the editor. * * @param bool|array $allowed_block_types Array of block type slugs, or true for all. * @param WP_Block_Editor_Context $editor_context Editor context. * @return bool|array */ public function filter_allowed_blocks( $allowed_block_types, $editor_context ) { // Check if admin exemption is enabled. $exempt_admin = get_option( 'blocur_exempt_admin', '1' ); if ( '1' === $exempt_admin && current_user_can( 'manage_options' ) ) { return $allowed_block_types; // Admins see all blocks. } $disabled = get_option( 'blocur_disabled_blocks', array() ); // If nothing is disabled, return as-is. if ( empty( $disabled ) ) { return $allowed_block_types; } // Get the full list of registered blocks. $registered = WP_Block_Type_Registry::get_instance()->get_all_registered(); $all_blocks = array_keys( $registered ); // If $allowed_block_types is already a specific array (set by another plugin), // respect that and only further remove our disabled ones. if ( is_array( $allowed_block_types ) ) { $all_blocks = $allowed_block_types; } // Remove disabled blocks. $allowed = array_values( array_filter( $all_blocks, function( $block_name ) use ( $disabled ) { return ! in_array( $block_name, $disabled, true ); } ) ); return $allowed; } /** * Return all registered blocks grouped by category. * Used by the admin settings page. * * @return array Associative array: category_slug => [ 'label' => string, 'blocks' => [] ] */ public static function get_blocks_by_category() { if ( ! function_exists( 'get_block_categories' ) ) { return array(); } // Retrieve categories. Pass a dummy post for context. $dummy_post = new WP_Post( (object) array( 'ID' => 0, 'post_type' => 'post' ) ); $categories = get_block_categories( $dummy_post ); // Build category map. $category_map = array(); foreach ( $categories as $cat ) { $category_map[ $cat['slug'] ] = array( 'label' => $cat['title'], 'blocks' => array(), ); } // Fallback for uncategorised. $category_map['uncategorized'] = array( 'label' => __( 'Uncategorized', '16deza-block-curator' ), 'blocks' => array(), ); $registered = WP_Block_Type_Registry::get_instance()->get_all_registered(); foreach ( $registered as $block_name => $block_type ) { $cat_slug = isset( $block_type->category ) && ! empty( $block_type->category ) ? $block_type->category : 'uncategorized'; if ( ! isset( $category_map[ $cat_slug ] ) ) { $category_map[ $cat_slug ] = array( 'label' => ucfirst( str_replace( '-', ' ', $cat_slug ) ), 'blocks' => array(), ); } // Determine human-readable block title. $title = ''; if ( ! empty( $block_type->title ) ) { $title = $block_type->title; } else { // Derive a readable name from the block slug. $parts = explode( '/', $block_name ); $title = ucwords( str_replace( '-', ' ', end( $parts ) ) ); } $category_map[ $cat_slug ]['blocks'][] = array( 'name' => $block_name, 'title' => $title, ); } // Remove empty categories. $category_map = array_filter( $category_map, function( $cat ) { return ! empty( $cat['blocks'] ); } ); // Sort blocks within each category alphabetically by title. foreach ( $category_map as $slug => &$cat ) { usort( $cat['blocks'], function( $a, $b ) { return strcmp( $a['title'], $b['title'] ); } ); } unset( $cat ); return $category_map; } }