humanmade / authorship

A modern approach to author attribution in WordPress.
GNU General Public License v3.0
66 stars 7 forks source link

Fix attribute post type handling #113

Open tomjn opened 1 year ago

tomjn commented 1 year ago

WIP fix for #111

tomjn commented 1 year ago

Noting this needs the phpstan fix merging in from #112 for CI to work

tomjn commented 1 year ago

@johnbillion I tracked the issue down further:

As a result, if a user does not have https://api.w.org/action-assign-author they can never have the Authorship equivalent, no matter what their caps are.

I've had some success with this filter:

add_filter( "rest_prepare_post", __NAMESPACE__ . '\\rest_prepare_post', 10, 3 );

/**
 * Filters the post data for a REST API response.
 *
 * This removes the `wp:action-assign-author` rel from the response so the default post author
 * control doesn't get shown on the block editor post editing screen.
 *
 * This also adds a new `authorship:action-assign-authorship` rel so custom clients can refer to this.
 *
 * @param WP_REST_Response $response The response object.
 * @param WP_Post          $post     Post object.
 * @param WP_REST_Request  $request  Request object.
 * @return WP_REST_Response The response object.
 */
function rest_prepare_post( WP_REST_Response $response, WP_Post $post, WP_REST_Request $request ) : WP_REST_Response {
    $links = $response->get_links();

    if ( current_user_can( 'attribute_post_type', $post->post_type ) ) {
        $response->remove_link( 'https://api.w.org/action-assign-author' );
        $response->add_link( \Authorship\REST_REL_LINK_ID, $links['self'][0]['href'] );
    }

    return $response;
}

With that I'm able to change the authors attributed when logged in as an author with the attribute_post_type capability.

This also fixes things without the changes in this PR though, so this PR should be considered at best a readability/inline documentation improvement.

This needs a test to be added to demonstrate what the current problem is.

I do not know how to write this test