| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WP_Post_Type; |
| 6 |
|
| 7 |
/** |
| 8 |
* A helper object for post types. |
| 9 |
*/ |
| 10 |
class Post_Type_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* The options helper. |
| 14 |
* |
| 15 |
* @var Options_Helper |
| 16 |
*/ |
| 17 |
protected $options_helper; |
| 18 |
|
| 19 |
/** |
| 20 |
* Post_Type_Helper constructor. |
| 21 |
* |
| 22 |
* @param Options_Helper $options_helper The options helper. |
| 23 |
*/ |
| 24 |
public function __construct( Options_Helper $options_helper ) { |
| 25 |
$this->options_helper = $options_helper; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Checks if the request post type is public and indexable. |
| 30 |
* |
| 31 |
* @codeCoverageIgnore We have to write test when this method contains own code. |
| 32 |
* |
| 33 |
* @param string $post_type_name The name of the post type to lookup. |
| 34 |
* |
| 35 |
* @return bool True when post type is set to index. |
| 36 |
*/ |
| 37 |
public function is_indexable( $post_type_name ) { |
| 38 |
if ( $this->options_helper->get( 'disable-' . $post_type_name, false ) ) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
return ( $this->options_helper->get( 'noindex-' . $post_type_name, false ) === false ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Checks if the request post type has the Yoast Metabox enabled. |
| 47 |
* |
| 48 |
* @param string $post_type_name The name of the post type to lookup. |
| 49 |
* |
| 50 |
* @return bool True if metabox is enabled. |
| 51 |
*/ |
| 52 |
public function has_metabox( $post_type_name ) { |
| 53 |
return ( $this->options_helper->get( 'display-metabox-pt-' . $post_type_name, true ) === true ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns an array with the public post types. |
| 58 |
* |
| 59 |
* @codeCoverageIgnore It only wraps a WordPress function. |
| 60 |
* |
| 61 |
* @param string $output The output type to use. |
| 62 |
* |
| 63 |
* @return array Array with all the public post_types. |
| 64 |
*/ |
| 65 |
public function get_public_post_types( $output = 'names' ) { |
| 66 |
return \get_post_types( [ 'public' => true ], $output ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns an array with the accessible post types. |
| 71 |
* |
| 72 |
* An accessible post type is a post type that is public and isn't set as no-index (robots). |
| 73 |
* |
| 74 |
* @return array Array with all the accessible post_types. |
| 75 |
*/ |
| 76 |
public function get_accessible_post_types() { |
| 77 |
$post_types = \get_post_types( [ 'public' => true ] ); |
| 78 |
$post_types = \array_filter( $post_types, 'is_post_type_viewable' ); |
| 79 |
|
| 80 |
/** |
| 81 |
* Filter: 'wpseo_accessible_post_types' - Allow changing the accessible post types. |
| 82 |
* |
| 83 |
* @param array $post_types The public post types. |
| 84 |
*/ |
| 85 |
$post_types = \apply_filters( 'wpseo_accessible_post_types', $post_types ); |
| 86 |
|
| 87 |
// When the array gets messed up somewhere. |
| 88 |
if ( ! \is_array( $post_types ) ) { |
| 89 |
return []; |
| 90 |
} |
| 91 |
|
| 92 |
return $post_types; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns an array of post types that are excluded from being indexed for the |
| 97 |
* indexables. |
| 98 |
* |
| 99 |
* @return array The excluded post types. |
| 100 |
*/ |
| 101 |
public function get_excluded_post_types_for_indexables() { |
| 102 |
/** |
| 103 |
* Filter: 'wpseo_indexable_excluded_post_types' - Allows excluding posts of a certain post |
| 104 |
* type from being saved to the indexable table. |
| 105 |
* |
| 106 |
* @param array $excluded_post_types The currently excluded post types that indexables will not be created for. |
| 107 |
*/ |
| 108 |
$excluded_post_types = \apply_filters( 'wpseo_indexable_excluded_post_types', [] ); |
| 109 |
|
| 110 |
// Failsafe, to always make sure that `excluded_post_types` is an array. |
| 111 |
if ( ! \is_array( $excluded_post_types ) ) { |
| 112 |
return []; |
| 113 |
} |
| 114 |
|
| 115 |
return $excluded_post_types; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Checks if the post type is excluded. |
| 120 |
* |
| 121 |
* @param string $post_type The post type to check. |
| 122 |
* |
| 123 |
* @return bool If the post type is exclude. |
| 124 |
*/ |
| 125 |
public function is_excluded( $post_type ) { |
| 126 |
return \in_array( $post_type, $this->get_excluded_post_types_for_indexables(), true ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Checks if the post type with the given name has an archive page. |
| 131 |
* |
| 132 |
* @param WP_Post_Type|string $post_type The name of the post type to check. |
| 133 |
* |
| 134 |
* @return bool True when the post type has an archive page. |
| 135 |
*/ |
| 136 |
public function has_archive( $post_type ) { |
| 137 |
if ( \is_string( $post_type ) ) { |
| 138 |
$post_type = \get_post_type_object( $post_type ); |
| 139 |
} |
| 140 |
|
| 141 |
return ( ! empty( $post_type->has_archive ) ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns the post types that should be indexed. |
| 146 |
* |
| 147 |
* @return array The post types that should be indexed. |
| 148 |
*/ |
| 149 |
public function get_indexable_post_types() { |
| 150 |
$public_post_types = $this->get_public_post_types(); |
| 151 |
$excluded_post_types = $this->get_excluded_post_types_for_indexables(); |
| 152 |
|
| 153 |
$included_post_types = \array_diff( $public_post_types, $excluded_post_types ); |
| 154 |
|
| 155 |
return $this->filter_included_post_types( $included_post_types ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns all indexable post types with archive pages. |
| 160 |
* |
| 161 |
* @return array All post types which are indexable and have archive pages. |
| 162 |
*/ |
| 163 |
public function get_indexable_post_archives() { |
| 164 |
return \array_filter( $this->get_indexable_post_type_objects(), [ $this, 'has_archive' ] ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Filters the post types that are included to be indexed. |
| 169 |
* |
| 170 |
* @param array $included_post_types The post types that are included to be indexed. |
| 171 |
* |
| 172 |
* @return array The filtered post types that are included to be indexed. |
| 173 |
*/ |
| 174 |
protected function filter_included_post_types( $included_post_types ) { |
| 175 |
/** |
| 176 |
* Filter: 'wpseo_indexable_forced_included_post_types' - Allows force including posts of a certain post |
| 177 |
* type to be saved to the indexable table. |
| 178 |
* |
| 179 |
* @param array $included_post_types The currently included post types that indexables will be created for. |
| 180 |
*/ |
| 181 |
$filtered_included_post_types = \apply_filters( 'wpseo_indexable_forced_included_post_types', $included_post_types ); |
| 182 |
|
| 183 |
if ( ! \is_array( $filtered_included_post_types ) ) { |
| 184 |
// If the filter got misused, let's return the unfiltered array. |
| 185 |
return \array_values( $included_post_types ); |
| 186 |
} |
| 187 |
|
| 188 |
// Add sanity check to make sure everything is an actual post type. |
| 189 |
foreach ( $filtered_included_post_types as $key => $post_type ) { |
| 190 |
if ( ! \post_type_exists( $post_type ) ) { |
| 191 |
unset( $filtered_included_post_types[ $key ] ); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// `array_values`, to make sure that the keys are reset. |
| 196 |
return \array_values( $filtered_included_post_types ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Checks if the given post type should be indexed. |
| 201 |
* |
| 202 |
* @param string $post_type The post type that is checked. |
| 203 |
* |
| 204 |
* @return bool |
| 205 |
*/ |
| 206 |
public function is_of_indexable_post_type( $post_type ) { |
| 207 |
$public_types = $this->get_indexable_post_types(); |
| 208 |
if ( ! \in_array( $post_type, $public_types, true ) ) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Checks if the archive of a post type is indexable. |
| 217 |
* |
| 218 |
* @param string $post_type The post type to check. |
| 219 |
* |
| 220 |
* @return bool if the archive is indexable. |
| 221 |
*/ |
| 222 |
public function is_post_type_archive_indexable( $post_type ) { |
| 223 |
$public_type_objects = $this->get_indexable_post_archives(); |
| 224 |
$public_types = \array_map( |
| 225 |
static function ( $post_type_object ) { |
| 226 |
return $post_type_object->name; |
| 227 |
}, |
| 228 |
$public_type_objects, |
| 229 |
); |
| 230 |
|
| 231 |
return \in_array( $post_type, $public_types, true ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Returns an array of complete post type objects for all indexable post types. |
| 236 |
* |
| 237 |
* @return array List of indexable post type objects. |
| 238 |
*/ |
| 239 |
public function get_indexable_post_type_objects() { |
| 240 |
$post_type_objects = []; |
| 241 |
$indexable_post_types = $this->get_indexable_post_types(); |
| 242 |
foreach ( $indexable_post_types as $post_type ) { |
| 243 |
$post_type_object = \get_post_type_object( $post_type ); |
| 244 |
if ( ! empty( $post_type_object ) ) { |
| 245 |
$post_type_objects[ $post_type ] = $post_type_object; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
return $post_type_objects; |
| 250 |
} |
| 251 |
} |
| 252 |
|