admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'bkbg_save_blocks' ), 'i18n' => array( 'saved' => __( 'Settings saved.', 'blockenberg' ), 'saving' => __( 'Saving…', 'blockenberg' ), 'error' => __( 'Save failed. Please try again.', 'blockenberg' ), 'enableAll' => __( 'Enable All', 'blockenberg' ), 'disableAll' => __( 'Disable All', 'blockenberg' ), ), ) ); } ); /* ────────────────────────────────────────────── * 3. AJAX handler — save disabled blocks * ────────────────────────────────────────────── */ add_action( 'wp_ajax_bkbg_save_disabled_blocks', function () { check_ajax_referer( 'bkbg_save_blocks', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'Unauthorized', 403 ); } $raw = isset( $_POST['disabled'] ) ? $_POST['disabled'] : array(); if ( is_string( $raw ) ) { $raw = json_decode( stripslashes( $raw ), true ); } if ( ! is_array( $raw ) ) { $raw = array(); } $sanitized = array(); foreach ( $raw as $block_name ) { $clean = sanitize_text_field( $block_name ); if ( '' !== $clean && strpos( $clean, 'blockenberg/' ) === 0 ) { $sanitized[] = $clean; } } update_option( 'blockenberg_disabled_blocks', $sanitized, false ); wp_send_json_success( array( 'disabled' => $sanitized, 'count' => count( $sanitized ), ) ); } ); /* ────────────────────────────────────────────── * 4. Helper — collect blocks metadata * ────────────────────────────────────────────── */ function bkbg_get_all_blocks_meta() { $blocks_dir = dirname( __DIR__, 2 ) . '/blocks'; $blocks = array(); foreach ( glob( $blocks_dir . '/*/block.json' ) as $json_path ) { $raw = file_get_contents( $json_path ); if ( ! $raw ) { continue; } // WordPress block.json can have trailing commas — strip them. $raw = preg_replace( '/,\s*([\]}])/', '$1', $raw ); $data = json_decode( $raw, true ); if ( ! is_array( $data ) || empty( $data['name'] ) ) { continue; } $blocks[] = array( 'name' => $data['name'], 'title' => isset( $data['title'] ) ? $data['title'] : basename( dirname( $json_path ) ), 'category' => isset( $data['category'] ) ? $data['category'] : 'blockenberg', 'icon' => isset( $data['icon'] ) ? $data['icon'] : 'block-default', 'keywords' => isset( $data['keywords'] ) ? $data['keywords'] : array(), ); } // Sort by title. usort( $blocks, function ( $a, $b ) { return strcasecmp( $a['title'], $b['title'] ); } ); return $blocks; } /* ────────────────────────────────────────────── * 5. Category labels (mirrors block_categories_all). * ────────────────────────────────────────────── */ function bkbg_get_category_labels() { return array( 'bkbg-layout' => __( 'Layout & Structure', 'blockenberg' ), 'bkbg-content' => __( 'Content & Typography', 'blockenberg' ), 'bkbg-media' => __( 'Media & Images', 'blockenberg' ), 'bkbg-marketing' => __( 'Marketing & Conversion', 'blockenberg' ), 'bkbg-business' => __( 'Business & Services', 'blockenberg' ), 'bkbg-blog' => __( 'Blog & Editorial', 'blockenberg' ), 'bkbg-interactive' => __( 'Interactive & Games', 'blockenberg' ), 'bkbg-charts' => __( 'Charts & Data', 'blockenberg' ), 'bkbg-calculators' => __( 'Calculators & Tools', 'blockenberg' ), 'bkbg-effects' => __( 'Effects & Animation', 'blockenberg' ), 'bkbg-dev' => __( 'Developer Tools', 'blockenberg' ), 'blockenberg' => __( 'General', 'blockenberg' ), ); } /* Category dashicons */ function bkbg_get_category_icons() { return array( 'bkbg-layout' => 'dashicons-layout', 'bkbg-content' => 'dashicons-editor-paragraph', 'bkbg-media' => 'dashicons-format-image', 'bkbg-marketing' => 'dashicons-megaphone', 'bkbg-business' => 'dashicons-building', 'bkbg-blog' => 'dashicons-welcome-write-blog', 'bkbg-interactive' => 'dashicons-games', 'bkbg-charts' => 'dashicons-chart-bar', 'bkbg-calculators' => 'dashicons-calculator', 'bkbg-effects' => 'dashicons-art', 'bkbg-dev' => 'dashicons-code-standards', 'blockenberg' => 'dashicons-screenoptions', ); } /* ────────────────────────────────────────────── * 6. Render the dashboard page * ────────────────────────────────────────────── */ function bkbg_render_dashboard() { $blocks = bkbg_get_all_blocks_meta(); $disabled = get_option( 'blockenberg_disabled_blocks', array() ); $category_labels = bkbg_get_category_labels(); $category_icons = bkbg_get_category_icons(); if ( ! is_array( $disabled ) ) { $disabled = array(); } // Group blocks by category. $by_category = array(); foreach ( $blocks as $block ) { $cat = $block['category']; if ( ! isset( $by_category[ $cat ] ) ) { $by_category[ $cat ] = array(); } $by_category[ $cat ][] = $block; } // Sort categories in the same order as $category_labels. $ordered = array(); foreach ( array_keys( $category_labels ) as $cat_slug ) { if ( isset( $by_category[ $cat_slug ] ) ) { $ordered[ $cat_slug ] = $by_category[ $cat_slug ]; } } // Append any unknown categories. foreach ( $by_category as $cat_slug => $cat_blocks ) { if ( ! isset( $ordered[ $cat_slug ] ) ) { $ordered[ $cat_slug ] = $cat_blocks; } } $total_blocks = count( $blocks ); $enabled_blocks = $total_blocks - count( $disabled ); ?>

' . intval( $enabled_blocks ) . '', '' . intval( $total_blocks ) . '' ); ?>

$cat_blocks ) : $label = isset( $category_labels[ $cat_slug ] ) ? $category_labels[ $cat_slug ] : $cat_slug; $icon = isset( $category_icons[ $cat_slug ] ) ? $category_icons[ $cat_slug ] : 'dashicons-block-default'; ?>
$cat_blocks ) : $label = isset( $category_labels[ $cat_slug ] ) ? $category_labels[ $cat_slug ] : $cat_slug; ?>

|