| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* A helper object for post types. |
| 7 |
*/ |
| 8 |
class Post_Type_Helper { |
| 9 |
|
| 10 |
/** |
| 11 |
* The options helper. |
| 12 |
* |
| 13 |
* @var Options_Helper |
| 14 |
*/ |
| 15 |
protected $options_helper; |
| 16 |
|
| 17 |
/** |
| 18 |
* Post_Type_Helper constructor. |
| 19 |
* |
| 20 |
* @param Options_Helper $options_helper The options helper. |
| 21 |
*/ |
| 22 |
public function __construct( Options_Helper $options_helper ) { |
| 23 |
$this->options_helper = $options_helper; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Checks if the request post type is public and indexable. |
| 28 |
* |
| 29 |
* @codeCoverageIgnore We have to write test when this method contains own code. |
| 30 |
* |
| 31 |
* @param string $post_type_name The name of the post type to lookup. |
| 32 |
* |
| 33 |
* @return bool True when post type is set to index. |
| 34 |
*/ |
| 35 |
public function is_indexable( $post_type_name ) { |
| 36 |
if ( $this->options_helper->get( 'disable-' . $post_type_name, false ) ) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
return ( $this->options_helper->get( 'noindex-' . $post_type_name, false ) === false ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns an array with the public post types. |
| 45 |
* |
| 46 |
* @codeCoverageIgnore It only wraps a WordPress function. |
| 47 |
* |
| 48 |
* @param string $output The output type to use. |
| 49 |
* |
| 50 |
* @return array Array with all the public post_types. |
| 51 |
*/ |
| 52 |
public function get_public_post_types( $output = 'names' ) { |
| 53 |
return \get_post_types( [ 'public' => true ], $output ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns an array with the accessible post types. |
| 58 |
* |
| 59 |
* An accessible post type is a post type that is public and isn't set as no-index (robots). |
| 60 |
* |
| 61 |
* @return array Array with all the accessible post_types. |
| 62 |
*/ |
| 63 |
public function get_accessible_post_types() { |
| 64 |
$post_types = \get_post_types( [ 'public' => true ] ); |
| 65 |
$post_types = \array_filter( $post_types, 'is_post_type_viewable' ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Filter: 'wpseo_accessible_post_types' - Allow changing the accessible post types. |
| 69 |
* |
| 70 |
* @api array $post_types The public post types. |
| 71 |
*/ |
| 72 |
$post_types = \apply_filters( 'wpseo_accessible_post_types', $post_types ); |
| 73 |
|
| 74 |
// When the array gets messed up somewhere. |
| 75 |
if ( ! \is_array( $post_types ) ) { |
| 76 |
return []; |
| 77 |
} |
| 78 |
|
| 79 |
return $post_types; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns an array of post types that are excluded from being indexed for the |
| 84 |
* indexables. |
| 85 |
* |
| 86 |
* @return array The excluded post types. |
| 87 |
*/ |
| 88 |
public function get_excluded_post_types_for_indexables() { |
| 89 |
/** |
| 90 |
* Filter: 'wpseo_indexable_excluded_post_types' - Allow developers to prevent posts of a certain post |
| 91 |
* type from being saved to the indexable table. |
| 92 |
* |
| 93 |
* @param array $excluded_post_types The currently excluded post types. |
| 94 |
*/ |
| 95 |
$excluded_post_types = \apply_filters( 'wpseo_indexable_excluded_post_types', [] ); |
| 96 |
|
| 97 |
// Failsafe, to always make sure that `excluded_post_types` is an array. |
| 98 |
if ( ! \is_array( $excluded_post_types ) ) { |
| 99 |
return []; |
| 100 |
} |
| 101 |
|
| 102 |
return $excluded_post_types; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Checks if the post type is excluded. |
| 107 |
* |
| 108 |
* @param string $post_type The post type to check. |
| 109 |
* |
| 110 |
* @return bool If the post type is exclude. |
| 111 |
*/ |
| 112 |
public function is_excluded( $post_type ) { |
| 113 |
return \in_array( $post_type, $this->get_excluded_post_types_for_indexables(), true ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Checks if the post type with the given name has an archive page. |
| 118 |
* |
| 119 |
* @param WP_Post_Type|string $post_type The name of the post type to check. |
| 120 |
* |
| 121 |
* @return bool True when the post type has an archive page. |
| 122 |
*/ |
| 123 |
public function has_archive( $post_type ) { |
| 124 |
if ( \is_string( $post_type ) ) { |
| 125 |
$post_type = \get_post_type_object( $post_type ); |
| 126 |
} |
| 127 |
|
| 128 |
return ( ! empty( $post_type->has_archive ) ); |
| 129 |
} |
| 130 |
} |
| 131 |
|