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 205 forks source link

Fix PHP Warnings on accessing $coauthor instance #975

Closed cobianzo closed 1 year ago

cobianzo commented 1 year ago

at https://github.com/Automattic/Co-Authors-Plus/blob/master/template-tags.php#L75 we are continously getting PHP warnings for accessnig to inexisting properties user_login and linked_account. This could be improved with a simple isset verificationi before accessing the values.

if ( ( isset( $coauthor->user_login ) && $user == $coauthor->user_login ) || 
        ( isset( $coauthor->linked_account ) && $user == $coauthor->linked_account ) ) {
            return true;
        }
GaryJones commented 1 year ago

Thanks @cobianzo - are you able to determine why $coauthors ends up being an array of items that are not the expected objects?

cobianzo commented 1 year ago

@GaryJones Hi I should investigate about it. We have quite a few of customized code which uses $coauthors instances every time we need to access to the author of a post. I'll put some time aside to find out in what moment that happens.

I am not sure these items end up being an array as much as they are objects without the expected properties user_login and `linked_account'.

Here the PHP warning:

Warning: Undefined property: stdClass::$linked_account in /wp/wp-content/plugins/co-authors-plus/co-authors-plus.php on line 1122 [inews61.vipdev.lndo.site/wp-json/wp/v2/posts/2376374?_embed=true&access_token=Uo9HQbCl76rL] [wp-includes/class-wp-hook.php:310 CoAuthors_Plus->filter_count_user_posts(), wp-includes/plugin.php:205 WP_Hook->apply_filters(), wp-includes/user.php:573 apply_filters('get_usernumposts'), wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php:445 count_user_posts(), wp-includes/rest-api.php:821 WP_REST_Users_Controller->get_item_permissions_check(), wp-includes/class-wp-hook.php:308 rest_send_allow_header(), wp-includes/plugin.php:205 WP_Hook->apply_filters(), wp-includes/rest-api/class-wp-rest-server.php:731 apply_filters('rest_post_dispatch'), wp-includes/rest-api/class-wp-rest-server.php:584 WP_REST_Server->embed_links(), wp-includes/rest-api/class-wp-rest-server.php:503 WP_REST_Server->response_to_data(), wp-includes/rest-api.php:410 WP_REST_Server->serve_request(), wp-includes/class-wp-hook.php:308 rest_api_loaded(), wp-includes/class-wp-hook.php:332 WP_Hook->apply_filters(), wp-includes/plugin.php:565 WP_Hook->do_action(), wp-includes/class-wp.php:399 do_action_ref_array(), wp-includes/class-wp.php:780 WP->parse_request(), wp-includes/functions.php:1332 WP->main(), wp-blog-header.php:16 wp(), index.php:17 require('wp-blog-header.php')]