Skip to content

Commit

Permalink
Update GET coauthors API endpoint to be public
Browse files Browse the repository at this point in the history
* Adds `get_authors_with_api_schema` function that returns the co-authors from the WP_REST_Server so the schema and data match the editor.
* Uses that new function in the Co-Authors Block when getting the authors for a post.
  • Loading branch information
douglas-johnson committed Oct 30, 2023
1 parent 062a2d6 commit 153dfb4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
28 changes: 1 addition & 27 deletions php/api/endpoints/class-coauthors-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function register_coauthors_route(): void {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permission_check' ),
'permission_callback' => '__return_true'
),
)
);
Expand Down Expand Up @@ -198,32 +198,6 @@ function( $author ) use ( $request ) : array {
);
}

/**
* Get Items Permission Check
*
* @since 3.6.0
* @param WP_REST_Request $request
* @return bool|WP_Error
*/
public function get_items_permission_check( WP_REST_Request $request ) {

$post_id = $request->get_param( 'post_id' );

if ( current_user_can( 'edit_post', $post_id ) ) {
return true;
}

if ( is_coauthor_for_post( get_current_user(), $post_id ) ) {
return true;
}

return new WP_Error(
'rest_cannot_view',
__( 'Sorry, you are not allowed to view co-authors of this post.', 'co-authors-plus' ),
array( 'status' => rest_authorization_required_code() )
);
}

/**
* Retrieves the CoAuthor schema, conforming to JSON Schema.
*
Expand Down
15 changes: 3 additions & 12 deletions php/blocks/block-coauthors/class-block-coauthors.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace CoAuthors\Blocks;

use WP_Block;
use CoAuthors\Blocks;

/**
* Block CoAuthors
Expand Down Expand Up @@ -96,19 +97,9 @@ public static function render_block( array $attributes, string $content, WP_Bloc
return '';
}

$authors = array_values(
array_filter(
array_map(
'CoAuthors\Blocks::get_author_with_api_schema',
get_coauthors( $post_id )
),
function( $author ) : bool {
return ! is_null( $author );
}
)
);
$authors = Blocks::get_authors_with_api_schema( $post_id );

if ( ! is_array( $authors ) || empty( $authors ) ) {
if ( empty( $authors ) ) {
return '';
}

Expand Down
35 changes: 35 additions & 0 deletions php/blocks/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,39 @@ public static function get_author_with_api_schema( $author ): ?array {

return $data;
}

/**
* Get CoAuthors with API Schema
*
* Use the global WP_REST_Server to fetch co-authors for a post,
* so that it matches what a user would see in the editor.
*
* @since 3.6.0
* @param int $post_id Post ID for querying co-authors.
* @param array $data Co-authors as returned by the REST API.
*/
public static function get_authors_with_api_schema( int $post_id ): array {

$data = rest_get_server()->dispatch(
WP_REST_Request::from_url(
home_url(
sprintf(
'/wp-json/coauthors/v1/coauthors?post_id=%d',
$post_id
)
)
)
)->get_data();

if ( ! is_array( $data ) ) {
return array();
}

// The presence of `code` indicates this is an error response.
if ( array_key_exists( 'code', $data ) ) {
return array();
}

return $data;
}
}

0 comments on commit 153dfb4

Please sign in to comment.