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

Fix attribute post type handling #113

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

tomjn
Copy link
Contributor

@tomjn tomjn commented Nov 15, 2022

WIP fix for #111

@tomjn
Copy link
Contributor Author

tomjn commented Nov 15, 2022

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

inc/namespace.php Outdated Show resolved Hide resolved
Copy link
Member

@johnbillion johnbillion left a comment

Choose a reason for hiding this comment

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

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

@tomjn
Copy link
Contributor Author

tomjn commented Dec 15, 2022

@johnbillion I tracked the issue down further:

  • authors cannot change the author of a post because they can only see their own posts. If they changed the author the post would immediatley lose access to this
  • For this reason Author changing is locked behind edit_others_posts in WP
  • This means in the REST API https://api.w.org/action-assign-author is what's used to tell the block editor if they can or can't assign an author
  • Authorship looks for https://api.w.org/action-assign-author and if it finds it, it removes it and puts its own rel in

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants