PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-includes/taxonomy.php

https://bitbucket.org/bsnowman/classyblog
PHP | 3248 lines | 1623 code | 436 blank | 1189 comment | 477 complexity | 586835777b55c8de6197be1696170f40 MD5 | raw file
Possible License(s): BSD-3-Clause

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

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