Open eddiesshop opened 11 months ago
The previous 2 commits address another issue I found during some internal testing.
What was happening was that using get_coauthor_by
and searching with a Guest Author, if that Guest Author has a valid link to a WP_User, it is summarily ignored. Functions like add_coauthors
expect at least one coauthor to be a valid WP_User so that the wp_posts.post_author
column can be appropriately updated. The only case where this function is returning an expected value is when you search by the WP_User first. When it arrives at $guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force );
, $guest_author === false
. It is then forced to move to the switch statement to find a user via their WP_User data.
With this refactor, when you arrive at $guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force );
and a valid $guest_author
is obtained, it will now check if the linked_account
attribute is set. If so, it will attempt to find the corresponding user for the Guest Account. Crucially, this change still gives priority to returning a Guest Author first and foremost. Only when a Guest Author is not found, will it search for a WP_User. If found, it will also search to see if a linked Guest Author account exists. If it does, it will return that Guest Author object instead, without losing the fact that this account also has a WP_User associated with it. If the plugin is disabled, then it will attempt to find a WP_User.
So using the examples outlined in the description, the below scenario should be tested as well:
$cap->add_coauthors( $post_id, [ 'wp_user_2', 'ga_1', 'wp_user_and_ga_1' ] )
; result should be true. Notice that wp_posts.post_author
is equal to the ID for wp_user_2
. Also, all 3 authors can be found when looking at the terms associated to $post_id
.$cap->add_coauthors( $post_id, [ 'wp_user_and_ga_1', 'ga_1', 'wp_user_2' ] )
; result should be same as above, except now wp_posts.post_author
is linked to wp_user_and_ga_1
.$cap->add_coauthors( $post_id, [ 'ga_1' ] )
; result should be false
. Since this is a pure GA account, wp_post.post_author
is not updated, however we can see a term-taxonomy associated with this $post_id
. Follow this up by executing $cap->add_coauthors( $post_id, [ 'wp_user_2', 'wp_user_and_ga_1' ], true )
; the result should be true
. Notice that wp_posts.post_author
is equal to wp_user_2
's user ID.Thanks @eddiesshop. A couple of quick meta questions:
Description
I came across this bug when attempting to do the following:
I placed my
WP_User
first in the array because of this block of code. It will update thewp_posts.post_author
column for the first WP_User it finds.However, I noticed that the
wp_posts.post_author
column was not being updated as I was expecting.I started digging through the code and found that this block of code was essentially being skipped, due to the fact that this function always returns a Guest Author object.
This is actually causing the
add_coauthors
function to always respond withfalse
for instances wherewp_posts.post_author
= 0 or where the user no longer exists on the system. Even if you're passing in a valid WP_User who is also a Guest Author, it would respond withfalse
, as was my case.With this fix, we retain the fact that the original
user_nicename
we passed is also a WP_User. In this way, we don't waste compute cycles in that foreach loop where it would never find a valid WP_User it's trying to look for.Deploy Notes
Are there any new dependencies added that should be taken into account when deploying to WordPress.org? No
Steps to Test
Outline the steps to test and verify the PR here.
You will need 5 authors:
Example:
$cap = new CoAuthor_plus();
$cap->add_coauthors( $post_id, [ 'wp_user_1' ] )
; result should betrue
.wp_posts.post_author
should be equal towp_users.ID
$cap->add_coauthors( $post_id, [ 'ga_1', 'ga_2' ] )
; result should befalse
. This is because$append = false
and there was nowp_user
passed as a CoAuthor. Notice also thatwp_posts.post_author
remains untouched.$append = true
, like so:$cap->add_coauthors( $post_id, [ 'ga_1', 'ga_2' ], true )
; result should betrue
. Notice, that all 3 authors are correctly displayed.$cap->add_coauthors( $post_id, [ 'ga_1', 'wp_user_2' ], true )
; result should be true. Notice thatwp_posts.post_author
is not overwritten. This is because we are appending authors to the post.$cap->add_coauthors( $post_id, [ 'ga_2', 'wp_user_and_ga_1' ] )
; result should betrue
. Notice here that the WP_User is placed second in the array, but it is correctly picked up now, and thewp_posts.post_author
column is correctly updated to match the user'swp_users.ID
column.