Skip to content

Commit

Permalink
fix: also search for coauthor posts by term slug (#1954)
Browse files Browse the repository at this point in the history
* fix: also search for coauthor posts by term slug

* fix: account for author__in AND tax_query combination

* fix: make it work if guest authors are disabeld

* test: fix tests

* test: fix tests

* docs: tiny fix
  • Loading branch information
leogermani authored Nov 18, 2024
1 parent 1af11a4 commit 49357ff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
58 changes: 25 additions & 33 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,19 +674,16 @@ public static function build_articles_query( $attributes, $block_name ) {
}
}

$is_co_authors_plus_active = class_exists( 'CoAuthors_Guest_Authors' );
$is_co_authors_plus_active = class_exists( 'CoAuthors_Plus' );
$co_authors_guest_authors = class_exists( 'CoAuthors_Guest_Authors' ) ? new CoAuthors_Guest_Authors() : null;

if ( $authors && count( $authors ) ) {
$co_authors_names = [];
$author_names = [];
$author_emails = [];

if ( $is_co_authors_plus_active ) {
$co_authors_guest_authors = new CoAuthors_Guest_Authors();

foreach ( $authors as $index => $author_id ) {
// If the given ID is a guest author.
$co_author = $co_authors_guest_authors->get_guest_author_by( 'id', $author_id );
$co_author = $co_authors_guest_authors ? $co_authors_guest_authors->get_guest_author_by( 'id', $author_id ) : null;
if ( $co_author ) {
if ( ! empty( $co_author->linked_account ) ) {
$linked_account = get_user_by( 'login', $co_author->linked_account );
Expand All @@ -697,20 +694,23 @@ public static function build_articles_query( $attributes, $block_name ) {
$co_authors_names[] = $co_author->user_nicename;
unset( $authors[ $index ] );
} else {
// If the given ID is linked to a guest author.
$authors_controller = new WP_REST_Newspack_Authors_Controller();
$author_data = get_userdata( $author_id );
if ( $author_data ) {
$linked_guest_author = $authors_controller->get_linked_guest_author( $author_data->user_login );
// If the given ID is linked to a guest author.
if ( $linked_guest_author ) {
$guest_author_name = sanitize_title( $linked_guest_author->post_title );
if ( ! in_array( $guest_author_name, $co_authors_names, true ) ) {
$co_authors_names[] = $guest_author_name;
$co_authors_names[] = $linked_guest_author->post_name;
unset( $authors[ $index ] );
}
} else {
$author_names[] = $author_data->user_login;
$author_emails[] = $author_data->user_email;
$co_authors_names[] = $author_data->user_login;
$co_authors_names[] = $author_data->user_nicename;
$co_authors_names[] = 'cap-' . $author_data->user_nicename;
$co_authors_names[] = $author_data->user_email;
}
}
}
Expand All @@ -719,42 +719,28 @@ public static function build_articles_query( $attributes, $block_name ) {

// Reset numeric indexes.
$authors = array_values( $authors );

if ( empty( $authors ) && count( $co_authors_names ) ) {
// Look for co-authors posts.
// We are only looking for Guest Authors posts. So we need to only search by taxonomy.
$args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'relation' => 'OR',
[
'field' => 'slug',
'taxonomy' => 'author',
'terms' => $co_authors_names,
],
[
'field' => 'name',
'taxonomy' => 'author',
'terms' => $co_authors_names,
],
];
} elseif ( empty( $co_authors_names ) && count( $authors ) ) {
// Simple search by author. Co-Authors plus is not active.
$args['author__in'] = $authors;

if ( $is_co_authors_plus_active ) {
// Don't get any posts that are attributed to other CAP guest authors.
$args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
[
'relation' => 'OR',
[
'taxonomy' => 'author',
'operator' => 'NOT EXISTS',
],
[
'field' => 'name',
'taxonomy' => 'author',
'terms' => $author_names,
],
[
'field' => 'name',
'taxonomy' => 'author',
'terms' => $author_emails,
],
],
];
}
} else {
// The query contains both WP users and CAP guest authors. We need to filter the SQL query.
// That's because author__in and tax_query would be combined with AND, not OR.
self::$filter_clauses = [
'authors' => $authors,
'coauthors' => $co_authors_names,
Expand Down Expand Up @@ -1254,11 +1240,17 @@ public static function filter_posts_clauses_when_co_authors( $clauses, $query )

// co-author tax query.
$tax_query = [
'relation' => 'OR',
[
'taxonomy' => 'author',
'field' => 'name',
'terms' => $co_authors_names,
],
[
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $co_authors_names,
],
];

// Generate the tax query SQL.
Expand Down
1 change: 0 additions & 1 deletion tests/test-homepage-posts-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function test_hpb_build_articles_query() {
'resulting_query_partial' => [
'posts_per_page' => 1,
'post_type' => 'some-type',
'author__in' => [ 1 ],
],
'description' => 'With custom post type and author',
'ignore_tax_query' => true,
Expand Down
5 changes: 5 additions & 0 deletions tests/wp-unittestcase-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* @package Newspack_Blocks
*/

/**
* Mock CoAuthors_Plus class to pretend the plugin is active.
*/
class CoAuthors_Plus {} // phpcs:ignore

/**
* Mock CoAuthors_Guest_Authors class.
*/
Expand Down

0 comments on commit 49357ff

Please sign in to comment.