Skip to content

Commit

Permalink
Use author display name as term name
Browse files Browse the repository at this point in the history
- Update term name to user display name when creating terms
- Sync term name when user display name is updated
- Add tests
  • Loading branch information
nlemoine committed Sep 24, 2023
1 parent bca9c79 commit 81852d8
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
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 @@ -57,6 +59,61 @@ function bootstrap() : void {
add_filter( 'the_author', __NAMESPACE__ . '\\filter_the_author_for_rss' );
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( '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
2 changes: 1 addition & 1 deletion inc/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function get_author_ids( WP_Post $post ) : array {
}

return array_map( function ( WP_Term $term ) : int {
return intval( $term->name );
return intval( $term->slug );
}, $authors );
}

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 );
}
}

0 comments on commit 81852d8

Please sign in to comment.