| 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 |
|