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

Pass post to block type details filters #4

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/).

## 1.2.0

### Changed

- The post object is now passed to the `alley_block_audit_block_type_details` and `alley_block_audit_{$block_name}_block_type_details` filters.

## 1.1.0

### Changed
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can add more details to the report with the `alley_block_audit_block_type_de
```php
add_filter(
'alley_block_audit_block_type_details',
function ( $details, $block_name, $attrs, $inner_html, $block ) {
function ( $details, $block_name, $attrs, $inner_html, $block, $post ) {
if ( isset( $attrs['fontSize'] ) && is_string( $attrs['fontSize'] ) ) {
$details['fontSize'][ $attrs['fontSize'] ] ??= 0;
$details['fontSize'][ $attrs['fontSize'] ]++;
Expand All @@ -32,12 +32,12 @@ add_filter(
return $details;
},
10,
5,
6,
);

add_filter(
'alley_block_audit_core/image_block_type_details',
function ( $details, $block_name, $attrs, $inner_html, $block ) {
function ( $details, $block_name, $attrs, $inner_html, $block, $post ) {
if ( isset( $attrs['aspectRatio'] ) && is_string( $attrs['aspectRatio'] ) ) {
$details['aspectRatio'][ $attrs['aspectRatio'] ] ??= 0;
$details['aspectRatio'][ $attrs['aspectRatio'] ]++;
Expand All @@ -46,7 +46,7 @@ add_filter(
return $details;
},
10,
5,
6,
);
```

Expand Down
34 changes: 19 additions & 15 deletions src/features/class-block-audit-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function ( \WP_Post $post ) use ( &$out ) {
$out[ $block_name ]['Details'] = $this->with_details(
$out[ $block_name ]['Details'], // @phpstan-ignore-line
$block, // @phpstan-ignore-line
$post,
);
}
},
Expand Down Expand Up @@ -235,11 +236,12 @@ function ( \WP_Post $post ) use ( &$out ) {
* @phpstan-param array{blockName: string, attrs: array<string, mixed>, innerHTML: string} $block
* @phpstan-return array<string, mixed>
*
* @param array $details Accumulated details about blocks of this type so far.
* @param array $block Block being audited.
* @param array $details Accumulated details about blocks of this type so far.
* @param array $block Block being audited.
* @param WP_Post $post Post where the block was found.
* @return array Updated details.
*/
private function with_details( array $details, array $block ): array {
private function with_details( array $details, array $block, WP_Post $post ): array {
static $has_filter = [];

$html = new \WP_HTML_Tag_Processor( $block['innerHTML'] );
Expand Down Expand Up @@ -288,28 +290,30 @@ private function with_details( array $details, array $block ): array {
/**
* Filters the details about a block.
*
* @param array $details Details about blocks of this type so far.
* @param string $block_name Name of the block being audited.
* @param array $attrs Block attributes.
* @param string $innerHTML Inner HTML of the block.
* @param array $block Block being audited.
* @param array $details Details about blocks of this type so far.
* @param string $block_name Name of the block being audited.
* @param array $attrs Block attributes.
* @param string $innerHTML Inner HTML of the block.
* @param array $block Block being audited.
* @param WP_Post $post The post where the block was found.
* @return array Updated block type details.
*/
$details = apply_filters( 'alley_block_audit_block_type_details', $details, $block_name, $attrs, $block['innerHTML'], $block );
$details = apply_filters( 'alley_block_audit_block_type_details', $details, $block_name, $attrs, $block['innerHTML'], $block, $post );

/**
* Filters the details about a block.
*
* The dynamic portion of the hook name, `$block_name`, refers to the name of the block being audited.
*
* @param array $details Details about blocks of this type so far.
* @param string $block_name Name of the block being audited.
* @param array $attrs Block attributes.
* @param string $innerHTML Inner HTML of the block.
* @param array $block Block being audited.
* @param array $details Details about blocks of this type so far.
* @param string $block_name Name of the block being audited.
* @param array $attrs Block attributes.
* @param string $innerHTML Inner HTML of the block.
* @param array $block Block being audited.
* @param WP_Post $post The post where the block was found.
* @return array Updated block type details.
*/
$details = apply_filters( "alley_block_audit_{$block_name}_block_type_details", $details, $block_name, $attrs, $block['innerHTML'], $block );
$details = apply_filters( "alley_block_audit_{$block_name}_block_type_details", $details, $block_name, $attrs, $block['innerHTML'], $block, $post );
}

if ( ! is_array( $details ) ) {
Expand Down