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