-
Notifications
You must be signed in to change notification settings - Fork 2
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
Closed
Changes from 5 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
303c20d
Merge branch 'main' into s3/MOOSE-52/tag-category-custom-blocks
GeoffDusome 8eee1c0
[MOOSE-52]: filter post terms block by classes
GeoffDusome d4c3119
Update reset.pcss to add iframe comma
GeoffDusome b58e065
Merge branch 'main' into s3/MOOSE-52/tag-category-custom-blocks
GeoffDusome 595ff3b
Merge branch 'main' into s3/MOOSE-52/tag-category-custom-blocks
GeoffDusome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
wp-content/plugins/core/src/Blocks/Filters/Post_Terms_Filter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...