Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Add directive processing loop
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jan 2, 2023
1 parent d79c08f commit 079799e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions wp-directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
* Text Domain: wp-directives
*/

require_once __DIR__ . '/src/html/index.php';

require_once __DIR__ . '/src/directives/wp-context.php';

// Check if Gutenberg plugin is active.
if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
Expand Down Expand Up @@ -196,3 +200,34 @@ function bhe_inner_blocks( $parsed_block, $source_block, $parent_block ) {
return $parsed_block;
}
add_filter( 'render_block_data', 'bhe_inner_blocks', 10, 3 );

function wp_process_directives( $block_content, $block, $instance ) {
// TODO: Add some directive/components registration mechanism.
$directives = array(
'wp-context' => 'process_wp_context',
'wp-show' => 'process_wp_show',
);

$tags = new WP_HTML_Tag_Processor( $block_content );

$context = new WP_Directive_Context;
while ( $tags->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
$tag_name = strtolower( $tags->get_tag() );
if ( array_key_exists( $tag_name, $directives ) ) {
call_user_func_array( $directives[$tag_name], array( &$tags, &$context ) );
} else {
// Components can't have directives (unless we change our mind about this).
foreach ( $directives as $directive => $directive_processor ) {
$attributes = $tags->get_attributes_by_prefix( $directive );
if ( empty( $attributes ) ) {
continue;
}

call_user_func_array( $directive_processor, array( &$tags, &$context ) );
}
}
}

return $block_content;
}
add_filter( 'render_block', 'wp_process_directives', 10, 3 );

0 comments on commit 079799e

Please sign in to comment.