PageRenderTime 1047ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 1ms

/forum/bb-includes/backpress/class.wp-taxonomy.php

http://cartonbank.googlecode.com/
PHP | 1835 lines | 845 code | 230 blank | 760 comment | 298 complexity | eede6f385f091a8638d98e95cb8fb9fa MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1, AGPL-1.0, LGPL-3.0
  1. <?php
  2. // Last sync [WP11537]
  3. /**
  4. * Taxonomy API
  5. *
  6. * @package WordPress
  7. * @subpackage Taxonomy
  8. * @since 2.3.0
  9. */
  10. /**
  11. * WordPress Taxonomy based off of WordPress revision 8782.
  12. *
  13. * @since 2.3.0
  14. */
  15. class WP_Taxonomy {
  16. /**
  17. * Stores the database.
  18. *
  19. * @var unknown_type
  20. */
  21. var $db;
  22. var $taxonomies = array();
  23. function WP_Taxonomy( &$db ) {
  24. $this->__construct( $db );
  25. register_shutdown_function( array(&$this, '__destruct') );
  26. }
  27. /**
  28. * PHP5 constructor - Assigns the database to an attribute of the class.
  29. *
  30. * @param unknown_type $db
  31. */
  32. function __construct( &$db ) {
  33. $this->db =& $db;
  34. }
  35. /**
  36. * Does nothing.
  37. *
  38. * @package BackPress
  39. * @subpackage Taxonomy
  40. */
  41. function __destruct() {
  42. }
  43. /**
  44. * Return all of the taxonomy names that are of $object_type.
  45. *
  46. * It appears that this function can be used to find all of the names inside of
  47. * $this->taxonomies variable.
  48. *
  49. * <code><?php $taxonomies = $this->get_object_taxonomies('post'); ?></code> Should
  50. * result in <code>Array('category', 'post_tag')</code>
  51. *
  52. * @package WordPress
  53. * @subpackage Taxonomy
  54. * @since 2.3.0
  55. *
  56. * @uses $this->taxonomies
  57. *
  58. * @param array|string|object $object_type Name of the type of taxonomy object, or an object (row from posts)
  59. * @return array The names of all taxonomy of $object_type.
  60. */
  61. function get_object_taxonomies($object_type) {
  62. $object_type = (array) $object_type;
  63. // WP DIFF
  64. $taxonomies = array();
  65. foreach ( (array) $this->taxonomies as $taxonomy ) {
  66. if ( array_intersect($object_type, (array) $taxonomy->object_type) )
  67. $taxonomies[] = $taxonomy->name;
  68. }
  69. return $taxonomies;
  70. }
  71. /**
  72. * Retrieves the taxonomy object of $taxonomy.
  73. *
  74. * The get_taxonomy function will first check that the parameter string given
  75. * is a taxonomy object and if it is, it will return it.
  76. *
  77. * @package WordPress
  78. * @subpackage Taxonomy
  79. * @since 2.3.0
  80. *
  81. * @uses $this->taxonomies
  82. * @uses $this->is_taxonomy() Checks whether taxonomy exists
  83. *
  84. * @param string $taxonomy Name of taxonomy object to return
  85. * @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist
  86. */
  87. function get_taxonomy( $taxonomy ) {
  88. if ( !$this->is_taxonomy($taxonomy) )
  89. return false;
  90. return $this->taxonomies[$taxonomy];
  91. }
  92. /**
  93. * Checks that the taxonomy name exists.
  94. *
  95. * @package WordPress
  96. * @subpackage Taxonomy
  97. * @since 2.3.0
  98. *
  99. * @uses $this->taxonomies
  100. *
  101. * @param string $taxonomy Name of taxonomy object
  102. * @return bool Whether the taxonomy exists or not.
  103. */
  104. function is_taxonomy( $taxonomy ) {
  105. return isset($this->taxonomies[$taxonomy]);
  106. }
  107. /**
  108. * Whether the taxonomy object is hierarchical.
  109. *
  110. * Checks to make sure that the taxonomy is an object first. Then Gets the
  111. * object, and finally returns the hierarchical value in the object.
  112. *
  113. * A false return value might also mean that the taxonomy does not exist.
  114. *
  115. * @package WordPress
  116. * @subpackage Taxonomy
  117. * @since 2.3.0
  118. *
  119. * @uses $this->is_taxonomy() Checks whether taxonomy exists
  120. * @uses $this->get_taxonomy() Used to get the taxonomy object
  121. *
  122. * @param string $taxonomy Name of taxonomy object
  123. * @return bool Whether the taxonomy is hierarchical
  124. */
  125. function is_taxonomy_hierarchical($taxonomy) {
  126. if ( !$this->is_taxonomy($taxonomy) )
  127. return false;
  128. $taxonomy = $this->get_taxonomy($taxonomy);
  129. return $taxonomy->hierarchical;
  130. }
  131. /**
  132. * Create or modify a taxonomy object. Do not use before init.
  133. *
  134. * A simple function for creating or modifying a taxonomy object based on the
  135. * parameters given. The function will accept an array (third optional
  136. * parameter), along with strings for the taxonomy name and another string for
  137. * the object type.
  138. *
  139. * The function keeps a default set, allowing for the $args to be optional but
  140. * allow the other functions to still work. It is possible to overwrite the
  141. * default set, which contains two keys: hierarchical and update_count_callback.
  142. *
  143. * Nothing is returned, so expect error maybe or use is_taxonomy() to check
  144. * whether taxonomy exists.
  145. *
  146. * Optional $args contents:
  147. *
  148. * hierarachical - has some defined purpose at other parts of the API and is a
  149. * boolean value.
  150. *
  151. * update_count_callback - works much like a hook, in that it will be called
  152. * when the count is updated.
  153. *
  154. * @package WordPress
  155. * @subpackage Taxonomy
  156. * @since 2.3.0
  157. * @uses $this->taxonomies Inserts new taxonomy object into the list
  158. *
  159. * @param string $taxonomy Name of taxonomy object
  160. * @param string $object_type Name of the object type for the taxonomy object.
  161. * @param array|string $args See above description for the two keys values.
  162. */
  163. function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
  164. $defaults = array('hierarchical' => false, 'update_count_callback' => '');
  165. $args = wp_parse_args($args, $defaults);
  166. $args['name'] = $taxonomy;
  167. $args['object_type'] = $object_type;
  168. $this->taxonomies[$taxonomy] = (object) $args;
  169. }
  170. //
  171. // Term API
  172. //
  173. /**
  174. * Retrieve object_ids of valid taxonomy and term.
  175. *
  176. * The strings of $taxonomies must exist before this function will continue. On
  177. * failure of finding a valid taxonomy, it will return an WP_Error class, kind
  178. * of like Exceptions in PHP 5, except you can't catch them. Even so, you can
  179. * still test for the WP_Error class and get the error message.
  180. *
  181. * The $terms aren't checked the same as $taxonomies, but still need to exist
  182. * for $object_ids to be returned.
  183. *
  184. * It is possible to change the order that object_ids is returned by either
  185. * using PHP sort family functions or using the database by using $args with
  186. * either ASC or DESC array. The value should be in the key named 'order'.
  187. *
  188. * @package WordPress
  189. * @subpackage Taxonomy
  190. * @since 2.3.0
  191. *
  192. * @uses wp_parse_args() Creates an array from string $args.
  193. *
  194. * @param string|array $terms String of term or array of string values of terms that will be used
  195. * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names
  196. * @param array|string $args Change the order of the object_ids, either ASC or DESC
  197. * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success
  198. * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
  199. */
  200. function get_objects_in_term( $terms, $taxonomies, $args = null ) {
  201. if ( !is_array($terms) )
  202. $terms = array($terms);
  203. if ( !is_array($taxonomies) )
  204. $taxonomies = array($taxonomies);
  205. foreach ( (array) $taxonomies as $taxonomy ) {
  206. if ( !$this->is_taxonomy($taxonomy) )
  207. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  208. }
  209. $defaults = array('order' => 'ASC', 'field' => 'term_id');
  210. $args = wp_parse_args( $args, $defaults );
  211. extract($args, EXTR_SKIP);
  212. if ( 'tt_id' == $field )
  213. $field = 'tt.term_taxonomy_id';
  214. else
  215. $field = 'tt.term_id';
  216. $order = ( 'desc' == strtolower($order) ) ? 'DESC' : 'ASC';
  217. $terms = array_map('intval', $terms);
  218. $taxonomies = "'" . implode("', '", $taxonomies) . "'";
  219. $terms = "'" . implode("', '", $terms) . "'";
  220. $object_ids = $this->db->get_col("SELECT tr.object_id FROM {$this->db->term_relationships} AS tr INNER JOIN {$this->db->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND $field IN ($terms) ORDER BY tr.object_id $order");
  221. if ( ! $object_ids )
  222. return array();
  223. return $object_ids;
  224. }
  225. /**
  226. * Get all Term data from database by Term ID.
  227. *
  228. * The usage of the get_term function is to apply filters to a term object. It
  229. * is possible to get a term object from the database before applying the
  230. * filters.
  231. *
  232. * $term ID must be part of $taxonomy, to get from the database. Failure, might
  233. * be able to be captured by the hooks. Failure would be the same value as $this->db
  234. * returns for the get_row method.
  235. *
  236. * There are two hooks, one is specifically for each term, named 'get_term', and
  237. * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
  238. * term object, and the taxonomy name as parameters. Both hooks are expected to
  239. * return a Term object.
  240. *
  241. * 'get_term' hook - Takes two parameters the term Object and the taxonomy name.
  242. * Must return term object. Used in get_term() as a catch-all filter for every
  243. * $term.
  244. *
  245. * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy
  246. * name. Must return term object. $taxonomy will be the taxonomy name, so for
  247. * example, if 'category', it would be 'get_category' as the filter name. Useful
  248. * for custom taxonomies or plugging into default taxonomies.
  249. *
  250. * @package WordPress
  251. * @subpackage Taxonomy
  252. * @since 2.3.0
  253. *
  254. * @uses $this->sanitize_term() Cleanses the term based on $filter context before returning.
  255. * @see $this->sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  256. *
  257. * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
  258. * @param string $taxonomy Taxonomy name that $term is part of.
  259. * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
  260. * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
  261. * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
  262. * exist then WP_Error will be returned.
  263. */
  264. function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
  265. if ( empty($term) ) {
  266. $error = new WP_Error('invalid_term', __('Empty Term'));
  267. return $error;
  268. }
  269. if ( !$this->is_taxonomy($taxonomy) ) {
  270. $error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  271. return $error;
  272. }
  273. if ( is_object($term) ) {
  274. wp_cache_add($term->term_id, $term, $taxonomy);
  275. $_term = $term;
  276. } else {
  277. $term = (int) $term;
  278. if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
  279. $_term = $this->db->get_row( $this->db->prepare( "SELECT t.*, tt.* FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %s LIMIT 1", $taxonomy, $term) );
  280. wp_cache_add($term, $_term, $taxonomy);
  281. }
  282. }
  283. $_term = apply_filters('get_term', $_term, $taxonomy);
  284. $_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
  285. $_term = $this->sanitize_term($_term, $taxonomy, $filter);
  286. backpress_convert_object( $_term, $output );
  287. return $_term;
  288. }
  289. /**
  290. * Get all Term data from database by Term field and data.
  291. *
  292. * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
  293. * required.
  294. *
  295. * The default $field is 'id', therefore it is possible to also use null for
  296. * field, but not recommended that you do so.
  297. *
  298. * If $value does not exist, the return value will be false. If $taxonomy exists
  299. * and $field and $value combinations exist, the Term will be returned.
  300. *
  301. * @package WordPress
  302. * @subpackage Taxonomy
  303. * @since 2.3.0
  304. *
  305. * @uses $this->sanitize_term() Cleanses the term based on $filter context before returning.
  306. * @see $this->sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  307. *
  308. * @param string $field Either 'slug', 'name', 'id', or 'tt_id'
  309. * @param string|int $value Search for this term value
  310. * @param string $taxonomy Taxonomy Name
  311. * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
  312. * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
  313. * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
  314. */
  315. function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
  316. if ( !$this->is_taxonomy($taxonomy) )
  317. return false;
  318. if ( 'slug' == $field ) {
  319. $field = 't.slug';
  320. $value = $this->sanitize_term_slug($value, $taxonomy);
  321. if ( empty($value) )
  322. return false;
  323. } else if ( 'name' == $field ) {
  324. // Assume already escaped
  325. $field = 't.name';
  326. } else if ( 'tt_id' == $field ) {
  327. $field = 'tt.term_taxonomy_id';
  328. $value = (int) $value;
  329. } else {
  330. $field = 't.term_id';
  331. $value = (int) $value;
  332. }
  333. $term = $this->db->get_row( $this->db->prepare( "SELECT t.*, tt.* FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
  334. if ( !$term )
  335. return false;
  336. wp_cache_add($term->term_id, $term, $taxonomy);
  337. $term = $this->sanitize_term($term, $taxonomy, $filter);
  338. backpress_convert_object( $term, $output );
  339. return $term;
  340. }
  341. /**
  342. * Merge all term children into a single array of their IDs.
  343. *
  344. * This recursive function will merge all of the children of $term into the same
  345. * array of term IDs. Only useful for taxonomies which are hierarchical.
  346. *
  347. * Will return an empty array if $term does not exist in $taxonomy.
  348. *
  349. * @package WordPress
  350. * @subpackage Taxonomy
  351. * @since 2.3.0
  352. *
  353. * @uses $this->_get_term_hierarchy()
  354. * @uses $this->get_term_children() Used to get the children of both $taxonomy and the parent $term
  355. *
  356. * @param string $term ID of Term to get children
  357. * @param string $taxonomy Taxonomy Name
  358. * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
  359. */
  360. function get_term_children( $term_id, $taxonomy ) {
  361. if ( !$this->is_taxonomy($taxonomy) )
  362. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  363. $term_id = intval( $term_id );
  364. $terms = $this->_get_term_hierarchy($taxonomy);
  365. if ( ! isset($terms[$term_id]) )
  366. return array();
  367. $children = $terms[$term_id];
  368. foreach ( (array) $terms[$term_id] as $child ) {
  369. if ( isset($terms[$child]) )
  370. $children = array_merge($children, $this->get_term_children($child, $taxonomy));
  371. }
  372. return $children;
  373. }
  374. /**
  375. * Get sanitized Term field.
  376. *
  377. * Does checks for $term, based on the $taxonomy. The function is for contextual
  378. * reasons and for simplicity of usage. See sanitize_term_field() for more
  379. * information.
  380. *
  381. * @package WordPress
  382. * @subpackage Taxonomy
  383. * @since 2.3.0
  384. *
  385. * @uses $this->sanitize_term_field() Passes the return value in sanitize_term_field on success.
  386. *
  387. * @param string $field Term field to fetch
  388. * @param int $term Term ID
  389. * @param string $taxonomy Taxonomy Name
  390. * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
  391. * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
  392. */
  393. function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
  394. $term = (int) $term;
  395. $term = $this->get_term( $term, $taxonomy );
  396. if ( is_wp_error($term) )
  397. return $term;
  398. if ( !is_object($term) )
  399. return '';
  400. if ( !isset($term->$field) )
  401. return '';
  402. return $this->sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
  403. }
  404. /**
  405. * Sanitizes Term for editing.
  406. *
  407. * Return value is sanitize_term() and usage is for sanitizing the term for
  408. * editing. Function is for contextual and simplicity.
  409. *
  410. * @package WordPress
  411. * @subpackage Taxonomy
  412. * @since 2.3.0
  413. *
  414. * @uses $this->sanitize_term() Passes the return value on success
  415. *
  416. * @param int|object $id Term ID or Object
  417. * @param string $taxonomy Taxonomy Name
  418. * @return mixed|null|WP_Error Will return empty string if $term is not an object.
  419. */
  420. function get_term_to_edit( $id, $taxonomy ) {
  421. $term = $this->get_term( $id, $taxonomy );
  422. if ( is_wp_error($term) )
  423. return $term;
  424. if ( !is_object($term) )
  425. return '';
  426. return $this->sanitize_term($term, $taxonomy, 'edit');
  427. }
  428. /**
  429. * Retrieve the terms in a given taxonomy or list of taxonomies.
  430. *
  431. * You can fully inject any customizations to the query before it is sent, as
  432. * well as control the output with a filter.
  433. *
  434. * The 'get_terms' filter will be called when the cache has the term and will
  435. * pass the found term along with the array of $taxonomies and array of $args.
  436. * This filter is also called before the array of terms is passed and will pass
  437. * the array of terms, along with the $taxonomies and $args.
  438. *
  439. * The 'list_terms_exclusions' filter passes the compiled exclusions along with
  440. * the $args.
  441. *
  442. * The 'get_terms_orderby' filter passes the ORDER BY clause for the query
  443. * along with the $args array.
  444. *
  445. * The 'get_terms_fields' filter passes the fields for the SELECT query
  446. * along with the $args array.
  447. *
  448. * The list of arguments that $args can contain, which will overwrite the defaults:
  449. *
  450. * orderby - Default is 'name'. Can be name, count, term_group, slug or nothing
  451. * (will use term_id), Passing a custom value other than these will cause it to
  452. * order based on the custom value.
  453. *
  454. * order - Default is ASC. Can use DESC.
  455. *
  456. * hide_empty - Default is true. Will not return empty terms, which means
  457. * terms whose count is 0 according to the given taxonomy.
  458. *
  459. * exclude - Default is an empty string. A comma- or space-delimited string
  460. * of term ids to exclude from the return array. If 'include' is non-empty,
  461. * 'exclude' is ignored.
  462. *
  463. * include - Default is an empty string. A comma- or space-delimited string
  464. * of term ids to include in the return array.
  465. *
  466. * number - The maximum number of terms to return. Default is empty.
  467. *
  468. * offset - The number by which to offset the terms query.
  469. *
  470. * fields - Default is 'all', which returns an array of term objects.
  471. * If 'fields' is 'ids' or 'names', returns an array of
  472. * integers or strings, respectively.
  473. *
  474. * slug - Returns terms whose "slug" matches this value. Default is empty string.
  475. *
  476. * hierarchical - Whether to include terms that have non-empty descendants
  477. * (even if 'hide_empty' is set to true).
  478. *
  479. * search - Returned terms' names will contain the value of 'search',
  480. * case-insensitive. Default is an empty string.
  481. *
  482. * name__like - Returned terms' names will begin with the value of 'name__like',
  483. * case-insensitive. Default is empty string.
  484. *
  485. * The argument 'pad_counts', if set to true will include the quantity of a term's
  486. * children in the quantity of each term's "count" object variable.
  487. *
  488. * The 'get' argument, if set to 'all' instead of its default empty string,
  489. * returns terms regardless of ancestry or whether the terms are empty.
  490. *
  491. * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default
  492. * is 0. If set to a non-zero value, all returned terms will be descendants
  493. * of that term according to the given taxonomy. Hence 'child_of' is set to 0
  494. * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies
  495. * make term ancestry ambiguous.
  496. *
  497. * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is
  498. * the empty string '', which has a different meaning from the integer 0.
  499. * If set to an integer value, all returned terms will have as an immediate
  500. * ancestor the term whose ID is specified by that integer according to the given taxonomy.
  501. * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent'
  502. * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
  503. *
  504. * @package WordPress
  505. * @subpackage Taxonomy
  506. * @since 2.3.0
  507. *
  508. * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.
  509. *
  510. * @param string|array Taxonomy name or list of Taxonomy names
  511. * @param string|array $args The values of what to search for when returning terms
  512. * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
  513. */
  514. function &get_terms($taxonomies, $args = '') {
  515. $empty_array = array();
  516. $single_taxonomy = false;
  517. if ( !is_array($taxonomies) ) {
  518. $single_taxonomy = true;
  519. $taxonomies = array($taxonomies);
  520. }
  521. foreach ( (array) $taxonomies as $taxonomy ) {
  522. if ( ! $this->is_taxonomy($taxonomy) ) {
  523. $error = & new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  524. return $error;
  525. }
  526. }
  527. $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
  528. $defaults = array('orderby' => 'name', 'order' => 'ASC',
  529. 'hide_empty' => true, 'exclude' => '', 'exclude_tree' => '', 'include' => '',
  530. 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
  531. 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
  532. 'pad_counts' => false, 'offset' => '', 'search' => '');
  533. $args = wp_parse_args( $args, $defaults );
  534. $args['number'] = absint( $args['number'] );
  535. $args['offset'] = absint( $args['offset'] );
  536. if ( !$single_taxonomy || !$this->is_taxonomy_hierarchical($taxonomies[0]) ||
  537. '' !== $args['parent'] ) {
  538. $args['child_of'] = 0;
  539. $args['hierarchical'] = false;
  540. $args['pad_counts'] = false;
  541. }
  542. if ( 'all' == $args['get'] ) {
  543. $args['child_of'] = 0;
  544. $args['hide_empty'] = 0;
  545. $args['hierarchical'] = false;
  546. $args['pad_counts'] = false;
  547. }
  548. extract($args, EXTR_SKIP);
  549. if ( $child_of ) {
  550. $hierarchy = $this->_get_term_hierarchy($taxonomies[0]);
  551. if ( !isset($hierarchy[$child_of]) )
  552. return $empty_array;
  553. }
  554. if ( $parent ) {
  555. $hierarchy = $this->_get_term_hierarchy($taxonomies[0]);
  556. if ( !isset($hierarchy[$parent]) )
  557. return $empty_array;
  558. }
  559. // $args can be whatever, only use the args defined in defaults to compute the key
  560. $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
  561. $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
  562. $last_changed = wp_cache_get('last_changed', 'terms');
  563. if ( !$last_changed ) {
  564. $last_changed = time();
  565. wp_cache_set('last_changed', $last_changed, 'terms');
  566. }
  567. $cache_key = "get_terms:$key:$last_changed";
  568. $cache = wp_cache_get( $cache_key, 'terms' );
  569. if ( false !== $cache ) {
  570. $cache = apply_filters('get_terms', $cache, $taxonomies, $args);
  571. return $cache;
  572. }
  573. $_orderby = strtolower($orderby);
  574. if ( 'count' == $_orderby )
  575. $orderby = 'tt.count';
  576. else if ( 'name' == $_orderby )
  577. $orderby = 't.name';
  578. else if ( 'slug' == $_orderby )
  579. $orderby = 't.slug';
  580. else if ( 'term_group' == $_orderby )
  581. $orderby = 't.term_group';
  582. elseif ( empty($_orderby) || 'id' == $_orderby )
  583. $orderby = 't.term_id';
  584. $orderby = apply_filters( 'get_terms_orderby', $orderby, $args );
  585. $where = '';
  586. $inclusions = '';
  587. if ( !empty($include) ) {
  588. $exclude = '';
  589. $exclude_tree = '';
  590. $interms = preg_split('/[\s,]+/',$include);
  591. if ( count($interms) ) {
  592. foreach ( (array) $interms as $interm ) {
  593. if (empty($inclusions))
  594. $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
  595. else
  596. $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
  597. }
  598. }
  599. }
  600. if ( !empty($inclusions) )
  601. $inclusions .= ')';
  602. $where .= $inclusions;
  603. $exclusions = '';
  604. if ( ! empty( $exclude_tree ) ) {
  605. $excluded_trunks = preg_split('/[\s,]+/',$exclude_tree);
  606. foreach( (array) $excluded_trunks as $extrunk ) {
  607. $excluded_children = (array) $this->get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids'));
  608. $excluded_children[] = $extrunk;
  609. foreach( (array) $excluded_children as $exterm ) {
  610. if ( empty($exclusions) )
  611. $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
  612. else
  613. $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
  614. }
  615. }
  616. }
  617. if ( !empty($exclude) ) {
  618. $exterms = preg_split('/[\s,]+/',$exclude);
  619. if ( count($exterms) ) {
  620. foreach ( (array) $exterms as $exterm ) {
  621. if ( empty($exclusions) )
  622. $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
  623. else
  624. $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
  625. }
  626. }
  627. }
  628. if ( !empty($exclusions) )
  629. $exclusions .= ')';
  630. $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
  631. $where .= $exclusions;
  632. if ( !empty($slug) ) {
  633. $slug = $this->sanitize_term_slug($slug);
  634. $where .= " AND t.slug = '$slug'";
  635. }
  636. if ( !empty($name__like) )
  637. $where .= " AND t.name LIKE '{$name__like}%'";
  638. if ( '' !== $parent ) {
  639. $parent = (int) $parent;
  640. $where .= " AND tt.parent = '$parent'";
  641. }
  642. if ( $hide_empty && !$hierarchical )
  643. $where .= ' AND tt.count > 0';
  644. // don't limit the query results when we have to descend the family tree
  645. if ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {
  646. if( $offset )
  647. $limit = 'LIMIT ' . $offset . ',' . $number;
  648. else
  649. $limit = 'LIMIT ' . $number;
  650. } else
  651. $limit = '';
  652. if ( !empty($search) ) {
  653. $search = like_escape($search);
  654. $where .= " AND (t.name LIKE '%$search%')";
  655. }
  656. if ( !in_array( $fields, array( 'all', 'ids', 'names', 'tt_ids' ) ) )
  657. $fields = 'all';
  658. $selects = array();
  659. if ( 'all' == $fields )
  660. $selects = array('t.*', 'tt.*');
  661. else if ( 'ids' == $fields )
  662. $selects = array('t.term_id', 'tt.parent', 'tt.count');
  663. else if ( 'names' == $fields )
  664. $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
  665. $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));
  666. $query = "SELECT $select_this FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $limit";
  667. $terms = $this->db->get_results($query);
  668. if ( 'all' == $fields ) {
  669. $this->update_term_cache($terms);
  670. }
  671. if ( empty($terms) ) {
  672. wp_cache_add( $cache_key, array(), 'terms' );
  673. $terms = apply_filters('get_terms', array(), $taxonomies, $args);
  674. return $terms;
  675. }
  676. if ( $child_of || $hierarchical ) {
  677. $children = $this->_get_term_hierarchy($taxonomies[0]);
  678. if ( ! empty($children) )
  679. $terms = & $this->_get_term_children($child_of, $terms, $taxonomies[0]);
  680. }
  681. // Update term counts to include children.
  682. if ( $pad_counts )
  683. $this->_pad_term_counts($terms, $taxonomies[0]);
  684. // Make sure we show empty categories that have children.
  685. if ( $hierarchical && $hide_empty && is_array($terms) ) {
  686. foreach ( $terms as $k => $term ) {
  687. if ( ! $term->count ) {
  688. $children = $this->_get_term_children($term->term_id, $terms, $taxonomies[0]);
  689. if( is_array($children) )
  690. foreach ( $children as $child )
  691. if ( $child->count )
  692. continue 2;
  693. // It really is empty
  694. unset($terms[$k]);
  695. }
  696. }
  697. }
  698. reset ( $terms );
  699. $_terms = array();
  700. if ( 'ids' == $fields ) {
  701. while ( $term = array_shift($terms) )
  702. $_terms[] = $term->term_id;
  703. $terms = $_terms;
  704. } elseif ( 'names' == $fields ) {
  705. while ( $term = array_shift($terms) )
  706. $_terms[] = $term->name;
  707. $terms = $_terms;
  708. }
  709. if ( 0 < $number && intval(@count($terms)) > $number ) {
  710. $terms = array_slice($terms, $offset, $number);
  711. }
  712. wp_cache_add( $cache_key, $terms, 'terms' );
  713. $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
  714. return $terms;
  715. }
  716. /**
  717. * Check if Term exists.
  718. *
  719. * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
  720. *
  721. * @package WordPress
  722. * @subpackage Taxonomy
  723. * @since 2.3.0
  724. *
  725. * @param int|string $term The term to check
  726. * @param string $taxonomy The taxonomy name to use
  727. * @param int $parent ID of parent term under which to confine the exists search.
  728. * @return mixed Get the term id or Term Object, if exists.
  729. */
  730. function is_term($term, $taxonomy = '', $parent = 0) {
  731. $select = "SELECT term_id FROM {$this->db->terms} as t WHERE ";
  732. $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE ";
  733. if ( is_int($term) ) {
  734. if ( 0 == $term )
  735. return 0;
  736. $where = 't.term_id = %d';
  737. if ( !empty($taxonomy) )
  738. return $this->db->get_row( $this->db->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
  739. else
  740. return $this->db->get_var( $this->db->prepare( $select . $where, $term ) );
  741. }
  742. $term = trim( stripslashes( $term ) );
  743. if ( '' === $slug = $this->sanitize_term_slug($term) )
  744. return 0;
  745. $where = 't.slug = %s';
  746. $else_where = 't.name = %s';
  747. $where_fields = array($slug);
  748. $else_where_fields = array($term);
  749. if ( !empty($taxonomy) ) {
  750. $parent = (int) $parent;
  751. if ( $parent > 0 ) {
  752. $where_fields[] = $parent;
  753. $else_where_fields[] = $parent;
  754. $where .= ' AND tt.parent = %d';
  755. $else_where .= ' AND tt.parent = %d';
  756. }
  757. $where_fields[] = $taxonomy;
  758. $else_where_fields[] = $taxonomy;
  759. if ( $result = $this->db->get_row( $this->db->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $where_fields), ARRAY_A) )
  760. return $result;
  761. return $this->db->get_row( $this->db->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $else_where_fields), ARRAY_A);
  762. }
  763. if ( $result = $this->db->get_var( $this->db->prepare("SELECT term_id FROM {$this->db->terms} as t WHERE $where", $where_fields) ) )
  764. return $result;
  765. return $this->db->get_var( $this->db->prepare("SELECT term_id FROM {$this->db->terms} as t WHERE $else_where", $else_where_fields) );
  766. }
  767. function sanitize_term_slug( $title, $taxonomy = '', $term_id = 0 ) {
  768. return apply_filters( 'pre_term_slug', $title, $taxonomy, $term_id );
  769. }
  770. function format_to_edit( $text ) {
  771. return format_to_edit( $text );
  772. }
  773. /**
  774. * Sanitize Term all fields
  775. *
  776. * Relies on sanitize_term_field() to sanitize the term. The difference
  777. * is that this function will sanitize <strong>all</strong> fields. The
  778. * context is based on sanitize_term_field().
  779. *
  780. * The $term is expected to be either an array or an object.
  781. *
  782. * @package WordPress
  783. * @subpackage Taxonomy
  784. * @since 2.3.0
  785. *
  786. * @uses $this->sanitize_term_field Used to sanitize all fields in a term
  787. *
  788. * @param array|object $term The term to check
  789. * @param string $taxonomy The taxonomy name to use
  790. * @param string $context Default is 'display'.
  791. * @return array|object Term with all fields sanitized
  792. */
  793. function sanitize_term($term, $taxonomy, $context = 'display') {
  794. if ( 'raw' == $context )
  795. return $term;
  796. $fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');
  797. $do_object = false;
  798. if ( is_object($term) )
  799. $do_object = true;
  800. $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
  801. foreach ( (array) $fields as $field ) {
  802. if ( $do_object ) {
  803. if ( isset($term->$field) )
  804. $term->$field = $this->sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
  805. } else {
  806. if ( isset($term[$field]) )
  807. $term[$field] = $this->sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
  808. }
  809. }
  810. if ( $do_object )
  811. $term->filter = $context;
  812. else
  813. $term['filter'] = $context;
  814. return $term;
  815. }
  816. /**
  817. * Cleanse the field value in the term based on the context.
  818. *
  819. * Passing a term field value through the function should be assumed to have
  820. * cleansed the value for whatever context the term field is going to be used.
  821. *
  822. * If no context or an unsupported context is given, then default filters will
  823. * be applied.
  824. *
  825. * There are enough filters for each context to support a custom filtering
  826. * without creating your own filter function. Simply create a function that
  827. * hooks into the filter you need.
  828. *
  829. * @package WordPress
  830. * @subpackage Taxonomy
  831. * @since 2.3.0
  832. *
  833. * @param string $field Term field to sanitize
  834. * @param string $value Search for this term value
  835. * @param int $term_id Term ID
  836. * @param string $taxonomy Taxonomy Name
  837. * @param string $context Either edit, db, display, attribute, or js.
  838. * @return mixed sanitized field
  839. */
  840. function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
  841. if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {
  842. $value = (int) $value;
  843. if ( $value < 0 )
  844. $value = 0;
  845. }
  846. if ( 'raw' == $context )
  847. return $value;
  848. if ( 'edit' == $context ) {
  849. $value = apply_filters("edit_term_$field", $value, $term_id, $taxonomy);
  850. $value = apply_filters("edit_${taxonomy}_$field", $value, $term_id);
  851. if ( 'description' == $field )
  852. $value = $this->format_to_edit($value);
  853. else
  854. $value = esc_attr($value);
  855. } else if ( 'db' == $context ) {
  856. $value = apply_filters("pre_term_$field", $value, $taxonomy);
  857. $value = apply_filters("pre_${taxonomy}_$field", $value);
  858. // WP DIFF
  859. } else if ( 'rss' == $context ) {
  860. $value = apply_filters("term_${field}_rss", $value, $taxonomy);
  861. $value = apply_filters("${taxonomy}_${field}_rss", $value);
  862. } else {
  863. // Use display filters by default.
  864. $value = apply_filters("term_$field", $value, $term_id, $taxonomy, $context);
  865. $value = apply_filters("${taxonomy}_$field", $value, $term_id, $context);
  866. }
  867. if ( 'attribute' == $context )
  868. $value = esc_attr($value);
  869. else if ( 'js' == $context )
  870. $value = esc_js($value);
  871. return $value;
  872. }
  873. /**
  874. * Count how many terms are in Taxonomy.
  875. *
  876. * Default $args is 'ignore_empty' which can be <code>'ignore_empty=true'</code>
  877. * or <code>array('ignore_empty' => true);</code>.
  878. *
  879. * @package WordPress
  880. * @subpackage Taxonomy
  881. * @since 2.3.0
  882. *
  883. * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.
  884. *
  885. * @param string $taxonomy Taxonomy name
  886. * @param array|string $args Overwrite defaults
  887. * @return int How many terms are in $taxonomy
  888. */
  889. function count_terms( $taxonomy, $args = array() ) {
  890. $defaults = array('ignore_empty' => false);
  891. $args = wp_parse_args($args, $defaults);
  892. extract($args, EXTR_SKIP);
  893. $where = '';
  894. if ( $ignore_empty )
  895. $where = 'AND count > 0';
  896. return $this->db->get_var( $this->db->prepare( "SELECT COUNT(*) FROM {$this->db->term_taxonomy} WHERE taxonomy = %s $where", $taxonomy ) );
  897. }
  898. /**
  899. * Will unlink the term from the taxonomy.
  900. *
  901. * Will remove the term's relationship to the taxonomy, not the term or taxonomy
  902. * itself. The term and taxonomy will still exist. Will require the term's
  903. * object ID to perform the operation.
  904. *
  905. * @package WordPress
  906. * @subpackage Taxonomy
  907. * @since 2.3.0
  908. *
  909. * @param int $object_id The term Object Id that refers to the term
  910. * @param string|array $taxonomy List of Taxonomy Names or single Taxonomy name.
  911. */
  912. function delete_object_term_relationships( $object_id, $taxonomies ) {
  913. $object_id = (int) $object_id;
  914. if ( !is_array($taxonomies) )
  915. $taxonomies = array($taxonomies);
  916. foreach ( (array) $taxonomies as $taxonomy ) {
  917. $terms = $this->get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
  918. $in_terms = "'" . implode("', '", $terms) . "'";
  919. $this->db->query( $this->db->prepare( "DELETE FROM {$this->db->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ($in_terms)", $object_id ) );
  920. $this->update_term_count($terms, $taxonomy);
  921. }
  922. }
  923. /**
  924. * Removes a term from the database.
  925. *
  926. * If the term is a parent of other terms, then the children will be updated to
  927. * that term's parent.
  928. *
  929. * The $args 'default' will only override the terms found, if there is only one
  930. * term found. Any other and the found terms are used.
  931. *
  932. * The $args 'force_default' will force the term supplied as default to be
  933. * assigned even if the object was not going to be termless
  934. * @package WordPress
  935. * @subpackage Taxonomy
  936. * @since 2.3.0
  937. *
  938. * @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action
  939. * hooks, passing term object, term id. 'delete_term' gets an additional
  940. * parameter with the $taxonomy parameter.
  941. *
  942. * @param int $term Term ID
  943. * @param string $taxonomy Taxonomy Name
  944. * @param array|string $args Optional. Change 'default' term id and override found term ids.
  945. * @return bool|WP_Error Returns false if not term; true if completes delete action.
  946. */
  947. function delete_term( $term, $taxonomy, $args = array() ) {
  948. $term = (int) $term;
  949. if ( ! $ids = $this->is_term($term, $taxonomy) )
  950. return false;
  951. if ( is_wp_error( $ids ) )
  952. return $ids;
  953. $tt_id = $ids['term_taxonomy_id'];
  954. $defaults = array();
  955. $args = wp_parse_args($args, $defaults);
  956. extract($args, EXTR_SKIP);
  957. if ( isset($default) ) {
  958. $default = (int) $default;
  959. if ( !$this->is_term($default, $taxonomy) )
  960. unset($default);
  961. }
  962. // Update children to point to new parent
  963. if ( $this->is_taxonomy_hierarchical($taxonomy) ) {
  964. $term_obj = $this->get_term($term, $taxonomy);
  965. if ( is_wp_error( $term_obj ) )
  966. return $term_obj;
  967. $parent = $term_obj->parent;
  968. $this->db->update( $this->db->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id ) + compact( 'taxonomy' ) );
  969. }
  970. $objects = $this->db->get_col( $this->db->prepare( "SELECT object_id FROM {$this->db->term_relationships} WHERE term_taxonomy_id = %d", $tt_id ) );
  971. foreach ( (array) $objects as $object ) {
  972. $terms = $this->get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
  973. if ( 1 == count($terms) && isset($default) ) {
  974. $terms = array($default);
  975. } else {
  976. $terms = array_diff($terms, array($term));
  977. if (isset($default) && isset($force_default) && $force_default)
  978. $terms = array_merge($terms, array($default));
  979. }
  980. $terms = array_map('intval', $terms);
  981. $this->set_object_terms($object, $terms, $taxonomy);
  982. }
  983. $this->db->query( $this->db->prepare( "DELETE FROM {$this->db->term_taxonomy} WHERE term_taxonomy_id = %d", $tt_id ) );
  984. // Delete the term if no taxonomies use it.
  985. if ( !$this->db->get_var( $this->db->prepare( "SELECT COUNT(*) FROM {$this->db->term_taxonomy} WHERE term_id = %d", $term) ) )
  986. $this->db->query( $this->db->prepare( "DELETE FROM {$this->db->terms} WHERE term_id = %d", $term) );
  987. $this->clean_term_cache($term, $taxonomy);
  988. do_action('delete_term', $term, $tt_id, $taxonomy);
  989. do_action("delete_$taxonomy", $term, $tt_id);
  990. return true;
  991. }
  992. /**
  993. * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
  994. *
  995. * The following information has to do the $args parameter and for what can be
  996. * contained in the string or array of that parameter, if it exists.
  997. *
  998. * The first argument is called, 'orderby' and has the default value of 'name'.
  999. * The other value that is supported is 'count'.
  1000. *
  1001. * The second argument is called, 'order' and has the default value of 'ASC'.
  1002. * The only other value that will be acceptable is 'DESC'.
  1003. *
  1004. * The final argument supported is called, 'fields' and has the default value of
  1005. * 'all'. There are multiple other options that can be used instead. Supported
  1006. * values are as follows: 'all', 'ids', 'names', and finally
  1007. * 'all_with_object_id'.
  1008. *
  1009. * The fields argument also decides what will be returned. If 'all' or
  1010. * 'all_with_object_id' is choosen or the default kept intact, then all matching
  1011. * terms objects will be returned. If either 'ids' or 'names' is used, then an
  1012. * array of all matching term ids or term names will be returned respectively.
  1013. *
  1014. * @package WordPress
  1015. * @subpackage Taxonomy
  1016. * @since 2.3.0
  1017. *
  1018. * @param int|array $object_id The id of the object(s) to retrieve.
  1019. * @param string|array $taxonomies The taxonomies to retrieve terms from.
  1020. * @param array|string $args Change what is returned
  1021. * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.
  1022. */
  1023. function get_object_terms($object_ids, $taxonomies, $args = array()) {
  1024. if ( !is_array($taxonomies) )
  1025. $taxonomies = array($taxonomies);
  1026. foreach ( (array) $taxonomies as $taxonomy ) {
  1027. if ( !$this->is_taxonomy($taxonomy) )
  1028. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  1029. }
  1030. if ( !is_array($object_ids) )
  1031. $object_ids = array($object_ids);
  1032. $object_ids = array_map('intval', $object_ids);
  1033. $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
  1034. $args = wp_parse_args( $args, $defaults );
  1035. $terms = array();
  1036. if ( count($taxonomies) > 1 ) {
  1037. foreach ( $taxonomies as $index => $taxonomy ) {
  1038. $t = $this->get_taxonomy($taxonomy);
  1039. if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
  1040. unset($taxonomies[$index]);
  1041. $terms = array_merge($terms, $this->get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
  1042. }
  1043. }
  1044. } else {
  1045. $t = $this->get_taxonomy($taxonomies[0]);
  1046. if ( isset($t->args) && is_array($t->args) )
  1047. $args = array_merge($args, $t->args);
  1048. }
  1049. extract($args, EXTR_SKIP);
  1050. if ( 'count' == $orderby )
  1051. $orderby = 'tt.count';
  1052. else if ( 'name' == $orderby )
  1053. $orderby = 't.name';
  1054. else if ( 'slug' == $orderby )
  1055. $orderby = 't.slug';
  1056. else if ( 'term_group' == $orderby )
  1057. $orderby = 't.term_group';
  1058. else if ( 'term_order' == $orderby )
  1059. $orderby = 'tr.term_order';
  1060. else if ( 'none' == $orderby ) {
  1061. $orderby = '';
  1062. $order = '';
  1063. } else {
  1064. $orderby = 't.term_id';
  1065. }
  1066. // tt_ids queries can only be none or tr.term_taxonomy_id
  1067. if ( ('tt_ids' == $fields) && !empty($orderby) )
  1068. $orderby = 'tr.term_taxonomy_id';
  1069. if ( !empty($orderby) )
  1070. $orderby = "ORDER BY $orderby";
  1071. $taxonomies = "'" . implode("', '", $taxonomies) . "'";
  1072. $object_ids = implode(', ', $object_ids);
  1073. $select_this = '';
  1074. if ( 'all' == $fields )
  1075. $select_this = 't.*, tt.*';
  1076. else if ( 'ids' == $fields )
  1077. $select_this = 't.term_id';
  1078. else if ( 'names' == $fields )
  1079. $select_this = 't.name';
  1080. else if ( 'all_with_object_id' == $fields )
  1081. $select_this = 't.*, tt.*, tr.object_id';
  1082. $query = "SELECT $select_this FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} AS tt ON tt.term_id = t.term_id INNER JOIN {$this->db->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
  1083. if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
  1084. $terms = array_merge($terms, $this->db->get_results($query));
  1085. $this->update_term_cache($terms);
  1086. } else if ( 'ids' == $fields || 'names' == $fields ) {
  1087. $terms = array_merge($terms, $this->db->get_col($query));
  1088. } else if ( 'tt_ids' == $fields ) {
  1089. $terms = $this->db->get_col("SELECT tr.term_taxonomy_id FROM {$this->db->term_relationships} AS tr INNER JOIN {$this->db->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");
  1090. }
  1091. if ( ! $terms )
  1092. $terms = array();
  1093. return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);
  1094. }
  1095. /**
  1096. * Adds a new term to the database. Optionally marks it as an alias of an existing term.
  1097. *
  1098. * Error handling is assigned for the nonexistance of the $taxonomy and $term
  1099. * parameters before inserting. If both the term id and taxonomy exist
  1100. * previously, then an array will be returned that contains the term id and the
  1101. * contents of what is returned. The keys of the array are 'term_id' and
  1102. * 'term_taxonomy_id' containing numeric values.
  1103. *
  1104. * It is assumed that the term does not yet exist or the above will apply. The
  1105. * term will be first added to the term table and then related to the taxonomy
  1106. * if everything is well. If everything is correct, then several actions will be
  1107. * run prior to a filter and then several actions will be run after the filter
  1108. * is run.
  1109. *
  1110. * The arguments decide how the term is handled based on the $args parameter.
  1111. * The following is a list of the available overrides and the defaults.
  1112. *
  1113. * 'alias_of'. There is no default, but if added, expected is the slug that the
  1114. * term will be an alias of. Expected to be a string.
  1115. *
  1116. * 'description'. There is no default. If exists, will be added to the database
  1117. * along with the term. Expected to be a string.
  1118. *
  1119. * 'parent'. Expected to be numeric and default is 0 (zero). Will assign value
  1120. * of 'parent' to the term.
  1121. *
  1122. * 'slug'. Expected to be a string. There is no default.
  1123. *
  1124. * If 'slug' argument exists then the slug will be checked to see if it is not
  1125. * a valid term. If that check succeeds (it is not a valid term), then it is
  1126. * added and the term id is given. If it fails, then a check is made to whether
  1127. * the taxonomy is hierarchical and the parent argument is not empty. If the
  1128. * second check succeeds, the term will be inserted and the term id will be
  1129. * given.
  1130. *
  1131. * @package WordPress
  1132. * @subpackage Taxonomy
  1133. * @since 2.3.0
  1134. *
  1135. * @uses do_action() Calls 'create_term' hook with the term id and taxonomy id as parameters.
  1136. * @uses do_action() Calls 'create_$taxonomy' hook with term id and taxonomy id as parameters.
  1137. * @uses apply_filters() Calls 'term_id_filter' hook with term id and taxonomy id as parameters.
  1138. * @uses do_action() Calls 'created_term' hook with the term id and taxonomy id as parameters.
  1139. * @uses do_action() Calls 'created_$taxonomy' hook with term id and taxonomy id as parameters.
  1140. *
  1141. * @param int|string $term The term to add or update.
  1142. * @param string $taxonomy The taxonomy to which to add the term
  1143. * @param array|string $args Change the values of the inserted term
  1144. * @return array|WP_Error The Term ID and Term Taxonomy ID
  1145. */
  1146. function insert_term( $term, $taxonomy, $args = array() ) {
  1147. if ( !$this->is_taxonomy($taxonomy) )
  1148. return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
  1149. if ( is_int($term) && 0 == $term )
  1150. return new WP_Error('invalid_term_id', __('Invalid term ID'));
  1151. if ( '' == trim($term) )
  1152. return new WP_Error('empty_term_name', __('A name is required for this term'));
  1153. $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
  1154. $args = wp_parse_args($args, $defaults);
  1155. $args['name'] = $term;
  1156. $args['taxonomy'] = $taxonomy;
  1157. $args = $this->sanitize_term($args, $taxonomy, 'db');
  1158. extract($args, EXTR_SKIP);
  1159. // expected_slashed ($name)
  1160. $name = stripslashes($name);
  1161. $description = stripslashes($description);
  1162. if ( empty($slug) )
  1163. $slug = $this->sanitize_term_slug($name, $taxonomy);
  1164. $term_group = 0;
  1165. if ( $alias_of ) {
  1166. $alias = $this->db->get_row( $this->db->prepare( "SELECT term_id, term_group FROM {$this->db->terms} WHERE slug = %s", $alias_of) );
  1167. if ( $alias->term_group ) {
  1168. // The alias we want is already in a group, so let's use that one.
  1169. $term_group = $alias->term_group;
  1170. } else {
  1171. // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
  1172. $term_group = $this->db->get_var("SELECT MAX(term_group) FROM {$this->db->terms}") + 1;
  1173. $this->db->query( $this->db->prepare( "UPDATE {$this->db->terms} SET term_group = %d WHERE term_id = %d", $term_group, $alias->term_id ) );
  1174. }
  1175. }
  1176. if ( ! $term_id = $this->is_term($slug) ) {
  1177. if ( false === $this->db->insert( $this->db->terms, compact( 'name', 'slug', 'term_group' ) ) )
  1178. return new WP_Error('db_insert_error', __('Could not insert term into the database'), $this->db->last_error);
  1179. $term_id = (int) $this->db->insert_id;
  1180. } else if ( $this->is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) {
  1181. // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
  1182. // by incorporating parent slugs.
  1183. $slug = $this->unique_term_slug($slug, (object) $args);
  1184. if ( false === $this->db->insert( $this->db->terms, compact( 'name', 'slug', 'term_group' ) ) )
  1185. return new WP_Error('db_insert_error', __('Could not insert term into the database'), $this->db->last_error);
  1186. $term_id = (int) $this->db->insert_id;
  1187. }
  1188. if ( empty($slug) ) {
  1189. $slug = $this->sanitize_term_slug($slug, $taxonomy, $term_id);
  1190. $this->db->update( $this->db->terms, compact( 'slug' ), compact( 'term_id' ) );
  1191. }
  1192. $tt_id = $this->db->get_var( $this->db->prepare( "SELECT tt.term_taxonomy_id FROM {$this->db->term_taxonomy} AS tt INNER JOIN {$this->db->terms} AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id ) );
  1193. if ( !empty($tt_id) )
  1194. return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
  1195. $this->db->insert( $this->db->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
  1196. $tt_id = (int) $this->db->insert_id;
  1197. do_action("create_term", $term_id, $tt_id);
  1198. do_action("create_$taxonomy", $term_id, $tt_id);
  1199. $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
  1200. $this->clean_term_cache($term_id, $taxonomy);
  1201. do_action("created_term", $term_id, $tt_id);
  1202. do_action("created_$taxonomy", $term_id, $tt_id);
  1203. return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
  1204. }
  1205. /**
  1206. * Create Term and Taxonomy Relationships.
  1207. *
  1208. * Relates an object (post, link etc) to a term and taxonomy type. Creates the
  1209. * term and taxonomy relationship if it doesn't already exist. Creates a term if
  1210. * it doesn't exist (using the slug).
  1211. *
  1212. * A relationship means that the term is grouped in or belongs to the taxonomy.
  1213. * A term has no meaning until it is given context by defining which taxonomy it
  1214. * exists under.
  1215. *
  1216. * @package WordPress
  1217. * @subpackage Taxonomy
  1218. * @since 2.3.0
  1219. *
  1220. * @param int $object_id The object to relate to.
  1221. * @param array|int|string $term The slug or id of the term, will replace all existing
  1222. * related terms in this taxonomy.
  1223. * @param array|string $taxonomy The context in which to relate the term to the object.
  1224. * @param bool $append If false will delete difference of terms.
  1225. * @return array|WP_Error Affected Term IDs
  1226. */
  1227. function set_object_terms($object_id, $terms, $taxonomy, $append = false) {
  1228. $object_id = (int) $object_id;
  1229. if ( !$this->is_taxonomy($taxonomy) )
  1230. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  1231. if ( !is_array($terms) )
  1232. $terms = array($terms);
  1233. if ( ! $append )
  1234. $old_tt_ids = $this->get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));
  1235. $tt_ids = array();
  1236. $term_ids = array();
  1237. foreach ( (array) $terms as $term ) {
  1238. if ( !strlen(trim($term)) )
  1239. continue;
  1240. if ( !$id = $this->is_term($term, $taxonomy) )
  1241. $id = $this->insert_term($term, $taxonomy);
  1242. if ( is_wp_error($id) )
  1243. return $id;
  1244. $term_ids[] = $id['term_id'];
  1245. $id = $id['term_taxonomy_id'];
  1246. $tt_ids[] = $id;
  1247. if ( $this->db->get_var( $this->db->prepare( "SELECT term_taxonomy_id FROM {$this->db->term_relationships} WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $id ) ) )
  1248. continue;
  1249. $this->db->insert( $this->db->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $id ) );
  1250. }
  1251. $this->update_term_count($tt_ids, $taxonomy);
  1252. if ( ! $append ) {
  1253. $delete_terms = array_diff($old_tt_ids, $tt_ids);
  1254. if ( $delete_terms ) {
  1255. $in_delete_terms = "'" . implode("', '", $delete_terms) . "'";
  1256. $this->db->query( $this->db->prepare("DELETE FROM {$this->db->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id) );
  1257. $this->update_term_count($delete_terms, $taxonomy);
  1258. }
  1259. }
  1260. $t = $this->get_taxonomy($taxonomy);
  1261. if ( ! $append && isset($t->sort) && $t->sort ) {
  1262. $values = array();
  1263. $term_order = 0;
  1264. $final_tt_ids = $this->get_object_terms($object_id, $taxonomy, 'fields=tt_ids');
  1265. foreach ( $tt_ids as $tt_id )
  1266. if ( in_array($tt_id, $final_tt_ids) )
  1267. $values[] = $this->db->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
  1268. if ( $values )
  1269. $this->db->query("INSERT INTO {$this->db->term_relationships} (object_id, term_taxonomy_id, term_order) VALUES " . join(',', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)");
  1270. }
  1271. do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append);
  1272. return $tt_ids;
  1273. }
  1274. /**
  1275. * Will make slug unique, if it isn't already.
  1276. *
  1277. * The $slug has to be unique global to every taxonomy, meaning that one
  1278. * taxonomy term can't have a matching slug with another taxonomy term. Each
  1279. * slug has to be globally unique for every taxonomy.
  1280. *
  1281. * The way this works is that if the taxonomy that the term belongs to is
  1282. * heirarchical and has a parent, it will append that parent to the $slug.
  1283. *
  1284. * If that still doesn't return an unique slug, then it try to append a number
  1285. * until it finds a number that is truely unique.
  1286. *
  1287. * The only purpose for $term is for appending a parent, if one exists.
  1288. *
  1289. * @package WordPress
  1290. * @subpackage Taxonomy
  1291. * @since 2.3.0
  1292. *
  1293. * @param string $slug The string that will be tried for a unique slug
  1294. * @param object $term The term object that the $slug will belong too
  1295. * @return string Will return a true unique slug.
  1296. */
  1297. function unique_term_slug($slug, $term) {
  1298. // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
  1299. // by incorporating parent slugs.
  1300. if ( $this->is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
  1301. $the_parent = $term->parent;
  1302. while ( ! empty($the_parent) ) {
  1303. $parent_term = $this->get_term($the_parent, $term->taxonomy);
  1304. if ( is_wp_error($parent_term) || empty($parent_term) )
  1305. break;
  1306. $slug .= '-' . $parent_term->slug;
  1307. if ( empty($parent_term->parent) )
  1308. break;
  1309. $the_parent = $parent_term->parent;
  1310. }
  1311. }
  1312. // If we didn't get a unique slug, try appending a number to make it unique.
  1313. if ( !empty($args['term_id']) )
  1314. $query = $this->db->prepare( "SELECT slug FROM {$this->db->terms} WHERE slug = %s AND term_id != %d", $slug, $args['term_id'] );
  1315. else
  1316. $query = $this->db->prepare( "SELECT slug FROM {$this->db->terms} WHERE slug = %s", $slug );
  1317. if ( $this->db->get_var( $query ) ) {
  1318. $num = 2;
  1319. do {
  1320. $alt_slug = $slug . "-$num";
  1321. $num++;
  1322. $slug_check = $this->db->get_var( $this->db->prepare( "SELECT slug FROM {$this->db->terms} WHERE slug = %s", $alt_slug ) );
  1323. } while ( $slug_check );
  1324. $slug = $alt_slug;
  1325. }
  1326. return $slug;
  1327. }
  1328. /**
  1329. * Update term based on arguments provided.
  1330. *
  1331. * The $args will indiscriminately override all values with the same field name.
  1332. * Care must be taken to not override important information need to update or
  1333. * update will fail (or perhaps create a new term, neither would be acceptable).
  1334. *
  1335. * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
  1336. * defined in $args already.
  1337. *
  1338. * 'alias_of' will create a term group, if it doesn't already exist, and update
  1339. * it for the $term.
  1340. *
  1341. * If the 'slug' argument in $args is missing, then the 'name' in $args will be
  1342. * used. It should also be noted that if you set 'slug' and it isn't unique then
  1343. * a WP_Error will be passed back. If you don't pass any slug, then a unique one
  1344. * will be created for you.
  1345. *
  1346. * For what can be overrode in $args, check the term scheme can contain and stay
  1347. * away from the term keys.
  1348. *
  1349. * @package WordPress
  1350. * @subpackage Taxonomy
  1351. * @since 2.3.0
  1352. *
  1353. * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice.
  1354. * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term
  1355. * id and taxonomy id.
  1356. *
  1357. * @param int $term_id The ID of the term
  1358. * @param string $taxonomy The context in which to relate the term to the object.
  1359. * @param array|string $args Overwrite term field values
  1360. * @return array|WP_Error Returns Term ID and Taxonomy Term ID
  1361. */
  1362. function update_term( $term_id, $taxonomy, $args = array() ) {
  1363. if ( !$this->is_taxonomy($taxonomy) )
  1364. return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
  1365. $term_id = (int) $term_id;
  1366. // First, get all of the original args
  1367. $term = $this->get_term($term_id, $taxonomy, ARRAY_A);
  1368. if ( is_wp_error( $term ) )
  1369. return $term;
  1370. // Merge old and new args with new args overwriting old ones.
  1371. $args = array_merge($term, $args);
  1372. $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
  1373. $args = wp_parse_args($args, $defaults);
  1374. $args = $this->sanitize_term($args, $taxonomy, 'db');
  1375. extract($args, EXTR_SKIP);
  1376. // expected_slashed ($name)
  1377. $name = stripslashes($name);
  1378. $description = stripslashes($description);
  1379. if ( '' == trim($name) )
  1380. return new WP_Error('empty_term_name', __('A name is required for this term'));
  1381. $empty_slug = false;
  1382. if ( empty($slug) ) {
  1383. $empty_slug = true;
  1384. $slug = $this->sanitize_term_slug($name, $taxonomy, $term_id);
  1385. }
  1386. if ( $alias_of ) {
  1387. $alias = $this->db->get_row( $this->db->prepare( "SELECT term_id, term_group FROM {$this->db->terms} WHERE slug = %s", $alias_of) );
  1388. if ( $alias->term_group ) {
  1389. // The alias we want is already in a group, so let's use that one.
  1390. $term_group = $alias->term_group;
  1391. } else {
  1392. // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
  1393. $term_group = $this->db->get_var("SELECT MAX(term_group) FROM {$this->db->terms}") + 1;
  1394. $this->db->update( $this->db->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
  1395. }
  1396. }
  1397. // Check for duplicate slug
  1398. $id = $this->db->get_var( $this->db->prepare( "SELECT term_id FROM {$this->db->terms} WHERE slug = %s", $slug ) );
  1399. if ( $id && ($id != $term_id) ) {
  1400. // If an empty slug was passed or the parent changed, reset the slug to something unique.
  1401. // Otherwise, bail.
  1402. if ( $empty_slug || ( $parent != $term->parent) )
  1403. $slug = $this->unique_term_slug($slug, (object) $args);
  1404. else
  1405. return new WP_Error('duplicate_term_slug', sprintf(__('The slug &#8220;%s&#8221; is already in use by another term'), $slug));
  1406. }
  1407. $this->db->update($this->db->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
  1408. if ( empty($slug) ) {
  1409. $slug = $this->sanitize_term_slug($name, $taxonomy, $term_id);
  1410. $this->db->update( $this->db->terms, compact( 'slug' ), compact( 'term_id' ) );
  1411. }
  1412. $tt_id = $this->db->get_var( $this->db->prepare( "SELECT tt.term_taxonomy_id FROM {$this->db->term_taxonomy} AS tt INNER JOIN {$this->db->terms} AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) );
  1413. $this->db->update( $this->db->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
  1414. do_action("edit_term", $term_id, $tt_id);
  1415. do_action("edit_$taxonomy", $term_id, $tt_id);
  1416. $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
  1417. $this->clean_term_cache($term_id, $taxonomy);
  1418. do_action("edited_term", $term_id, $tt_id);
  1419. do_action("edited_$taxonomy", $term_id, $tt_id);
  1420. return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
  1421. }
  1422. /**
  1423. * Enable or disable term counting.
  1424. *
  1425. * @since 2.5.0
  1426. *
  1427. * @param bool $defer Optional. Enable if true, disable if false.
  1428. * @return bool Whether term counting is enabled or disabled.
  1429. */
  1430. function defer_term_counting($defer=NULL) {
  1431. static $_defer = false;
  1432. if ( is_bool($defer) ) {
  1433. $_defer = $defer;
  1434. // flush any deferred counts
  1435. if ( !$defer )
  1436. $this->update_term_count( NULL, NULL, true );
  1437. }
  1438. return $_defer;
  1439. }
  1440. /**
  1441. * Updates the amount of terms in taxonomy.
  1442. *
  1443. * If there is a taxonomy callback applied, then it will be called for updating
  1444. * the count.
  1445. *
  1446. * The default action is to count what the amount of terms have the relationship
  1447. * of term ID. Once that is done, then update the database.
  1448. *
  1449. * @package WordPress
  1450. * @subpackage Taxonomy
  1451. * @since 2.3.0
  1452. * @uses $this->db
  1453. *
  1454. * @param int|array $terms The ID of the terms
  1455. * @param string $taxonomy The context of the term.
  1456. * @return bool If no terms will return false, and if successful will return true.
  1457. */
  1458. function update_term_count( $terms, $taxonomy, $do_deferred=false ) {
  1459. static $_deferred = array();
  1460. if ( $do_deferred ) {
  1461. foreach ( (array) array_keys($_deferred) as $tax ) {
  1462. $this->update_term_count_now( $_deferred[$tax], $tax );
  1463. unset( $_deferred[$tax] );
  1464. }
  1465. }
  1466. if ( empty($terms) )
  1467. return false;
  1468. if ( !is_array($terms) )
  1469. $terms = array($terms);
  1470. if ( $this->defer_term_counting() ) {
  1471. if ( !isset($_deferred[$taxonomy]) )
  1472. $_deferred[$taxonomy] = array();
  1473. $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
  1474. return true;
  1475. }
  1476. return $this->update_term_count_now( $terms, $taxonomy );
  1477. }
  1478. /**
  1479. * Perform term count update immediately.
  1480. *
  1481. * @since 2.5.0
  1482. *
  1483. * @param array $terms The term_taxonomy_id of terms to update.
  1484. * @param string $taxonomy The context of the term.
  1485. * @return bool Always true when complete.
  1486. */
  1487. function update_term_count_now( $terms, $taxonomy ) {
  1488. $terms = array_map('intval', $terms);
  1489. $taxonomy = $this->get_taxonomy($taxonomy);
  1490. if ( !empty($taxonomy->update_count_callback) ) {
  1491. call_user_func($taxonomy->update_count_callback, $terms);
  1492. } else {
  1493. // Default count updater
  1494. foreach ( (array) $terms as $term ) {
  1495. $count = $this->db->get_var( $this->db->prepare( "SELECT COUNT(*) FROM {$this->db->term_relationships} WHERE term_taxonomy_id = %d", $term) );
  1496. $this->db->update( $this->db->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
  1497. }
  1498. }
  1499. $this->clean_term_cache($terms);
  1500. return true;
  1501. }
  1502. //
  1503. // Cache
  1504. //
  1505. /**
  1506. * Removes the taxonomy relationship to terms from the cache.
  1507. *
  1508. * Will remove the entire taxonomy relationship containing term $object_id. The
  1509. * term IDs have to exist within the taxonomy $object_type for the deletion to
  1510. * take place.
  1511. *
  1512. * @package WordPress
  1513. * @subpackage Taxonomy
  1514. * @since 2.3
  1515. *
  1516. * @see $this->get_object_taxonomies() for more on $object_type
  1517. * @uses do_action() Will call action hook named, 'clean_object_term_cache' after completion.
  1518. * Passes, function params in same order.
  1519. *
  1520. * @param int|array $object_ids Single or list of term object ID(s)
  1521. * @param string $object_type The taxonomy object type
  1522. */
  1523. function clean_object_term_cache($object_ids, $object_type) {
  1524. if ( !is_array($object_ids) )
  1525. $object_ids = array($object_ids);
  1526. foreach ( $object_ids as $id )
  1527. foreach ( $this->get_object_taxonomies($object_type) as $taxonomy )
  1528. wp_cache_delete($id, "{$taxonomy}_relationships");
  1529. do_action('clean_object_term_cache', $object_ids, $object_type);
  1530. }
  1531. /**
  1532. * Will remove all of the term ids from the cache.
  1533. *
  1534. * @package WordPress
  1535. * @subpackage Taxonomy
  1536. * @since 2.3.0
  1537. *
  1538. * @param int|array $ids Single or list of Term IDs
  1539. * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.
  1540. */
  1541. function clean_term_cache($ids, $taxonomy = '') {
  1542. static $cleaned = array();
  1543. if ( !is_array($ids) )
  1544. $ids = array($ids);
  1545. $taxonomies = array();
  1546. // If no taxonomy, assume tt_ids.
  1547. if ( empty($taxonomy) ) {
  1548. $tt_ids = implode(', ', $ids);
  1549. $terms = $this->db->get_results("SELECT term_id, taxonomy FROM {$this->db->term_taxonomy} WHERE term_taxonomy_id IN ($tt_ids)");
  1550. foreach ( (array) $terms as $term ) {
  1551. $taxonomies[] = $term->taxonomy;
  1552. wp_cache_delete($term->term_id, $term->taxonomy);
  1553. }
  1554. $taxonomies = array_unique($taxonomies);
  1555. } else {
  1556. foreach ( $ids as $id ) {
  1557. wp_cache_delete($id, $taxonomy);
  1558. }
  1559. $taxonomies = array($taxonomy);
  1560. }
  1561. foreach ( $taxonomies as $taxonomy ) {
  1562. if ( isset($cleaned[$taxonomy]) )
  1563. continue;
  1564. $cleaned[$taxonomy] = true;
  1565. wp_cache_delete('all_ids', $taxonomy);
  1566. wp_cache_delete('get', $taxonomy);
  1567. $this->delete_children_cache($taxonomy);
  1568. }
  1569. wp_cache_delete('get_terms', 'terms');
  1570. do_action('clean_term_cache', $ids, $taxonomy);
  1571. }
  1572. /**
  1573. * Retrieves the taxonomy relationship to the term object id.
  1574. *
  1575. * @package WordPress
  1576. * @subpackage Taxonomy
  1577. * @since 2.3.0
  1578. *
  1579. * @uses wp_cache_get() Retrieves taxonomy relationship from cache
  1580. *
  1581. * @param int|array $id Term object ID
  1582. * @param string $taxonomy Taxonomy Name
  1583. * @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
  1584. */
  1585. function &get_object_term_cache($id, $taxonomy) {
  1586. $cache = wp_cache_get($id, "{$taxonomy}_relationships");
  1587. return $cache;
  1588. }
  1589. /**
  1590. * Updates the cache for Term ID(s).
  1591. *
  1592. * Will only update the cache for terms not already cached.
  1593. *
  1594. * The $object_ids expects that the ids be separated by commas, if it is a
  1595. * string.
  1596. *
  1597. * It should be noted that update_object_term_cache() is very time extensive. It
  1598. * is advised that the function is not called very often or at least not for a
  1599. * lot of terms that exist in a lot of taxonomies. The amount of time increases
  1600. * for each term and it also increases for each taxonomy the term belongs to.
  1601. *
  1602. * @package WordPress
  1603. * @subpackage Taxonomy
  1604. * @since 2.3.0
  1605. * @uses $this->get_object_terms() Used to get te