PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
bbpress / includes / common / engagements.php

engagements.php in bbPress 2.6.17, at includes/common/engagements.php

982 lines 27.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Common Engagements
5 *
6 * This file contains the common classes and functions for interacting with the
7 * bbPress engagements API. See `includes/users/engagements.php` for more.
8 *
9 * @package bbPress
10 * @subpackage Common
11 */
12
13 // Exit if accessed directly
14 defined( 'ABSPATH' ) || exit;
15
16 /**
17 * Return the strategy used for storing user engagements
18 *
19 * @since 2.6.0 bbPress (r6722)
20 *
21 * @param string $rel_key The key used to index this relationship
22 * @param string $rel_type The type of meta to look in
23 *
24 * @return string
25 */
26 function bbp_user_engagements_interface( $rel_key = '', $rel_type = 'post' ) {
27 return apply_filters( 'bbp_user_engagements_interface', bbpress()->engagements, $rel_key, $rel_type );
28 }
29
30 /**
31 * Base strategy class for interfacing with User Engagements, which other
32 * classes will extend.
33 *
34 * @since 2.6.0 bbPress (r6722)
35 */
36 class BBP_User_Engagements_Base {
37
38 /**
39 * Type of strategy being used.
40 *
41 * @since 2.6.0 bbPress (r6737)
42 *
43 * @var string
44 */
45 public $type = '';
46
47 /**
48 * Add a user id to an object
49 *
50 * @since 2.6.0 bbPress (r6722)
51 *
52 * @param int $object_id The object id
53 * @param int $user_id The user id
54 * @param string $meta_key The relationship key
55 * @param string $meta_type The relationship type (usually 'post')
56 * @param bool $unique Whether meta key should be unique to the object
57 *
58 * @return bool Returns true on success, false on failure
59 */
60 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
61 // Intentionally empty
62 }
63
64 /**
65 * Remove a user id from an object
66 *
67 * @since 2.6.0 bbPress (r6722)
68 *
69 * @param int $object_id The object id
70 * @param int $user_id The user id
71 * @param string $meta_key The relationship key
72 * @param string $meta_type The relationship type (usually 'post')
73 *
74 * @return bool Returns true on success, false on failure
75 */
76 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
77 // Intentionally empty
78 }
79
80 /**
81 * Remove a user id from all objects
82 *
83 * @since 2.6.0 bbPress (r6722)
84 *
85 * @param int $user_id The user id
86 * @param string $meta_key The relationship key
87 * @param string $meta_type The relationship type (usually 'post')
88 *
89 * @return bool Returns true on success, false on failure
90 */
91 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
92 // Intentionally empty
93 }
94
95 /**
96 * Remove an object from all users
97 *
98 * @since 2.6.0 bbPress (r6722)
99 *
100 * @param int $object_id The object id
101 * @param int $user_id The user id
102 * @param string $meta_key The relationship key
103 * @param string $meta_type The relationship type (usually 'post')
104 *
105 * @return bool Returns true on success, false on failure
106 */
107 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
108 // Intentionally empty
109 }
110
111 /**
112 * Remove all users from all objects
113 *
114 * @since 2.6.0 bbPress (r6722)
115 *
116 * @param string $meta_key The relationship key
117 * @param string $meta_type The relationship type (usually 'post')
118 *
119 * @return bool Returns true on success, false on failure
120 */
121 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
122 // Intentionally empty
123 }
124
125 /**
126 * Get users of an object
127 *
128 * @since 2.6.0 bbPress (r6722)
129 *
130 * @param int $object_id The object id
131 * @param string $meta_key The key used to index this relationship
132 * @param string $meta_type The type of meta to look in
133 *
134 * @return array Returns ids of users
135 */
136 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
137 // Intentionally empty
138 }
139
140 /**
141 * Get the part of the query responsible for JOINing objects to relationships.
142 *
143 * @since 2.6.0 bbPress (r6737)
144 *
145 * @param array $args
146 * @param string $meta_key
147 * @param string $meta_type
148 *
149 * @return array
150 */
151 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
152 // Intentionally empty
153 }
154 }
155
156 /**
157 * Meta strategy for interfacing with User Engagements
158 *
159 * @since 2.6.0 bbPress (r6722)
160 */
161 class BBP_User_Engagements_Meta extends BBP_User_Engagements_Base {
162
163 /**
164 * Type of strategy being used.
165 *
166 * @since 2.6.0 bbPress (r6737)
167 *
168 * @var string
169 */
170 public $type = 'meta';
171
172 /**
173 * Add a user id to an object
174 *
175 * @since 2.6.0 bbPress (r6722)
176 *
177 * @param int $object_id The object id
178 * @param int $user_id The user id
179 * @param string $meta_key The relationship key
180 * @param string $meta_type The relationship type (usually 'post')
181 * @param bool $unique Whether meta key should be unique to the object
182 *
183 * @return bool Returns true on success, false on failure
184 */
185 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
186 return add_metadata( $meta_type, $object_id, $meta_key, $user_id, $unique );
187 }
188
189 /**
190 * Remove a user id from an object
191 *
192 * @since 2.6.0 bbPress (r6722)
193 *
194 * @param int $object_id The object id
195 * @param int $user_id The user id
196 * @param string $meta_key The relationship key
197 * @param string $meta_type The relationship type (usually 'post')
198 *
199 * @return bool Returns true on success, false on failure
200 */
201 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
202 return delete_metadata( $meta_type, $object_id, $meta_key, $user_id, false );
203 }
204
205 /**
206 * Remove a user id from all objects
207 *
208 * @since 2.6.0 bbPress (r6722)
209 *
210 * @param int $user_id The user id
211 * @param string $meta_key The relationship key
212 * @param string $meta_type The relationship type (usually 'post')
213 *
214 * @return bool Returns true on success, false on failure
215 */
216 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
217 return delete_metadata( $meta_type, null, $meta_key, $user_id, true );
218 }
219
220 /**
221 * Remove an object from all users
222 *
223 * @since 2.6.0 bbPress (r6722)
224 *
225 * @param int $object_id The object id
226 * @param int $user_id The user id
227 * @param string $meta_key The relationship key
228 * @param string $meta_type The relationship type (usually 'post')
229 *
230 * @return bool Returns true on success, false on failure
231 */
232 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
233 return delete_metadata( $meta_type, $object_id, $meta_key, null, false );
234 }
235
236 /**
237 * Remove all users from all objects
238 *
239 * @since 2.6.0 bbPress (r6722)
240 *
241 * @param string $meta_key The relationship key
242 * @param string $meta_type The relationship type (usually 'post')
243 *
244 * @return bool Returns true on success, false on failure
245 */
246 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
247 return delete_metadata( $meta_type, null, $meta_key, null, true );
248 }
249
250 /**
251 * Get users of an object
252 *
253 * @since 2.6.0 bbPress (r6722)
254 *
255 * @param int $object_id The object id
256 * @param string $meta_key The key used to index this relationship
257 * @param string $meta_type The type of meta to look in
258 *
259 * @return array Returns ids of users
260 */
261 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
262 return wp_parse_id_list( get_metadata( $meta_type, $object_id, $meta_key, false ) );
263 }
264
265 /**
266 * Get the part of the query responsible for JOINing objects to relationships.
267 *
268 * @since 2.6.0 bbPress (r6737)
269 *
270 * @param array $args
271 * @param string $meta_key
272 * @param string $meta_type
273 *
274 * @return array
275 */
276 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
277
278 // Backwards compat for pre-2.6.0
279 if ( is_numeric( $args ) ) {
280 $args = array(
281 'meta_query' => array(
282 array(
283 'key' => $meta_key,
284 'value' => bbp_get_user_id( $args, false, false ),
285 'compare' => 'NUMERIC'
286 )
287 )
288 );
289 }
290
291 // Default arguments
292 $defaults = array(
293 'meta_query' => array(
294 array(
295 'key' => $meta_key,
296 'value' => bbp_get_displayed_user_id(),
297 'compare' => 'NUMERIC'
298 )
299 )
300 );
301
302 // Parse arguments
303 return bbp_parse_args( $args, $defaults, $context_key );
304 }
305 }
306
307 /**
308 * Term strategy for interfacing with User Engagements
309 *
310 * @since 2.6.0 bbPress (r6737)
311 */
312 class BBP_User_Engagements_Term extends BBP_User_Engagements_Base {
313
314 /**
315 * Type of strategy being used.
316 *
317 * @since 2.6.0 bbPress (r6737)
318 *
319 * @var string
320 */
321 public $type = 'term';
322
323 /**
324 * Register an engagement taxonomy just-in-time for immediate use
325 *
326 * @since 2.6.0 bbPress (r6737)
327 *
328 * @param string $tax_key
329 * @param string $object_type
330 */
331 private function jit_taxonomy( $tax_key = '', $object_type = 'user' ) {
332
333 // Bail if taxonomy already exists
334 if ( taxonomy_exists( $tax_key ) ) {
335 return;
336 }
337
338 // Register the taxonomy
339 register_taxonomy(
340 $tax_key,
341 'bbp_' . $object_type,
342 array(
343 'labels' => array(),
344 'description' => '',
345 'public' => false,
346 'publicly_queryable' => false,
347 'hierarchical' => false,
348 'show_ui' => false,
349 'show_in_menu' => false,
350 'show_in_nav_menus' => false,
351 'show_tagcloud' => false,
352 'show_in_quick_edit' => false,
353 'show_admin_column' => false,
354 'meta_box_cb' => false,
355 'capabilities' => array(),
356 'rewrite' => false,
357 'query_var' => '',
358 'update_count_callback' => '',
359 'show_in_rest' => false,
360 'rest_base' => false,
361 'rest_controller_class' => false,
362 '_builtin' => false
363 )
364 );
365 }
366
367 /**
368 * Add a user id to an object
369 *
370 * @since 2.6.0 bbPress (r6737)
371 *
372 * @param int $object_id The object id
373 * @param int $user_id The user id
374 * @param string $meta_key The relationship key
375 * @param string $meta_type The relationship type (usually 'post')
376 * @param bool $unique Whether meta key should be unique to the object
377 *
378 * @return bool Returns true on success, false on failure
379 */
380 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
381 $user_key = "{$meta_key}_user_id_{$user_id}";
382 $tax_key = "{$meta_key}_{$meta_type}";
383 $this->jit_taxonomy( $tax_key );
384
385 return wp_add_object_terms( $object_id, $user_key, $tax_key );
386 }
387
388 /**
389 * Remove a user id from an object
390 *
391 * @since 2.6.0 bbPress (r6737)
392 *
393 * @param int $object_id The object id
394 * @param int $user_id The user id
395 * @param string $meta_key The relationship key
396 * @param string $meta_type The relationship type (usually 'post')
397 *
398 * @return bool Returns true on success, false on failure
399 */
400 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
401 $user_key = "{$meta_key}_user_id_{$user_id}";
402 $tax_key = "{$meta_key}_{$meta_type}";
403 $this->jit_taxonomy( $tax_key );
404
405 return wp_remove_object_terms( $object_id, $user_key, $tax_key );
406 }
407
408 /**
409 * Remove a user id from all objects
410 *
411 * @since 2.6.0 bbPress (r6737)
412 *
413 * @param int $user_id The user id
414 * @param string $meta_key The relationship key
415 * @param string $meta_type The relationship type (usually 'post')
416 *
417 * @return bool Returns true on success, false on failure
418 */
419 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
420 $user_key = "{$meta_key}_user_id_{$user_id}";
421 $tax_key = "{$meta_key}_{$meta_type}";
422 $this->jit_taxonomy( $tax_key );
423 $term = get_term_by( 'slug', $user_key, $tax_key );
424
425 return wp_delete_term( $term->term_id, $tax_key );
426 }
427
428 /**
429 * Remove an object from all users
430 *
431 * @since 2.6.0 bbPress (r6737)
432 * @since 2.6.17 Limit removal to the requested relationship taxonomy.
433 *
434 * @param int $object_id The object id
435 * @param int $user_id The user id
436 * @param string $meta_key The relationship key
437 * @param string $meta_type The relationship type (usually 'post')
438 *
439 * @return bool Returns true on success, false on failure
440 */
441 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
442 $tax_key = "{$meta_key}_{$meta_type}";
443 $this->jit_taxonomy( $tax_key );
444
445 return wp_delete_object_term_relationships( $object_id, $tax_key );
446 }
447
448 /**
449 * Remove all users from all objects
450 *
451 * @since 2.6.0 bbPress (r6737)
452 *
453 * @param string $meta_key The relationship key
454 * @param string $meta_type The relationship type (usually 'post')
455 *
456 * @return bool Returns true on success, false on failure
457 */
458 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
459 // TODO
460 }
461
462 /**
463 * Get users of an object
464 *
465 * @since 2.6.0 bbPress (r6737)
466 *
467 * @param int $object_id The object id
468 * @param string $meta_key The key used to index this relationship
469 * @param string $meta_type The type of meta to look in
470 *
471 * @return array Returns ids of users
472 */
473 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
474 $user_key = "{$meta_key}_user_id_";
475 $tax_key = "{$meta_key}_{$meta_type}";
476 $this->jit_taxonomy( $tax_key );
477
478 // Get terms
479 $terms = get_terms(
480 array(
481 'object_ids' => $object_id,
482 'taxonomy' => $tax_key
483 )
484 );
485
486 // Slug part to replace
487 $user_ids = array();
488
489 // Loop through terms and get the user ID
490 foreach ( $terms as $term ) {
491 $user_ids[] = str_replace( $user_key, '', $term->slug );
492 }
493
494 // Parse & return
495 return wp_parse_id_list( $user_ids );
496 }
497
498 /**
499 * Get the part of the query responsible for JOINing objects to relationships.
500 *
501 * @since 2.6.0 bbPress (r6737)
502 *
503 * @param array $args
504 * @param string $meta_key
505 * @param string $meta_type
506 *
507 * @return array
508 */
509 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
510 $tax_key = "{$meta_key}_{$meta_type}";
511 $user_key = "{$meta_key}_user_id_";
512
513 // Make sure the taxonomy is registered
514 $this->jit_taxonomy( $tax_key );
515
516 // Backwards compat for pre-2.6.0
517 if ( is_numeric( $args ) ) {
518 $args = array(
519 'tax_query' => array(
520 array(
521 'taxonomy' => $tax_key,
522 'terms' => $user_key . bbp_get_user_id( $args, false, false ),
523 'field' => 'slug'
524 )
525 )
526 );
527 }
528
529 // Default arguments
530 $defaults = array(
531 'tax_query' => array(
532 array(
533 'taxonomy' => $tax_key,
534 'terms' => $user_key . bbp_get_displayed_user_id(),
535 'field' => 'slug'
536 )
537 )
538 );
539
540 // Parse arguments
541 return bbp_parse_args( $args, $defaults, $context_key );
542 }
543 }
544
545 /**
546 * User strategy for interfacing with User Engagements
547 *
548 * This strategy largely exists for backwards compatibility with bbPress 2.5,
549 * or installations that have not upgraded their databases to 2.6 or above.
550 *
551 * Note: this strategy is going to be a bit less tidy than the others, because
552 * it needs to do weird things to maintain the 2.5 status-quo. Do not use this
553 * strategy as an example when building your own.
554 *
555 * @since 2.6.0 bbPress (r6844)
556 */
557 class BBP_User_Engagements_User extends BBP_User_Engagements_Base {
558
559 /**
560 * Type of strategy being used.
561 *
562 * @since 2.6.0 bbPress (r6844)
563 *
564 * @var string
565 */
566 public $type = 'user';
567
568 /**
569 * Private function to map 2.6 meta keys to 2.5 user-option keys.
570 *
571 * @since 2.6.0 bbPress (r6844)
572 *
573 * @param string $meta_key
574 * @param int $object_id
575 * @param bool $prefix
576 *
577 * @return string
578 */
579 private function get_user_option_key( $meta_key = '', $object_id = 0, $prefix = false ) {
580 switch ( $meta_key ) {
581
582 // Favorites
583 case '_bbp_favorite' :
584 $key = '_bbp_favorites';
585 break;
586
587 // Subscriptions
588 case '_bbp_subscription' :
589
590 // Maybe guess at post type
591 $post_type = ! empty( $object_id )
592 ? get_post_type( $object_id )
593 : bbp_get_topic_post_type();
594
595 // Forums & Topics used different keys :/
596 $key = ( bbp_get_forum_post_type() === $post_type )
597 ? '_bbp_forum_subscriptions'
598 : '_bbp_subscriptions';
599
600 break;
601
602 // Unknown, so pluralize
603 default :
604 $key = "{$meta_key}s";
605 break;
606 }
607
608 // Maybe prefix the key (for use in raw database queries)
609 if ( true === $prefix ) {
610 $key = bbp_db()->get_blog_prefix() . $key;
611 }
612
613 // Return the old (pluralized) user option key
614 return $key;
615 }
616
617 /**
618 * Private function to get a 2.5 compatible cache key.
619 *
620 * This method exists to provide backwards compatibility with bbPress 2.5,
621 * which had caching surrounding the FIND_IN_SET usermeta queries.
622 *
623 * @since 2.6.3 bbPress (r6991)
624 *
625 * @param string $meta_key
626 * @param int $object_id
627 *
628 * @return string
629 */
630 private function get_cache_key( $meta_key = '', $object_id = 0 ) {
631
632 // No negative numbers in cache keys (zero is weird, but not disallowed)
633 $object_id = absint( $object_id );
634
635 // Maybe guess at post type
636 $post_type = ! empty( $object_id )
637 ? get_post_type( $object_id )
638 : bbp_get_topic_post_type();
639
640 switch ( $meta_key ) {
641
642 // Favorites
643 case '_bbp_favorite' :
644 $key = 'bbp_get_topic_favoriters_';
645 break;
646
647 // Subscriptions
648 case '_bbp_subscription' :
649
650 // Forums & Topics used different keys :/
651 $key = ( bbp_get_forum_post_type() === $post_type )
652 ? 'bbp_get_forum_subscribers_'
653 : 'bbp_get_topic_subscribers_';
654
655 break;
656
657 // Unknown, so pluralize
658 default :
659 $nounize = rtrim( $meta_key, 'e' );
660 $key = "bbp_get_{$post_type}_{$nounize}ers_";
661 break;
662 }
663
664 // Return the old (pluralized) user option key with object ID appended
665 return "{$key}{$object_id}";
666 }
667
668 /**
669 * Get the user engagement cache for a given meta key and object ID.
670 *
671 * This method exists to provide backwards compatibility with bbPress 2.5,
672 * which had caching surrounding the FIND_IN_SET queries in usermeta.
673 *
674 * @since 2.6.3 bbPress (r6991)
675 *
676 * @param string $meta_key
677 * @param int $object_id
678 *
679 * @return mixed Results from cache get
680 */
681 private function cache_get( $meta_key = '', $object_id = 0 ) {
682 $cache_key = $this->get_cache_key( $meta_key, $object_id );
683
684 return wp_cache_get( $cache_key, 'bbpress_engagements' );
685 }
686
687 /**
688 * Set the user engagement cache for a given meta key and object ID.
689 *
690 * This method exists to provide backwards compatibility with bbPress 2.5,
691 * which had caching surrounding the FIND_IN_SET queries in usermeta.
692 *
693 * @since 2.6.3 bbPress (r6991)
694 *
695 * @param string $meta_key
696 * @param int $object_id
697 *
698 * @return mixed Results from cache set
699 */
700 private function cache_set( $meta_key = '', $object_id = 0, $user_ids = array() ) {
701 $cache_key = $this->get_cache_key( $meta_key, $object_id );
702 $user_ids = $this->parse_comma_list( $user_ids );
703
704 return wp_cache_set( $cache_key, $user_ids, 'bbpress_engagements' );
705 }
706
707 /**
708 * Delete the user engagement cache for a given meta key and object ID.
709 *
710 * This method exists to provide backwards compatibility with bbPress 2.5,
711 * which had caching surrounding the FIND_IN_SET queries in usermeta.
712 *
713 * @since 2.6.3 bbPress (r6991)
714 *
715 * @param string $meta_key
716 * @param int $object_id
717 *
718 * @return mixed Results from cache delete
719 */
720 private function cache_delete( $meta_key = '', $object_id = 0 ) {
721 $cache_key = $this->get_cache_key( $meta_key, $object_id );
722
723 return wp_cache_delete( $cache_key, 'bbpress_engagements' );
724 }
725
726 /**
727 * Turn a comma-separated string into an array of integers
728 *
729 * @since 2.6.0 bbPress (r6844)
730 *
731 * @param string $results
732 * @return array
733 */
734 private function parse_comma_list( $results = '' ) {
735 return array_filter( wp_parse_id_list( $results ) );
736 }
737
738 /**
739 * Add a user id to an object
740 *
741 * @since 2.6.0 bbPress (r6844)
742 *
743 * @param int $object_id The object id
744 * @param int $user_id The user id
745 * @param string $meta_key The relationship key
746 * @param string $meta_type The relationship type (usually 'post')
747 * @param bool $unique Whether meta key should be unique to the object
748 *
749 * @return bool Returns true on success, false on failure
750 */
751 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
752 $retval = false;
753 $option_key = $this->get_user_option_key( $meta_key, $object_id );
754 $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
755 $exists = array_search( $object_id, $object_ids );
756
757 // Not already added, so add it
758 if ( false === $exists ) {
759 $object_ids[] = $object_id;
760 $object_ids = implode( ',', $this->parse_comma_list( $object_ids ) );
761 $retval = update_user_option( $user_id, $option_key, $object_ids );
762
763 // Delete cache if successful (accounts for int & true)
764 if ( false !== $retval ) {
765 $this->cache_delete( $meta_key, $object_id );
766 }
767 }
768
769 // Return true if added, or false if not
770 return $retval;
771 }
772
773 /**
774 * Remove a user id from an object
775 *
776 * @since 2.6.0 bbPress (r6844)
777 *
778 * @param int $object_id The object id
779 * @param int $user_id The user id
780 * @param string $meta_key The relationship key
781 * @param string $meta_type The relationship type (usually 'post')
782 *
783 * @return bool Returns true on success, false on failure
784 */
785 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
786 $retval = false;
787 $option_key = $this->get_user_option_key( $meta_key, $object_id );
788 $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
789 $exists = array_search( $object_id, $object_ids );
790
791 // Exists, so remove it
792 if ( false !== $exists ) {
793 unset( $object_ids[ $exists ] );
794
795 $object_ids = implode( ',', $this->parse_comma_list( $object_ids ) );
796 $retval = ! empty( $object_ids )
797 ? update_user_option( $user_id, $option_key, $object_ids )
798 : delete_user_option( $user_id, $option_key );
799
800 // Delete cache if successful (accounts for int & true)
801 if ( false !== $retval ) {
802 $this->cache_delete( $meta_key, $object_id );
803 }
804 }
805
806 // Return true if removed, or false if not
807 return $retval;
808 }
809
810 /**
811 * Remove a user id from all objects
812 *
813 * @since 2.6.0 bbPress (r6844)
814 *
815 * @param int $user_id The user id
816 * @param string $meta_key The relationship key
817 * @param string $meta_type The relationship type (usually 'post')
818 *
819 * @return bool Returns true on success, false on failure
820 */
821 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
822
823 // Get the key
824 $option_key = $this->get_user_option_key( $meta_key );
825
826 // Get the option
827 $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
828
829 // Attempt to delete the user option
830 $retval = delete_user_option( $user_id, $option_key );
831
832 // Try to delete caches, but only if everything else succeeded
833 if ( ! empty( $retval ) && ! empty( $object_ids ) ) {
834 foreach ( $object_ids as $object_id ) {
835 $this->cache_delete( $meta_key, $object_id );
836 }
837 }
838
839 // Return true if user was removed, or false if not
840 return $retval;
841 }
842
843 /**
844 * Remove an object from all users
845 *
846 * @since 2.6.0 bbPress (r6844)
847 *
848 * @param int $object_id The object id
849 * @param int $user_id The user id
850 * @param string $meta_key The relationship key
851 * @param string $meta_type The relationship type (usually 'post')
852 *
853 * @return bool Returns true on success, false on failure
854 */
855 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
856
857 // Query for users
858 $user_ids = $this->get_users_for_object( $object_id, $meta_key, $meta_type );
859 $u_count = count( $user_ids );
860
861 // Count number of removals
862 $removed = array();
863 $r_count = 0;
864
865 // Users have engaged, so remove them
866 if ( ! empty( $u_count ) ) {
867
868 // Loop through users and remove them from the object
869 foreach ( $user_ids as $user_id ) {
870 $removed[] = $this->remove_user_from_object( $object_id, $user_id, $meta_key, $meta_type );
871 }
872
873 // Count the removed users
874 $r_count = count( $removed );
875 }
876
877 // Return true if successfully removed from all users
878 return ( $r_count === $u_count );
879 }
880
881 /**
882 * Remove all users from all objects
883 *
884 * @since 2.6.0 bbPress (r6844)
885 *
886 * @param string $meta_key The relationship key
887 * @param string $meta_type The relationship type (usually 'post')
888 *
889 * @return bool Returns true on success, false on failure
890 */
891 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
892
893 // Query for users
894 $option_key = $this->get_user_option_key( $meta_key, 0, true );
895 $bbp_db = bbp_db();
896 $user_ids = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}'" );
897 $u_count = count( $user_ids );
898
899 // Count number of removals
900 $removed = array();
901 $r_count = 0;
902
903 // Users have engaged, so remove them
904 if ( ! empty( $u_count ) ) {
905
906 // Loop through users and remove their user options
907 foreach ( $user_ids as $user_id ) {
908 $removed[] = $this->remove_user_from_all_objects( $user_id, $meta_key );
909 }
910
911 // Count the removed users
912 $r_count = count( $removed );
913 }
914
915 // Return true if successfully removed from all users
916 return ( $r_count === $u_count );
917 }
918
919 /**
920 * Get users of an object
921 *
922 * The database queries in this function were cached in bbPress versions
923 * older than 2.6, but no longer are to avoid cache pollution.
924 *
925 * @since 2.6.0 bbPress (r6844)
926 *
927 * @param int $object_id The object id
928 * @param string $meta_key The key used to index this relationship
929 * @param string $meta_type The type of meta to look in
930 *
931 * @return array Returns ids of users
932 */
933 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
934
935 // Try to get user IDs from cache
936 $user_ids = $this->cache_get( $meta_key, $object_id );
937
938 // Cache is empty, so hit the database
939 if ( false === $user_ids ) {
940 $option_key = $this->get_user_option_key( $meta_key, $object_id, true );
941 $bbp_db = bbp_db();
942 $user_ids = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}' and FIND_IN_SET('{$object_id}', meta_value) > 0" );
943
944 // Always cache results (even if empty, to prevent multiple misses)
945 $this->cache_set( $meta_key, $object_id, $user_ids );
946 }
947
948 // Return parsed IDs
949 return $this->parse_comma_list( $user_ids );
950 }
951
952 /**
953 * Get the part of the query responsible for JOINing objects to relationships.
954 *
955 * @since 2.6.0 bbPress (r6844)
956 *
957 * @param array $args
958 * @param string $meta_key
959 * @param string $meta_type
960 *
961 * @return array
962 */
963 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
964 $user_id = bbp_get_user_id( $args, true, true );
965 $option_key = $this->get_user_option_key( $meta_key );
966 $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
967
968 // Maybe trick WP_Query into ".ID IN (0)" to return no results
969 if ( empty( $object_ids ) ) {
970 $object_ids = array( 0 );
971 }
972
973 // Maybe include these post IDs
974 $args = array(
975 'post__in' => $object_ids
976 );
977
978 // Parse arguments
979 return bbp_parse_args( $args, array(), $context_key );
980 }
981 }
982