Automattic / Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
https://wordpress.org/plugins/co-authors-plus/
GNU General Public License v2.0
291 stars 204 forks source link

get_coauthors() and is_coauthor_for_post() with faulty condition #856

Closed biophonc closed 2 years ago

biophonc commented 2 years ago

Environment: PHP 8.0, WP 5.9+

Hi, my PHP log is flooded with these entries for a while:

[08-Jun-2022 10:10:10] WARNING: [pool foo] child 32581 said into stderr: "NOTICE: PHP message: PHP Warning: Attempt to read property "ID" on int in /var/www/foo/wp-content/plugins/co-authors-plus/template-tags.php on line 13"

Basically, it's this condition:

if ( ! $post_id && $post ) { 
    $post_id = $post->ID;
}

Both $post_id and $post are integers. This means, that in get_coauthors() the $post_id will always be 0, resulting in, that the final filter will always be called with these values:

$coauthors = apply_filters( 'get_coauthors', [], 0);

The function is_coauthor_for_post() will permanently return early, with the value false because of this.

lschuyler commented 2 years ago

Hi @biophonc,

If this is still happening on your site, could you try outputting the global post object before the get_coauthors function is called to check its value?

It sounds like the post object has been inadvertently set to an integer, maybe in your theme or by another plugin or with something you're testing? Or possibly, could be calling get_coauthors from outside the loop where the post object doesn't exist?

biophonc commented 2 years ago

Hi @lschuyler,

Unfortunately I'm currently using a patched version of co authors (using this repo as upstream) but I'll prepare a test to see whether it's most likely caused by a 3rd party plugin, my theme or the co authors plugin.

biophonc commented 2 years ago

Hi @lschuyler,

I finally have found the time to investigate the issue further, and you were right, it was caused by my theme that:

  1. uses global $post in one template, without being encapsulated (haven't noticed it earlier)
  2. included template parts then inherit the object/variable
  3. and in one template was a foreach loop for a SearchWP result set, that simplified did this:
    foreach ($related_content as $post) { // note: $post is still global here and thus gets overwritten
    setup_postdata($post); 
    }
    wp_reset_postdata(); 

I've also tested it with a fresh installation of WP 6.0 on PHP 8, with the twentyone theme and everything works fine as expected :-) This means it's not a Co-Authors-Plus bug.

I feel so stupid right now, because the cause was so obvious :facepalm:. This probably happens only, when you work on a codebase for 5 years+ and it's your own ^^

Thanks!