PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/taxonomy.php

https://bitbucket.org/MaheshDhaduk/androidmobiles
PHP | 2684 lines | 1322 code | 348 blank | 1014 comment | 400 complexity | 976fd741c342768e704b6a964dd70fc8 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Taxonomy API
  4. *
  5. * @package WordPress
  6. * @subpackage Taxonomy
  7. * @since 2.3.0
  8. */
  9. //
  10. // Taxonomy Registration
  11. //
  12. /**
  13. * Creates the initial taxonomies when 'init' action is fired.
  14. */
  15. function create_initial_taxonomies() {
  16. register_taxonomy( 'category', 'post', array(
  17. 'hierarchical' => true,
  18. 'update_count_callback' => '_update_post_term_count',
  19. 'query_var' => false,
  20. 'rewrite' => false,
  21. 'public' => true,
  22. 'show_ui' => true,
  23. '_builtin' => true,
  24. ) ) ;
  25. register_taxonomy( 'post_tag', 'post', array(
  26. 'hierarchical' => false,
  27. 'update_count_callback' => '_update_post_term_count',
  28. 'query_var' => false,
  29. 'rewrite' => false,
  30. 'public' => true,
  31. 'show_ui' => true,
  32. '_builtin' => true,
  33. ) );
  34. register_taxonomy( 'nav_menu', 'nav_menu_item', array(
  35. 'hierarchical' => false,
  36. 'labels' => array(
  37. 'name' => __( 'Navigation Menus' ),
  38. 'singular_name' => __( 'Navigation Menu' ),
  39. ),
  40. 'query_var' => false,
  41. 'rewrite' => false,
  42. 'show_ui' => false,
  43. '_builtin' => true,
  44. 'show_in_nav_menus' => false,
  45. ) ) ;
  46. register_taxonomy( 'link_category', 'link', array(
  47. 'hierarchical' => false,
  48. 'labels' => array(
  49. 'name' => __( 'Categories' ),
  50. 'singular_name' => __( 'Category' ),
  51. 'update_item' => __( 'Update Category' ),
  52. ),
  53. 'query_var' => false,
  54. 'rewrite' => false,
  55. 'public' => false,
  56. 'show_ui' => false,
  57. '_builtin' => true,
  58. ) ) ;
  59. }
  60. add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
  61. /**
  62. * Get a list of registered taxonomy objects.
  63. *
  64. * @package WordPress
  65. * @subpackage Taxonomy
  66. * @since 3.0.0
  67. * @uses $wp_taxonomies
  68. * @see register_taxonomy
  69. *
  70. * @param array $args An array of key => value arguments to match against the taxonomy objects.
  71. * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
  72. * @param string $operator The logical operation to perform. 'or' means only one element
  73. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  74. * @return array A list of taxonomy names or objects
  75. */
  76. function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
  77. global $wp_taxonomies;
  78. $field = ('names' == $output) ? 'name' : false;
  79. return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
  80. }
  81. /**
  82. * Return all of the taxonomy names that are of $object_type.
  83. *
  84. * It appears that this function can be used to find all of the names inside of
  85. * $wp_taxonomies global variable.
  86. *
  87. * <code><?php $taxonomies = get_object_taxonomies('post'); ?></code> Should
  88. * result in <code>Array('category', 'post_tag')</code>
  89. *
  90. * @package WordPress
  91. * @subpackage Taxonomy
  92. * @since 2.3.0
  93. *
  94. * @uses $wp_taxonomies
  95. *
  96. * @param array|string|object $object Name of the type of taxonomy object, or an object (row from posts)
  97. * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
  98. * @return array The names of all taxonomy of $object_type.
  99. */
  100. function get_object_taxonomies($object, $output = 'names') {
  101. global $wp_taxonomies;
  102. if ( is_object($object) ) {
  103. if ( $object->post_type == 'attachment' )
  104. return get_attachment_taxonomies($object);
  105. $object = $object->post_type;
  106. }
  107. $object = (array) $object;
  108. $taxonomies = array();
  109. foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
  110. if ( array_intersect($object, (array) $tax_obj->object_type) ) {
  111. if ( 'names' == $output )
  112. $taxonomies[] = $tax_name;
  113. else
  114. $taxonomies[ $tax_name ] = $tax_obj;
  115. }
  116. }
  117. return $taxonomies;
  118. }
  119. /**
  120. * Retrieves the taxonomy object of $taxonomy.
  121. *
  122. * The get_taxonomy function will first check that the parameter string given
  123. * is a taxonomy object and if it is, it will return it.
  124. *
  125. * @package WordPress
  126. * @subpackage Taxonomy
  127. * @since 2.3.0
  128. *
  129. * @uses $wp_taxonomies
  130. * @uses taxonomy_exists() Checks whether taxonomy exists
  131. *
  132. * @param string $taxonomy Name of taxonomy object to return
  133. * @return object|bool The Taxonomy Object or false if $taxonomy doesn't exist
  134. */
  135. function get_taxonomy( $taxonomy ) {
  136. global $wp_taxonomies;
  137. if ( ! taxonomy_exists( $taxonomy ) )
  138. return false;
  139. return $wp_taxonomies[$taxonomy];
  140. }
  141. /**
  142. * Checks that the taxonomy name exists.
  143. *
  144. * Formerly is_taxonomy(), introduced in 2.3.0.
  145. *
  146. * @package WordPress
  147. * @subpackage Taxonomy
  148. * @since 3.0.0
  149. *
  150. * @uses $wp_taxonomies
  151. *
  152. * @param string $taxonomy Name of taxonomy object
  153. * @return bool Whether the taxonomy exists.
  154. */
  155. function taxonomy_exists( $taxonomy ) {
  156. global $wp_taxonomies;
  157. return isset( $wp_taxonomies[$taxonomy] );
  158. }
  159. /**
  160. * Whether the taxonomy object is hierarchical.
  161. *
  162. * Checks to make sure that the taxonomy is an object first. Then Gets the
  163. * object, and finally returns the hierarchical value in the object.
  164. *
  165. * A false return value might also mean that the taxonomy does not exist.
  166. *
  167. * @package WordPress
  168. * @subpackage Taxonomy
  169. * @since 2.3.0
  170. *
  171. * @uses taxonomy_exists() Checks whether taxonomy exists
  172. * @uses get_taxonomy() Used to get the taxonomy object
  173. *
  174. * @param string $taxonomy Name of taxonomy object
  175. * @return bool Whether the taxonomy is hierarchical
  176. */
  177. function is_taxonomy_hierarchical($taxonomy) {
  178. if ( ! taxonomy_exists($taxonomy) )
  179. return false;
  180. $taxonomy = get_taxonomy($taxonomy);
  181. return $taxonomy->hierarchical;
  182. }
  183. /**
  184. * Create or modify a taxonomy object. Do not use before init.
  185. *
  186. * A simple function for creating or modifying a taxonomy object based on the
  187. * parameters given. The function will accept an array (third optional
  188. * parameter), along with strings for the taxonomy name and another string for
  189. * the object type.
  190. *
  191. * Nothing is returned, so expect error maybe or use taxonomy_exists() to check
  192. * whether taxonomy exists.
  193. *
  194. * Optional $args contents:
  195. *
  196. * label - Name of the taxonomy shown in the menu. Usually plural. If not set, labels['name'] will be used.
  197. *
  198. * hierarchical - has some defined purpose at other parts of the API and is a
  199. * boolean value.
  200. *
  201. * update_count_callback - works much like a hook, in that it will be called
  202. * when the count is updated.
  203. *
  204. * rewrite - false to prevent rewrite, or array('slug'=>$slug) to customize
  205. * permastruct; default will use $taxonomy as slug.
  206. *
  207. * query_var - false to prevent queries, or string to customize query var
  208. * (?$query_var=$term); default will use $taxonomy as query var.
  209. *
  210. * public - If the taxonomy should be publically queryable; //@TODO not implemented.
  211. * defaults to true.
  212. *
  213. * show_ui - If the WordPress UI admin tags UI should apply to this taxonomy;
  214. * defaults to public.
  215. *
  216. * show_in_nav_menus - true makes this taxonomy available for selection in navigation menus.
  217. * Defaults to public.
  218. *
  219. * show_tagcloud - false to prevent the taxonomy being listed in the Tag Cloud Widget;
  220. * defaults to show_ui which defalts to public.
  221. *
  222. * labels - An array of labels for this taxonomy. You can see accepted values in {@link get_taxonomy_labels()}. By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.
  223. *
  224. * @package WordPress
  225. * @subpackage Taxonomy
  226. * @since 2.3.0
  227. * @uses $wp_taxonomies Inserts new taxonomy object into the list
  228. * @uses $wp_rewrite Adds rewrite tags and permastructs
  229. * @uses $wp Adds query vars
  230. *
  231. * @param string $taxonomy Name of taxonomy object
  232. * @param array|string $object_type Name of the object type for the taxonomy object.
  233. * @param array|string $args See above description for the two keys values.
  234. */
  235. function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
  236. global $wp_taxonomies, $wp_rewrite, $wp;
  237. if ( ! is_array($wp_taxonomies) )
  238. $wp_taxonomies = array();
  239. $defaults = array( 'hierarchical' => false,
  240. 'update_count_callback' => '',
  241. 'rewrite' => true,
  242. 'query_var' => $taxonomy,
  243. 'public' => true,
  244. 'show_ui' => null,
  245. 'show_tagcloud' => null,
  246. '_builtin' => false,
  247. 'labels' => array(),
  248. 'capabilities' => array(),
  249. 'show_in_nav_menus' => null,
  250. );
  251. $args = wp_parse_args($args, $defaults);
  252. if ( false !== $args['query_var'] && !empty($wp) ) {
  253. if ( true === $args['query_var'] )
  254. $args['query_var'] = $taxonomy;
  255. $args['query_var'] = sanitize_title_with_dashes($args['query_var']);
  256. $wp->add_query_var($args['query_var']);
  257. }
  258. if ( false !== $args['rewrite'] && '' != get_option('permalink_structure') ) {
  259. $args['rewrite'] = wp_parse_args($args['rewrite'], array(
  260. 'slug' => sanitize_title_with_dashes($taxonomy),
  261. 'with_front' => true,
  262. ));
  263. $wp_rewrite->add_rewrite_tag("%$taxonomy%", '([^/]+)', $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=");
  264. $wp_rewrite->add_permastruct($taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite']['with_front']);
  265. }
  266. if ( is_null($args['show_ui']) )
  267. $args['show_ui'] = $args['public'];
  268. // Whether to show this type in nav-menus.php. Defaults to the setting for public.
  269. if ( null === $args['show_in_nav_menus'] )
  270. $args['show_in_nav_menus'] = $args['public'];
  271. if ( is_null($args['show_tagcloud']) )
  272. $args['show_tagcloud'] = $args['show_ui'];
  273. $default_caps = array(
  274. 'manage_terms' => 'manage_categories',
  275. 'edit_terms' => 'manage_categories',
  276. 'delete_terms' => 'manage_categories',
  277. 'assign_terms' => 'edit_posts',
  278. );
  279. $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
  280. unset( $args['capabilities'] );
  281. $args['name'] = $taxonomy;
  282. $args['object_type'] = (array) $object_type;
  283. $args['labels'] = get_taxonomy_labels( (object) $args );
  284. $args['label'] = $args['labels']->name;
  285. $wp_taxonomies[$taxonomy] = (object) $args;
  286. // register callback handling for metabox
  287. add_filter('wp_ajax_add-'.$taxonomy, '_wp_ajax_add_hierarchical_term');
  288. }
  289. /**
  290. * Builds an object with all taxonomy labels out of a taxonomy object
  291. *
  292. * Accepted keys of the label array in the taxonomy object:
  293. * - name - general name for the taxonomy, usually plural. The same as and overriden by $tax->label. Default is Post Tags/Categories
  294. * - singular_name - name for one object of this taxonomy. Default is Post Tag/Category
  295. * - search_items - Default is Search Tags/Search Categories
  296. * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags
  297. * - all_items - Default is All Tags/All Categories
  298. * - parent_item - This string isn't used on non-hierarchical taxonomies. In hierarchical ones the default is Parent Category
  299. * - parent_item_colon - The same as <code>parent_item</code>, but with colon <code>:</code> in the end
  300. * - edit_item - Default is Edit Tag/Edit Category
  301. * - update_item - Default is Update Tag/Update Category
  302. * - add_new_item - Default is Add New Tag/Add New Category
  303. * - new_item_name - Default is New Tag Name/New Category Name
  304. * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas," used in the meta box.
  305. * - add_or_remove_items - This string isn't used on hierarchical taxonomies. Default is "Add or remove tags," used in the meta box when JavaScript is disabled.
  306. * - choose_from_most_used - This string isn't used on hierarchical taxonomies. Default is "Choose from the most used tags," used in the meta box.
  307. *
  308. * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories.)
  309. *
  310. * @since 3.0.0
  311. * @param object $tax Taxonomy object
  312. * @return object object with all the labels as member variables
  313. */
  314. function get_taxonomy_labels( $tax ) {
  315. if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) )
  316. $tax->labels['separate_items_with_commas'] = $tax->helps;
  317. $nohier_vs_hier_defaults = array(
  318. 'name' => array( _x( 'Post Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
  319. 'singular_name' => array( _x( 'Post Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
  320. 'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
  321. 'popular_items' => array( __( 'Popular Tags' ), null ),
  322. 'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),
  323. 'parent_item' => array( null, __( 'Parent Category' ) ),
  324. 'parent_item_colon' => array( null, __( 'Parent Category:' ) ),
  325. 'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
  326. 'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
  327. 'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
  328. 'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
  329. 'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
  330. 'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
  331. 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
  332. );
  333. return _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );
  334. }
  335. /**
  336. * Add an already registered taxonomy to an object type.
  337. *
  338. * @package WordPress
  339. * @subpackage Taxonomy
  340. * @since 3.0.0
  341. * @uses $wp_taxonomies Modifies taxonomy object
  342. *
  343. * @param string $taxonomy Name of taxonomy object
  344. * @param array|string $object_type Name of the object type
  345. * @return bool True if successful, false if not
  346. */
  347. function register_taxonomy_for_object_type( $taxonomy, $object_type) {
  348. global $wp_taxonomies;
  349. if ( !isset($wp_taxonomies[$taxonomy]) )
  350. return false;
  351. if ( ! get_post_type_object($object_type) )
  352. return false;
  353. $wp_taxonomies[$taxonomy]->object_type[] = $object_type;
  354. return true;
  355. }
  356. //
  357. // Term API
  358. //
  359. /**
  360. * Retrieve object_ids of valid taxonomy and term.
  361. *
  362. * The strings of $taxonomies must exist before this function will continue. On
  363. * failure of finding a valid taxonomy, it will return an WP_Error class, kind
  364. * of like Exceptions in PHP 5, except you can't catch them. Even so, you can
  365. * still test for the WP_Error class and get the error message.
  366. *
  367. * The $terms aren't checked the same as $taxonomies, but still need to exist
  368. * for $object_ids to be returned.
  369. *
  370. * It is possible to change the order that object_ids is returned by either
  371. * using PHP sort family functions or using the database by using $args with
  372. * either ASC or DESC array. The value should be in the key named 'order'.
  373. *
  374. * @package WordPress
  375. * @subpackage Taxonomy
  376. * @since 2.3.0
  377. *
  378. * @uses $wpdb
  379. * @uses wp_parse_args() Creates an array from string $args.
  380. *
  381. * @param int|array $term_ids Term id or array of term ids of terms that will be used
  382. * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names
  383. * @param array|string $args Change the order of the object_ids, either ASC or DESC
  384. * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success
  385. * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
  386. */
  387. function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
  388. global $wpdb;
  389. if ( ! is_array( $term_ids ) )
  390. $term_ids = array( $term_ids );
  391. if ( ! is_array( $taxonomies ) )
  392. $taxonomies = array( $taxonomies );
  393. foreach ( (array) $taxonomies as $taxonomy ) {
  394. if ( ! taxonomy_exists( $taxonomy ) )
  395. return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
  396. }
  397. $defaults = array( 'order' => 'ASC' );
  398. $args = wp_parse_args( $args, $defaults );
  399. extract( $args, EXTR_SKIP );
  400. $order = ( 'desc' == strtolower( $order ) ) ? 'DESC' : 'ASC';
  401. $term_ids = array_map('intval', $term_ids );
  402. $taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
  403. $term_ids = "'" . implode( "', '", $term_ids ) . "'";
  404. $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");
  405. if ( ! $object_ids )
  406. return array();
  407. return $object_ids;
  408. }
  409. /**
  410. * Get all Term data from database by Term ID.
  411. *
  412. * The usage of the get_term function is to apply filters to a term object. It
  413. * is possible to get a term object from the database before applying the
  414. * filters.
  415. *
  416. * $term ID must be part of $taxonomy, to get from the database. Failure, might
  417. * be able to be captured by the hooks. Failure would be the same value as $wpdb
  418. * returns for the get_row method.
  419. *
  420. * There are two hooks, one is specifically for each term, named 'get_term', and
  421. * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
  422. * term object, and the taxonomy name as parameters. Both hooks are expected to
  423. * return a Term object.
  424. *
  425. * 'get_term' hook - Takes two parameters the term Object and the taxonomy name.
  426. * Must return term object. Used in get_term() as a catch-all filter for every
  427. * $term.
  428. *
  429. * 'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy
  430. * name. Must return term object. $taxonomy will be the taxonomy name, so for
  431. * example, if 'category', it would be 'get_category' as the filter name. Useful
  432. * for custom taxonomies or plugging into default taxonomies.
  433. *
  434. * @package WordPress
  435. * @subpackage Taxonomy
  436. * @since 2.3.0
  437. *
  438. * @uses $wpdb
  439. * @uses sanitize_term() Cleanses the term based on $filter context before returning.
  440. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  441. *
  442. * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
  443. * @param string $taxonomy Taxonomy name that $term is part of.
  444. * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
  445. * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
  446. * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
  447. * exist then WP_Error will be returned.
  448. */
  449. function &get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
  450. global $wpdb;
  451. $null = null;
  452. if ( empty($term) ) {
  453. $error = new WP_Error('invalid_term', __('Empty Term'));
  454. return $error;
  455. }
  456. if ( ! taxonomy_exists($taxonomy) ) {
  457. $error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  458. return $error;
  459. }
  460. if ( is_object($term) && empty($term->filter) ) {
  461. wp_cache_add($term->term_id, $term, $taxonomy);
  462. $_term = $term;
  463. } else {
  464. if ( is_object($term) )
  465. $term = $term->term_id;
  466. $term = (int) $term;
  467. if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
  468. $_term = $wpdb->get_row( $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 tt.taxonomy = %s AND t.term_id = %s LIMIT 1", $taxonomy, $term) );
  469. if ( ! $_term )
  470. return $null;
  471. wp_cache_add($term, $_term, $taxonomy);
  472. }
  473. }
  474. $_term = apply_filters('get_term', $_term, $taxonomy);
  475. $_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
  476. $_term = sanitize_term($_term, $taxonomy, $filter);
  477. if ( $output == OBJECT ) {
  478. return $_term;
  479. } elseif ( $output == ARRAY_A ) {
  480. $__term = get_object_vars($_term);
  481. return $__term;
  482. } elseif ( $output == ARRAY_N ) {
  483. $__term = array_values(get_object_vars($_term));
  484. return $__term;
  485. } else {
  486. return $_term;
  487. }
  488. }
  489. /**
  490. * Get all Term data from database by Term field and data.
  491. *
  492. * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
  493. * required.
  494. *
  495. * The default $field is 'id', therefore it is possible to also use null for
  496. * field, but not recommended that you do so.
  497. *
  498. * If $value does not exist, the return value will be false. If $taxonomy exists
  499. * and $field and $value combinations exist, the Term will be returned.
  500. *
  501. * @package WordPress
  502. * @subpackage Taxonomy
  503. * @since 2.3.0
  504. *
  505. * @uses $wpdb
  506. * @uses sanitize_term() Cleanses the term based on $filter context before returning.
  507. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
  508. *
  509. * @param string $field Either 'slug', 'name', or 'id'
  510. * @param string|int $value Search for this term value
  511. * @param string $taxonomy Taxonomy Name
  512. * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
  513. * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
  514. * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
  515. */
  516. function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
  517. global $wpdb;
  518. if ( ! taxonomy_exists($taxonomy) )
  519. return false;
  520. if ( 'slug' == $field ) {
  521. $field = 't.slug';
  522. $value = sanitize_title($value);
  523. if ( empty($value) )
  524. return false;
  525. } else if ( 'name' == $field ) {
  526. // Assume already escaped
  527. $value = stripslashes($value);
  528. $field = 't.name';
  529. } else {
  530. return get_term( (int) $value, $taxonomy, $output, $filter);
  531. }
  532. $term = $wpdb->get_row( $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 tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value) );
  533. if ( !$term )
  534. return false;
  535. wp_cache_add($term->term_id, $term, $taxonomy);
  536. $term = apply_filters('get_term', $term, $taxonomy);
  537. $term = apply_filters("get_$taxonomy", $term, $taxonomy);
  538. $term = sanitize_term($term, $taxonomy, $filter);
  539. if ( $output == OBJECT ) {
  540. return $term;
  541. } elseif ( $output == ARRAY_A ) {
  542. return get_object_vars($term);
  543. } elseif ( $output == ARRAY_N ) {
  544. return array_values(get_object_vars($term));
  545. } else {
  546. return $term;
  547. }
  548. }
  549. /**
  550. * Merge all term children into a single array of their IDs.
  551. *
  552. * This recursive function will merge all of the children of $term into the same
  553. * array of term IDs. Only useful for taxonomies which are hierarchical.
  554. *
  555. * Will return an empty array if $term does not exist in $taxonomy.
  556. *
  557. * @package WordPress
  558. * @subpackage Taxonomy
  559. * @since 2.3.0
  560. *
  561. * @uses $wpdb
  562. * @uses _get_term_hierarchy()
  563. * @uses get_term_children() Used to get the children of both $taxonomy and the parent $term
  564. *
  565. * @param string $term ID of Term to get children
  566. * @param string $taxonomy Taxonomy Name
  567. * @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
  568. */
  569. function get_term_children( $term_id, $taxonomy ) {
  570. if ( ! taxonomy_exists($taxonomy) )
  571. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  572. $term_id = intval( $term_id );
  573. $terms = _get_term_hierarchy($taxonomy);
  574. if ( ! isset($terms[$term_id]) )
  575. return array();
  576. $children = $terms[$term_id];
  577. foreach ( (array) $terms[$term_id] as $child ) {
  578. if ( isset($terms[$child]) )
  579. $children = array_merge($children, get_term_children($child, $taxonomy));
  580. }
  581. return $children;
  582. }
  583. /**
  584. * Get sanitized Term field.
  585. *
  586. * Does checks for $term, based on the $taxonomy. The function is for contextual
  587. * reasons and for simplicity of usage. See sanitize_term_field() for more
  588. * information.
  589. *
  590. * @package WordPress
  591. * @subpackage Taxonomy
  592. * @since 2.3.0
  593. *
  594. * @uses sanitize_term_field() Passes the return value in sanitize_term_field on success.
  595. *
  596. * @param string $field Term field to fetch
  597. * @param int $term Term ID
  598. * @param string $taxonomy Taxonomy Name
  599. * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
  600. * @return mixed Will return an empty string if $term is not an object or if $field is not set in $term.
  601. */
  602. function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
  603. $term = (int) $term;
  604. $term = get_term( $term, $taxonomy );
  605. if ( is_wp_error($term) )
  606. return $term;
  607. if ( !is_object($term) )
  608. return '';
  609. if ( !isset($term->$field) )
  610. return '';
  611. return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
  612. }
  613. /**
  614. * Sanitizes Term for editing.
  615. *
  616. * Return value is sanitize_term() and usage is for sanitizing the term for
  617. * editing. Function is for contextual and simplicity.
  618. *
  619. * @package WordPress
  620. * @subpackage Taxonomy
  621. * @since 2.3.0
  622. *
  623. * @uses sanitize_term() Passes the return value on success
  624. *
  625. * @param int|object $id Term ID or Object
  626. * @param string $taxonomy Taxonomy Name
  627. * @return mixed|null|WP_Error Will return empty string if $term is not an object.
  628. */
  629. function get_term_to_edit( $id, $taxonomy ) {
  630. $term = get_term( $id, $taxonomy );
  631. if ( is_wp_error($term) )
  632. return $term;
  633. if ( !is_object($term) )
  634. return '';
  635. return sanitize_term($term, $taxonomy, 'edit');
  636. }
  637. /**
  638. * Retrieve the terms in a given taxonomy or list of taxonomies.
  639. *
  640. * You can fully inject any customizations to the query before it is sent, as
  641. * well as control the output with a filter.
  642. *
  643. * The 'get_terms' filter will be called when the cache has the term and will
  644. * pass the found term along with the array of $taxonomies and array of $args.
  645. * This filter is also called before the array of terms is passed and will pass
  646. * the array of terms, along with the $taxonomies and $args.
  647. *
  648. * The 'list_terms_exclusions' filter passes the compiled exclusions along with
  649. * the $args.
  650. *
  651. * The 'get_terms_orderby' filter passes the ORDER BY clause for the query
  652. * along with the $args array.
  653. *
  654. * The 'get_terms_fields' filter passes the fields for the SELECT query
  655. * along with the $args array.
  656. *
  657. * The list of arguments that $args can contain, which will overwrite the defaults:
  658. *
  659. * orderby - Default is 'name'. Can be name, count, term_group, slug or nothing
  660. * (will use term_id), Passing a custom value other than these will cause it to
  661. * order based on the custom value.
  662. *
  663. * order - Default is ASC. Can use DESC.
  664. *
  665. * hide_empty - Default is true. Will not return empty terms, which means
  666. * terms whose count is 0 according to the given taxonomy.
  667. *
  668. * exclude - Default is an empty array. An array, comma- or space-delimited string
  669. * of term ids to exclude from the return array. If 'include' is non-empty,
  670. * 'exclude' is ignored.
  671. *
  672. * exclude_tree - Default is an empty array. An array, comma- or space-delimited
  673. * string of term ids to exclude from the return array, along with all of their
  674. * descendant terms according to the primary taxonomy. If 'include' is non-empty,
  675. * 'exclude_tree' is ignored.
  676. *
  677. * include - Default is an empty array. An array, comma- or space-delimited string
  678. * of term ids to include in the return array.
  679. *
  680. * number - The maximum number of terms to return. Default is to return them all.
  681. *
  682. * offset - The number by which to offset the terms query.
  683. *
  684. * fields - Default is 'all', which returns an array of term objects.
  685. * If 'fields' is 'ids' or 'names', returns an array of
  686. * integers or strings, respectively.
  687. *
  688. * slug - Returns terms whose "slug" matches this value. Default is empty string.
  689. *
  690. * hierarchical - Whether to include terms that have non-empty descendants
  691. * (even if 'hide_empty' is set to true).
  692. *
  693. * search - Returned terms' names will contain the value of 'search',
  694. * case-insensitive. Default is an empty string.
  695. *
  696. * name__like - Returned terms' names will begin with the value of 'name__like',
  697. * case-insensitive. Default is empty string.
  698. *
  699. * The argument 'pad_counts', if set to true will include the quantity of a term's
  700. * children in the quantity of each term's "count" object variable.
  701. *
  702. * The 'get' argument, if set to 'all' instead of its default empty string,
  703. * returns terms regardless of ancestry or whether the terms are empty.
  704. *
  705. * The 'child_of' argument, when used, should be set to the integer of a term ID. Its default
  706. * is 0. If set to a non-zero value, all returned terms will be descendants
  707. * of that term according to the given taxonomy. Hence 'child_of' is set to 0
  708. * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies
  709. * make term ancestry ambiguous.
  710. *
  711. * The 'parent' argument, when used, should be set to the integer of a term ID. Its default is
  712. * the empty string '', which has a different meaning from the integer 0.
  713. * If set to an integer value, all returned terms will have as an immediate
  714. * ancestor the term whose ID is specified by that integer according to the given taxonomy.
  715. * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent'
  716. * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
  717. *
  718. * @package WordPress
  719. * @subpackage Taxonomy
  720. * @since 2.3.0
  721. *
  722. * @uses $wpdb
  723. * @uses wp_parse_args() Merges the defaults with those defined by $args and allows for strings.
  724. *
  725. * @param string|array Taxonomy name or list of Taxonomy names
  726. * @param string|array $args The values of what to search for when returning terms
  727. * @return array|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
  728. */
  729. function &get_terms($taxonomies, $args = '') {
  730. global $wpdb;
  731. $empty_array = array();
  732. $single_taxonomy = false;
  733. if ( !is_array($taxonomies) ) {
  734. $single_taxonomy = true;
  735. $taxonomies = array($taxonomies);
  736. }
  737. foreach ( (array) $taxonomies as $taxonomy ) {
  738. if ( ! taxonomy_exists($taxonomy) ) {
  739. $error = & new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  740. return $error;
  741. }
  742. }
  743. $in_taxonomies = "'" . implode("', '", $taxonomies) . "'";
  744. $defaults = array('orderby' => 'name', 'order' => 'ASC',
  745. 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
  746. 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
  747. 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
  748. 'pad_counts' => false, 'offset' => '', 'search' => '');
  749. $args = wp_parse_args( $args, $defaults );
  750. $args['number'] = absint( $args['number'] );
  751. $args['offset'] = absint( $args['offset'] );
  752. if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) ||
  753. '' !== $args['parent'] ) {
  754. $args['child_of'] = 0;
  755. $args['hierarchical'] = false;
  756. $args['pad_counts'] = false;
  757. }
  758. if ( 'all' == $args['get'] ) {
  759. $args['child_of'] = 0;
  760. $args['hide_empty'] = 0;
  761. $args['hierarchical'] = false;
  762. $args['pad_counts'] = false;
  763. }
  764. extract($args, EXTR_SKIP);
  765. if ( $child_of ) {
  766. $hierarchy = _get_term_hierarchy($taxonomies[0]);
  767. if ( !isset($hierarchy[$child_of]) )
  768. return $empty_array;
  769. }
  770. if ( $parent ) {
  771. $hierarchy = _get_term_hierarchy($taxonomies[0]);
  772. if ( !isset($hierarchy[$parent]) )
  773. return $empty_array;
  774. }
  775. // $args can be whatever, only use the args defined in defaults to compute the key
  776. $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
  777. $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
  778. $last_changed = wp_cache_get('last_changed', 'terms');
  779. if ( !$last_changed ) {
  780. $last_changed = time();
  781. wp_cache_set('last_changed', $last_changed, 'terms');
  782. }
  783. $cache_key = "get_terms:$key:$last_changed";
  784. $cache = wp_cache_get( $cache_key, 'terms' );
  785. if ( false !== $cache ) {
  786. $cache = apply_filters('get_terms', $cache, $taxonomies, $args);
  787. return $cache;
  788. }
  789. $_orderby = strtolower($orderby);
  790. if ( 'count' == $_orderby )
  791. $orderby = 'tt.count';
  792. else if ( 'name' == $_orderby )
  793. $orderby = 't.name';
  794. else if ( 'slug' == $_orderby )
  795. $orderby = 't.slug';
  796. else if ( 'term_group' == $_orderby )
  797. $orderby = 't.term_group';
  798. else if ( 'none' == $_orderby )
  799. $orderby = '';
  800. elseif ( empty($_orderby) || 'id' == $_orderby )
  801. $orderby = 't.term_id';
  802. $orderby = apply_filters( 'get_terms_orderby', $orderby, $args );
  803. if ( !empty($orderby) )
  804. $orderby = "ORDER BY $orderby";
  805. else
  806. $order = '';
  807. $where = '';
  808. $inclusions = '';
  809. if ( !empty($include) ) {
  810. $exclude = '';
  811. $exclude_tree = '';
  812. $interms = wp_parse_id_list($include);
  813. foreach ( $interms as $interm ) {
  814. if ( empty($inclusions) )
  815. $inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
  816. else
  817. $inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
  818. }
  819. }
  820. if ( !empty($inclusions) )
  821. $inclusions .= ')';
  822. $where .= $inclusions;
  823. $exclusions = '';
  824. if ( !empty( $exclude_tree ) ) {
  825. $excluded_trunks = wp_parse_id_list($exclude_tree);
  826. foreach ( $excluded_trunks as $extrunk ) {
  827. $excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids'));
  828. $excluded_children[] = $extrunk;
  829. foreach( $excluded_children as $exterm ) {
  830. if ( empty($exclusions) )
  831. $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
  832. else
  833. $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
  834. }
  835. }
  836. }
  837. if ( !empty($exclude) ) {
  838. $exterms = wp_parse_id_list($exclude);
  839. foreach ( $exterms as $exterm ) {
  840. if ( empty($exclusions) )
  841. $exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
  842. else
  843. $exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
  844. }
  845. }
  846. if ( !empty($exclusions) )
  847. $exclusions .= ')';
  848. $exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
  849. $where .= $exclusions;
  850. if ( !empty($slug) ) {
  851. $slug = sanitize_title($slug);
  852. $where .= " AND t.slug = '$slug'";
  853. }
  854. if ( !empty($name__like) )
  855. $where .= " AND t.name LIKE '{$name__like}%'";
  856. if ( '' !== $parent ) {
  857. $parent = (int) $parent;
  858. $where .= " AND tt.parent = '$parent'";
  859. }
  860. if ( $hide_empty && !$hierarchical )
  861. $where .= ' AND tt.count > 0';
  862. // don't limit the query results when we have to descend the family tree
  863. if ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {
  864. if ( $offset )
  865. $limit = 'LIMIT ' . $offset . ',' . $number;
  866. else
  867. $limit = 'LIMIT ' . $number;
  868. } else {
  869. $limit = '';
  870. }
  871. if ( !empty($search) ) {
  872. $search = like_escape($search);
  873. $where .= " AND (t.name LIKE '%$search%')";
  874. }
  875. $selects = array();
  876. switch ( $fields ) {
  877. case 'all':
  878. $selects = array('t.*', 'tt.*');
  879. break;
  880. case 'ids':
  881. case 'id=>parent':
  882. $selects = array('t.term_id', 'tt.parent', 'tt.count');
  883. break;
  884. case 'names':
  885. $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
  886. break;
  887. case 'count':
  888. $orderby = '';
  889. $order = '';
  890. $selects = array('COUNT(*)');
  891. }
  892. $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));
  893. $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where $orderby $order $limit";
  894. if ( 'count' == $fields ) {
  895. $term_count = $wpdb->get_var($query);
  896. return $term_count;
  897. }
  898. $terms = $wpdb->get_results($query);
  899. if ( 'all' == $fields ) {
  900. update_term_cache($terms);
  901. }
  902. if ( empty($terms) ) {
  903. wp_cache_add( $cache_key, array(), 'terms' );
  904. $terms = apply_filters('get_terms', array(), $taxonomies, $args);
  905. return $terms;
  906. }
  907. if ( $child_of ) {
  908. $children = _get_term_hierarchy($taxonomies[0]);
  909. if ( ! empty($children) )
  910. $terms = & _get_term_children($child_of, $terms, $taxonomies[0]);
  911. }
  912. // Update term counts to include children.
  913. if ( $pad_counts && 'all' == $fields )
  914. _pad_term_counts($terms, $taxonomies[0]);
  915. // Make sure we show empty categories that have children.
  916. if ( $hierarchical && $hide_empty && is_array($terms) ) {
  917. foreach ( $terms as $k => $term ) {
  918. if ( ! $term->count ) {
  919. $children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
  920. if ( is_array($children) )
  921. foreach ( $children as $child )
  922. if ( $child->count )
  923. continue 2;
  924. // It really is empty
  925. unset($terms[$k]);
  926. }
  927. }
  928. }
  929. reset ( $terms );
  930. $_terms = array();
  931. if ( 'id=>parent' == $fields ) {
  932. while ( $term = array_shift($terms) )
  933. $_terms[$term->term_id] = $term->parent;
  934. $terms = $_terms;
  935. } elseif ( 'ids' == $fields ) {
  936. while ( $term = array_shift($terms) )
  937. $_terms[] = $term->term_id;
  938. $terms = $_terms;
  939. } elseif ( 'names' == $fields ) {
  940. while ( $term = array_shift($terms) )
  941. $_terms[] = $term->name;
  942. $terms = $_terms;
  943. }
  944. if ( 0 < $number && intval(@count($terms)) > $number ) {
  945. $terms = array_slice($terms, $offset, $number);
  946. }
  947. wp_cache_add( $cache_key, $terms, 'terms' );
  948. $terms = apply_filters('get_terms', $terms, $taxonomies, $args);
  949. return $terms;
  950. }
  951. /**
  952. * Check if Term exists.
  953. *
  954. * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
  955. *
  956. * Formerly is_term(), introduced in 2.3.0.
  957. *
  958. * @package WordPress
  959. * @subpackage Taxonomy
  960. * @since 3.0.0
  961. *
  962. * @uses $wpdb
  963. *
  964. * @param int|string $term The term to check
  965. * @param string $taxonomy The taxonomy name to use
  966. * @param int $parent ID of parent term under which to confine the exists search.
  967. * @return mixed Get the term id or Term Object, if exists.
  968. */
  969. function term_exists($term, $taxonomy = '', $parent = 0) {
  970. global $wpdb;
  971. $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
  972. $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE ";
  973. if ( is_int($term) ) {
  974. if ( 0 == $term )
  975. return 0;
  976. $where = 't.term_id = %d';
  977. if ( !empty($taxonomy) )
  978. return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
  979. else
  980. return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
  981. }
  982. $term = trim( stripslashes( $term ) );
  983. if ( '' === $slug = sanitize_title($term) )
  984. return 0;
  985. $where = 't.slug = %s';
  986. $else_where = 't.name = %s';
  987. $where_fields = array($slug);
  988. $else_where_fields = array($term);
  989. if ( !empty($taxonomy) ) {
  990. $parent = (int) $parent;
  991. if ( $parent > 0 ) {
  992. $where_fields[] = $parent;
  993. $else_where_fields[] = $parent;
  994. $where .= ' AND tt.parent = %d';
  995. $else_where .= ' AND tt.parent = %d';
  996. }
  997. $where_fields[] = $taxonomy;
  998. $else_where_fields[] = $taxonomy;
  999. if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s", $where_fields), ARRAY_A) )
  1000. return $result;
  1001. return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s", $else_where_fields), ARRAY_A);
  1002. }
  1003. if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where", $where_fields) ) )
  1004. return $result;
  1005. return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where", $else_where_fields) );
  1006. }
  1007. /**
  1008. * Sanitize Term all fields.
  1009. *
  1010. * Relys on sanitize_term_field() to sanitize the term. The difference is that
  1011. * this function will sanitize <strong>all</strong> fields. The context is based
  1012. * on sanitize_term_field().
  1013. *
  1014. * The $term is expected to be either an array or an object.
  1015. *
  1016. * @package WordPress
  1017. * @subpackage Taxonomy
  1018. * @since 2.3.0
  1019. *
  1020. * @uses sanitize_term_field Used to sanitize all fields in a term
  1021. *
  1022. * @param array|object $term The term to check
  1023. * @param string $taxonomy The taxonomy name to use
  1024. * @param string $context Default is 'display'.
  1025. * @return array|object Term with all fields sanitized
  1026. */
  1027. function sanitize_term($term, $taxonomy, $context = 'display') {
  1028. if ( 'raw' == $context )
  1029. return $term;
  1030. $fields = array('term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group');
  1031. $do_object = false;
  1032. if ( is_object($term) )
  1033. $do_object = true;
  1034. $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
  1035. foreach ( (array) $fields as $field ) {
  1036. if ( $do_object ) {
  1037. if ( isset($term->$field) )
  1038. $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
  1039. } else {
  1040. if ( isset($term[$field]) )
  1041. $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
  1042. }
  1043. }
  1044. if ( $do_object )
  1045. $term->filter = $context;
  1046. else
  1047. $term['filter'] = $context;
  1048. return $term;
  1049. }
  1050. /**
  1051. * Cleanse the field value in the term based on the context.
  1052. *
  1053. * Passing a term field value through the function should be assumed to have
  1054. * cleansed the value for whatever context the term field is going to be used.
  1055. *
  1056. * If no context or an unsupported context is given, then default filters will
  1057. * be applied.
  1058. *
  1059. * There are enough filters for each context to support a custom filtering
  1060. * without creating your own filter function. Simply create a function that
  1061. * hooks into the filter you need.
  1062. *
  1063. * @package WordPress
  1064. * @subpackage Taxonomy
  1065. * @since 2.3.0
  1066. *
  1067. * @uses $wpdb
  1068. *
  1069. * @param string $field Term field to sanitize
  1070. * @param string $value Search for this term value
  1071. * @param int $term_id Term ID
  1072. * @param string $taxonomy Taxonomy Name
  1073. * @param string $context Either edit, db, display, attribute, or js.
  1074. * @return mixed sanitized field
  1075. */
  1076. function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
  1077. if ( 'parent' == $field || 'term_id' == $field || 'count' == $field || 'term_group' == $field ) {
  1078. $value = (int) $value;
  1079. if ( $value < 0 )
  1080. $value = 0;
  1081. }
  1082. if ( 'raw' == $context )
  1083. return $value;
  1084. if ( 'edit' == $context ) {
  1085. $value = apply_filters("edit_term_$field", $value, $term_id, $taxonomy);
  1086. $value = apply_filters("edit_${taxonomy}_$field", $value, $term_id);
  1087. if ( 'description' == $field )
  1088. $value = format_to_edit($value);
  1089. else
  1090. $value = esc_attr($value);
  1091. } else if ( 'db' == $context ) {
  1092. $value = apply_filters("pre_term_$field", $value, $taxonomy);
  1093. $value = apply_filters("pre_${taxonomy}_$field", $value);
  1094. // Back compat filters
  1095. if ( 'slug' == $field )
  1096. $value = apply_filters('pre_category_nicename', $value);
  1097. } else if ( 'rss' == $context ) {
  1098. $value = apply_filters("term_${field}_rss", $value, $taxonomy);
  1099. $value = apply_filters("${taxonomy}_${field}_rss", $value);
  1100. } else {
  1101. // Use display filters by default.
  1102. $value = apply_filters("term_$field", $value, $term_id, $taxonomy, $context);
  1103. $value = apply_filters("${taxonomy}_$field", $value, $term_id, $context);
  1104. }
  1105. if ( 'attribute' == $context )
  1106. $value = esc_attr($value);
  1107. else if ( 'js' == $context )
  1108. $value = esc_js($value);
  1109. return $value;
  1110. }
  1111. /**
  1112. * Count how many terms are in Taxonomy.
  1113. *
  1114. * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true).
  1115. *
  1116. * @package WordPress
  1117. * @subpackage Taxonomy
  1118. * @since 2.3.0
  1119. *
  1120. * @uses get_terms()
  1121. * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.
  1122. *
  1123. * @param string $taxonomy Taxonomy name
  1124. * @param array|string $args Overwrite defaults. See get_terms()
  1125. * @return int How many terms are in $taxonomy
  1126. */
  1127. function wp_count_terms( $taxonomy, $args = array() ) {
  1128. $defaults = array('hide_empty' => false);
  1129. $args = wp_parse_args($args, $defaults);
  1130. // backwards compatibility
  1131. if ( isset($args['ignore_empty']) ) {
  1132. $args['hide_empty'] = $args['ignore_empty'];
  1133. unset($args['ignore_empty']);
  1134. }
  1135. $args['fields'] = 'count';
  1136. return get_terms($taxonomy, $args);
  1137. }
  1138. /**
  1139. * Will unlink the term from the taxonomy.
  1140. *
  1141. * Will remove the term's relationship to the taxonomy, not the term or taxonomy
  1142. * itself. The term and taxonomy will still exist. Will require the term's
  1143. * object ID to perform the operation.
  1144. *
  1145. * @package WordPress
  1146. * @subpackage Taxonomy
  1147. * @since 2.3.0
  1148. * @uses $wpdb
  1149. *
  1150. * @param int $object_id The term Object Id that refers to the term
  1151. * @param string|array $taxonomy List of Taxonomy Names or single Taxonomy name.
  1152. */
  1153. function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
  1154. global $wpdb;
  1155. $object_id = (int) $object_id;
  1156. if ( !is_array($taxonomies) )
  1157. $taxonomies = array($taxonomies);
  1158. foreach ( (array) $taxonomies as $taxonomy ) {
  1159. $tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
  1160. $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
  1161. do_action( 'delete_term_relationships', $object_id, $tt_ids );
  1162. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id) );
  1163. do_action( 'deleted_term_relationships', $object_id, $tt_ids );
  1164. wp_update_term_count($tt_ids, $taxonomy);
  1165. }
  1166. }
  1167. /**
  1168. * Removes a term from the database.
  1169. *
  1170. * If the term is a parent of other terms, then the children will be updated to
  1171. * that term's parent.
  1172. *
  1173. * The $args 'default' will only override the terms found, if there is only one
  1174. * term found. Any other and the found terms are used.
  1175. *
  1176. * The $args 'force_default' will force the term supplied as default to be
  1177. * assigned even if the object was not going to be termless
  1178. * @package WordPress
  1179. * @subpackage Taxonomy
  1180. * @since 2.3.0
  1181. *
  1182. * @uses $wpdb
  1183. * @uses do_action() Calls both 'delete_term' and 'delete_$taxonomy' action
  1184. * hooks, passing term object, term id. 'delete_term' gets an additional
  1185. * parameter with the $taxonomy parameter.
  1186. *
  1187. * @param int $term Term ID
  1188. * @param string $taxonomy Taxonomy Name
  1189. * @param array|string $args Optional. Change 'default' term id and override found term ids.
  1190. * @return bool|WP_Error Returns false if not term; true if completes delete action.
  1191. */
  1192. function wp_delete_term( $term, $taxonomy, $args = array() ) {
  1193. global $wpdb;
  1194. $term = (int) $term;
  1195. if ( ! $ids = term_exists($term, $taxonomy) )
  1196. return false;
  1197. if ( is_wp_error( $ids ) )
  1198. return $ids;
  1199. $tt_id = $ids['term_taxonomy_id'];
  1200. $defaults = array();
  1201. $args = wp_parse_args($args, $defaults);
  1202. extract($args, EXTR_SKIP);
  1203. if ( isset($default) ) {
  1204. $default = (int) $default;
  1205. if ( ! term_exists($default, $taxonomy) )
  1206. unset($default);
  1207. }
  1208. // Update children to point to new parent
  1209. if ( is_taxonomy_hierarchical($taxonomy) ) {
  1210. $term_obj = get_term($term, $taxonomy);
  1211. if ( is_wp_error( $term_obj ) )
  1212. return $term_obj;
  1213. $parent = $term_obj->parent;
  1214. $edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
  1215. do_action( 'edit_term_taxonomies', $edit_tt_ids );
  1216. $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
  1217. do_action( 'edited_term_taxonomies', $edit_tt_ids );
  1218. }
  1219. $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
  1220. foreach ( (array) $objects as $object ) {
  1221. $terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
  1222. if ( 1 == count($terms) && isset($default) ) {
  1223. $terms = array($default);
  1224. } else {
  1225. $terms = array_diff($terms, array($term));
  1226. if (isset($default) && isset($force_default) && $force_default)
  1227. $terms = array_merge($terms, array($default));
  1228. }
  1229. $terms = array_map('intval', $terms);
  1230. wp_set_object_terms($object, $terms, $taxonomy);
  1231. }
  1232. do_action( 'delete_term_taxonomy', $tt_id );
  1233. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
  1234. do_action( 'deleted_term_taxonomy', $tt_id );
  1235. // Delete the term if no taxonomies use it.
  1236. if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
  1237. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
  1238. clean_term_cache($term, $taxonomy);
  1239. do_action('delete_term', $term, $tt_id, $taxonomy);
  1240. do_action("delete_$taxonomy", $term, $tt_id);
  1241. return true;
  1242. }
  1243. /**
  1244. * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
  1245. *
  1246. * The following information has to do the $args parameter and for what can be
  1247. * contained in the string or array of that parameter, if it exists.
  1248. *
  1249. * The first argument is called, 'orderby' and has the default value of 'name'.
  1250. * The other value that is supported is 'count'.
  1251. *
  1252. * The second argument is called, 'order' and has the default value of 'ASC'.
  1253. * The only other value that will be acceptable is 'DESC'.
  1254. *
  1255. * The final argument supported is called, 'fields' and has the default value of
  1256. * 'all'. There are multiple other options that can be used instead. Supported
  1257. * values are as follows: 'all', 'ids', 'names', and finally
  1258. * 'all_with_object_id'.
  1259. *
  1260. * The fields argument also decides what will be returned. If 'all' or
  1261. * 'all_with_object_id' is choosen or the default kept intact, then all matching
  1262. * terms objects will be returned. If either 'ids' or 'names' is used, then an
  1263. * array of all matching term ids or term names will be returned respectively.
  1264. *
  1265. * @package WordPress
  1266. * @subpackage Taxonomy
  1267. * @since 2.3.0
  1268. * @uses $wpdb
  1269. *
  1270. * @param int|array $object_id The id of the object(s) to retrieve.
  1271. * @param string|array $taxonomies The taxonomies to retrieve terms from.
  1272. * @param array|string $args Change what is returned
  1273. * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.
  1274. */
  1275. function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
  1276. global $wpdb;
  1277. if ( !is_array($taxonomies) )
  1278. $taxonomies = array($taxonomies);
  1279. foreach ( (array) $taxonomies as $taxonomy ) {
  1280. if ( ! taxonomy_exists($taxonomy) )
  1281. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
  1282. }
  1283. if ( !is_array($object_ids) )
  1284. $object_ids = array($object_ids);
  1285. $object_ids = array_map('intval', $object_ids);
  1286. $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
  1287. $args = wp_parse_args( $args, $defaults );
  1288. $terms = array();
  1289. if ( count($taxonomies) > 1 ) {
  1290. foreach ( $taxonomies as $index => $taxonomy ) {
  1291. $t = get_taxonomy($taxonomy);
  1292. if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
  1293. unset($taxonomies[$index]);
  1294. $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
  1295. }
  1296. }
  1297. } else {
  1298. $t

Large files files are truncated, but you can click here to view the full file