PluginProbe
Parse.ly / 3.20.2
Parse.ly v3.20.2
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.20.2, at src/Metadata/class-author-archive-builder.php

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