PageRenderTime 68ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/taxonomy.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 3140 lines | 1566 code | 418 blank | 1156 comment | 451 complexity | a82431bfc8bcf2aa4ba8e5f75295ca6a MD5 | raw file

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

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