diff --git a/phpunit/directives/attributes/wp-show.php b/phpunit/directives/attributes/wp-show.php new file mode 100644 index 00000000..3a550212 --- /dev/null +++ b/phpunit/directives/attributes/wp-show.php @@ -0,0 +1,64 @@ + + I should be shown! + +EOF; + $tags = new WP_Directive_Processor( $markup ); + $tags->next_tag(); + + $context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => true ) ) ); + $context = clone $context_before; + process_wp_show( $tags, $context ); + + $tags->next_tag( array( 'tag_closers' => 'visit' ) ); + process_wp_show( $tags, $context ); + + $this->assertSame( $markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-show directive changed context' ); + } + + public function test_directive_wraps_content_in_template_if_when_is_false() { + $markup = << + I should be shown! + +EOF; + $tags = new WP_Directive_Processor( $markup ); + $tags->next_tag(); + + $context_before = new WP_Directive_Context( array( 'myblock' => array( 'open' => false ) ) ); + $context = clone $context_before; + process_wp_show( $tags, $context ); + + $tags->next_tag( array( 'tag_closers' => 'visit' ) ); + process_wp_show( $tags, $context ); + + $updated_markup = << +EOF; + $this->assertSame( $updated_markup, $tags->get_updated_html() ); + $this->assertSame( $context_before->get_context(), $context->get_context(), 'wp-show directive changed context' ); + } +} diff --git a/src/directives/attributes/wp-show.php b/src/directives/attributes/wp-show.php new file mode 100644 index 00000000..89ed6126 --- /dev/null +++ b/src/directives/attributes/wp-show.php @@ -0,0 +1,27 @@ +is_tag_closer() ) { // TODO: Exclude void and self-closing! + set_bookmark_for_directive( $tags, 'wp-show' ); + return; + } + + $end = 'wp-show-closer'; + $tags->set_bookmark( 'wp-show-closer' ); + $start = seek_bookmark_for_directive( $tags, 'wp-show' ); + + $value = $tags->get_attribute( 'wp-show' ); + if ( null !== $value ) { + $show = evaluate( $value, $context->get_context() ); + + if ( ! $show ) { + $content = $tags->get_content_inside_bookmarks( $start, $end ); + $tags->set_content_inside_bookmarks( $start, $end, '' ); + } + } + $tags->seek( $end ); + $tags->release_bookmark( $start ); + $tags->release_bookmark( $end ); +} diff --git a/src/directives/class-wp-directive-processor.php b/src/directives/class-wp-directive-processor.php new file mode 100644 index 00000000..e514cb1f --- /dev/null +++ b/src/directives/class-wp-directive-processor.php @@ -0,0 +1,37 @@ +bookmarks[ $start_bookmark ], $this->bookmarks[ $end_bookmark ] ) ) { + return null; + } + + $start = $this->bookmarks[ $start_bookmark ]; + $end = $this->bookmarks[ $end_bookmark ]; + + return substr( $this->get_updated_html(), $start->end + 1, $end->start - $start->end - 2 ); + } + + public function set_content_inside_bookmarks( $start_bookmark, $end_bookmark, $content ) { + if ( ! isset( $this->bookmarks[ $start_bookmark ], $this->bookmarks[ $end_bookmark ] ) ) { + return null; + } + + $start = $this->bookmarks[ $start_bookmark ]; + $end = $this->bookmarks[ $end_bookmark ]; + + $this->lexical_updates[] = new WP_HTML_Text_Replacement( $start->end + 1, $end->start - 1, $content ); + } + + /** + * Checks whether a bookmark with the given name exists. + * + * @param string $bookmark_name Name to identify a bookmark that potentially exists. + * @return bool Whether that bookmark exists. + */ + public function has_bookmark( $bookmark_name ) { + return array_key_exists( $bookmark_name, $this->bookmarks ); + } +} \ No newline at end of file diff --git a/src/directives/utils.php b/src/directives/utils.php index 6280ceb8..e69132d0 100644 --- a/src/directives/utils.php +++ b/src/directives/utils.php @@ -49,3 +49,25 @@ function set_style( $style, $name, $value ) { } return implode( ';', $style_assignments ); } + +function count_bookmarks_for_directive( $tags, $directive ) { + $i = 0; + while ( $tags->has_bookmark( $directive . '-' . $i ) ) { + ++$i; + } + return $i; +} + +function set_bookmark_for_directive( $tags, $directive ) { + $i = count_bookmarks_for_directive( $tags, $directive ); + $bookmark = $directive . '-' . $i; + $tags->set_bookmark( $bookmark ); + return $bookmark; +} + +function seek_bookmark_for_directive( $tags, $directive ) { + $i = count_bookmarks_for_directive( $tags, $directive ) - 1; + $bookmark = $directive . '-' . $i; + $tags->seek( $bookmark ); + return $bookmark; +} \ No newline at end of file diff --git a/wp-directives.php b/wp-directives.php index a3a856ba..5a9c9415 100644 --- a/wp-directives.php +++ b/wp-directives.php @@ -43,6 +43,7 @@ function () { require_once __DIR__ . '/src/directives/attributes/wp-bind.php'; require_once __DIR__ . '/src/directives/attributes/wp-class.php'; +require_once __DIR__ . '/src/directives/attributes/wp-show.php'; require_once __DIR__ . '/src/directives/attributes/wp-style.php'; require_once __DIR__ . '/src/directives/tags/wp-context.php'; @@ -216,6 +217,7 @@ function process_directives_in_block( $block_content ) { 'wp-context' => 'process_wp_context', 'wp-bind' => 'process_wp_bind', 'wp-class' => 'process_wp_class', + 'wp-show' => 'process_wp_show', 'wp-style' => 'process_wp_style', );