array( 'Concept', 'lightbulb', 'Explain a core idea clearly.' ), 'formula' => array( 'Formula', 'editor-code', 'Add a formula or equation.' ), 'shortcut' => array( 'Shortcut / Trick', 'dashboard', 'Share a useful shortcut or trick.' ), 'solved-example' => array( 'Solved Example', 'welcome-learn-more', 'Walk through a solved example.' ), 'important' => array( 'Important', 'warning', 'Highlight something important.' ), 'common-mistake' => array( 'Common Mistake', 'dismiss', 'Call out a frequent mistake.' ), 'tip' => array( 'Tip', 'star-filled', 'Add a practical tip.' ), 'warning' => array( 'Warning', 'flag', 'Show an important warning.' ), 'revision-summary' => array( 'Revision Summary', 'list-view', 'Summarize key revision points.' ), 'code-snippet' => array( 'Code Snippet', 'editor-code', 'Display code with syntax highlighting and copy functionality.' ), ); public static function register_hooks() { add_action( 'init', array( __CLASS__, 'register' ) ); } public static function register() { if ( ! function_exists( 'register_block_type' ) ) { return; } wp_register_script( 'docsstation-learning-blocks', DOCSSTATION_URL . 'assets/js/learning-blocks.js', array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-block-editor', 'wp-components' ), DOCSSTATION_VERSION, true ); wp_register_style( 'docsstation-learning-blocks', DOCSSTATION_URL . 'assets/css/learning-blocks.css', array(), DOCSSTATION_VERSION ); $supports_api_v2 = version_compare( get_bloginfo( 'version' ), '5.6', '>=' ); foreach ( self::$blocks as $slug => $block ) { $attributes = array( 'title' => array( 'type' => 'string', 'default' => $block[0] ), 'content' => array( 'type' => 'string', 'default' => '' ), ); if ( 'code-snippet' === $slug ) { $attributes['language'] = array( 'type' => 'string', 'default' => 'javascript' ); $attributes['filename'] = array( 'type' => 'string', 'default' => '' ); } $args = array( 'editor_script' => 'docsstation-learning-blocks', 'style' => 'docsstation-learning-blocks', 'attributes' => $attributes, 'render_callback' => function ( $block_attributes = array(), $content = '', $block_instance = null ) use ( $slug ) { return self::render( $block_attributes, $content, $block_instance, $slug ); }, ); if ( $supports_api_v2 ) { $args['api_version'] = 2; } register_block_type( 'docsstation/' . $slug, $args ); } } public static function render( $attributes = array(), $content = '', $block = null, $slug = '' ) { $attributes = is_array( $attributes ) ? $attributes : array(); if ( '' !== $slug ) { $name = $slug; } elseif ( is_object( $block ) && isset( $block->name ) ) { $name = str_replace( 'docsstation/', '', $block->name ); } else { $name = 'concept'; } $title = isset( $attributes['title'] ) ? (string) $attributes['title'] : ''; $text = isset( $attributes['content'] ) ? (string) $attributes['content'] : ''; if ( 'code-snippet' === $name ) { $language = isset( $attributes['language'] ) ? sanitize_html_class( $attributes['language'] ) : 'javascript'; $filename = isset( $attributes['filename'] ) ? sanitize_text_field( $attributes['filename'] ) : ''; $body = sprintf( '
%2$s%3$s
%6$s
', esc_attr( $text ), esc_html( $filename ? $filename : __( 'Code snippet', 'docsstation' ) ), esc_html( $language ), esc_html__( 'Copy', 'docsstation' ), esc_attr( $language ), esc_html( $text ) ); } elseif ( 'formula' === $name ) { $body = '
' . esc_html( $text ) . '
'; } else { $body = wpautop( wp_kses_post( $text ) ); } return sprintf( '', esc_attr( $name ), esc_html( $title ), $body ); } }