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

Deprecation notice (PHP 8.1 and above) in PullListTable when distributed posts are not allowed to be edited #1164

Closed
1 task done
lakrisgubben opened this issue Dec 1, 2023 · 0 comments · Fixed by #1245
Labels
help wanted type:bug Something isn't working.
Milestone

Comments

@lakrisgubben
Copy link
Contributor

Describe the bug

On the "pulled" list in the PullListTable view (/wp-admin/admin.php?page=pull&status=pulled), the following deprecation notice is thrown (if running php 8.1 or above) for each post in the list if the current user cannot edit that post:
Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /wp-includes/formatting.php on line 4494

The issue is that the Edit link is added to the list table (here: https://github.com/10up/distributor/blob/develop/includes/classes/PullListTable.php#L370) without a check for if the current user can edit that post. This results in the call to get_edit_post_link returning null and passing that to esc_url triggers the deprecation notice above.

On our setup we don't allow the pulled posts to be edited so we have a filter kind of like this to make sure they're not:

add_filter(
	'map_meta_cap',
	static function( array $caps, string $cap, int $user_id, array $args ): array {
		if ( 'edit_post' === $cap ) {
			$post = get_post( $args[0] );
			if (
				! is_null( $post )
				&& get_post_meta( $post->ID, 'dt_original_blog_id', true )
			) {
				$caps   = [];
				$caps[] = 'do_not_allow';
			}
		}
		return $caps;
	},
	10,
	4
);

The easiest fix for this is probably to change these lines https://github.com/10up/distributor/blob/develop/includes/classes/PullListTable.php#L368-L373 to something like:

if ( ! empty( $new_post ) ) {
	$actions = [ 'view' => '<a href="' . esc_url( get_permalink( $new_post_id ) ) . '">' . esc_html__( 'View', 'distributor' ) . '</a>' ];
	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>';
	}
}

Happy to make a PR if y'all deem it worthwhile. :)

Steps to Reproduce

  1. Make sure your site runs PHP 8.1 or above
  2. Add a filter like the one above on map_meta_cap so that the current user is not allowed to edit distributed posts.
  3. Pull a post
  4. Go to the list of pulled posts with WP_DEBUG set to true
  5. You'll see the deprecation notice

Screenshots, screen recording, code snippet

No response

Environment information

No response

WordPress information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@lakrisgubben lakrisgubben added the type:bug Something isn't working. label Dec 1, 2023
@jeffpaul jeffpaul added this to the 2.1.0 milestone Dec 1, 2023
@jeffpaul jeffpaul moved this from Incoming to Backlog in Open Source Practice Dec 1, 2023
@faisal-alvi faisal-alvi moved this from Backlog to Done in Open Source Practice Jul 12, 2024
@dkotter dkotter modified the milestones: 2.1.0, 2.0.5 Aug 15, 2024
@dkotter dkotter linked a pull request Aug 15, 2024 that will close this issue
4 tasks
@dkotter dkotter closed this as completed Aug 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted type:bug Something isn't working.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants