• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/presentations/indexable-post-type-presentation.php
1
<?php
2

3
namespace Yoast\WP\SEO\Presentations;
4

5
use Yoast\WP\SEO\Helpers\Date_Helper;
6
use Yoast\WP\SEO\Helpers\Pagination_Helper;
7
use Yoast\WP\SEO\Helpers\Post_Helper;
8
use Yoast\WP\SEO\Helpers\Post_Type_Helper;
9

10
/**
11
 * Class Indexable_Post_Type_Presentation.
12
 *
13
 * Presentation object for indexables.
14
 */
15
class Indexable_Post_Type_Presentation extends Indexable_Presentation {
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 Date_Helper instance.
26
         *
27
         * @var Date_Helper
28
         */
29
        protected $date;
30

31
        /**
32
         * Holds the Pagination_Helper instance.
33
         *
34
         * @var Pagination_Helper
35
         */
36
        protected $pagination;
37

38
        /**
39
         * Holds the Post_Helper instance.
40
         *
41
         * @var Post_Helper
42
         */
43
        protected $post;
44

45
        /**
46
         * Indexable_Post_Type_Presentation constructor.
47
         *
48
         * @codeCoverageIgnore
49
         *
50
         * @param Post_Type_Helper  $post_type  The post type helper.
51
         * @param Date_Helper       $date       The date helper.
52
         * @param Pagination_Helper $pagination The pagination helper.
53
         * @param Post_Helper       $post       The post helper.
54
         */
55
        public function __construct(
56
                Post_Type_Helper $post_type,
57
                Date_Helper $date,
58
                Pagination_Helper $pagination,
59
                Post_Helper $post
60
        ) {
61
                $this->post_type  = $post_type;
62
                $this->date       = $date;
63
                $this->pagination = $pagination;
64
                $this->post       = $post;
65
        }
66

67
        /**
68
         * Generates the canonical.
69
         *
70
         * @return string The canonical.
71
         */
72
        public function generate_canonical() {
73
                if ( $this->model->canonical ) {
×
74
                        return $this->model->canonical;
×
75
                }
76

77
                $permalink = $this->permalink;
×
78

79
                // Fix paginated pages canonical, but only if the page is truly paginated.
80
                $current_page = $this->pagination->get_current_post_page_number();
×
81
                if ( $current_page > 1 ) {
×
82
                        $number_of_pages = $this->model->number_of_pages;
×
83
                        if ( $number_of_pages && $current_page <= $number_of_pages ) {
×
84
                                $permalink = $this->get_paginated_url( $permalink, $current_page );
×
85
                        }
86
                }
87

88
                return $this->url->ensure_absolute_url( $permalink );
×
89
        }
90

91
        /**
92
         * Generates the rel prev.
93
         *
94
         * @return string The rel prev value.
95
         */
96
        public function generate_rel_prev() {
97
                if ( $this->model->number_of_pages === null ) {
×
98
                        return '';
×
99
                }
100

101
                if ( $this->pagination->is_rel_adjacent_disabled() ) {
×
102
                        return '';
×
103
                }
104

105
                $current_page = \max( 1, $this->pagination->get_current_post_page_number() );
×
106

107
                // Check if there is a previous page.
108
                if ( $current_page < 2 ) {
×
109
                        return '';
×
110
                }
111

112
                // Check if the previous page is the first page.
113
                if ( $current_page === 2 ) {
×
114
                        return $this->model->permalink;
×
115
                }
116

117
                return $this->get_paginated_url( $this->model->permalink, ( $current_page - 1 ) );
×
118
        }
119

120
        /**
121
         * Generates the rel next.
122
         *
123
         * @return string The rel prev next.
124
         */
125
        public function generate_rel_next() {
126
                if ( $this->model->number_of_pages === null ) {
×
127
                        return '';
×
128
                }
129

130
                if ( $this->pagination->is_rel_adjacent_disabled() ) {
×
131
                        return '';
×
132
                }
133

134
                $current_page = \max( 1, $this->pagination->get_current_post_page_number() );
×
135
                if ( $this->model->number_of_pages <= $current_page ) {
×
136
                        return '';
×
137
                }
138

139
                return $this->get_paginated_url( $this->model->permalink, ( $current_page + 1 ) );
×
140
        }
141

142
        /**
143
         * Generates the open graph title.
144
         *
145
         * @return string The open graph title.
146
         */
147
        public function generate_title() {
148
                if ( $this->model->title ) {
×
149
                        return $this->model->title;
×
150
                }
151

152
                // Get SEO title as entered in Search appearance.
153
                $post_type = $this->model->object_sub_type;
×
154
                $title     = $this->options->get( 'title-' . $this->model->object_sub_type );
×
155
                if ( $title ) {
×
156
                        return $title;
×
157
                }
158

159
                // Get installation default title.
160
                return $this->options->get_title_default( 'title-' . $post_type );
×
161
        }
162

163
        /**
164
         * Generates the meta description.
165
         *
166
         * @return string The meta description.
167
         */
168
        public function generate_meta_description() {
169
                if ( $this->model->description ) {
×
170
                        return $this->model->description;
×
171
                }
172

173
                return $this->options->get( 'metadesc-' . $this->model->object_sub_type );
×
174
        }
175

176
        /**
177
         * Generates the open graph description.
178
         *
179
         * @return string The open graph description.
180
         */
181
        public function generate_open_graph_description() {
182
                if ( $this->model->open_graph_description ) {
×
183
                        $open_graph_description = $this->model->open_graph_description;
×
184
                }
185

186
                if ( empty( $open_graph_description ) ) {
×
187
                        // The helper applies a filter, but we don't have a default value at this stage so we pass an empty string.
188
                        $open_graph_description = $this->values_helper->get_open_graph_description( '', $this->model->object_type, $this->model->object_sub_type );
×
189
                }
190

191
                if ( empty( $open_graph_description ) ) {
×
192
                        $open_graph_description = $this->meta_description;
×
193
                }
194

195
                if ( empty( $open_graph_description ) ) {
×
196
                        $open_graph_description = $this->post->get_the_excerpt( $this->model->object_id );
×
197
                        $open_graph_description = $this->post->strip_shortcodes( $open_graph_description );
×
198
                }
199

200
                return $open_graph_description;
×
201
        }
202

203
        /**
204
         * Generates the open graph images.
205
         *
206
         * @return array The open graph images.
207
         */
208
        public function generate_open_graph_images() {
209
                if ( \post_password_required() ) {
×
210
                        return [];
×
211
                }
212

213
                return parent::generate_open_graph_images();
×
214
        }
215

216
        /**
217
         * Generates the Open Graph type.
218
         *
219
         * @return string The Open Graph type.
220
         */
221
        public function generate_open_graph_type() {
222
                return 'article';
×
223
        }
224

225
        /**
226
         * Generates the open graph article author.
227
         *
228
         * @return string The open graph article author.
229
         */
230
        public function generate_open_graph_article_author() {
231
                if ( $this->model->object_sub_type !== 'post' ) {
×
232
                        return '';
×
233
                }
234

235
                $post = $this->source;
×
236

237
                $open_graph_article_author = $this->user->get_the_author_meta( 'facebook', $post->post_author );
×
238

239
                if ( $open_graph_article_author ) {
×
240
                        return $open_graph_article_author;
×
241
                }
242

243
                return '';
×
244
        }
245

246
        /**
247
         * Generates the open graph article publisher.
248
         *
249
         * @return string The open graph article publisher.
250
         */
251
        public function generate_open_graph_article_publisher() {
252
                $open_graph_article_publisher = $this->context->open_graph_publisher;
×
253

254
                if ( $open_graph_article_publisher ) {
×
255
                        return $open_graph_article_publisher;
×
256
                }
257

258
                return '';
×
259
        }
260

261
        /**
262
         * Generates the open graph article published time.
263
         *
264
         * @return string The open graph article published time.
265
         */
266
        public function generate_open_graph_article_published_time() {
267
                if ( $this->model->object_sub_type !== 'post' ) {
×
268
                        /**
269
                         * Filter: 'wpseo_opengraph_show_publish_date' - Allow showing publication date for other post types.
270
                         *
271
                         * @param string $post_type The current URL's post type.
272
                         *
273
                         * @api bool Whether or not to show publish date.
274
                         */
275
                        if ( ! \apply_filters( 'wpseo_opengraph_show_publish_date', false, $this->post->get_post_type( $this->source ) ) ) {
×
276
                                return '';
×
277
                        }
278
                }
279

280
                return $this->date->format( $this->source->post_date_gmt );
×
281
        }
282

283
        /**
284
         * Generates the open graph article modified time.
285
         *
286
         * @return string The open graph article modified time.
287
         */
288
        public function generate_open_graph_article_modified_time() {
289
                if ( $this->source->post_modified_gmt !== $this->source->post_date_gmt ) {
×
290
                        return $this->date->format( $this->source->post_modified_gmt );
×
291
                }
292

293
                return '';
×
294
        }
295

296
        /**
297
         * Generates the source.
298
         *
299
         * @return array The source.
300
         */
301
        public function generate_source() {
302
                return \get_post( $this->model->object_id );
×
303
        }
304

305
        /**
306
         * Generates the robots value.
307
         *
308
         * @return array The robots value.
309
         */
310
        public function generate_robots() {
311
                $robots = $this->get_base_robots();
×
312
                $robots = \array_merge(
×
313
                        $robots,
×
314
                        [
×
315
                                'imageindex' => ( $this->model->is_robots_noimageindex === true ) ? 'noimageindex' : null,
×
316
                                'archive'    => ( $this->model->is_robots_noarchive === true ) ? 'noarchive' : null,
×
317
                                'snippet'    => ( $this->model->is_robots_nosnippet === true ) ? 'nosnippet' : null,
×
318
                        ]
×
319
                );
×
320

321
                // No snippet means max snippet can be omitted.
322
                if ( $this->model->is_robots_nosnippet === true ) {
×
323
                        $robots['max-snippet'] = null;
×
324
                }
325

326
                // No image index means max image preview can be omitted.
327
                if ( $this->model->is_robots_noimageindex === true ) {
×
328
                        $robots['max-image-preview'] = null;
×
329
                }
330

331
                // When the post specific index is not set, look to the post status and default of the post type.
332
                if ( $this->model->is_robots_noindex === null ) {
×
333
                        $post_status_private = \get_post_status( $this->model->object_id ) === 'private';
×
334
                        $post_type_noindex   = ! $this->post_type->is_indexable( $this->model->object_sub_type );
×
335

336
                        if ( $post_status_private || $post_type_noindex ) {
×
337
                                $robots['index'] = 'noindex';
×
338
                        }
339
                }
340

341
                return $this->filter_robots( $robots );
×
342
        }
343

344
        /**
345
         * Generates the Twitter description.
346
         *
347
         * @return string The Twitter description.
348
         */
349
        public function generate_twitter_description() {
350
                $twitter_description = parent::generate_twitter_description();
×
351

352
                if ( $twitter_description ) {
×
353
                        return $twitter_description;
×
354
                }
355

356
                if ( $this->open_graph_description && $this->context->open_graph_enabled === true ) {
×
357
                        return '';
×
358
                }
359

360
                return $this->post->get_the_excerpt( $this->model->object_id );
×
361
        }
362

363
        /**
364
         * Generates the Twitter image.
365
         *
366
         * @return string The Twitter image.
367
         */
368
        public function generate_twitter_image() {
369
                if ( \post_password_required() ) {
×
370
                        return '';
×
371
                }
372

373
                return parent::generate_twitter_image();
×
374
        }
375

376
        /**
377
         * Generates the Twitter creator.
378
         *
379
         * @return string The Twitter creator.
380
         */
381
        public function generate_twitter_creator() {
382
                if ( $this->model->object_sub_type !== 'post' ) {
×
383
                        return '';
×
384
                }
385

386
                $twitter_creator = \ltrim( \trim( \get_the_author_meta( 'twitter', $this->source->post_author ) ), '@' );
×
387

388
                /**
389
                 * Filter: 'wpseo_twitter_creator_account' - Allow changing the Twitter account as output in the Twitter card by Yoast SEO.
390
                 *
391
                 * @api string $twitter The twitter account name string.
392
                 */
393
                $twitter_creator = \apply_filters( 'wpseo_twitter_creator_account', $twitter_creator );
×
394

395
                if ( \is_string( $twitter_creator ) && $twitter_creator !== '' ) {
×
396
                        return '@' . $twitter_creator;
×
397
                }
398

399
                $site_twitter = $this->options->get( 'twitter_site', '' );
×
400
                if ( \is_string( $site_twitter ) && $site_twitter !== '' ) {
×
401
                        return '@' . $site_twitter;
×
402
                }
403

404
                return '';
×
405
        }
406

407
        /**
408
         * Wraps the get_paginated_url pagination helper method.
409
         *
410
         * @codeCoverageIgnore A wrapper method.
411
         *
412
         * @param string $url  The un-paginated URL of the current archive.
413
         * @param string $page The page number to add on to $url for the $link tag.
414
         *
415
         * @return string The paginated URL.
416
         */
417
        protected function get_paginated_url( $url, $page ) {
418
                return $this->pagination->get_paginated_url( $url, $page, false );
419
        }
420
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc