generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from alleyinteractive/fix/implementation-fixes
add support for post_types to rest api
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"wp_curate_deduplication": { | ||
"post_types": [ | ||
"post" | ||
"all" | ||
], | ||
"type": "boolean" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters