diff --git a/.github/workflows/all-pr-tests.yml b/.github/workflows/all-pr-tests.yml index f2397763..c4ec5108 100644 --- a/.github/workflows/all-pr-tests.yml +++ b/.github/workflows/all-pr-tests.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b2001a9..e5ffa456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/blocks/post-title/edit.tsx b/blocks/post-title/edit.tsx index e6b1060b..859de56b 100644 --- a/blocks/post-title/edit.tsx +++ b/blocks/post-title/edit.tsx @@ -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 = [], diff --git a/blocks/subquery/block.json b/blocks/subquery/block.json index 2fb3af2c..cce17cb9 100644 --- a/blocks/subquery/block.json +++ b/blocks/subquery/block.json @@ -18,7 +18,9 @@ "postId" ], "providesContext": { - "query": "query" + "query": "query", + "pinnedPosts": "posts", + "customPostTitles": "customPostTitles" }, "attributes": { "deduplication": { @@ -101,6 +103,14 @@ "uniqueId": { "default": "", "type": "string" + }, + "customPostTitles": { + "default": [], + "items": { + "default": {}, + "type": "object" + }, + "type": "array" } } } \ No newline at end of file diff --git a/blocks/subquery/edit.tsx b/blocks/subquery/edit.tsx index 5b758e9a..88c9a9d4 100644 --- a/blocks/subquery/edit.tsx +++ b/blocks/subquery/edit.tsx @@ -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 }], ], ], ], diff --git a/composer.json b/composer.json index 1ddd1f04..678a3c09 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/features/class-query-block-context.php b/src/features/class-query-block-context.php index 943dedce..c06f8565 100644 --- a/src/features/class-query-block-context.php +++ b/src/features/class-query-block-context.php @@ -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']; diff --git a/src/post-ids/class-pinned-in-post-content.php b/src/post-ids/class-pinned-in-post-content.php index 2f5e871b..b60a09a5 100644 --- a/src/post-ids/class-pinned-in-post-content.php +++ b/src/post-ids/class-pinned-in-post-content.php @@ -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', ], diff --git a/tests/feature/UniquePinnedPostTest.php b/tests/feature/UniquePinnedPostTest.php index cea4970a..de7fe672 100644 --- a/tests/feature/UniquePinnedPostTest.php +++ b/tests/feature/UniquePinnedPostTest.php @@ -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. @@ -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' ) ); } /** @@ -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 ''; } } diff --git a/wp-curate.php b/wp-curate.php index bc290973..4065e4af 100644 --- a/wp-curate.php +++ b/wp-curate.php @@ -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 *