Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling archiveless posts in REST API queries #65

Merged
merged 2 commits into from
Nov 13, 2023
Merged
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
42 changes: 37 additions & 5 deletions inc/class-archiveless.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,45 @@ public function updated_post_meta( $meta_id, $object_id, $meta_key, $meta_value
}

/**
* Add a filter to all post types for REST response modification.
* Filters to all post types with REST support.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should read "Add filters..."

*/
public function filter_rest_response(): void {
// Override the post status in the REST response to avoid Gutenbugs.
foreach ( get_post_types() as $allowed_post_type ) {
add_filter( 'rest_prepare_' . $allowed_post_type, [ $this, 'rest_prepare_post_data' ] );
foreach ( get_post_types( [ 'show_in_rest' => true ] ) as $allowed_post_type ) {
// Override the post status in the REST response to avoid Gutenbugs.
add_filter( "rest_prepare_{$allowed_post_type}", [ $this, 'filter__rest_prepare_post_data' ] );

// Add the `include_archiveless` parameter to the REST API.
add_filter( "rest_{$allowed_post_type}_query", [ $this, 'filter__rest_post_type_query' ], 10, 2 );
}
}

/**
* Filter the REST API query, with the edit context, to include archiveless posts.
*
* @param array $args Array of arguments for WP_Query.
* @param WP_REST_Request $request The REST API request.
* @return array
*/
public function filter__rest_post_type_query( $args, $request ): array {
/**
renatonascalves marked this conversation as resolved.
Show resolved Hide resolved
* Check the edit context from WP_REST_Request.
*
* That means we can reasonably assume that REST API requests using the `edit` context,
* in the back-end or front-end of the site, are being performed for editing purposes,
* where we want to show the `archived` posts.
*
* @see <https://github.com/alleyinteractive/archiveless/issues/61>
*/
if ( 'edit' !== $request->get_param( 'context' ) ) {
return $args;
}

// Respect any existing query rules.
if ( ! isset( $args['include_archiveless'] ) ) {
$args['include_archiveless'] = true;
}

return $args;
}

/**
Expand All @@ -260,7 +292,7 @@ public function filter_rest_response(): void {
* @param WP_REST_Response $response The response object.
* @return WP_REST_Response The modified response.
*/
public function rest_prepare_post_data( $response ): WP_REST_Response {
public function filter__rest_prepare_post_data( $response ) {
// Override the post status if it is 'archiveless'.
if ( ! empty( $response->data['status'] ) && self::$status === $response->data['status'] ) {
$response->data['status'] = 'publish';
Expand Down