PluginProbe
Parse.ly / 3.6.0
Parse.ly v3.6.0
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / Metadata / class-author-archive-builder.php

class-author-archive-builder.php in Parse.ly 3.6.0, at src/Metadata/class-author-archive-builder.php

90 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Author Archive Page Metadata Builder class
4 *
5 * @package Parsely
6 * @since 3.4.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Metadata;
12
13 use WP_User;
14
15 /**
16 * Implements abstract Metadata Builder class to generate the metadata array
17 * for an author archive page.
18 *
19 * @since 3.4.0
20 */
21 class Author_Archive_Builder extends Metadata_Builder {
22 /**
23 * Generates the metadata object by calling the build_* methods and
24 * returns the value.
25 *
26 * @since 3.4.0
27 *
28 * @return array<string, mixed>
29 */
30 public function get_metadata(): array {
31 $this->build_basic();
32 $this->build_headline();
33 $this->build_url();
34
35 return $this->metadata;
36 }
37
38 /**
39 * Populates the `headline` field in the metadata object. Integrates with
40 * the Co-Authors Plus plugin if it is installed and activated.
41 *
42 * @since 3.4.0
43 */
44 private function build_headline(): void {
45 // Use the author's username as a display name fallback.
46 $author_username = get_query_var( 'author_name' );
47 $author_display_name = $author_username;
48
49 // Attempt to get the author from the Co-Authors Plus plugin or from
50 // WordPress core if this fails.
51 $author = $this->get_display_name_from_coauthors( $author_username );
52 if ( false === $author ) {
53 $author = get_user_by( 'slug', $author_username );
54 if ( false === $author ) {
55 $author = get_userdata( get_query_var( 'author' ) );
56 }
57 }
58
59 // Set the display name and populate the metadata.
60 if ( true === is_object( $author ) && isset( $author->display_name ) ) {
61 $author_display_name = $author->display_name;
62 }
63 $this->metadata['headline'] = $this->clean_value(
64 /* translators: %s: Author name. */
65 sprintf( __( 'Author - %s', 'wp-parsely' ), $author_display_name )
66 );
67 }
68
69 /**
70 * Returns the author's display name using the Co-Authors Plus plugin when
71 * it is installed and activated.
72 *
73 * Note: The object that gets returned for Guest Authors is not of the
74 * `WP_User` type, but it contains a display_name property.
75 *
76 * @since 3.6.0
77 *
78 * @param string $author_username The author's username.
79 * @return WP_User|stdClass|false The object or false on failure.
80 */
81 private function get_display_name_from_coauthors( string $author_username ) {
82 if ( class_exists( 'coauthors_plus' ) ) {
83 global $coauthors_plus;
84 return $coauthors_plus->get_coauthor_by( 'user_nicename', $author_username );
85 }
86
87 return false;
88 }
89 }
90