[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/common/ -> engagements.php (source)

   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  
  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  
  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  
  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  
 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  
 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  
 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  
 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( array(
 282                      'key'     => $meta_key,
 283                      'value'   => bbp_get_user_id( $args, false, false ),
 284                      'compare' => 'NUMERIC'
 285                  ) )
 286              );
 287          }
 288  
 289          // Default arguments
 290          $defaults = array(
 291              'meta_query' => array( array(
 292                  'key'     => $meta_key,
 293                  'value'   => bbp_get_displayed_user_id(),
 294                  'compare' => 'NUMERIC'
 295              ) )
 296          );
 297  
 298          // Parse arguments
 299          return bbp_parse_args( $args, $defaults, $context_key );
 300      }
 301  }
 302  
 303  /**
 304   * Term strategy for interfacing with User Engagements
 305   *
 306   * @since 2.6.0 bbPress (r6737)
 307   */
 308  class BBP_User_Engagements_Term extends BBP_User_Engagements_Base {
 309  
 310      /**
 311       * Type of strategy being used.
 312       *
 313       * @since 2.6.0 bbPress (r6737)
 314       *
 315       * @var string
 316       */
 317      public $type = 'term';
 318  
 319      /**
 320       * Register an engagement taxonomy just-in-time for immediate use
 321       *
 322       * @since 2.6.0 bbPress (r6737)
 323       *
 324       * @param string $tax_key
 325       * @param string $object_type
 326       */
 327  	private function jit_taxonomy( $tax_key = '', $object_type = 'user' ) {
 328  
 329          // Bail if taxonomy already exists
 330          if ( taxonomy_exists( $tax_key ) ) {
 331              return;
 332          }
 333  
 334          // Register the taxonomy
 335          register_taxonomy( $tax_key, 'bbp_' . $object_type, array(
 336              'labels'                => array(),
 337              'description'           => '',
 338              'public'                => false,
 339              'publicly_queryable'    => false,
 340              'hierarchical'          => false,
 341              'show_ui'               => false,
 342              'show_in_menu'          => false,
 343              'show_in_nav_menus'     => false,
 344              'show_tagcloud'         => false,
 345              'show_in_quick_edit'    => false,
 346              'show_admin_column'     => false,
 347              'meta_box_cb'           => false,
 348              'capabilities'          => array(),
 349              'rewrite'               => false,
 350              'query_var'             => '',
 351              'update_count_callback' => '',
 352              'show_in_rest'          => false,
 353              'rest_base'             => false,
 354              'rest_controller_class' => false,
 355              '_builtin'              => false
 356          ) );
 357      }
 358  
 359      /**
 360       * Add a user id to an object
 361       *
 362       * @since 2.6.0 bbPress (r6737)
 363       *
 364       * @param int    $object_id The object id
 365       * @param int    $user_id   The user id
 366       * @param string $meta_key  The relationship key
 367       * @param string $meta_type The relationship type (usually 'post')
 368       * @param bool   $unique    Whether meta key should be unique to the object
 369       *
 370       * @return bool Returns true on success, false on failure
 371       */
 372  	public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
 373          $user_key = "{$meta_key}_user_id_{$user_id}";
 374          $tax_key  = "{$meta_key}_{$meta_type}";
 375          $this->jit_taxonomy( $tax_key );
 376  
 377          return wp_add_object_terms( $object_id, $user_key, $tax_key );
 378      }
 379  
 380      /**
 381       * Remove a user id from an object
 382       *
 383       * @since 2.6.0 bbPress (r6737)
 384       *
 385       * @param int    $object_id The object id
 386       * @param int    $user_id   The user id
 387       * @param string $meta_key  The relationship key
 388       * @param string $meta_type The relationship type (usually 'post')
 389       *
 390       * @return bool Returns true on success, false on failure
 391       */
 392  	public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
 393          $user_key = "{$meta_key}_user_id_{$user_id}";
 394          $tax_key  = "{$meta_key}_{$meta_type}";
 395          $this->jit_taxonomy( $tax_key );
 396  
 397          return wp_remove_object_terms( $object_id, $user_key, $tax_key );
 398      }
 399  
 400      /**
 401       * Remove a user id from all objects
 402       *
 403       * @since 2.6.0 bbPress (r6737)
 404       *
 405       * @param int    $user_id   The user id
 406       * @param string $meta_key  The relationship key
 407       * @param string $meta_type The relationship type (usually 'post')
 408       *
 409       * @return bool Returns true on success, false on failure
 410       */
 411  	public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
 412          $user_key = "{$meta_key}_user_id_{$user_id}";
 413          $tax_key  = "{$meta_key}_{$meta_type}";
 414          $this->jit_taxonomy( $tax_key );
 415          $term     = get_term_by( 'slug', $user_key, $tax_key );
 416  
 417          return wp_delete_term( $term->term_id, $tax_key );
 418      }
 419  
 420      /**
 421       * Remove an object from all users
 422       *
 423       * @since 2.6.0 bbPress (r6737)
 424       *
 425       * @param int    $object_id The object id
 426       * @param int    $user_id   The user id
 427       * @param string $meta_key  The relationship key
 428       * @param string $meta_type The relationship type (usually 'post')
 429       *
 430       * @return bool Returns true on success, false on failure
 431       */
 432  	public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
 433          return wp_delete_object_term_relationships( $object_id, get_object_taxonomies( 'bbp_user' ) );
 434      }
 435  
 436      /**
 437       * Remove all users from all objects
 438       *
 439       * @since 2.6.0 bbPress (r6737)
 440       *
 441       * @param string $meta_key  The relationship key
 442       * @param string $meta_type The relationship type (usually 'post')
 443       *
 444       * @return bool Returns true on success, false on failure
 445       */
 446  	public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
 447          // TODO
 448      }
 449  
 450      /**
 451       * Get users of an object
 452       *
 453       * @since 2.6.0 bbPress (r6737)
 454       *
 455       * @param int    $object_id The object id
 456       * @param string $meta_key  The key used to index this relationship
 457       * @param string $meta_type The type of meta to look in
 458       *
 459       * @return array Returns ids of users
 460       */
 461  	public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
 462          $user_key = "{$meta_key}_user_id_";
 463          $tax_key  = "{$meta_key}_{$meta_type}";
 464          $this->jit_taxonomy( $tax_key );
 465  
 466          // Get terms
 467          $terms = get_terms( array(
 468              'object_ids' => $object_id,
 469              'taxonomy'   => $tax_key
 470          ) );
 471  
 472          // Slug part to replace
 473          $user_ids = array();
 474  
 475          // Loop through terms and get the user ID
 476          foreach ( $terms as $term ) {
 477              $user_ids[] = str_replace( $user_key, '', $term->slug );
 478          }
 479  
 480          // Parse & return
 481          return wp_parse_id_list( $user_ids );
 482      }
 483  
 484      /**
 485       * Get the part of the query responsible for JOINing objects to relationships.
 486       *
 487       * @since 2.6.0 bbPress (r6737)
 488       *
 489       * @param array  $args
 490       * @param string $meta_key
 491       * @param string $meta_type
 492       *
 493       * @return array
 494       */
 495  	public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
 496          $tax_key  = "{$meta_key}_{$meta_type}";
 497          $user_key = "{$meta_key}_user_id_";
 498  
 499          // Make sure the taxonomy is registered
 500          $this->jit_taxonomy( $tax_key );
 501  
 502          // Backwards compat for pre-2.6.0
 503          if ( is_numeric( $args ) ) {
 504              $args = array(
 505                  'tax_query' => array( array(
 506                      'taxonomy' => $tax_key,
 507                      'terms'    => $user_key . bbp_get_user_id( $args, false, false ),
 508                      'field'    => 'slug'
 509                  ) )
 510              );
 511          }
 512  
 513          // Default arguments
 514          $defaults = array(
 515              'tax_query' => array( array(
 516                  'taxonomy' => $tax_key,
 517                  'terms'    => $user_key . bbp_get_displayed_user_id(),
 518                  'field'    => 'slug'
 519              ) )
 520          );
 521  
 522          // Parse arguments
 523          return bbp_parse_args( $args, $defaults, $context_key );
 524      }
 525  }
 526  
 527  /**
 528   * User strategy for interfacing with User Engagements
 529   *
 530   * @since 2.6.0 bbPress (r6844)
 531   */
 532  class BBP_User_Engagements_User extends BBP_User_Engagements_Base {
 533  
 534      /**
 535       * Type of strategy being used.
 536       *
 537       * @since 2.6.0 bbPress (r6844)
 538       *
 539       * @var string
 540       */
 541      public $type = 'user';
 542  
 543      /**
 544       * Private function to map 2.6 meta keys to 2.5 user-option keys.
 545       *
 546       * @since 2.6.0 bbPress (r6844)
 547       *
 548       * @param string $meta_key
 549       * @param int    $object_id
 550       * @param bool   $prefix
 551       *
 552       * @return string
 553       */
 554  	private function get_user_option_key( $meta_key = '', $object_id = 0, $prefix = false ) {
 555          switch ( $meta_key ) {
 556  
 557              // Favorites
 558              case '_bbp_favorite' :
 559                  $key = '_bbp_favorites';
 560                  break;
 561  
 562              // Subscriptions
 563              case '_bbp_subscription' :
 564  
 565                  // Maybe guess at post type
 566                  $post_type = ! empty( $object_id )
 567                      ? get_post_type( $object_id )
 568                      : bbp_get_topic_post_type();
 569  
 570                  // Forums & Topics used different keys :/
 571                  $key       = ( bbp_get_forum_post_type() === $post_type )
 572                      ? '_bbp_forum_subscriptions'
 573                      : '_bbp_subscriptions';
 574  
 575                  break;
 576  
 577              // Unknown, so pluralize
 578              default :
 579                  $key = "{$meta_key}s";
 580                  break;
 581          }
 582  
 583          // Maybe prefix the key (for use in raw database queries)
 584          if ( true === $prefix ) {
 585              $key = bbp_db()->get_blog_prefix() . $key;
 586          }
 587  
 588          // Return the old (pluralized) user option key
 589          return $key;
 590      }
 591  
 592      /**
 593       * Turn a comma-separated string into an array of integers
 594       *
 595       * @since 2.6.0 bbPress (r6844)
 596       *
 597       * @param string $results
 598       * @return array
 599       */
 600  	private function parse_comma_list( $results = '' ) {
 601          return array_filter( wp_parse_id_list( $results ) );
 602      }
 603  
 604      /**
 605       * Add a user id to an object
 606       *
 607       * @since 2.6.0 bbPress (r6844)
 608       *
 609       * @param int    $object_id The object id
 610       * @param int    $user_id   The user id
 611       * @param string $meta_key  The relationship key
 612       * @param string $meta_type The relationship type (usually 'post')
 613       * @param bool   $unique    Whether meta key should be unique to the object
 614       *
 615       * @return bool Returns true on success, false on failure
 616       */
 617  	public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
 618          $retval     = false;
 619          $option_key = $this->get_user_option_key( $meta_key, $object_id );
 620          $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
 621          $exists     = array_search( $object_id, $object_ids );
 622  
 623          // Not already added, so add it
 624          if ( false === $exists ) {
 625              $object_ids[] = $object_id;
 626              $object_ids   = implode( ',', $this->parse_comma_list( $object_ids ) );
 627              $retval       = update_user_option( $user_id, $option_key, $object_ids );
 628          }
 629  
 630          // Return true if added, or false if not
 631          return $retval;
 632      }
 633  
 634      /**
 635       * Remove a user id from an object
 636       *
 637       * @since 2.6.0 bbPress (r6844)
 638       *
 639       * @param int    $object_id The object id
 640       * @param int    $user_id   The user id
 641       * @param string $meta_key  The relationship key
 642       * @param string $meta_type The relationship type (usually 'post')
 643       *
 644       * @return bool Returns true on success, false on failure
 645       */
 646  	public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
 647          $retval     = false;
 648          $option_key = $this->get_user_option_key( $meta_key, $object_id );
 649          $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
 650          $exists     = array_search( $object_id, $object_ids );
 651  
 652          // Exists, so remove it
 653          if ( false !== $exists ) {
 654              unset( $object_ids[ $exists ] );
 655  
 656              $object_ids = implode( ',', $this->parse_comma_list( $object_ids ) );
 657              $retval     = ! empty( $object_ids )
 658                  ? update_user_option( $user_id, $option_key, $object_ids )
 659                  : delete_user_option( $user_id, $option_key );
 660          }
 661  
 662          // Return true if removed, or false if not
 663          return $retval;
 664      }
 665  
 666      /**
 667       * Remove a user id from all objects
 668       *
 669       * @since 2.6.0 bbPress (r6844)
 670       *
 671       * @param int    $user_id   The user id
 672       * @param string $meta_key  The relationship key
 673       * @param string $meta_type The relationship type (usually 'post')
 674       *
 675       * @return bool Returns true on success, false on failure
 676       */
 677  	public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
 678          $option_key = $this->get_user_option_key( $meta_key );
 679          return delete_user_option( $user_id, $option_key );
 680      }
 681  
 682      /**
 683       * Remove an object from all users
 684       *
 685       * @since 2.6.0 bbPress (r6844)
 686       *
 687       * @param int    $object_id The object id
 688       * @param int    $user_id   The user id
 689       * @param string $meta_key  The relationship key
 690       * @param string $meta_type The relationship type (usually 'post')
 691       *
 692       * @return bool Returns true on success, false on failure
 693       */
 694  	public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
 695  
 696          // Query for users
 697          $user_ids = $this->get_users_for_object( $object_id, $meta_key, $meta_type );
 698          $u_count  = count( $user_ids );
 699  
 700          // Count number of removals
 701          $removed  = array();
 702          $r_count  = 0;
 703  
 704          // Users have engaged, so remove them
 705          if ( ! empty( $u_count ) ) {
 706  
 707              // Loop through users and remove them from the object
 708              foreach ( $user_ids as $user_id ) {
 709                  $removed[] = $this->remove_user_from_object( $object_id, $user_id, $meta_key, $meta_type );
 710              }
 711  
 712              // Count the removed users
 713              $r_count = count( $removed );
 714          }
 715  
 716          // Return true if successfully removed from all users
 717          return ( $r_count === $u_count );
 718      }
 719  
 720      /**
 721       * Remove all users from all objects
 722       *
 723       * @since 2.6.0 bbPress (r6844)
 724       *
 725       * @param string $meta_key  The relationship key
 726       * @param string $meta_type The relationship type (usually 'post')
 727       *
 728       * @return bool Returns true on success, false on failure
 729       */
 730  	public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {
 731  
 732          // Query for users
 733          $option_key = $this->get_user_option_key( $meta_key, 0, true );
 734          $bbp_db     = bbp_db();
 735          $user_ids   = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}'" );
 736          $u_count    = count( $user_ids );
 737  
 738          // Count number of removals
 739          $removed    = array();
 740          $r_count    = 0;
 741  
 742          // Users have engaged, so remove them
 743          if ( ! empty( $u_count ) ) {
 744  
 745              // Loop through users and remove their user options
 746              foreach ( $user_ids as $user_id ) {
 747                  $removed[] = delete_user_option( $user_id, $option_key );
 748              }
 749  
 750              // Count the removed users
 751              $r_count = count( $removed );
 752          }
 753  
 754          // Return true if successfully removed from all users
 755          return ( $r_count === $u_count );
 756      }
 757  
 758      /**
 759       * Get users of an object
 760       *
 761       * The database queries in this function were cached in bbPress versions
 762       * older than 2.6, but no longer are to avoid cache pollution.
 763       *
 764       * @since 2.6.0 bbPress (r6844)
 765       *
 766       * @param int    $object_id The object id
 767       * @param string $meta_key  The key used to index this relationship
 768       * @param string $meta_type The type of meta to look in
 769       *
 770       * @return array Returns ids of users
 771       */
 772  	public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
 773          $option_key = $this->get_user_option_key( $meta_key, $object_id, true );
 774          $bbp_db     = bbp_db();
 775          $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" );
 776  
 777          return wp_parse_id_list( $user_ids );
 778      }
 779  
 780      /**
 781       * Get the part of the query responsible for JOINing objects to relationships.
 782       *
 783       * @since 2.6.0 bbPress (r6844)
 784       *
 785       * @param array  $args
 786       * @param string $meta_key
 787       * @param string $meta_type
 788       *
 789       * @return array
 790       */
 791  	public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {
 792          $user_id    = bbp_get_user_id( $args, true, true );
 793          $option_key = $this->get_user_option_key( $meta_key );
 794          $object_ids = $this->parse_comma_list( get_user_option( $option_key, $user_id ) );
 795  
 796          // Maybe include these post IDs
 797          if ( ! empty( $object_ids ) ) {
 798              $args = array(
 799                  'post__in' => $object_ids
 800              );
 801          }
 802  
 803          // Parse arguments
 804          return bbp_parse_args( $args, array(), $context_key );
 805      }
 806  }


Generated: Mon Jul 22 01:01:32 2019 Cross-referenced by PHPXref 0.7.1