'karma-docs', 'title' => __( 'Karma Docs', 'karma-docs' ), ) ); return $categories; } /** * Removes private notes from block content (search index, summaries, * Markdown, embeds: everything that is not shown to an editor directly). * * @param string $content Block content. * @return string */ public static function strip_private( $content ) { if ( false === strpos( (string) $content, 'wp:karma-docs/private-note' ) ) { return (string) $content; } $content = preg_replace( '#.*?#s', '', (string) $content ); return preg_replace( '##', '', $content ); } /** * Registers blocks. */ public function register_blocks() { $asset_file = KDOCS_PATH . 'build/blocks.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $asset = require $asset_file; wp_register_script( 'kdocs-blocks', KDOCS_URL . 'build/blocks.js', $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( 'kdocs-blocks', 'karma-docs', KDOCS_PATH . 'languages' ); wp_register_style( 'kdocs-blocks-editor', KDOCS_URL . 'build/frontend.css', array(), $asset['version'] ); $common = array( 'editor_script' => 'kdocs-blocks', 'editor_style' => 'kdocs-blocks-editor', ); register_block_type( 'karma-docs/docs', $common + array( 'attributes' => array( 'search' => array( 'type' => 'boolean', 'default' => true, ), 'title' => array( 'type' => 'string', 'default' => '', ), 'columns' => array( 'type' => 'number', 'default' => 3, ), ), 'render_callback' => static function ( $attrs ) { return Shortcodes::docs( array( 'search' => ! empty( $attrs['search'] ) ? 'yes' : 'no', 'title' => isset( $attrs['title'] ) ? $attrs['title'] : '', 'columns' => isset( $attrs['columns'] ) ? (int) $attrs['columns'] : 3, ) ); }, ) ); register_block_type( 'karma-docs/search', $common + array( 'attributes' => array( 'kb' => array( 'type' => 'number', 'default' => 0, ), 'placeholder' => array( 'type' => 'string', 'default' => '', ), 'size' => array( 'type' => 'string', 'default' => 'large', ), ), 'render_callback' => static function ( $attrs ) { $args = array( 'kb' => isset( $attrs['kb'] ) ? (int) $attrs['kb'] : 0, 'size' => isset( $attrs['size'] ) ? $attrs['size'] : 'large', ); if ( ! empty( $attrs['placeholder'] ) ) { $args['placeholder'] = $attrs['placeholder']; } return Shortcodes::search( $args ); }, ) ); register_block_type( 'karma-docs/kb', $common + array( 'attributes' => array( 'kb' => array( 'type' => 'number', 'default' => 0, ), 'search' => array( 'type' => 'boolean', 'default' => false, ), 'columns' => array( 'type' => 'number', 'default' => 3, ), ), 'render_callback' => static function ( $attrs ) { return Shortcodes::kb( array( 'id' => isset( $attrs['kb'] ) ? (int) $attrs['kb'] : 0, 'search' => ! empty( $attrs['search'] ) ? 'yes' : 'no', 'columns' => isset( $attrs['columns'] ) ? (int) $attrs['columns'] : 3, ) ); }, ) ); $static_blocks = array( 'karma-docs/callout', 'karma-docs/faq', 'karma-docs/faq-item', 'karma-docs/tabs', 'karma-docs/tab', 'karma-docs/steps', 'karma-docs/step', 'karma-docs/code-group', 'karma-docs/code-tab', 'karma-docs/cards', 'karma-docs/card', 'karma-docs/accordion', 'karma-docs/api-endpoint', 'karma-docs/changelog', ); // Notes for the team, shown only to people who can edit articles. register_block_type( 'karma-docs/private-note', $common + array( 'render_callback' => static function ( $attrs, $content ) { if ( ! current_user_can( 'edit_posts' ) ) { return ''; } Assets::request(); return ''; }, ) ); // A section of another article, kept in sync with it. register_block_type( 'karma-docs/section-embed', $common + array( 'attributes' => array( 'article' => array( 'type' => 'number', 'default' => 0, ), 'anchor' => array( 'type' => 'string', 'default' => '', ), ), 'render_callback' => array( Media::class, 'section_embed' ), ) ); // Key points written by hand, shown like the AI summary card. register_block_type( 'karma-docs/key-points', $common + array( 'attributes' => array( 'title' => array( 'type' => 'string', 'default' => '', ), 'points' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ), 'default' => array(), ), ), 'render_callback' => static function ( $attrs ) { $data = \KarmaDocs\Summary::validate( array( 'points' => $attrs['points'] ?? array() ) ); if ( is_wp_error( $data ) ) { return ''; } Assets::request(); return \KarmaDocs\Summary::card( $data, array( 'title' => (string) ( $attrs['title'] ?? '' ) ) ); }, ) ); foreach ( $static_blocks as $block_name ) { register_block_type( $block_name, array( 'editor_script' => 'kdocs-blocks', 'editor_style' => 'kdocs-blocks-editor', 'render_callback' => static function ( $attrs, $content ) { Assets::request(); return $content; }, ) ); } } }