PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / presentations / indexable-term-archive-presentation.php

indexable-term-archive-presentation.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at src/presentations/indexable-term-archive-presentation.php

226 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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( WP_Query_Wrapper $wp_query_wrapper, Taxonomy_Helper $taxonomy ) {
43 $this->wp_query_wrapper = $wp_query_wrapper;
44 $this->taxonomy = $taxonomy;
45 }
46
47 /**
48 * Generates the canonical.
49 *
50 * @return string The canonical.
51 */
52 public function generate_canonical() {
53 if ( $this->is_multiple_terms_query() ) {
54 return '';
55 }
56
57 if ( $this->model->canonical ) {
58 return $this->model->canonical;
59 }
60
61 if ( ! $this->permalink ) {
62 return '';
63 }
64
65 $current_page = $this->pagination->get_current_archive_page_number();
66 if ( $current_page > 1 ) {
67 return $this->pagination->get_paginated_url( $this->permalink, $current_page );
68 }
69
70 return $this->permalink;
71 }
72
73 /**
74 * Generates the meta description.
75 *
76 * @return string The meta description.
77 */
78 public function generate_meta_description() {
79 if ( $this->model->description ) {
80 return $this->model->description;
81 }
82
83 return $this->options->get( 'metadesc-tax-' . $this->model->object_sub_type );
84 }
85
86 /**
87 * Generates the source.
88 *
89 * @return array The source.
90 */
91 public function generate_source() {
92 if ( ! empty( $this->model->object_id ) || \get_queried_object() === null ) {
93 return \get_term( $this->model->object_id, $this->model->object_sub_type );
94 }
95
96 return \get_term( \get_queried_object()->term_id, \get_queried_object()->taxonomy );
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 ( \is_wp_error( $this->source ) || ! $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 if ( \is_wp_error( $this->source ) ) {
177 return $this->model->title;
178 }
179
180 // Get the SEO title as entered in Search Appearance.
181 $title = $this->options->get( 'title-tax-' . $this->source->taxonomy );
182 if ( $title ) {
183 return $title;
184 }
185
186 // Get the installation default title.
187 $title = $this->options->get_title_default( 'title-tax-' . $this->source->taxonomy );
188
189 return $title;
190 }
191
192 /**
193 * Generates the Open Graph type.
194 *
195 * @return string The Open Graph type.
196 */
197 public function generate_open_graph_type() {
198 return 'article';
199 }
200
201 /**
202 * Checks if term archive query is for multiple terms (/term-1,term-2/ or /term-1+term-2/).
203 *
204 * @return bool Whether the query contains multiple terms.
205 */
206 protected function is_multiple_terms_query() {
207 $query = $this->wp_query_wrapper->get_query();
208
209 if ( ! isset( $query->tax_query ) ) {
210 return false;
211 }
212
213 if ( \is_wp_error( $this->source ) ) {
214 return false;
215 }
216
217 $queried_terms = $query->tax_query->queried_terms;
218
219 if ( empty( $queried_terms[ $this->source->taxonomy ]['terms'] ) ) {
220 return false;
221 }
222
223 return \count( $queried_terms[ $this->source->taxonomy ]['terms'] ) > 1;
224 }
225 }
226