| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presentations; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Helpers\Author_Archive_Helper; |
| 6 |
use Yoast\WP\SEO\Helpers\Post_Type_Helper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Indexable_Author_Archive_Presentation. |
| 10 |
* |
| 11 |
* Presentation object for indexables. |
| 12 |
*/ |
| 13 |
class Indexable_Author_Archive_Presentation extends Indexable_Presentation { |
| 14 |
|
| 15 |
use Archive_Adjacent; |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the post type helper instance. |
| 19 |
* |
| 20 |
* @var Post_Type_Helper |
| 21 |
*/ |
| 22 |
protected $post_type; |
| 23 |
|
| 24 |
/** |
| 25 |
* Holds the author archive helper instance. |
| 26 |
* |
| 27 |
* @var Author_Archive_Helper |
| 28 |
*/ |
| 29 |
protected $author_archive; |
| 30 |
|
| 31 |
/** |
| 32 |
* Indexable_Author_Archive_Presentation constructor. |
| 33 |
* |
| 34 |
* @codeCoverageIgnore |
| 35 |
* |
| 36 |
* @param Post_Type_Helper $post_type The post type helper. |
| 37 |
* @param Author_Archive_Helper $author_archive The author archive helper. |
| 38 |
*/ |
| 39 |
public function __construct( Post_Type_Helper $post_type, Author_Archive_Helper $author_archive ) { |
| 40 |
$this->post_type = $post_type; |
| 41 |
$this->author_archive = $author_archive; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Generates the canonical. |
| 46 |
* |
| 47 |
* @return string The canonical. |
| 48 |
*/ |
| 49 |
public function generate_canonical() { |
| 50 |
if ( $this->model->canonical ) { |
| 51 |
return $this->model->canonical; |
| 52 |
} |
| 53 |
|
| 54 |
$permalink = $this->get_permalink(); |
| 55 |
|
| 56 |
if ( ! $permalink ) { |
| 57 |
return ''; |
| 58 |
} |
| 59 |
|
| 60 |
$current_page = $this->pagination->get_current_archive_page_number(); |
| 61 |
if ( $current_page > 1 ) { |
| 62 |
return $this->pagination->get_paginated_url( $permalink, $current_page ); |
| 63 |
} |
| 64 |
|
| 65 |
return $permalink; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Generates the title. |
| 70 |
* |
| 71 |
* @return string The title. |
| 72 |
*/ |
| 73 |
public function generate_title() { |
| 74 |
if ( $this->model->title ) { |
| 75 |
return $this->model->title; |
| 76 |
} |
| 77 |
|
| 78 |
$option_titles_key = 'title-author-wpseo'; |
| 79 |
$title = $this->options->get( $option_titles_key ); |
| 80 |
if ( $title ) { |
| 81 |
return $title; |
| 82 |
} |
| 83 |
|
| 84 |
return $this->options->get_title_default( $option_titles_key ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Generates the meta description. |
| 89 |
* |
| 90 |
* @return string The meta description. |
| 91 |
*/ |
| 92 |
public function generate_meta_description() { |
| 93 |
if ( $this->model->description ) { |
| 94 |
return $this->model->description; |
| 95 |
} |
| 96 |
|
| 97 |
$option_titles_key = 'metadesc-author-wpseo'; |
| 98 |
$description = $this->options->get( $option_titles_key ); |
| 99 |
if ( $description ) { |
| 100 |
return $description; |
| 101 |
} |
| 102 |
|
| 103 |
return $this->options->get_title_default( $option_titles_key ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Generates the robots value. |
| 108 |
* |
| 109 |
* @return array The robots value. |
| 110 |
*/ |
| 111 |
public function generate_robots() { |
| 112 |
$robots = $this->get_base_robots(); |
| 113 |
|
| 114 |
// Global option: "Show author archives in search results". |
| 115 |
if ( $this->options->get( 'noindex-author-wpseo', false ) ) { |
| 116 |
$robots['index'] = 'noindex'; |
| 117 |
return $this->filter_robots( $robots ); |
| 118 |
} |
| 119 |
|
| 120 |
$current_author = \get_userdata( $this->model->object_id ); |
| 121 |
|
| 122 |
// Safety check. The call to `get_user_data` could return false (called in `get_queried_object`). |
| 123 |
if ( $current_author === false ) { |
| 124 |
$robots['index'] = 'noindex'; |
| 125 |
return $this->filter_robots( $robots ); |
| 126 |
} |
| 127 |
|
| 128 |
$author_archive_post_types = $this->author_archive->get_author_archive_post_types(); |
| 129 |
|
| 130 |
// Global option: "Show archives for authors without posts in search results". |
| 131 |
if ( $this->options->get( 'noindex-author-noposts-wpseo', false ) && $this->user->count_posts( $current_author->ID, $author_archive_post_types ) === 0 ) { |
| 132 |
$robots['index'] = 'noindex'; |
| 133 |
return $this->filter_robots( $robots ); |
| 134 |
} |
| 135 |
|
| 136 |
// User option: "Do not allow search engines to show this author's archives in search results". |
| 137 |
if ( $this->user->get_meta( $current_author->ID, 'wpseo_noindex_author', true ) === 'on' ) { |
| 138 |
$robots['index'] = 'noindex'; |
| 139 |
return $this->filter_robots( $robots ); |
| 140 |
} |
| 141 |
|
| 142 |
return $this->filter_robots( $robots ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Generates the Open Graph type. |
| 147 |
* |
| 148 |
* @return string The Open Graph type. |
| 149 |
*/ |
| 150 |
public function generate_open_graph_type() { |
| 151 |
return 'profile'; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Generates the open graph images. |
| 156 |
* |
| 157 |
* @return array The open graph images. |
| 158 |
*/ |
| 159 |
public function generate_open_graph_images() { |
| 160 |
if ( $this->context->open_graph_enabled === false ) { |
| 161 |
return []; |
| 162 |
} |
| 163 |
|
| 164 |
return $this->open_graph_image_generator->generate_for_author_archive( $this->context ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Generates the source. |
| 169 |
* |
| 170 |
* @return array The source. |
| 171 |
*/ |
| 172 |
public function generate_source() { |
| 173 |
return [ 'post_author' => $this->model->object_id ]; |
| 174 |
} |
| 175 |
} |
| 176 |
|