| 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 |
/** |
| 119 |
* Returns an array of taxonomies that are excluded from being indexed for the |
| 120 |
* indexables. |
| 121 |
* |
| 122 |
* @return array The excluded taxonomies. |
| 123 |
*/ |
| 124 |
public function get_excluded_taxonomies_for_indexables() { |
| 125 |
/** |
| 126 |
* Filter: 'wpseo_indexable_excluded_taxonomies' - Allow developers to prevent a certain taxonomy |
| 127 |
* from being saved to the indexable table. |
| 128 |
* |
| 129 |
* @param array $excluded_taxonomies The currently excluded taxonomies. |
| 130 |
*/ |
| 131 |
$excluded_taxonomies = \apply_filters( 'wpseo_indexable_excluded_taxonomies', [] ); |
| 132 |
|
| 133 |
// Failsafe, to always make sure that `excluded_taxonomies` is an array. |
| 134 |
if ( ! \is_array( $excluded_taxonomies ) ) { |
| 135 |
return []; |
| 136 |
} |
| 137 |
|
| 138 |
return $excluded_taxonomies; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Checks if the taxonomy is excluded. |
| 143 |
* |
| 144 |
* @param string $taxonomy The taxonomy to check. |
| 145 |
* |
| 146 |
* @return bool If the taxonomy is excluded. |
| 147 |
*/ |
| 148 |
public function is_excluded( $taxonomy ) { |
| 149 |
return \in_array( $taxonomy, $this->get_excluded_taxonomies_for_indexables(), true ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* This builds a list of indexable taxonomies. |
| 154 |
* |
| 155 |
* @return array The indexable taxonomies. |
| 156 |
*/ |
| 157 |
public function get_indexable_taxonomies() { |
| 158 |
$public_taxonomies = $this->get_public_taxonomies(); |
| 159 |
$excluded_taxonomies = $this->get_excluded_taxonomies_for_indexables(); |
| 160 |
|
| 161 |
// `array_values`, to make sure that the keys are reset. |
| 162 |
return \array_values( \array_diff( $public_taxonomies, $excluded_taxonomies ) ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Returns an array of complete taxonomy objects for all indexable taxonomies. |
| 167 |
* |
| 168 |
* @return array List of indexable indexables objects. |
| 169 |
*/ |
| 170 |
public function get_indexable_taxonomy_objects() { |
| 171 |
$taxonomy_objects = []; |
| 172 |
$indexable_taxonomies = $this->get_indexable_taxonomies(); |
| 173 |
foreach ( $indexable_taxonomies as $taxonomy ) { |
| 174 |
$taxonomy_object = \get_taxonomy( $taxonomy ); |
| 175 |
if ( ! empty( $taxonomy_object ) ) { |
| 176 |
$taxonomy_objects[ $taxonomy ] = $taxonomy_object; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return $taxonomy_objects; |
| 181 |
} |
| 182 |
} |
| 183 |
|