Skip to content
Open
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
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.

## Unreleased

- Upgrade to PHPStan to 2.0.

## 2.7.2 - 2025-10-15

- Enhancement: Post Picker results filtered to match Query block parameters by default.
Expand Down
8 changes: 5 additions & 3 deletions blocks/post-title/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
* @package wp-curate
*/

$current_post_id = intval( $block->context['postId'] );
use function Mantle\Support\Helpers\mixed;

$current_post_id = mixed( $block->context['postId'] )->int();

if ( empty( $current_post_id ) ) {
return;
}

$custom_post_titles = ! empty( $block->context['customPostTitles'] ) ? $block->context['customPostTitles'] : [];
$custom_post_titles = ! empty( $block->context['customPostTitles'] ) && is_array( $block->context['customPostTitles'] ) ? $block->context['customPostTitles'] : [];
$post_title = get_the_title( $current_post_id );
$post_link = get_the_permalink( $current_post_id );
$level = is_int( $attributes['level'] ) ? intval( $attributes['level'] ) : 0;
Expand All @@ -32,7 +34,7 @@
// Use custom post title, if available.
if ( 0 < count( $custom_post_titles ) ) {
foreach ( $custom_post_titles as $value ) {
if ( $value['postId'] === $current_post_id ) {
if ( is_array( $value ) && isset( $value['postId'] ) && $value['postId'] === $current_post_id && is_string( $value['title'] ) ) {
$post_title = $value['title'];
break;
}
Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
"alleyinteractive/composer-wordpress-autoloader": "^1.0",
"alleyinteractive/traverse-reshape": "^2.0",
"alleyinteractive/wp-match-blocks": "^4.2",
"alleyinteractive/wp-type-extensions": "^4.0"
"alleyinteractive/wp-type-extensions": "^4.0",
"mantle-framework/support": "^1.12"
},
"require-dev": {
"alleyinteractive/alley-coding-standards": "^2.0",
"mantle-framework/testkit": "^1.1",
"simplehtmldom/simplehtmldom": "^2.0@RC",
"szepeviktor/phpstan-wordpress": "^1.1"
"mantle-framework/testkit": "^1.12",
"szepeviktor/phpstan-wordpress": "^2.0",
"wp-graphql/wp-graphql": "^2.3"
},
"config": {
"allow-plugins": {
Expand All @@ -47,6 +48,11 @@
}
},
"extra": {
"installer-paths": {
"vendor/wp-graphql/wp-graphql": [
"wp-graphql/wp-graphql"
]
},
"wordpress-autoloader": {
"autoload": {
"Alley\\WP\\WP_Curate": "src"
Expand Down
10 changes: 9 additions & 1 deletion entries/slotfills/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ function register_slotfills_scripts(): void {
return;
}

// Automatically load dependencies and version.
/**
* Asset file generated by wp-scripts.
*
* @var array{dependencies: string[], version: string}|false $asset_file
*/
$asset_file = include __DIR__ . '/index.asset.php';

if ( ! $asset_file ) {
return;
}

wp_register_script(
'wp-curate_slotfills',
plugins_url( 'index.js', __FILE__ ),
Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ parameters:
# Level 9 is the highest level
level: max

scanDirectories:
- ./vendor

ignoreErrors:
- '#maybeint of function absint#'
- '#of function sanitize_text_field expects string#'

excludePaths:
- src/features/class-parsely-support.php

paths:
- blocks/
- entries/
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
backupGlobals="false"
colors="true"
cacheDirectory=".phpunit.cache"
failOnAllIssues="true"
>
<testsuites>
<testsuite name="Feature">
Expand Down
6 changes: 4 additions & 2 deletions src/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ function get_entry_asset_map( string $dir_entry_name ): array {
$asset_file_path = trailingslashit( $base_path ) . 'index.asset.php';

if ( validate_path( $asset_file_path ) ) {
return include $asset_file_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.IncludingFile, WordPressVIPMinimum.Files.IncludingFile.UsingVariable
$asset_map = include $asset_file_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.IncludingFile, WordPressVIPMinimum.Files.IncludingFile.UsingVariable

return is_array( $asset_map ) ? $asset_map : []; // @phpstan-ignore-line returns array
}
}

Expand All @@ -123,7 +125,7 @@ function get_entry_asset_map( string $dir_entry_name ): array {
*
* @param string $dir_entry_name The entry point directory name.
*
* @return array<int, string> The asset's dependency array.
* @return array<string> The asset's dependency array.
*/
function get_asset_dependency_array( string $dir_entry_name ): array {
$asset_arr = get_entry_asset_map( $dir_entry_name );
Expand Down
18 changes: 12 additions & 6 deletions src/class-plugin-curated-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Alley\WP\Types\Post_Queries;
use WP_Block_Type;

use function Mantle\Support\Helpers\data_get;
use function Mantle\Support\Helpers\mixed;

/**
* The posts that match 'wp-curate/query' block attributes.
*/
Expand Down Expand Up @@ -41,13 +44,13 @@ public function with_query_context( array $context, array $attributes, WP_Block_
$args = [
'fields' => 'ids',
'no_found_rows' => true,
'offset' => $attributes['offset'] ?? $block_type->attributes['offset']['default'],
'offset' => mixed( $attributes['offset'] ?? data_get( $block_type->attributes, 'offset.default', 0 ) )->int(),
'order' => $attributes['order'] ?? 'DESC',
'orderby' => $attributes['orderby'] ?? 'date',
'meta_key' => $attributes['metaKey'] ?? '',
'posts_per_page' => $attributes['numberOfPosts'] ?? $block_type->attributes['numberOfPosts']['default'],
'posts_per_page' => mixed( $attributes['numberOfPosts'] ?? data_get( $block_type->attributes, 'numberOfPosts.default', 0 ) )->int(),
'post_status' => 'publish',
'post_type' => $attributes['postTypes'] ?? $block_type->attributes['postTypes']['default'],
'post_type' => $attributes['postTypes'] ?? data_get( $block_type->attributes, 'postTypes.default', [] ),
];

if ( isset( $attributes['terms'] ) && is_array( $attributes['terms'] ) && count( $attributes['terms'] ) > 0 ) {
Expand All @@ -68,14 +71,17 @@ public function with_query_context( array $context, array $attributes, WP_Block_
}
}

$search_term = $attributes['searchTerm'] ?? $block_type->attributes['searchTerm']['default'];
$search_term = $attributes['searchTerm'] ?? data_get( $block_type->attributes, 'searchTerm.default', '' );

if ( is_string( $search_term ) && strlen( $search_term ) > 0 ) {
$args['s'] = $search_term;
}

$pinned_posts = $attributes['posts'] ?? $block_type->attributes['posts']['default'];
$pinned_posts = array_map( fn ( $id ) => $id && 'publish' === get_post_status( $id ) ? $id : null, $pinned_posts );
$pinned_posts = $attributes['posts'] ?? data_get( $block_type->attributes, 'posts.default', [] );
$pinned_posts = array_map(
fn ( $id ) => is_numeric( $id ) && 'publish' === get_post_status( (int) $id ) ? (int) $id : null,
is_array( $pinned_posts ) ? $pinned_posts : [],
);

$queries = new Positioned_Post_Queries(
positioned: $pinned_posts,
Expand Down
9 changes: 7 additions & 2 deletions src/class-recorded-curated-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ public function __construct(
* @param array<string, mixed> $context Query block context.
* @param array<string, mixed> $attributes Curation field settings.
* @param WP_Block_Type $block_type Block type.
* @return array{"query": array<string, mixed>} Updated context.
* @return array{query?: array{include?: int[]}} Updated context.
*/
public function with_query_context( array $context, array $attributes, WP_Block_Type $block_type ): array {
/**
* Query context.
*
* @var array{query?: array{include?: int[]}} $context
*/
$context = $this->origin->with_query_context( $context, $attributes, $block_type );

if ( isset( $context['query']['include'] ) && is_array( $context['query']['include'] ) ) {
if ( isset( $context['query']['include'] ) && is_array( $context['query']['include'] ) ) { // @phpstan-ignore-line booleanAnd.rightAlwaysTrue
$this->history->record( $context['query']['include'] );
}

Expand Down
2 changes: 2 additions & 0 deletions src/class-supported-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Alley\WP\WP_Curate;

use function Mantle\Support\Helpers\register_meta_helper;

/**
* The post types that should allow the Curation blocks and related meta.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/features/class-core-query-block-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public function filter_query_vars( $query, $block ): array {

// Support arrays of post types.
if ( is_array( $post_type ) ) {
$query['post_type'] = array_filter( $post_type, 'is_post_type_viewable' );
$query['post_type'] = array_filter( $post_type, 'is_post_type_viewable' ); // @phpstan-ignore-line argument.type
}

if ( is_array( $include ) ) {
$query['post__in'] = array_map( 'intval', $include );
$query['post__in'] = array_map( 'intval', $include ); // @phpstan-ignore-line argument.type
}

if ( is_string( $orderby ) ) {
Expand Down
27 changes: 17 additions & 10 deletions src/features/class-graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ private function get_types_from_allowed_post_types(): array {
* Set up.
*/
public function __construct() {
/**
* Define the allowed post types.
*
* @param string[] $allowed_post_types Array of allowed post types.
*/
$this->allowed_post_types = apply_filters( 'wp_curate_allowed_post_types', [ 'post' ] );
}

Expand All @@ -87,14 +92,14 @@ public function graphql_register_types(): void {
*
* @see https://www.wpgraphql.com/functions/register_graphql_interface_type
*/
register_graphql_interface_type( // @phpstan-ignore-line
register_graphql_interface_type(
'WPCurateInterface',
[
'description' => __( 'Represents the interface type a WP Curate post', 'wp-curate' ),
'interfaces' => [ 'ContentNode', 'NodeWithTitle', 'NodeWithFeaturedImage' ],
'fields' => [],
'resolveType' => function ( $node ) {
return $this->get_graphql_type_by_post_type( $node->post_type );
'resolveType' => function ( object $node ) {
return isset( $node->post_type ) && is_string( $node->post_type ) ? $this->get_graphql_type_by_post_type( $node->post_type ) : '';
},
]
);
Expand All @@ -106,7 +111,7 @@ public function graphql_register_types(): void {
*
* @see https://www.wpgraphql.com/functions/register_graphql_interfaces_to_types
*/
register_graphql_interfaces_to_types( [ 'WPCurateInterface' ], $this->get_types_from_allowed_post_types() ); // @phpstan-ignore-line
register_graphql_interfaces_to_types( [ 'WPCurateInterface' ], $this->get_types_from_allowed_post_types() );

/**
* Register a new connection field named 'wpCuratePosts' on `RootQuery`
Expand All @@ -115,7 +120,7 @@ public function graphql_register_types(): void {
*
* @see https://www.wpgraphql.com/functions/register_graphql_connection
*/
register_graphql_connection( // @phpstan-ignore-line
register_graphql_connection(
[
'fromType' => 'RootQuery',
'toType' => 'WPCurateInterface',
Expand All @@ -126,14 +131,16 @@ public function graphql_register_types(): void {
'description' => __( 'Array of IDs for the objects to retrieve', 'wp-curate' ),
],
],
// @phpstan-ignore-next-line
'resolve' => function ( $source, $args, AppContext $context, ResolveInfo $info ) {
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info ); // @phpstan-ignore-line
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info ); // @phpstan-ignore-line argument.type

if ( is_array( $args ) && isset( $args['where'] ) && is_array( $args['where'] ) && isset( $args['where']['in'] ) ) {
$resolver->set_query_arg( 'post__in', $args['where']['in'] );
}

$resolver->set_query_arg( 'post__in', $args['where']['in'] ); // @phpstan-ignore-line
$resolver->set_query_arg( 'post_type', $this->allowed_post_types ); // @phpstan-ignore-line
$resolver->set_query_arg( 'post_type', $this->allowed_post_types );

return $resolver->get_connection(); // @phpstan-ignore-line
return $resolver->get_connection();
},
],
);
Expand Down
9 changes: 8 additions & 1 deletion src/features/class-query-block-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ public function filter_query_context( $context, $parsed_block, $parent_block ):
&& 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'];
/**
* Custom post titles from the parent block.
*
* @var array<array{'postId': int, 'title': string}> $custom_post_titles
*/
$custom_post_titles = $parent_block->attributes['customPostTitles'];

$this->custom_post_titles = $custom_post_titles;
}

// Manually assign custom post titles to 'wp-curate/post-title' context.
Expand Down
18 changes: 11 additions & 7 deletions src/features/class-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public function get_posts( WP_REST_Request $request ): array { // phpcs:ignore S
if ( empty( $terms ) ) {
continue;
}
$terms = explode( ',', $terms );
$terms = explode( ',', $terms ); // @phpstan-ignore-line argument.type
$terms = array_map( 'intval', $terms );
$terms = array_filter( $terms, 'term_exists' ); // @phpstan-ignore-line
$terms = array_filter( $terms, 'term_exists' ); // @phpstan-ignore-line argument.type
$tax_query[] = [
'taxonomy' => $taxonomy->name,
'field' => 'term_id',
Expand Down Expand Up @@ -205,22 +205,26 @@ public function add_type_param( $query_args, $request ): array { // phpcs:ignore
* @return array<array<int, array<int, array<string, mixed>>|string>|string>
*/
public function add_term_support( $query_args, $request ): array {
/**
* Filter the taxonomies allowed.
*
* @param string[] $taxonomies
*/
$allowed_taxonomies = apply_filters( 'wp_curate_allowed_taxonomies', [ 'category', 'post_tag' ] );
$taxonomies = array_map( 'get_taxonomy', $allowed_taxonomies );
$taxonomies = array_filter( $taxonomies, 'is_object' );
$tax_query = [];
foreach ( $taxonomies as $taxonomy ) {
$tax_name = $taxonomy->name;
if ( empty( $tax_name ) || ! is_string( $tax_name ) ) {
if ( empty( $taxonomy->name ) ) {
continue;
}
if ( ! $request->get_param( $tax_name ) ) {
if ( ! $request->get_param( $taxonomy->name ) ) {
continue;
}
$tax_query[] = [
'taxonomy' => $tax_name,
'taxonomy' => $taxonomy->name,
'field' => 'term_id',
'terms' => $request->get_param( $tax_name ),
'terms' => $request->get_param( $taxonomy->name ),
];
}
if ( empty( $tax_query ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/features/class-stop-queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function boot(): void {
* @return array<string, mixed> Updated query arguments.
*/
public function filter_query_loop_block_query_vars( $query, $block ): array {
if ( isset( $block->context['query'][ $this->query_var ] ) ) {
if ( isset( $block->context['query'] ) && is_array( $block->context['query'] ) && isset( $block->context['query'][ $this->query_var ] ) ) {
$query[ $this->query_var ] = $block->context['query'][ $this->query_var ];
}

Expand Down
Loading