| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
use WP_Taxonomy; |
| 6 |
use WP_Term; |
| 7 |
use WPSEO_Taxonomy_Meta; |
| 8 |
|
| 9 |
/** |
| 10 |
* A helper object for terms. |
| 11 |
*/ |
| 12 |
class Taxonomy_Helper { |
| 13 |
|
| 14 |
/** |
| 15 |
* The options helper. |
| 16 |
* |
| 17 |
* @var Options_Helper |
| 18 |
*/ |
| 19 |
private $options; |
| 20 |
|
| 21 |
/** |
| 22 |
* The string helper. |
| 23 |
* |
| 24 |
* @var String_Helper |
| 25 |
*/ |
| 26 |
private $string; |
| 27 |
|
| 28 |
/** |
| 29 |
* Taxonomy_Helper constructor. |
| 30 |
* |
| 31 |
* @codeCoverageIgnore It only sets dependencies. |
| 32 |
* |
| 33 |
* @param Options_Helper $options The options helper. |
| 34 |
* @param String_Helper $string_helper The string helper. |
| 35 |
*/ |
| 36 |
public function __construct( Options_Helper $options, String_Helper $string_helper ) { |
| 37 |
$this->options = $options; |
| 38 |
$this->string = $string_helper; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Checks if the requested term is indexable. |
| 43 |
* |
| 44 |
* @param string $taxonomy The taxonomy slug. |
| 45 |
* |
| 46 |
* @return bool True when taxonomy is set to index. |
| 47 |
*/ |
| 48 |
public function is_indexable( $taxonomy ) { |
| 49 |
return ! $this->options->get( 'noindex-tax-' . $taxonomy, false ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns an array with the public taxonomies. |
| 54 |
* |
| 55 |
* @param string $output The output type to use. |
| 56 |
* |
| 57 |
* @return string[]|WP_Taxonomy[] Array with all the public taxonomies. |
| 58 |
* The type depends on the specified output variable. |
| 59 |
*/ |
| 60 |
public function get_public_taxonomies( $output = 'names' ) { |
| 61 |
return \get_taxonomies( [ 'public' => true ], $output ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Retrieves the term description (without tags). |
| 66 |
* |
| 67 |
* @param int $term_id Term ID. |
| 68 |
* |
| 69 |
* @return string Term description (without tags). |
| 70 |
*/ |
| 71 |
public function get_term_description( $term_id ) { |
| 72 |
return $this->string->strip_all_tags( \term_description( $term_id ) ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Retrieves the taxonomy term's meta values. |
| 77 |
* |
| 78 |
* @codeCoverageIgnore We have to write test when this method contains own code. |
| 79 |
* |
| 80 |
* @param WP_Term $term Term to get the meta value for. |
| 81 |
* |
| 82 |
* @return array|bool Array of all the meta data for the term. |
| 83 |
* False if the term does not exist or the $meta provided is invalid. |
| 84 |
*/ |
| 85 |
public function get_term_meta( $term ) { |
| 86 |
return WPSEO_Taxonomy_Meta::get_term_meta( $term, $term->taxonomy, null ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Gets the passed taxonomy's slug. |
| 91 |
* |
| 92 |
* @param string $taxonomy The name of the taxonomy. |
| 93 |
* |
| 94 |
* @return string The slug for the taxonomy. Returns the taxonomy's name if no slug could be found. |
| 95 |
*/ |
| 96 |
public function get_taxonomy_slug( $taxonomy ) { |
| 97 |
$taxonomy_object = \get_taxonomy( $taxonomy ); |
| 98 |
|
| 99 |
if ( $taxonomy_object && \property_exists( $taxonomy_object, 'rewrite' ) && \is_array( $taxonomy_object->rewrite ) && isset( $taxonomy_object->rewrite['slug'] ) ) { |
| 100 |
return $taxonomy_object->rewrite['slug']; |
| 101 |
} |
| 102 |
|
| 103 |
return \strtolower( $taxonomy_object->name ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns an array with the custom taxonomies. |
| 108 |
* |
| 109 |
* @param string $output The output type to use. |
| 110 |
* |
| 111 |
* @return string[]|WP_Taxonomy[] Array with all the custom taxonomies. |
| 112 |
* The type depends on the specified output variable. |
| 113 |
*/ |
| 114 |
public function get_custom_taxonomies( $output = 'names' ) { |
| 115 |
return \get_taxonomies( [ '_builtin' => false ], $output ); |
| 116 |
} |
| 117 |
} |
| 118 |
|