You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

256 line
5.3 KiB

  1. <?php
  2. /**
  3. * Taxonomy API: WP_Term class
  4. *
  5. * @package WordPress
  6. * @subpackage Taxonomy
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement the WP_Term object.
  11. *
  12. * @since 4.4.0
  13. */
  14. final class WP_Term {
  15. /**
  16. * Term ID.
  17. *
  18. * @since 4.4.0
  19. * @access public
  20. * @var int
  21. */
  22. public $term_id;
  23. /**
  24. * The term's name.
  25. *
  26. * @since 4.4.0
  27. * @access public
  28. * @var string
  29. */
  30. public $name = '';
  31. /**
  32. * The term's slug.
  33. *
  34. * @since 4.4.0
  35. * @access public
  36. * @var string
  37. */
  38. public $slug = '';
  39. /**
  40. * The term's term_group.
  41. *
  42. * @since 4.4.0
  43. * @access public
  44. * @var string
  45. */
  46. public $term_group = '';
  47. /**
  48. * Term Taxonomy ID.
  49. *
  50. * @since 4.4.0
  51. * @access public
  52. * @var int
  53. */
  54. public $term_taxonomy_id = 0;
  55. /**
  56. * The term's taxonomy name.
  57. *
  58. * @since 4.4.0
  59. * @access public
  60. * @var string
  61. */
  62. public $taxonomy = '';
  63. /**
  64. * The term's description.
  65. *
  66. * @since 4.4.0
  67. * @access public
  68. * @var string
  69. */
  70. public $description = '';
  71. /**
  72. * ID of a term's parent term.
  73. *
  74. * @since 4.4.0
  75. * @access public
  76. * @var int
  77. */
  78. public $parent = 0;
  79. /**
  80. * Cached object count for this term.
  81. *
  82. * @since 4.4.0
  83. * @access public
  84. * @var int
  85. */
  86. public $count = 0;
  87. /**
  88. * Stores the term object's sanitization level.
  89. *
  90. * Does not correspond to a database field.
  91. *
  92. * @since 4.4.0
  93. * @access public
  94. * @var string
  95. */
  96. public $filter = 'raw';
  97. /**
  98. * Retrieve WP_Term instance.
  99. *
  100. * @since 4.4.0
  101. * @access public
  102. * @static
  103. *
  104. * @global wpdb $wpdb WordPress database abstraction object.
  105. *
  106. * @param int $term_id Term ID.
  107. * @param string $taxonomy Optional. Limit matched terms to those matching `$taxonomy`. Only used for
  108. * disambiguating potentially shared terms.
  109. * @return WP_Term|WP_Error|false Term object, if found. WP_Error if `$term_id` is shared between taxonomies and
  110. * there's insufficient data to distinguish which term is intended.
  111. * False for other failures.
  112. */
  113. public static function get_instance( $term_id, $taxonomy = null ) {
  114. global $wpdb;
  115. $term_id = (int) $term_id;
  116. if ( ! $term_id ) {
  117. return false;
  118. }
  119. $_term = wp_cache_get( $term_id, 'terms' );
  120. // If there isn't a cached version, hit the database.
  121. if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) {
  122. // Grab all matching terms, in case any are shared between taxonomies.
  123. $terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d", $term_id ) );
  124. if ( ! $terms ) {
  125. return false;
  126. }
  127. // If a taxonomy was specified, find a match.
  128. if ( $taxonomy ) {
  129. foreach ( $terms as $match ) {
  130. if ( $taxonomy === $match->taxonomy ) {
  131. $_term = $match;
  132. break;
  133. }
  134. }
  135. // If only one match was found, it's the one we want.
  136. } elseif ( 1 === count( $terms ) ) {
  137. $_term = reset( $terms );
  138. // Otherwise, the term must be shared between taxonomies.
  139. } else {
  140. // If the term is shared only with invalid taxonomies, return the one valid term.
  141. foreach ( $terms as $t ) {
  142. if ( ! taxonomy_exists( $t->taxonomy ) ) {
  143. continue;
  144. }
  145. // Only hit if we've already identified a term in a valid taxonomy.
  146. if ( $_term ) {
  147. return new WP_Error( 'ambiguous_term_id', __( 'Term ID is shared between multiple taxonomies' ), $term_id );
  148. }
  149. $_term = $t;
  150. }
  151. }
  152. if ( ! $_term ) {
  153. return false;
  154. }
  155. // Don't return terms from invalid taxonomies.
  156. if ( ! taxonomy_exists( $_term->taxonomy ) ) {
  157. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
  158. }
  159. $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' );
  160. // Don't cache terms that are shared between taxonomies.
  161. if ( 1 === count( $terms ) ) {
  162. wp_cache_add( $term_id, $_term, 'terms' );
  163. }
  164. }
  165. $term_obj = new WP_Term( $_term );
  166. $term_obj->filter( $term_obj->filter );
  167. return $term_obj;
  168. }
  169. /**
  170. * Constructor.
  171. *
  172. * @since 4.4.0
  173. * @access public
  174. *
  175. * @param WP_Term|object $term Term object.
  176. */
  177. public function __construct( $term ) {
  178. foreach ( get_object_vars( $term ) as $key => $value ) {
  179. $this->$key = $value;
  180. }
  181. }
  182. /**
  183. * Sanitizes term fields, according to the filter type provided.
  184. *
  185. * @since 4.4.0
  186. * @access public
  187. *
  188. * @param string $filter Filter context. Accepts 'edit', 'db', 'display', 'attribute', 'js', 'raw'.
  189. */
  190. public function filter( $filter ) {
  191. sanitize_term( $this, $this->taxonomy, $filter );
  192. }
  193. /**
  194. * Converts an object to array.
  195. *
  196. * @since 4.4.0
  197. * @access public
  198. *
  199. * @return array Object as array.
  200. */
  201. public function to_array() {
  202. return get_object_vars( $this );
  203. }
  204. /**
  205. * Getter.
  206. *
  207. * @since 4.4.0
  208. * @access public
  209. *
  210. * @param string $key Property to get.
  211. * @return mixed Property value.
  212. */
  213. public function __get( $key ) {
  214. switch ( $key ) {
  215. case 'data' :
  216. $data = new stdClass();
  217. $columns = array( 'term_id', 'name', 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', 'parent', 'count' );
  218. foreach ( $columns as $column ) {
  219. $data->{$column} = isset( $this->{$column} ) ? $this->{$column} : null;
  220. }
  221. return sanitize_term( $data, $data->taxonomy, 'raw' );
  222. }
  223. }
  224. }