82 lines
2.8 KiB
PHP
82 lines
2.8 KiB
PHP
<?php
|
|
/**
|
|
* Simplified Gutenberg Block Registration for Maple Code Blocks
|
|
*/
|
|
|
|
class MCB_Simple_Block {
|
|
|
|
public static function init() {
|
|
// Register block on init
|
|
add_action('init', array(__CLASS__, 'register_block'));
|
|
|
|
// Make sure script is enqueued in editor
|
|
add_action('enqueue_block_editor_assets', array(__CLASS__, 'enqueue_editor_assets'));
|
|
}
|
|
|
|
public static function register_block() {
|
|
// Only register if Gutenberg is available
|
|
if (!function_exists('register_block_type')) {
|
|
return;
|
|
}
|
|
|
|
// Register the block
|
|
register_block_type('maple-code-blocks/code-block', array(
|
|
'render_callback' => array(__CLASS__, 'render_block'),
|
|
'attributes' => array(
|
|
'repository' => array(
|
|
'type' => 'string',
|
|
'default' => ''
|
|
),
|
|
'theme' => array(
|
|
'type' => 'string',
|
|
'default' => 'dark'
|
|
),
|
|
'height' => array(
|
|
'type' => 'string',
|
|
'default' => '600px'
|
|
)
|
|
)
|
|
));
|
|
}
|
|
|
|
public static function enqueue_editor_assets() {
|
|
// Enqueue the block editor script
|
|
wp_enqueue_script(
|
|
'maple-code-blocks-editor',
|
|
MCB_PLUGIN_URL . 'assets/js/simple-block.js',
|
|
array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n'),
|
|
MCB_PLUGIN_VERSION,
|
|
false // Load in header, not footer
|
|
);
|
|
|
|
// Add inline script to ensure registration happens
|
|
wp_add_inline_script('maple-code-blocks-editor', '
|
|
console.log("Maple Code Blocks: Script loaded");
|
|
if (typeof wp !== "undefined" && wp.blocks) {
|
|
console.log("Maple Code Blocks: wp.blocks is available");
|
|
} else {
|
|
console.error("Maple Code Blocks: wp.blocks not available");
|
|
}
|
|
', 'after');
|
|
}
|
|
|
|
public static function render_block($attributes) {
|
|
$repository = isset($attributes['repository']) ? $attributes['repository'] : '';
|
|
$theme = isset($attributes['theme']) ? $attributes['theme'] : 'dark';
|
|
$height = isset($attributes['height']) ? $attributes['height'] : '600px';
|
|
|
|
if (empty($repository)) {
|
|
return '<div class="notice notice-warning">Please enter a repository (e.g., facebook/react)</div>';
|
|
}
|
|
|
|
// Use the shortcode for rendering
|
|
return do_shortcode(sprintf(
|
|
'[maple_code_block repo="%s" theme="%s" height="%s"]',
|
|
esc_attr($repository),
|
|
esc_attr($theme),
|
|
esc_attr($height)
|
|
));
|
|
}
|
|
}
|
|
|
|
MCB_Simple_Block::init();
|