Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOOSE-52] Filter Post Terms Block by Classes #66

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions wp-content/plugins/core/src/Blocks/Blocks_Definer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DI;
use Tribe\Libs\Container\Definer_Interface;
use Tribe\Plugin\Blocks\Filters\Contracts\Filter_Factory;
use Tribe\Plugin\Blocks\Filters\Post_Terms_Filter;
use Tribe\Theme\blocks\core\button\Button;
use Tribe\Theme\blocks\core\embed\Embed;
use Tribe\Theme\blocks\core\heading\Heading;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function define(): array {
] ),

self::FILTERS => DI\add( [
DI\get( Post_Terms_Filter::class ),
] ),

Filter_Factory::class => DI\autowire()->constructorParameter( 'filters', DI\get( self::FILTERS ) ),
Expand Down
2 changes: 1 addition & 1 deletion wp-content/plugins/core/src/Blocks/Blocks_Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function register(): void {
add_filter( 'render_block', function ( string $block_content, array $block ): string {
$filter = $this->container->get( Filter_Factory::class )->make( $block );

return $filter ? $filter->filter_block_content( $block_content ) : $block_content;
return $filter ? $filter->filter_block_content( $block_content, $block ) : $block_content;
}, 10, 2 );

add_action( 'after_setup_theme', function (): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ abstract class Block_Content_Filter {

public const BLOCK = '';

abstract public function filter_block_content( string $block_content ): string;
abstract public function filter_block_content( string $block_content, array $block ): string;

}
89 changes: 89 additions & 0 deletions wp-content/plugins/core/src/Blocks/Filters/Post_Terms_Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php declare(strict_types=1);

namespace Tribe\Plugin\Blocks\Filters;

use Tribe\Plugin\Blocks\Filters\Contracts\Block_Content_Filter;

class Post_Terms_Filter extends Block_Content_Filter {

public const BLOCK = 'core/post-terms';

public function filter_block_content( string $block_content, array $block ): string {
// if we don't have any filter classes (or classes at all), return early
if ( ! array_key_exists( 'className', $block['attrs'] ) ) {
return $block_content;
}

// determine what we should be filtering
$should_remove_links = str_contains( $block['attrs']['className'], 'filter-remove-links' );
$should_remove_separators = str_contains( $block['attrs']['className'], 'filter-remove-separators' );
$should_display_one_term = str_contains( $block['attrs']['className'], 'filter-display-one-term' );

// setup output variable
$output = '';

// remove all content between the block wrapper
$wrapper = preg_replace( '#(<div[^>]*>).*?(</div>)#', '$1$2', $block_content );

// split the wrapper so we can reassemble it later
$explode_wrapper = explode( '><', $wrapper );

// setup the start of the wrapper
$wrapper_start = $explode_wrapper[0] . '>';

// setup the end of the wrapper
$wrapper_end = '<' . $explode_wrapper[1];

// remove the start & end wrapper from the block content (so we're only left with the contents)
$removed_wrapper = str_replace( $wrapper_start, '', str_replace( $wrapper_end, '', $block_content ) );

// determine what we should split the contents by (separator)
$explode_by = $block['attrs']['separator'] !== '' ? $block['attrs']['separator'] : ' ';

// setup a separator variable so we can easily use it later
$separator = sprintf( '<span class="wp-block-post-terms__separator">%s</span>', $explode_by );

// change the contents into an array of items
$explode = explode( $separator, $removed_wrapper );

// rebuild block content

// start with the wrapper
$output .= $wrapper_start;

// determine if we should display one term or all of them
if ( $should_display_one_term ) {
// if we should remove links, strip all tags from the item
// if we're displaying one term, we don't have to worry abouot separators
$output .= $should_remove_links
? sprintf(
'<span class="%s">%s</span>',
esc_attr( 'wp-block-post-terms__term' ),
strip_tags( $explode[0] )
)
: sprintf( '%s', $explode[0] );
} else {
// if we should show all terms, loop through them
// if we should remove links, strip all tags from the item
// if we should show separators, don't show the last one
foreach ( $explode as $key => $item ) {
$output .= $should_remove_links
? sprintf(
'<span class="%s">%s</span>%s',
esc_attr( 'wp-block-post-terms__term' ),
strip_tags( $item ),
$should_remove_separators ? '' : ( $key + 1 < count( $explode ) ? $separator : '' )
)
: sprintf( '%s%s', $item, $should_remove_separators ? '' : $separator );
}
}

// end with the wrapper
// phpcs:ignore
$output .= $wrapper_end;

// return the new output
return $output;
}

}
29 changes: 17 additions & 12 deletions wp-content/themes/core/assets/pcss/cards/post-search-result.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@

.p-card-search-result {
position: relative;

&:hover,
&:focus-within {

.p-card-search-result__image img {
transform: scale(1.05);
}

.p-card-search-result__title {
text-decoration: underline;
}
}
}

/* -----------------------------------------------------------------------
Expand All @@ -37,5 +25,22 @@

& img {
transition: var(--transition);

.p-card-search-result:hover &,
.p-card-search-result:focus-within & {
transform: scale(1.05);
}
}
}

/* -----------------------------------------------------------------------
* Search Result Post Card - Title
* ----------------------------------------------------------------------- */

.p-card-search-result__title {

.p-card-search-result:hover &,
.p-card-search-result:focus-within & {
text-decoration: underline;
}
}
30 changes: 18 additions & 12 deletions wp-content/themes/core/assets/pcss/cards/post.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@

.p-card-post {
position: relative;

&:hover,
&:focus-within {

.p-card-post__image img {
transform: scale(1.05);
}

.wp-block-post-title {
text-decoration: underline;
}
}
}

/* -----------------------------------------------------------------------
Expand All @@ -39,6 +27,11 @@

& img {
transition: var(--transition);

.p-card-post:hover &,
.p-card-post:focus-within & {
transform: scale(1.05);
}
}

@media (--mq-wp-mobile-max) {
Expand All @@ -58,3 +51,16 @@
position: relative;
z-index: 2;
}

/* -----------------------------------------------------------------------
* Post Card - Title
* Handles hover/focus state of the title element
* ----------------------------------------------------------------------- */

.p-card-post__title {

.p-card-post:hover &,
.p-card-post:focus-within & {
text-decoration: underline;
}
}
6 changes: 6 additions & 0 deletions wp-content/themes/core/assets/pcss/global/reset.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ picture {
max-width: 100%;
}

/**
* @todo Need to ensure we can properly enqueue this reset to prevent pattern
* previews from disappearing in the editor. Although, they don't appear to
* currently be breaking, maybe something to keep an eye on.
* @see https://github.com/wpcomvip/careforth/pull/14
*/
iframe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't part of your edits, but are we missing a comma after iframe? All the other CSS properties are listed single line with a comma after, and this one isn't...

video,
embed {
Expand Down