Skip to content

Commit

Permalink
Add Block_Content_Filter library feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dlh01 committed Nov 13, 2024
1 parent 42e2c28 commit 82e8c4b
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/features/library/class-block-content-filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Block_Content_Filter class file
*
* @package wp-type-extensions
*/

namespace Alley\WP\Features\Library;

use Alley\WP\Blocks\Block_Content;
use Alley\WP\Types\Feature;
use Alley\WP\Types\Serialized_Blocks;

/**
* Filter block markup in 'the_content' for the post being viewed.
*/
final class Block_Content_Filter implements Feature {
/**
* Callback to merge blocks.
*
* @var callable
*/
private $block_merge;

/**
* Constructor.
*
* @phpstan-param callable(Serialized_Blocks $post_content): Serialized_Blocks $block_merge
*
* @param callable $block_merge Callback to merge blocks.
*/
public function __construct(
callable $block_merge,
) {
$this->block_merge = $block_merge;
}

/**
* Boot the feature.
*/
public function boot(): void {
add_filter( 'the_content', [ $this, 'filter_the_content' ], 8 );
}

/**
* Filters the post content.
*
* @param string $content Content of the current post.
* @return string Updated content.
*/
public function filter_the_content( $content ) {
$post = get_post();

// Skip if 'the_content' is running on non-block content or on content other than the post being viewed.
if ( $post && is_single( $post->ID ) && has_blocks( $content ) ) {
$content = ( $this->block_merge )( new Block_Content( $content ) )->serialized_blocks();
}

return $content;
}
}

0 comments on commit 82e8c4b

Please sign in to comment.