/wp-includes/class-wp-term.php

https://bitbucket.org/skyarch-iijima/wordpress · PHP · 245 lines · 83 code · 32 blank · 130 comment · 14 complexity · 4faafea3dbb8b264dc1f98d359f5a9d6 MD5 · raw file

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