BB_Taxonomy

Taxonomy API.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/class.bb-taxonomy.php  
  1. class BB_Taxonomy extends WP_Taxonomy 
  2. /** 
  3. * Retrieve object_ids of valid taxonomy and term. 
  4. * The strings of $taxonomies must exist before this function will continue. On 
  5. * failure of finding a valid taxonomy, it will return an WP_Error class, kind 
  6. * of like Exceptions in PHP 5, except you can't catch them. Even so, you can 
  7. * still test for the WP_Error class and get the error message. 
  8. * The $terms aren't checked the same as $taxonomies, but still need to exist 
  9. * for $object_ids to be returned. 
  10. * It is possible to change the order that object_ids is returned by either 
  11. * using PHP sort family functions or using the database by using $args with 
  12. * either ASC or DESC array. The value should be in the key named 'order'. 
  13. * @package bbPress 
  14. * @subpackage Taxonomy 
  15. * @since 1.0 
  16. * @uses wp_parse_args() Creates an array from string $args. 
  17. * @param string|array $terms String of term or array of string values of terms that will be used 
  18. * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names 
  19. * @param array|string $args Change the order of the object_ids, either ASC or DESC 
  20. * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success 
  21. * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found. 
  22. */ 
  23. function get_objects_in_term( $terms, $taxonomies, $args = null ) { 
  24. if ( !is_array($terms) ) 
  25. $terms = array($terms); 
  26.  
  27. if ( !is_array($taxonomies) ) 
  28. $taxonomies = array($taxonomies); 
  29.  
  30. foreach ( (array) $taxonomies as $taxonomy ) { 
  31. if ( !$this->is_taxonomy($taxonomy) ) 
  32. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); 
  33.  
  34. $defaults = array('order' => 'ASC', 'field' => 'term_id', 'user_id' => 0); 
  35. $args = wp_parse_args( $args, $defaults ); 
  36. extract($args, EXTR_SKIP); 
  37.  
  38. if ( 'tt_id' == $field ) 
  39. $field = 'tt.term_taxonomy_id'; 
  40. else 
  41. $field = 'tt.term_id'; 
  42.  
  43. $order = ( 'desc' == strtolower($order) ) ? 'DESC' : 'ASC'; 
  44. $user_id = (int) $user_id; 
  45.  
  46. $terms = array_map('intval', $terms); 
  47.  
  48. $taxonomies = "'" . implode("', '", $taxonomies) . "'"; 
  49. $terms = "'" . implode("', '", $terms) . "'"; 
  50.  
  51. $sql = "SELECT tr.object_id FROM {$this->db->term_relationships} AS tr INNER JOIN {$this->db->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND $field IN ($terms)"; 
  52. if ( $user_id ) 
  53. $sql .= " AND tr.user_id = '$user_id'"; 
  54. $sql .= " ORDER BY tr.object_id $order"; 
  55.  
  56. $object_ids = $this->db->get_col( $sql ); 
  57.  
  58. if ( ! $object_ids ) 
  59. return array(); 
  60.  
  61. return $object_ids; 
  62.  
  63. /** 
  64. * Will unlink the term from the taxonomy. 
  65. * Will remove the term's relationship to the taxonomy, not the term or taxonomy 
  66. * itself. The term and taxonomy will still exist. Will require the term's 
  67. * object ID to perform the operation. 
  68. * @package bbPress 
  69. * @subpackage Taxonomy 
  70. * @since 1.0 
  71. * @param int $object_id The term Object Id that refers to the term 
  72. * @param string|array $taxonomy List of Taxonomy Names or single Taxonomy name. 
  73. * @param int $user_id The ID of the user who created the relationship. 
  74. */ 
  75. function delete_object_term_relationships( $object_id, $taxonomies, $user_id = 0 ) { 
  76. $object_id = (int) $object_id; 
  77. $user_id = (int) $user_id; 
  78.  
  79. if ( !is_array($taxonomies) ) 
  80. $taxonomies = array($taxonomies); 
  81.  
  82. foreach ( (array) $taxonomies as $taxonomy ) { 
  83. $terms = $this->get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'user_id' => $user_id)); 
  84. $in_terms = "'" . implode("', '", $terms) . "'"; 
  85. $sql = "DELETE FROM {$this->db->term_relationships} WHERE object_id = %d AND term_taxonomy_id IN ($in_terms)"; 
  86. if ( $user_id ) 
  87. $sql .= " AND user_id = %d"; 
  88. $this->db->query( $this->db->prepare( $sql, $object_id, $user_id ) ); 
  89. $this->update_term_count($terms, $taxonomy); 
  90.  
  91. /** 
  92. * Retrieves the terms associated with the given object(s), in the supplied taxonomies. 
  93. * The following information has to do the $args parameter and for what can be 
  94. * contained in the string or array of that parameter, if it exists. 
  95. * The first argument is called, 'orderby' and has the default value of 'name'. 
  96. * The other value that is supported is 'count'. 
  97. * The second argument is called, 'order' and has the default value of 'ASC'. 
  98. * The only other value that will be acceptable is 'DESC'. 
  99. * The final argument supported is called, 'fields' and has the default value of 
  100. * 'all'. There are multiple other options that can be used instead. Supported 
  101. * values are as follows: 'all', 'ids', 'names', and finally 
  102. * 'all_with_object_id'. 
  103. * The fields argument also decides what will be returned. If 'all' or 
  104. * 'all_with_object_id' is choosen or the default kept intact, then all matching 
  105. * terms objects will be returned. If either 'ids' or 'names' is used, then an 
  106. * array of all matching term ids or term names will be returned respectively. 
  107. * @package bbPress 
  108. * @subpackage Taxonomy 
  109. * @since 1.0 
  110. * @param int|array $object_id The id of the object(s) to retrieve. 
  111. * @param string|array $taxonomies The taxonomies to retrieve terms from. 
  112. * @param array|string $args Change what is returned 
  113. * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist. 
  114. */ 
  115. function get_object_terms($object_ids, $taxonomies, $args = array()) { 
  116. if ( !is_array($taxonomies) ) 
  117. $taxonomies = array($taxonomies); 
  118.  
  119. foreach ( (array) $taxonomies as $taxonomy ) { 
  120. if ( !$this->is_taxonomy($taxonomy) ) 
  121. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); 
  122.  
  123. if ( !is_array($object_ids) ) 
  124. $object_ids = array($object_ids); 
  125. $object_ids = array_map('intval', $object_ids); 
  126.  
  127. $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all', 'user_id' => 0); 
  128. $args = wp_parse_args( $args, $defaults ); 
  129. $args['user_id'] = (int) $args['user_id']; 
  130.  
  131. $terms = array(); 
  132. if ( count($taxonomies) > 1 ) { 
  133. foreach ( $taxonomies as $index => $taxonomy ) { 
  134. $t = $this->get_taxonomy($taxonomy); 
  135. if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) { 
  136. unset($taxonomies[$index]); 
  137. $terms = array_merge($terms, $this->get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); 
  138. } else { 
  139. $t = $this->get_taxonomy($taxonomies[0]); 
  140. if ( isset($t->args) && is_array($t->args) ) 
  141. $args = array_merge($args, $t->args); 
  142.  
  143. extract($args, EXTR_SKIP); 
  144. $user_id = (int) $user_id; 
  145.  
  146. if ( 'count' == $orderby ) 
  147. $orderby = 'tt.count'; 
  148. else if ( 'name' == $orderby ) 
  149. $orderby = 't.name'; 
  150. else if ( 'slug' == $orderby ) 
  151. $orderby = 't.slug'; 
  152. else if ( 'term_group' == $orderby ) 
  153. $orderby = 't.term_group'; 
  154. else if ( 'term_order' == $orderby ) 
  155. $orderby = 'tr.term_order'; 
  156. else if ( 'none' == $orderby ) { 
  157. $orderby = ''; 
  158. $order = ''; 
  159. } else { 
  160. $orderby = 't.term_id'; 
  161.  
  162. // tt_ids queries can only be none or tr.term_taxonomy_id 
  163. if ( ('tt_ids' == $fields) && !empty($orderby) ) 
  164. $orderby = 'tr.term_taxonomy_id'; 
  165.  
  166. if ( !empty($orderby) ) 
  167. $orderby = "ORDER BY $orderby"; 
  168.  
  169. $taxonomies = "'" . implode("', '", $taxonomies) . "'"; 
  170. $object_ids = implode(', ', $object_ids); 
  171.  
  172. $select_this = ''; 
  173. if ( 'all' == $fields ) 
  174. $select_this = 't.*, tt.*, tr.user_id'; 
  175. else if ( 'ids' == $fields ) 
  176. $select_this = 't.term_id'; 
  177. else if ( 'names' == $fields ) 
  178. $select_this = 't.name'; 
  179. else if ( 'all_with_object_id' == $fields ) 
  180. $select_this = 't.*, tt.*, tr.user_id, tr.object_id'; 
  181.  
  182. $query = "SELECT $select_this FROM {$this->db->terms} AS t INNER JOIN {$this->db->term_taxonomy} AS tt ON tt.term_id = t.term_id INNER JOIN {$this->db->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids)"; 
  183. if ( $user_id ) 
  184. $query .= " AND user_id = '$user_id'"; 
  185. $query .= " $orderby $order"; 
  186.  
  187. if ( 'all' == $fields || 'all_with_object_id' == $fields ) { 
  188. $terms = array_merge($terms, $this->db->get_results($query)); 
  189. $this->update_term_cache($terms); 
  190. } else if ( 'ids' == $fields || 'names' == $fields ) { 
  191. $terms = array_merge($terms, $this->db->get_col($query)); 
  192. } else if ( 'tt_ids' == $fields ) { 
  193. $query = "SELECT tr.term_taxonomy_id FROM {$this->db->term_relationships} AS tr INNER JOIN {$this->db->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies)"; 
  194. if ( $user_id ) 
  195. $query .= " AND tr.user_id = '$user_id'"; 
  196. $query .= " $orderby $order"; 
  197. $terms = $this->db->get_col( $query ); 
  198.  
  199. if ( ! $terms ) 
  200. $terms = array(); 
  201.  
  202. return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args); 
  203.  
  204. /** 
  205. * Create Term and Taxonomy Relationships. 
  206. * Relates an object (post, link etc) to a term and taxonomy type. Creates the 
  207. * term and taxonomy relationship if it doesn't already exist. Creates a term if 
  208. * it doesn't exist (using the slug). 
  209. * A relationship means that the term is grouped in or belongs to the taxonomy. 
  210. * A term has no meaning until it is given context by defining which taxonomy it 
  211. * exists under. 
  212. * @package bbPress 
  213. * @subpackage Taxonomy 
  214. * @since 1.0 
  215. * @param int $object_id The object to relate to. 
  216. * @param array|int|string $term The slug or id of the term, will replace all existing 
  217. * related terms in this taxonomy. 
  218. * @param array|string $taxonomy The context in which to relate the term to the object. 
  219. * @param bool $append If false will delete difference of terms. 
  220. * @return array|WP_Error Affected Term IDs 
  221. */ 
  222. function set_object_terms($object_id, $terms, $taxonomy, $args = null) { 
  223. $object_id = (int) $object_id; 
  224.  
  225. $defaults = array( 'append' => false, 'user_id' => 0 ); 
  226. if ( is_scalar( $args ) ) 
  227. $args = array( 'append' => (bool) $args ); 
  228. $args = wp_parse_args( $args, $defaults ); 
  229. extract( $args, EXTR_SKIP ); 
  230. if ( !$user_id = (int) $user_id ) 
  231. return new WP_Error('invalid_user_id', __('Invalid User ID')); 
  232.  
  233. if ( !$this->is_taxonomy($taxonomy) ) 
  234. return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy')); 
  235.  
  236. if ( !is_array($terms) ) 
  237. $terms = array($terms); 
  238.  
  239. if ( ! $append ) 
  240. $old_tt_ids = $this->get_object_terms($object_id, $taxonomy, array('user_id' => $user_id, 'fields' => 'tt_ids', 'orderby' => 'none')); 
  241.  
  242. $tt_ids = array(); 
  243. $term_ids = array(); 
  244.  
  245. foreach ( (array) $terms as $term ) { 
  246. if ( !strlen(trim($term)) ) 
  247. continue; 
  248.  
  249. if ( !$id = $this->is_term($term, $taxonomy) ) 
  250. $id = $this->insert_term($term, $taxonomy); 
  251. if ( is_wp_error($id) ) 
  252. return $id; 
  253. $term_ids[] = $id['term_id']; 
  254. $id = $id['term_taxonomy_id']; 
  255. $tt_ids[] = $id; 
  256.  
  257. if ( $this->db->get_var( $this->db->prepare( "SELECT term_taxonomy_id FROM {$this->db->term_relationships} WHERE object_id = %d AND term_taxonomy_id = %d AND user_id = %d", $object_id, $id, $user_id ) ) ) 
  258. continue; 
  259. $this->db->insert( $this->db->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $id, 'user_id' => $user_id ) ); 
  260.  
  261. $this->update_term_count($tt_ids, $taxonomy); 
  262.  
  263. if ( ! $append ) { 
  264. $delete_terms = array_diff($old_tt_ids, $tt_ids); 
  265. if ( $delete_terms ) { 
  266. $in_delete_terms = "'" . implode("', '", $delete_terms) . "'"; 
  267. $this->db->query( $this->db->prepare("DELETE FROM {$this->db->term_relationships} WHERE object_id = %d AND user_id = %d AND term_taxonomy_id IN ($in_delete_terms)", $object_id, $user_id) ); 
  268. $this->update_term_count($delete_terms, $taxonomy); 
  269.  
  270. $t = $this->get_taxonomy($taxonomy); 
  271. if ( ! $append && isset($t->sort) && $t->sort ) { 
  272. $values = array(); 
  273. $term_order = 0; 
  274. $final_tt_ids = $this->get_object_terms($object_id, $taxonomy, array( 'user_id' => $user_id, 'fields' => 'tt_ids' )); 
  275. foreach ( $tt_ids as $tt_id ) 
  276. if ( in_array($tt_id, $final_tt_ids) ) 
  277. $values[] = $this->db->prepare( "(%d, %d, %d, %d)", $object_id, $tt_id, $user_id, ++$term_order); 
  278. if ( $values ) 
  279. $this->db->query("INSERT INTO {$this->db->term_relationships} (object_id, term_taxonomy_id, user_id, term_order) VALUES " . join(', ', $values) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)"); 
  280.  
  281. do_action('set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append); 
  282. return $tt_ids; 
  283. } // END class BB_Taxonomy extends WP_Taxonomy