Skip to content

Empty edit post link on pulled content screen if current user cannot edit post #463

Description

@lakrisgubben

Describe the bug

On the Distributor->pull content->pulled list table the href attribute of the edit link below the post title is empty if the current user cannot edit that post (see image).

I realise that this is probably an edge-case, but on our (multisite/internal connections) setup we don't allow editing of pulled/pushed posts on any other site than the origin. To enforce this we have a filter on the edit_post capability (using map_meta_cap) to check if the post is pulled/pushed from another site and if so we don't allow editing of that post. This leads to the edit link being empty since the call to get_edit_post_link() in class PullListTable won't return anything.

An easy fix for this is to move the edit key in the $actions list one line down and add a current_user_can check around it, like so:

if ( current_user_can( 'edit_post', $new_post_id ) ) {
	$actions['edit'] = '<a href="' . esc_url( get_edit_post_link( $new_post_id ) ) . '">' . esc_html__( 'Edit', 'distributor' ) . '</a>';
} 

Steps to Reproduce

  1. Add something like this to an mu-plugin or the functions.php file of your theme:
/**
 * Do not allow distributed posts to be edited on other sites than their origin site.
 */
add_filter(
	'map_meta_cap',
	function( array $caps, string $cap, int $user_id, array $args ): array {
		if ( 'edit_post' === $cap ) {
			$post = get_post( $args[0] );
			if (
				! is_null( $post )
				&& 'post' === $post->post_type
				&& get_post_meta( $post->ID, 'dt_original_blog_id', true )
			) {
				$caps   = [];
				$caps[] = 'do_not_allow';
			}
		}
		return $caps;
	},
	10,
	4
);
  1. Go to Distributor->pull content
  2. Pull content from an internal (multisite) connection
  3. Go to Distributor->pull content->pulled
  4. Check the edit links href attribute and notice it's empty

Expected behavior

The edit link is not shown if the current user cannot edit the post.

Screenshots

Screenshot 2019-10-02 at 11 40 30

Environment information

  • Distributor version: 1.5.0
  • Theme and version: Twenty Nineteen 1.4
  • Other installed plugin(s) and version(s): none
  • WordPress 5.2.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions