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

Feat add author name #140

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
57 changes: 57 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
use WP_Term;
use WP_User;

use function Asset_Loader\enqueue_asset;
Expand Down Expand Up @@ -47,6 +48,7 @@ function bootstrap() : void {
add_action( 'pre_get_posts', __NAMESPACE__ . '\\action_pre_get_posts', 9999 );
add_action( 'wp', __NAMESPACE__ . '\\action_wp' );
add_action( 'wp_insert_post', [ $insert_post_handler, 'action_wp_insert_post' ], 10, 3 );
add_action( 'profile_update', __NAMESPACE__ . '\\update_author_term_name', 10, 3 );

// Filters.
add_filter( 'wp_insert_post_data', [ $insert_post_handler, 'filter_wp_insert_post_data' ], 10, 3 );
Expand All @@ -58,6 +60,61 @@ function bootstrap() : void {
add_filter( 'comment_moderation_recipients', __NAMESPACE__ . '\\filter_comment_moderation_recipients', 10, 2 );
add_filter( 'comment_notification_recipients', __NAMESPACE__ . '\\filter_comment_notification_recipients', 10, 2 );
add_filter( 'quick_edit_dropdown_authors_args', __NAMESPACE__ . '\\hide_quickedit_authors' );
add_filter( 'wp_insert_term_data', __NAMESPACE__ . '\\add_term_name', 10, 3 );
}

/**
* Set term name to user display name on term creation.
*
* @param array{'name': string, 'slug': string, 'term_group': int} $data Term data.
* @param string $taxonomy Taxonomy.
* @param array{'alias_of': string, 'description': string, 'parent': int, 'slug': string} $args Term args
*
* @return array{'name': string, 'slug': string, 'term_group': int}
*/
function add_term_name( array $data, string $taxonomy, array $args ) : array {
if ( $taxonomy !== TAXONOMY ) {
return $data;
}
$user_id = (int) ( $data['slug'] ?? 0 );
if ( $user_id <= 0 ) {
return $data;
}

$user = get_userdata( $user_id );
if ( ! $user instanceof WP_User ) {
return $data;
}

$data['name'] = $user->display_name;
return $data;
}

/**
* Sync author term name with user display name.
*
* @param int $user_id User ID.
* @param WP_User $old_user_data Old user data.
* @param array<string> $userdata User data.
* @return void
*/
function update_author_term_name( int $user_id, WP_User $old_user_data, array $userdata ) : void {
// User doesn't exist in authorship taxonomy.
$term = get_term_by( 'slug', (string) $user_id, TAXONOMY );
if ( ! $term instanceof WP_Term ) {
return;
}

$display_name = $userdata['display_name'] ?? null;

// User display name hasn't changed.
if ( $term->name === $display_name ) {
return;
}

wp_update_term( $term->term_id, TAXONOMY, [
'name' => $userdata['display_name'],
] );
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/test-post-saving.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ public function testPostAuthorshipIsSetToAuthorWhenUpdatingPostWithNoExistingAut
$this->assertSame( [ self::$users['author']->ID ], $author_ids );
}

public function testPostAuthorshipTermNameIsSetToAuthorDisplaynameWhenCreatingPost() : void {
/** @var int */
$post_id = wp_insert_post( [
'post_title' => 'Testing',
'post_author' => self::$users['author']->ID,
], true );
/** @var \WP_Post */
$post = get_post( $post_id );

/** @var \WP_Term[] */
$author_terms = wp_get_post_terms( $post->ID, TAXONOMY );

$this->assertEquals( self::$users['author']->display_name, $author_terms[0]->name );
}

public function testPostAuthorshipIsSetToEmptyWhenUpdatingPostWithNoExistingAuthorshipAndFiltered() : void {
$factory = self::factory()->post;

Expand Down
34 changes: 34 additions & 0 deletions tests/phpunit/test-user-display-name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* General user saving tests.
*
* @package authorship
*/

declare( strict_types=1 );

namespace Authorship\Tests;

use const Authorship\TAXONOMY;

class TestUserDisplayName extends TestCase {
public function testPostAuthorshipUpdatesTermNameWhenUserDisplayNameIsUpdated() : void {
// Create an author term for the author user.
$author_term = get_term_by( 'slug', (string) self::$users['author']->ID, TAXONOMY );
if ( ! $author_term ) {
wp_insert_term( self::$users['author']->ID, TAXONOMY );
$author_term = get_term_by( 'slug', (string) self::$users['author']->ID, TAXONOMY );
}

$display_name = 'New Author Name';

wp_update_user( [
'ID' => self::$users['author']->ID,
'display_name' => $display_name,
] );

$author_term = get_term_by( 'slug', (string) self::$users['author']->ID, TAXONOMY );

$this->assertSame( $display_name, $author_term->name );
}
}