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