| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Presentations; |
| 4 |
|
| 5 |
use WP_Term; |
| 6 |
use Yoast\WP\SEO\Helpers\Taxonomy_Helper; |
| 7 |
use Yoast\WP\SEO\Wrappers\WP_Query_Wrapper; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Indexable_Term_Archive_Presentation. |
| 11 |
* |
| 12 |
* Presentation object for indexables. |
| 13 |
* |
| 14 |
* @property WP_Term $source |
| 15 |
*/ |
| 16 |
class Indexable_Term_Archive_Presentation extends Indexable_Presentation { |
| 17 |
|
| 18 |
use Archive_Adjacent; |
| 19 |
|
| 20 |
/** |
| 21 |
* Holds the WP query wrapper instance. |
| 22 |
* |
| 23 |
* @var WP_Query_Wrapper |
| 24 |
*/ |
| 25 |
private $wp_query_wrapper; |
| 26 |
|
| 27 |
/** |
| 28 |
* Holds the taxonomy helper instance. |
| 29 |
* |
| 30 |
* @var Taxonomy_Helper |
| 31 |
*/ |
| 32 |
private $taxonomy; |
| 33 |
|
| 34 |
/** |
| 35 |
* Indexable_Post_Type_Presentation constructor. |
| 36 |
* |
| 37 |
* @codeCoverageIgnore |
| 38 |
* |
| 39 |
* @param WP_Query_Wrapper $wp_query_wrapper The wp query wrapper. |
| 40 |
* @param Taxonomy_Helper $taxonomy The Taxonomy helper. |
| 41 |
*/ |
| 42 |
public function __construct( |
| 43 |
WP_Query_Wrapper $wp_query_wrapper, |
| 44 |
Taxonomy_Helper $taxonomy |
| 45 |
) { |
| 46 |
$this->wp_query_wrapper = $wp_query_wrapper; |
| 47 |
$this->taxonomy = $taxonomy; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Generates the canonical. |
| 52 |
* |
| 53 |
* @return string The canonical. |
| 54 |
*/ |
| 55 |
public function generate_canonical() { |
| 56 |
if ( $this->is_multiple_terms_query() ) { |
| 57 |
return ''; |
| 58 |
} |
| 59 |
|
| 60 |
if ( $this->model->canonical ) { |
| 61 |
return $this->model->canonical; |
| 62 |
} |
| 63 |
|
| 64 |
$permalink = $this->get_permalink(); |
| 65 |
if ( ! $permalink ) { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
|
| 69 |
$current_page = $this->pagination->get_current_archive_page_number(); |
| 70 |
if ( $current_page > 1 ) { |
| 71 |
return $this->pagination->get_paginated_url( $permalink, $current_page ); |
| 72 |
} |
| 73 |
|
| 74 |
return $permalink; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Generates the meta description. |
| 79 |
* |
| 80 |
* @return string The meta description. |
| 81 |
*/ |
| 82 |
public function generate_meta_description() { |
| 83 |
if ( $this->model->description ) { |
| 84 |
return $this->model->description; |
| 85 |
} |
| 86 |
|
| 87 |
return $this->options->get( 'metadesc-tax-' . $this->model->object_sub_type ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Generates the source. |
| 92 |
* |
| 93 |
* @return array The source. |
| 94 |
*/ |
| 95 |
public function generate_source() { |
| 96 |
return \get_term( $this->model->object_id, $this->model->object_sub_type ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Generates the Open Graph description. |
| 101 |
* |
| 102 |
* @return string The Open Graph description. |
| 103 |
*/ |
| 104 |
public function generate_open_graph_description() { |
| 105 |
$open_graph_description = parent::generate_open_graph_description(); |
| 106 |
if ( $open_graph_description ) { |
| 107 |
return $open_graph_description; |
| 108 |
} |
| 109 |
|
| 110 |
return $this->taxonomy->get_term_description( $this->model->object_id ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Generates the Twitter description. |
| 115 |
* |
| 116 |
* @return string The Twitter description. |
| 117 |
*/ |
| 118 |
public function generate_twitter_description() { |
| 119 |
$twitter_description = parent::generate_twitter_description(); |
| 120 |
if ( $twitter_description ) { |
| 121 |
return $twitter_description; |
| 122 |
} |
| 123 |
|
| 124 |
if ( $this->open_graph_description && $this->context->open_graph_enabled === true ) { |
| 125 |
return ''; |
| 126 |
} |
| 127 |
|
| 128 |
return $this->taxonomy->get_term_description( $this->model->object_id ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Generates the robots value. |
| 133 |
* |
| 134 |
* @return array The robots value. |
| 135 |
*/ |
| 136 |
public function generate_robots() { |
| 137 |
$robots = $this->get_base_robots(); |
| 138 |
|
| 139 |
/** |
| 140 |
* If its a multiple terms archive page return a noindex. |
| 141 |
*/ |
| 142 |
if ( $this->current_page->is_multiple_terms_page() ) { |
| 143 |
$robots['index'] = 'noindex'; |
| 144 |
|
| 145 |
return $this->filter_robots( $robots ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* First we get the no index option for this taxonomy, because it can be overwritten the indexable value for |
| 150 |
* this specific term. |
| 151 |
*/ |
| 152 |
if ( ! $this->taxonomy->is_indexable( $this->source->taxonomy ) ) { |
| 153 |
$robots['index'] = 'noindex'; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Overwrite the index directive when there is a term specific directive set. |
| 158 |
*/ |
| 159 |
if ( $this->model->is_robots_noindex !== null ) { |
| 160 |
$robots['index'] = ( $this->model->is_robots_noindex ) ? 'noindex' : 'index'; |
| 161 |
} |
| 162 |
|
| 163 |
return $this->filter_robots( $robots ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Generates the title. |
| 168 |
* |
| 169 |
* @return string The title. |
| 170 |
*/ |
| 171 |
public function generate_title() { |
| 172 |
if ( $this->model->title ) { |
| 173 |
return $this->model->title; |
| 174 |
} |
| 175 |
|
| 176 |
// Get the SEO title as entered in Search Appearance. |
| 177 |
$title = $this->options->get( 'title-tax-' . $this->source->taxonomy ); |
| 178 |
if ( $title ) { |
| 179 |
return $title; |
| 180 |
} |
| 181 |
|
| 182 |
// Get the installation default title. |
| 183 |
$title = $this->options->get_title_default( 'title-tax-' . $this->source->taxonomy ); |
| 184 |
|
| 185 |
return $title; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Generates the Open Graph type. |
| 190 |
* |
| 191 |
* @return string The Open Graph type. |
| 192 |
*/ |
| 193 |
public function generate_open_graph_type() { |
| 194 |
return 'article'; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Checks if term archive query is for multiple terms (/term-1,term-2/ or /term-1+term-2/). |
| 199 |
* |
| 200 |
* @return bool Whether the query contains multiple terms. |
| 201 |
*/ |
| 202 |
protected function is_multiple_terms_query() { |
| 203 |
$query = $this->wp_query_wrapper->get_query(); |
| 204 |
|
| 205 |
if ( ! isset( $query->tax_query ) ) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
$queried_terms = $query->tax_query->queried_terms; |
| 210 |
|
| 211 |
if ( empty( $queried_terms[ $this->source->taxonomy ]['terms'] ) ) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
return \count( $queried_terms[ $this->source->taxonomy ]['terms'] ) > 1; |
| 216 |
} |
| 217 |
} |
| 218 |
|