Skip to content

fix: verify Guest Authors when running CLI commands #1090

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

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
6 changes: 6 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install SVN (Subversion)
run: |
sudo apt-get update
sudo apt-get install subversion

- name: Push to WordPress.org
uses: 10up/action-wordpress-plugin-deploy@stable
env:
Expand Down
24 changes: 23 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.6.3] - 2024-10-25

### Fixed

* Prevent terms backfill CLI command to enter an infinite loop when a post is assigned to a user that no longer exists #1065

## [3.6.2] - 2024-10-10

### Added

* Support for Yoast Name variable #1046
* New CLI cmd to backfill missing author terms for posts #1060

### Fixed

* Correctly populates the post_author column for linked authors #988

### Maintenance
* Increase minimum supported WordPress version to 5.9 #1035

## [3.6.1] - 2024-04-25

### Fixed
Expand Down Expand Up @@ -494,7 +514,9 @@ Props to the many people who helped make this release possible: [catchmyfame](ht
**1.1.0 (Apr. 14, 2009)**
* Initial beta release.

[3.6.1]: https://github.com/automattic/co-authors-plus/compare/3.6.0..3.6.1
[3.6.3]: https://github.com/automattic/co-authors-plus/compare/3.6.2...3.6.3
[3.6.2]: https://github.com/automattic/co-authors-plus/compare/3.6.1...3.6.2
[3.6.1]: https://github.com/automattic/co-authors-plus/compare/3.6.0...3.6.1
[3.6.0]: https://github.com/automattic/co-authors-plus/compare/3.5.15...3.6.0
[3.5.15]: https://github.com/automattic/co-authors-plus/compare/3.5.14...3.5.15
[3.5.14]: https://github.com/automattic/co-authors-plus/compare/3.5.13...3.5.14
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Co-Authors Plus

Stable tag: 3.6.1
Requires at least: 4.1
Tested up to: 6.5
Requires PHP: 7.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: authors, users, multiple authors, co-authors, multi-author, publishing
Stable tag: 3.6.3
Requires at least: 5.9
Tested up to: 6.6
Requires PHP: 7.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: authors, users, multiple authors, co-authors, multi-author, publishing
Contributors: batmoo, danielbachhuber, automattic, GaryJ

Assign multiple bylines to posts, pages, and custom post types with a search-as-you-type input box.
Expand Down Expand Up @@ -67,4 +67,4 @@ Yes! You can disable guest authors entirely through a filter. Having the followi

## Change Log

[View the change log](https://github.com/Automattic/Co-Authors-Plus/blob/master/CHANGELOG.md).
[View the change log](https://github.com/Automattic/Co-Authors-Plus/blob/master/CHANGELOG.md).
4 changes: 2 additions & 2 deletions co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Plugin Name: Co-Authors Plus
* Plugin URI: https://wordpress.org/plugins/co-authors-plus/
* Description: Allows multiple authors to be assigned to a post. This plugin is an extended version of the Co-Authors plugin developed by Weston Ruter.
* Version: 3.6.1
* Version: 3.6.3
* Requires at least: 5.9
* Requires PHP: 7.4
* Author: Mohammad Jangda, Daniel Bachhuber, Automattic
Expand All @@ -21,7 +21,7 @@
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

const COAUTHORS_PLUS_VERSION = '3.6.1';
const COAUTHORS_PLUS_VERSION = '3.6.3';
const COAUTHORS_PLUS_FILE = __FILE__;

require_once __DIR__ . '/template-tags.php';
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "co-authors-plus",
"version": "3.6.1",
"version": "3.6.3",
"description": "Allows multiple authors to be assigned to a post.",
"license": "GPL-2.0-or-later",
"private": true,
Expand Down
131 changes: 92 additions & 39 deletions php/class-coauthors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,51 +307,88 @@ public function is_guest_authors_enabled(): bool {
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 ( null !== $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 ( 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->wp_user = $user;
$user = $guest_author;
}

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

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

$user->type = 'wpuser';

return $user;
}
return false;
}

/**
* Searches for authors by way of the WP_User table using a specific list of data points. If login or slug
* are provided as search parameters, this function will remove `cap-` from the search value, if present.
*
* @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 @@ -1016,18 +1053,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;
} elseif ( isset( $coauthor_object->wp_user ) && $coauthor_object->wp_user instanceof WP_User ) {
$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