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

Commit

Permalink
Use WP_Directive_Processor to implement wp-show
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Feb 23, 2023
1 parent dd6d633 commit ce06fd2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
5 changes: 3 additions & 2 deletions phpunit/directives/tags/wp-show.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_once __DIR__ . '/../../../src/directives/tags/wp-show.php';

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

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

Expand All @@ -22,7 +23,7 @@ public function test_directive_leaves_content_unchanged_if_when_is_true() {
<div>I should be shown!</div>
</wp-show>
EOF;
$tags = new WP_HTML_Tag_Processor( $markup );
$tags = new WP_Directive_Processor( $markup );
$tags->next_tag();

$context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => true ) ) );
Expand All @@ -40,7 +41,7 @@ public function test_directive_wraps_content_in_template_if_when_is_false() {
<div>I should be shown!</div>
</wp-show>
EOF;
$tags = new WP_HTML_Tag_Processor( $markup );
$tags = new WP_Directive_Processor( $markup );
$tags->next_tag();

$context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => false ) ) );
Expand Down
23 changes: 14 additions & 9 deletions src/directives/tags/wp-show.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
require_once __DIR__ . '/../utils.php';

function process_wp_show( $tags, $context ) {
if ( $tags->is_tag_closer() ) {
if ( ! $tags->is_tag_closer() ) { // TODO: Exclude void and self-closing!
set_bookmark_for_tag( $tags, 'wp-show' );
return;
}

$value = $tags->get_attribute( 'when' );
if ( null === $value ) {
return;
}
$end = $tags->set_bookmark( 'wp-show-closer' );
$start = seek_bookmark_for_tag( $tags, 'wp-show' );

$show = evaluate( $value, $context->get_context() );
$value = $tags->get_attribute( 'when' );
if ( null !== $value ) {
$show = evaluate( $value, $context->get_context() );

if ( ! $show ) {
$content = $tags->get_content_inside_balanced_tags(); // TODO: Implement in Gutenberg.
$tags->set_content_inside_balanced_tags( '<template>' . $content . '</template>' );
if ( ! $show ) {
$content = $tags->get_content_inside_bookmarks( $start, $end );
$tags->set_content_inside_bookmarks( $start, $end, '<template>' . $content . '</template>' );
}
}
$tags->seek( $end );
$tags->release_bookmark( $start );
$tags->release_bookmark( $end );
}

0 comments on commit ce06fd2

Please sign in to comment.