PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
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 / helpers / permalink-helper.php

permalink-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at src/helpers/permalink-helper.php

61 lines 1.6 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\Helpers;
4
5 use WP_Error;
6 use Yoast\WP\SEO\Models\Indexable;
7
8 /**
9 * A helper object for permalinks.
10 */
11 class Permalink_Helper {
12
13 /**
14 * Retrieves the permalink for an indexable.
15 *
16 * @param Indexable $indexable The indexable.
17 *
18 * @return string|null The permalink.
19 */
20 public function get_permalink_for_indexable( $indexable ) {
21 switch ( true ) {
22 case $indexable->object_type === 'post':
23 return $this->get_permalink_for_post( $indexable->object_sub_type, $indexable->object_id );
24 case $indexable->object_type === 'home-page':
25 return \home_url( '/' );
26 case $indexable->object_type === 'term':
27 $term = \get_term( $indexable->object_id );
28
29 if ( $term === null || \is_wp_error( $term ) ) {
30 return null;
31 }
32
33 return \get_term_link( $term, $term->taxonomy );
34 case $indexable->object_type === 'system-page' && $indexable->object_sub_type === 'search-page':
35 return \get_search_link();
36 case $indexable->object_type === 'post-type-archive':
37 return \get_post_type_archive_link( $indexable->object_sub_type );
38 case $indexable->object_type === 'user':
39 return \get_author_posts_url( $indexable->object_id );
40 }
41
42 return null;
43 }
44
45 /**
46 * Retrieves the permalink for a post with the given post type and ID.
47 *
48 * @param string $post_type The post type.
49 * @param int $post_id The post ID.
50 *
51 * @return WP_Error|string|false The permalink.
52 */
53 public function get_permalink_for_post( $post_type, $post_id ) {
54 if ( $post_type !== 'attachment' ) {
55 return \get_permalink( $post_id );
56 }
57
58 return \wp_get_attachment_url( $post_id );
59 }
60 }
61