| | 172 | /** User Taxonomy Terms *******************************************************/ |
| | 173 | |
| | 174 | /** |
| | 175 | * Check if the user term is set on an object |
| | 176 | * |
| | 177 | * @since 2.7 bbPress () |
| | 178 | * |
| | 179 | * @param int $object_id The object id |
| | 180 | * @param int $user_id The user id |
| | 181 | * @param string $taxonomy The object user taxonomy |
| | 182 | * @uses get_post_meta() To check if the user term is set on the object |
| | 183 | * @uses apply_filters() Calls 'bbp_object_has_user_term' with the object id, |
| | 184 | * user id, and taxonomy |
| | 185 | * @return bool Returns true if the user term is set on the object for the |
| | 186 | * taxonomy, otherwise false |
| | 187 | */ |
| | 188 | function bbp_object_has_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) { |
| | 189 | if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) { |
| | 190 | return; |
| | 191 | } |
| | 192 | |
| | 193 | $user_ids = get_post_meta( $object_id, $taxonomy, false ); |
| | 194 | $retval = (bool) in_array( $user_id, $user_ids ); |
| | 195 | return apply_filters( 'bbp_object_has_user_term', $retval, $object_id, $user_id, $taxonomy ); |
| | 196 | } |
| | 197 | |
| | 198 | /** |
| | 199 | * Set a user term on an object |
| | 200 | * |
| | 201 | * @since 2.7 bbPress () |
| | 202 | * |
| | 203 | * @param int $object_id The object id |
| | 204 | * @param int $user_id The user id |
| | 205 | * @param string $taxonomy The user term taxonomy |
| | 206 | * @uses bbp_object_has_user_term() To check if the term has already been set |
| | 207 | * @uses add_post_meta() To set the term on the object |
| | 208 | * @uses apply_filters() Calls 'bbp_add_object_user_term' with the object id, user |
| | 209 | * id, and taxonomy |
| | 210 | * @return bool Returns true if the user taxonomy term is added to the object, |
| | 211 | * otherwise false |
| | 212 | */ |
| | 213 | function bbp_add_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) { |
| | 214 | if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) { |
| | 215 | return; |
| | 216 | } |
| | 217 | |
| | 218 | if ( bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) { |
| | 219 | $retval = false; |
| | 220 | } else { |
| | 221 | $retval = (bool) add_post_meta( $object_id, $taxonomy, $user_id, false ); |
| | 222 | } |
| | 223 | return apply_filters( 'bbp_add_object_user_term', $retval, $object_id, $user_id, $taxonomy ); |
| | 224 | } |
| | 225 | |
| | 226 | /** |
| | 227 | * Remove a user term from an object |
| | 228 | * |
| | 229 | * @since 2.7 bbPress () |
| | 230 | * |
| | 231 | * @param int $object_id The post id |
| | 232 | * @param int $user_id The user id |
| | 233 | * @param string $taxonomy The user term taxonomy |
| | 234 | * @uses bbp_object_has_user_term() To check if the term is set |
| | 235 | * @uses delete_post_meta() To remove the term from the object |
| | 236 | * @uses apply_filters() Calls 'bbp_remove_object_user_term' with the object |
| | 237 | * id, user id, and taxonomy |
| | 238 | * @return bool Returns true is the user taxonomy term is removed from the object, |
| | 239 | * otherwise false |
| | 240 | */ |
| | 241 | function bbp_remove_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) { |
| | 242 | if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) { |
| | 243 | return; |
| | 244 | } |
| | 245 | |
| | 246 | if ( ! bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) { |
| | 247 | $retval = false; |
| | 248 | } else { |
| | 249 | $retval = delete_post_meta( $object_id, $taxonomy, $user_id ); |
| | 250 | } |
| | 251 | return apply_filters( 'bbp_remove_object_user_term', $retval, $object_id, $user_id, $taxonomy ); |
| | 252 | } |
| | 253 | |
| | 254 | /** |
| | 255 | * Get objects for a user taxonomy term |
| | 256 | * |
| | 257 | * @since 2.7 bbPress () |
| | 258 | * |
| | 259 | * @param int $user_id The user id |
| | 260 | * @param string $taxonomy The user term taxonomy |
| | 261 | * @param string $object_type Optional. The object type |
| | 262 | * @uses post_type_exists() To validate the object type |
| | 263 | * @uses get_posts() To get the objects with a meta query |
| | 264 | * @uses apply_filters() Calls 'bbp_get_objects_by_user_term' with the objects, |
| | 265 | * user_id, taxonomy, and object type |
| | 266 | * @return array Returns the objects with the user taxonomy term |
| | 267 | */ |
| | 268 | function bbp_get_objects_by_user_term( $user_id = 0, $taxonomy = '', $object_type = null ) { |
| | 269 | if ( empty( $user_id ) || empty( $taxonomy ) || $object_type && ! post_type_exists( $object_type ) ) { |
| | 270 | return; |
| | 271 | } |
| | 272 | |
| | 273 | $args = array( |
| | 274 | 'meta_key' => $taxonomy, |
| | 275 | 'meta_type' => 'NUMERIC', |
| | 276 | 'meta_value' => $user_id, |
| | 277 | 'numberposts' => -1 |
| | 278 | ); |
| | 279 | if ( $object_type ) { |
| | 280 | $args['post_type'] = $object_type; |
| | 281 | } |
| | 282 | $objects = get_posts( $args ); |
| | 283 | return apply_filters( 'bbp_get_objects_by_user_term', $objects, $user_id, $taxonomy, $object_type ); |
| | 284 | } |
| | 285 | |
| | 286 | /** |
| | 287 | * Get user taxonomy terms for an object |
| | 288 | * |
| | 289 | * @since 2.7 bbPress () |
| | 290 | * |
| | 291 | * @param int $object_id The object id |
| | 292 | * @param string $taxonomy The user term taxonomy |
| | 293 | * @uses get_post_meta() To get the user taxonomy terms |
| | 294 | * @uses apply_filters() Calls 'bbp_get_user_terms_by_object' with the user |
| | 295 | * taxonomy terms, object id, and taxonomy |
| | 296 | * @return array Returns the user taxonomy terms of the object |
| | 297 | */ |
| | 298 | function bbp_get_user_terms_by_object( $object_id = 0, $taxonomy = '' ) { |
| | 299 | if ( empty( $object_id ) || empty( $taxonomy ) ) { |
| | 300 | return; |
| | 301 | } |
| | 302 | |
| | 303 | $retval = get_post_meta( $object_id, $taxonomy, false ); |
| | 304 | return apply_filters( 'bbp_get_user_terms_by_object', $retval, $object_id, $taxonomy ); |
| | 305 | } |
| | 306 | |