Skip to content

Commit 81852d8

Browse files
committed
Use author display name as term name
- Update term name to user display name when creating terms - Sync term name when user display name is updated - Add tests
1 parent bca9c79 commit 81852d8

4 files changed

Lines changed: 107 additions & 1 deletion

File tree

inc/namespace.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use WP_REST_Request;
2121
use WP_REST_Response;
2222
use WP_REST_Server;
23+
use WP_Term;
2324
use WP_User;
2425

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

5153
// Filters.
5254
add_filter( 'wp_insert_post_data', [ $insert_post_handler, 'filter_wp_insert_post_data' ], 10, 3 );
@@ -57,6 +59,61 @@ function bootstrap() : void {
5759
add_filter( 'the_author', __NAMESPACE__ . '\\filter_the_author_for_rss' );
5860
add_filter( 'comment_moderation_recipients', __NAMESPACE__ . '\\filter_comment_moderation_recipients', 10, 2 );
5961
add_filter( 'comment_notification_recipients', __NAMESPACE__ . '\\filter_comment_notification_recipients', 10, 2 );
62+
add_filter( 'wp_insert_term_data', __NAMESPACE__ . '\\add_term_name', 10, 3 );
63+
}
64+
65+
/**
66+
* Set term name to user display name on term creation.
67+
*
68+
* @param array{'name': string, 'slug': string, 'term_group': int} $data Term data.
69+
* @param string $taxonomy Taxonomy.
70+
* @param array{'alias_of': string, 'description': string, 'parent': int, 'slug': string} $args Term args
71+
*
72+
* @return array{'name': string, 'slug': string, 'term_group': int}
73+
*/
74+
function add_term_name( array $data, string $taxonomy, array $args ) : array {
75+
if ( $taxonomy !== TAXONOMY ) {
76+
return $data;
77+
}
78+
$user_id = (int) ( $data['slug'] ?? 0 );
79+
if ( $user_id <= 0 ) {
80+
return $data;
81+
}
82+
83+
$user = get_userdata( $user_id );
84+
if ( ! $user instanceof WP_User ) {
85+
return $data;
86+
}
87+
88+
$data['name'] = $user->display_name;
89+
return $data;
90+
}
91+
92+
/**
93+
* Sync author term name with user display name.
94+
*
95+
* @param int $user_id User ID.
96+
* @param WP_User $old_user_data Old user data.
97+
* @param array<string> $userdata User data.
98+
* @return void
99+
*/
100+
function update_author_term_name( int $user_id, WP_User $old_user_data, array $userdata ) : void {
101+
// User doesn't exist in authorship taxonomy.
102+
$term = get_term_by( 'slug', (string) $user_id, TAXONOMY );
103+
if ( ! $term instanceof WP_Term ) {
104+
return;
105+
}
106+
107+
$display_name = $userdata['display_name'] ?? null;
108+
109+
// User display name hasn't changed.
110+
if ( $term->name === $display_name ) {
111+
return;
112+
}
113+
114+
wp_update_term($term->term_id, TAXONOMY, [
115+
'name' => $userdata['display_name'],
116+
]);
60117
}
61118

62119
/**

inc/template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function get_author_ids( WP_Post $post ) : array {
3535
}
3636

3737
return array_map( function ( WP_Term $term ) : int {
38-
return intval( $term->name );
38+
return intval( $term->slug );
3939
}, $authors );
4040
}
4141

tests/phpunit/test-post-saving.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ public function testPostAuthorshipIsSetToAuthorWhenUpdatingPostWithNoExistingAut
115115
$this->assertSame( [ self::$users['author']->ID ], $author_ids );
116116
}
117117

118+
public function testPostAuthorshipTermNameIsSetToAuthorDisplaynameWhenCreatingPost() : void {
119+
/** @var int */
120+
$post_id = wp_insert_post( [
121+
'post_title' => 'Testing',
122+
'post_author' => self::$users['author']->ID,
123+
], true );
124+
/** @var \WP_Post */
125+
$post = get_post( $post_id );
126+
127+
/** @var \WP_Term[] */
128+
$author_terms = wp_get_post_terms( $post->ID, TAXONOMY );
129+
130+
$this->assertEquals( self::$users['author']->display_name, $author_terms[0]->name );
131+
}
132+
118133
public function testPostAuthorshipIsSetToEmptyWhenUpdatingPostWithNoExistingAuthorshipAndFiltered() : void {
119134
$factory = self::factory()->post;
120135

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* General user saving tests.
4+
*
5+
* @package authorship
6+
*/
7+
8+
declare( strict_types=1 );
9+
10+
namespace Authorship\Tests;
11+
12+
use const Authorship\TAXONOMY;
13+
14+
class TestUserDisplayName extends TestCase {
15+
public function testPostAuthorshipUpdatesTermNameWhenUserDisplayNameIsUpdated() : void {
16+
// Create an author term for the author user.
17+
$author_term = get_term_by( 'slug', (string) self::$users['author']->ID, TAXONOMY );
18+
if ( ! $author_term ) {
19+
wp_insert_term( self::$users['author']->ID, TAXONOMY );
20+
$author_term = get_term_by( 'slug', (string) self::$users['author']->ID, TAXONOMY );
21+
}
22+
23+
$display_name = 'New Author Name';
24+
25+
wp_update_user( [
26+
'ID' => self::$users['author']->ID,
27+
'display_name' => $display_name,
28+
] );
29+
30+
$author_term = get_term_by( 'slug', (string) self::$users['author']->ID, TAXONOMY );
31+
32+
$this->assertSame( $display_name, $author_term->name );
33+
}
34+
}

0 commit comments

Comments
 (0)