Skip to content

Commit

Permalink
Merge pull request #256 from alleyinteractive/feature/issue-236/subqu…
Browse files Browse the repository at this point in the history
…ery-block-custom-post-titles-support

Issue-236: Subquery block missing support for custom post titles
  • Loading branch information
mogmarsh authored Nov 20, 2024
2 parents dd7e603 + ad0d89c commit 19dfe4a
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/all-pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
- name: Run Node Tests
# See https://github.com/alleyinteractive/action-test-node for more options
uses: alleyinteractive/action-test-node@develop
with:
node-version: '20'

- name: Run PHP Tests
# See https://github.com/alleyinteractive/action-test-php for more options
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `WP Curate` will be documented in this file.

## 2.4.3 - 2024-11-18

- Enhancement: Post Title block (which allows title overrides for pinned posts) works in Subquery block.

## 2.4.2 - 2024-09-27

- Bug Fix: Posts not of type 'post' are flagged as being deleted.
Expand Down
4 changes: 3 additions & 1 deletion blocks/post-title/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export default function Edit({
setAttributes,
}: PostTitleEditProps) {
// @ts-ignore
const queryParentId = select('core/block-editor').getBlockParentsByBlockName(clientId, 'wp-curate/query')[0];
const queryParentIds = select('core/block-editor').getBlockParentsByBlockName(clientId, ['wp-curate/query', 'wp-curate/subquery']);
const queryParentId = queryParentIds.length ? queryParentIds[queryParentIds.length - 1] : null;

const {
postId,
pinnedPosts = [],
Expand Down
12 changes: 11 additions & 1 deletion blocks/subquery/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"postId"
],
"providesContext": {
"query": "query"
"query": "query",
"pinnedPosts": "posts",
"customPostTitles": "customPostTitles"
},
"attributes": {
"deduplication": {
Expand Down Expand Up @@ -101,6 +103,14 @@
"uniqueId": {
"default": "",
"type": "string"
},
"customPostTitles": {
"default": [],
"items": {
"default": {},
"type": "object"
},
"type": "array"
}
}
}
2 changes: 1 addition & 1 deletion blocks/subquery/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export default function Edit({
'wp-curate/post',
{},
[
['core/post-title', { isLink: true, level: 3 }],
['wp-curate/post-title', { isLink: true, level: 3 }],
],
],
],
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"alleyinteractive/alley-coding-standards": "^2.0",
"mantle-framework/testkit": "^1.1",
"nunomaduro/collision": "^7.10",
"simplehtmldom/simplehtmldom": "^2.0@RC",
"szepeviktor/phpstan-wordpress": "^1.1"
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion src/features/class-query-block-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function filter_query_context( $context, $parsed_block, $parent_block ):
&& $current_block_type instanceof WP_Block_Type
&& in_array( 'query', $current_block_type->uses_context, true ) // @phpstan-ignore-line - uses_context is private in WP_Block_Type but can be accessed via a magic method.
&& $current_block->block_name() === 'core/post-template'
&& 'wp-curate/query' === $parent_block->name
&& in_array( $parent_block->name, [ 'wp-curate/query', 'wp-curate/subquery' ], true )
&& isset( $parent_block->attributes['customPostTitles'] )
) {
$this->custom_post_titles = $parent_block->attributes['customPostTitles'];
Expand Down
2 changes: 1 addition & 1 deletion src/post-ids/class-pinned-in-post-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function post_ids(): array {
$query_blocks = match_blocks(
$post->post_content,
[
'name' => 'wp-curate/query',
'name' => [ 'wp-curate/query', 'wp-curate/subquery' ],
'flatten' => true,
'with_attrs' => 'posts',
],
Expand Down
30 changes: 25 additions & 5 deletions tests/feature/UniquePinnedPostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Alley\WP\WP_Curate\Tests\Feature;

use Alley\WP\WP_Curate\Tests\Test_Case;
use simplehtmldom\HtmlDocument;

/**
* A test suite for unique pinned posts.
Expand Down Expand Up @@ -112,9 +113,9 @@ public function test_do_not_enforce_unique_pinned_posts_if_not_enabled() {
)
->create_and_get( [ 'post_content' => $content ] );

$page = $this->get( $test_post )->assertOk();

$this->assertEquals( 2, substr_count( $page->get_content(), 'Pinned Post 1' ) );
$page = $this->get( $test_post )->assertOk();
$wp_content = $this->extract_wp_content( $page->get_content() );
$this->assertEquals( 2, substr_count( $wp_content, 'Pinned Post 1' ) );
}

/**
Expand All @@ -141,8 +142,27 @@ public function test_pinned_post_only_displays_once() {
)
->create_and_get( [ 'post_content' => $content ] );

$page = $this->get( $test_post )->assertOk();
$page = $this->get( $test_post )->assertOk();
$wp_content = $this->extract_wp_content( $page->get_content() );
$this->assertEquals( 1, substr_count( $wp_content, 'Pinned Post 2' ) );
}

/**
* Gets the contents of the .entry-content div.
*
* @param string $html_content The content of the page.
* @return string
*/
private function extract_wp_content( $html_content ) {
$html = new HtmlDocument();
$html->load( $html_content );

$div = $html->find( 'div.entry-content', 0 );

if ( $div ) {
return $div->innertext;
}

$this->assertEquals( 1, substr_count( $page->get_content(), 'Pinned Post 2' ) );
return '';
}
}
4 changes: 2 additions & 2 deletions wp-curate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* Plugin Name: WP Curate
* Plugin URI: https://github.com/alleyinteractive/wp-curate
* Description: Plugin to curate homepages and other landing pages
* Version: 2.4.2
* Version: 2.4.3
* Author: Alley Interactive
* Author URI: https://github.com/alleyinteractive/wp-curate
* Requires at least: 6.4
* Requires PHP: 8.1
* Tested up to: 6.4
* Tested up to: 6.7
*
* Text Domain: wp-curate
*
Expand Down

0 comments on commit 19dfe4a

Please sign in to comment.