| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use stdClass; |
| 6 |
|
| 7 |
/** |
| 8 |
* A helper object for primary terms. |
| 9 |
*/ |
| 10 |
class Primary_Term_Helper { |
| 11 |
|
| 12 |
/** |
| 13 |
* Generate the primary term taxonomies. |
| 14 |
* |
| 15 |
* @param int $post_id ID of the post. |
| 16 |
* |
| 17 |
* @return array The taxonomies. |
| 18 |
*/ |
| 19 |
public function get_primary_term_taxonomies( $post_id ) { |
| 20 |
$post_type = \get_post_type( $post_id ); |
| 21 |
$all_taxonomies = \get_object_taxonomies( $post_type, 'objects' ); |
| 22 |
$all_taxonomies = \array_filter( $all_taxonomies, [ $this, 'filter_hierarchical_taxonomies' ] ); |
| 23 |
|
| 24 |
/** |
| 25 |
* Filters which taxonomies for which the user can choose the primary term. |
| 26 |
* |
| 27 |
* @api array $taxonomies An array of taxonomy objects that are primary_term enabled. |
| 28 |
* |
| 29 |
* @param string $post_type The post type for which to filter the taxonomies. |
| 30 |
* @param array $all_taxonomies All taxonomies for this post types, even ones that don't have primary term |
| 31 |
* enabled. |
| 32 |
*/ |
| 33 |
$taxonomies = (array) \apply_filters( 'wpseo_primary_term_taxonomies', $all_taxonomies, $post_type, $all_taxonomies ); |
| 34 |
|
| 35 |
return $taxonomies; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Returns whether or not a taxonomy is hierarchical. |
| 40 |
* |
| 41 |
* @param stdClass $taxonomy Taxonomy object. |
| 42 |
* |
| 43 |
* @return bool True for hierarchical taxonomy. |
| 44 |
*/ |
| 45 |
protected function filter_hierarchical_taxonomies( $taxonomy ) { |
| 46 |
return (bool) $taxonomy->hierarchical; |
| 47 |
} |
| 48 |
} |
| 49 |
|