Skip to content

Commit

Permalink
Merge pull request #59 from alleyinteractive/fix/implementation-fixes
Browse files Browse the repository at this point in the history
add support for post_types to rest api
  • Loading branch information
mogmarsh authored Sep 18, 2023
2 parents 42c57d8 + b7fe928 commit 4800d34
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/post-meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"wp_curate_deduplication": {
"post_types": [
"post"
"all"
],
"type": "boolean"
}
Expand Down
43 changes: 43 additions & 0 deletions src/features/class-rest-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Rest_Api class file
*
* @package wp-curate
*/

namespace Alley\WP\WP_Curate\Features;

use Alley\WP\Types\Feature;

/**
* Look for a special query var that indicates a query should not run.
*/
final class Rest_Api implements Feature {
/**
* Set up.
*/
public function __construct() {}

/**
* Boot the feature.
*/
public function boot(): void {
add_filter( 'rest_post_query', [ $this, 'add_type_param' ], 10, 2 );
}

/**
* Add post_type to rest post query if the type param is set.
*
* @param array<array<int, string>|string> $query_args The existing query args.
* @param \WP_REST_Request $request The REST request.
* @return array<array<int, string>|string>
*/
public function add_type_param( $query_args, $request ): array { // @phpstan-ignore-line
if ( ! empty( $request->get_param( 'type' ) ) && is_string( $request->get_param( 'type' ) ) ) {
$types = explode( ',', $request->get_param( 'type' ) );
$types = array_filter( $types, 'post_type_exists' );
$query_args['post_type'] = $types;
}
return $query_args;
}
}
2 changes: 2 additions & 0 deletions src/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function main(): void {
block_type_registry: WP_Block_Type_Registry::get_instance(),
);

$features[] = new Features\Rest_Api();

foreach ( $features as $feature ) {
$feature->boot();
}
Expand Down

0 comments on commit 4800d34

Please sign in to comment.