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

Fix/missing wp user type #988

Merged
merged 35 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
33ae3b0
Merge pull request #989 from Automattic/release/3.5.15
GaryJones Aug 28, 2023
92c6725
fix: preventing loss of fact that a guest author might also be a WP_User
eddiesshop Aug 25, 2023
b6a0598
fix: making the update operation dependent on $append flag.
eddiesshop Aug 26, 2023
017dc74
fix: attempting DB update only when $new_author is not empty.
eddiesshop Aug 26, 2023
a0635df
fix: need to ensure pure WP_User is processed correctly as post_author.
eddiesshop Aug 26, 2023
da28669
fix: a necessary refactor of the `get_coauthor_by` function.
eddiesshop Aug 31, 2023
b71ea29
fix: returning a plain WP_User if guest authors is not enabled.
eddiesshop Aug 31, 2023
fbf2087
feat: adding additional tests for co-authors-plus.php functionality.
eddiesshop Sep 8, 2023
756d9fc
Merge branch 'main' into fix/missing_wp_user_type
eddiesshop Sep 8, 2023
0d55c46
Merge branch 'develop' into fix/missing_wp_user_type
eddiesshop Sep 9, 2023
e64921e
fix: preventing loss of fact that a guest author might also be a WP_User
eddiesshop Sep 9, 2023
c0344db
fix: making the update operation dependent on $append flag.
eddiesshop Sep 9, 2023
7ece2ab
fix: attempting DB update only when $new_author is not empty.
eddiesshop Sep 9, 2023
741c053
fix: need to ensure pure WP_User is processed correctly as post_author.
eddiesshop Sep 9, 2023
e9e76af
fix: a necessary refactor of the get_coauthor_by function.
eddiesshop Sep 9, 2023
e7c7792
fix: renaming user_login's for new authors introduced for new tests.
eddiesshop Sep 9, 2023
b6b959b
fix: removing use of assertObjectHasProperty
eddiesshop Sep 9, 2023
41006b3
fix: typo in function call
eddiesshop Sep 9, 2023
1a2d06f
Merge branch 'develop' into fix/missing_wp_user_type
eddiesshop Oct 14, 2023
7e4bd8e
fix: using strict comparison instead of function call `is_null`
eddiesshop Oct 17, 2023
aacd37b
fix: using more descriptive assertion for array validation.
eddiesshop Oct 17, 2023
fc05f9d
fix: using `create_and_get` post factory func, to avoid query call.
eddiesshop Oct 17, 2023
a0b808a
fix: removing use of newly introduced is_wp_user property.
eddiesshop Oct 17, 2023
e3a120a
fix: PHPCS fixes and added commentary/descriptions to docblocks.
eddiesshop Oct 17, 2023
6afab35
fix: some small quick fixes for formatting and documentation
eddiesshop Oct 25, 2023
719c15c
Merge branch 'develop' into fix/missing_wp_user_type
eddiesshop Jan 18, 2024
3c27322
Merge branch 'develop' into fix/missing_wp_user_type
eddiesshop May 1, 2024
175d8fe
Merge branch 'develop' into fix/missing_wp_user_type
eddiesshop Jul 2, 2024
de55e39
fix: removing repetitive test.
eddiesshop Jul 2, 2024
8dd6d67
add: new assertion func that determines if an obj is not a WP_User class
eddiesshop Jul 3, 2024
db9a6d7
add: new assertion to help determine if a Post has the correct Authors
eddiesshop Jul 3, 2024
bbfc79a
add: new test solely for CoAuthorPlus::get_coauthor_by().
eddiesshop Jul 4, 2024
312d519
fix: was passing string values when I should've been passing Author objs
eddiesshop Jul 4, 2024
961ce40
fix: using a data provider for very similar tests
eddiesshop Oct 2, 2024
d7e0274
Merge branch 'develop' into fix/missing_wp_user_type
eddiesshop Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 91 additions & 39 deletions php/class-coauthors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,51 +269,87 @@ public function is_guest_authors_enabled() {
public function get_coauthor_by( $key, $value, $force = false ) {

// If Guest Authors are enabled, prioritize those profiles
if ( isset( $this->guest_authors ) && $this->is_guest_authors_enabled() ) {
if ( $this->is_guest_authors_enabled() && isset( $this->guest_authors ) ) {
$guest_author = $this->guest_authors->get_guest_author_by( $key, $value, $force );
if ( is_object( $guest_author ) ) {
return $guest_author;
}
}
if ( isset( $guest_author->linked_account ) ) {
$user = $this->get_user_by( 'login', $guest_author->linked_account );

switch ( $key ) {
case 'id':
case 'login':
case 'user_login':
case 'email':
case 'user_nicename':
case 'user_email':
if ( 'user_login' === $key ) {
$key = 'login';
}
if ( 'user_email' === $key ) {
$key = 'email';
}
if ( 'user_nicename' === $key ) {
$key = 'slug';
}
$user = get_user_by( $key, $value );
if ( ! $user && ( 'login' === $key || 'slug' === $key ) ) {
// Re-try lookup without prefixed value if no results found.
$value = preg_replace( '#^cap\-#', '', $value );
$user = get_user_by( $key, $value );
if ( ! is_null( $user ) ) {
$guest_author->is_wp_user = true; // Important not to lose the fact that this is a WP user.
$guest_author->wp_user = $user;
}
}
if ( ! $user ) {

return $guest_author;
} else {
// Guest Author was not found, so let's see if we are searching for a WP_User
$user = $this->get_user_by( $key, $value );

if ( is_null( $user ) ) {
return false;
}

// At this point we have a valid $user.
$user->type = 'wpuser';
// However, if guest authors are enabled and there's a guest author linked to this
// user account, we want to use that instead
if ( isset( $this->guest_authors ) && $this->is_guest_authors_enabled() ) {
$guest_author = $this->guest_authors->get_guest_author_by( 'linked_account', $user->user_login );
if ( is_object( $guest_author ) ) {
$user = $guest_author;
}

$guest_author = $this->guest_authors->get_guest_author_by( 'linked_account', $user->user_login );
if ( is_object( $guest_author ) ) {
$guest_author->is_wp_user = true; // Important not to lose the fact that this is a WP user.
$guest_author->wp_user = $user;
$user = $guest_author;
}

return $user;
}
} else {
$user = $this->get_user_by( $key, $value );

if ( is_null( $user ) ) {
eddiesshop marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

$user->type = 'wpuser';

return $user;
}
return false;
}

/**
GaryJones marked this conversation as resolved.
Show resolved Hide resolved
* @param string $key Key to search by, i.e. 'id', 'login', 'user_login', 'email', 'user_email', 'user_nicename'
* @param string $value Value to search for.
*
* @return WP_User|null
*/
protected function get_user_by( $key, $value ) {
$acceptable_keys = [
'id' => 'id',
'login' => 'login',
'user_login' => 'login',
'email' => 'email',
'user_email' => 'email',
'user_nicename' => 'slug',
];

if ( ! array_key_exists( $key, $acceptable_keys ) ) {
return null;
}

$key = $acceptable_keys[ $key ];

$user = get_user_by( $key, $value );

if ( ! $user && ( 'login' == $key || 'slug' == $key ) ) {
// Re-try lookup without prefixed value if no results found.
$value = preg_replace( '#^cap\-#', '', $value );
$user = get_user_by( $key, $value );
}

if ( false === $user ) {
return null;
}

return $user;
}

/**
Expand Down Expand Up @@ -968,18 +1004,34 @@ public function add_coauthors( $post_id, $coauthors, $append = false, $query_typ
if ( empty( $post_author_user )
|| ! in_array( $post_author_user->user_login, $coauthors ) ) {
foreach ( $coauthor_objects as $coauthor_object ) {
if ( 'wpuser' === $coauthor_object->type ) {
if ( $coauthor_object instanceof WP_User ) {
$new_author = $coauthor_object;
break;
} else if ( isset( $coauthor_object->is_wp_user ) && $coauthor_object->is_wp_user ) {
eddiesshop marked this conversation as resolved.
Show resolved Hide resolved
$new_author = $coauthor_object->wp_user;
break;
}
}
// Uh oh, no WP_Users assigned to the post
if ( empty( $new_author ) ) {

/*
* If setting a fresh group of authors for a post, (i.e. $append === false),
* then perhaps one of those authors should be a WP_USER. However,
* if $append === true, and we are perhaps unable to find a
* WP_USER (perhaps none was given), we don't really
* care whether post_author should be updated.
* */
if ( false === $append && empty( $new_author ) ) {
return false;
}

$wpdb->update( $wpdb->posts, array( 'post_author' => $new_author->ID ), array( 'ID' => $post_id ) );
clean_post_cache( $post_id );
if ( ! empty( $new_author ) ) {
$update = $wpdb->update( $wpdb->posts, array( 'post_author' => $new_author->ID ), array( 'ID' => $post_id ) );
clean_post_cache( $post_id );

if ( is_bool( $update ) ) {
return $update;
}
}
}
return true;

Expand Down
Loading