From 81852d83088dbe7308f4466ab8c5af9c67cdce4f Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Sat, 23 Sep 2023 16:28:28 +0200 Subject: [PATCH] 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 --- inc/namespace.php | 57 ++++++++++++++++++++++++ inc/template.php | 2 +- tests/phpunit/test-post-saving.php | 15 +++++++ tests/phpunit/test-user-display-name.php | 34 ++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/test-user-display-name.php diff --git a/inc/namespace.php b/inc/namespace.php index 0d360d3..351b8e2 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -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; @@ -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 ); @@ -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 $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'], + ]); } /** diff --git a/inc/template.php b/inc/template.php index 761288a..f1335c2 100644 --- a/inc/template.php +++ b/inc/template.php @@ -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 ); } diff --git a/tests/phpunit/test-post-saving.php b/tests/phpunit/test-post-saving.php index db82d27..81ac827 100644 --- a/tests/phpunit/test-post-saving.php +++ b/tests/phpunit/test-post-saving.php @@ -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; diff --git a/tests/phpunit/test-user-display-name.php b/tests/phpunit/test-user-display-name.php new file mode 100644 index 0000000..428393f --- /dev/null +++ b/tests/phpunit/test-user-display-name.php @@ -0,0 +1,34 @@ +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 ); + } +}