[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-templates/bp-nouveau/includes/groups/ -> classes.php (source)

   1  <?php
   2  /**
   3   * Groups classes
   4   *
   5   * @since 3.0.0
   6   * @version 3.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * Query to get members that are not already members of the group
  14   *
  15   * @since 3.0.0
  16   */
  17  class BP_Nouveau_Group_Invite_Query extends BP_User_Query {
  18      /**
  19       * Array of group member ids, cached to prevent redundant lookups
  20       *
  21       * @var null|array Null if not yet defined, otherwise an array of ints
  22       * @since 3.0.0
  23       */
  24      protected $group_member_ids;
  25  
  26      /**
  27       * Set up action hooks
  28       *
  29       * @since 3.0.0
  30       */
  31  	public function setup_hooks() {
  32          add_action( 'bp_pre_user_query_construct', array( $this, 'build_exclude_args' ) );
  33          add_action( 'bp_pre_user_query', array( $this, 'build_meta_query' ) );
  34      }
  35  
  36      /**
  37       * Exclude group members from the user query as it's not needed to invite members to join the group.
  38       *
  39       * @since 3.0.0
  40       */
  41  	public function build_exclude_args() {
  42          $this->query_vars = bp_parse_args(
  43              $this->query_vars,
  44              array(
  45                  'group_id'     => 0,
  46                  'is_confirmed' => true,
  47              ),
  48              'nouveau_group_invite_query_exlude_args'
  49          );
  50  
  51          $group_member_ids = $this->get_group_member_ids();
  52  
  53          /**
  54           * We want to exclude users who are already members or who have been
  55           * invited by **any** of the group members to join it.
  56           */
  57          $type = 'exclude';
  58  
  59          // We want to get the invited users who did not confirmed yet.
  60          if ( false === $this->query_vars['is_confirmed'] ) {
  61              $type = 'include';
  62          }
  63  
  64          if ( ! empty( $group_member_ids ) ) {
  65              $this->query_vars[ $type ] = $group_member_ids;
  66          }
  67      }
  68  
  69      /**
  70       * Get the members of the queried group
  71       *
  72       * @since 3.0.0
  73       *
  74       * @return array $ids User IDs of relevant group member ids
  75       */
  76  	protected function get_group_member_ids() {
  77          global $wpdb;
  78  
  79          if ( is_array( $this->group_member_ids ) ) {
  80              return $this->group_member_ids;
  81          }
  82  
  83          // Fetch **all** invited users.
  84          $pending_invites = groups_get_invites( array(
  85              'item_id'     => $this->query_vars['group_id'],
  86              'invite_sent' => 'sent',
  87              'fields'      => 'user_ids'
  88          ) );
  89  
  90          // This is a clue that we only want the invitations.
  91          if ( false === $this->query_vars['is_confirmed'] ) {
  92              return $pending_invites;
  93          }
  94  
  95          /**
  96           * Otherwise, we want group members _and_ users with outstanding invitations,
  97           * because we're doing an "exclude" query.
  98           */
  99          $bp  = buddypress();
 100          $sql = array(
 101              'select'  => "SELECT user_id FROM {$bp->groups->table_name_members}",
 102              'where'   => array(),
 103              'orderby' => '',
 104              'order'   => '',
 105              'limit'   => '',
 106          );
 107  
 108          /** WHERE clauses *****************************************************/
 109  
 110          // Group id
 111          $sql['where'][] = $wpdb->prepare( 'group_id = %d', $this->query_vars['group_id'] );
 112  
 113          // Join the query part
 114          $sql['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : '';
 115  
 116          /** ORDER BY clause ***************************************************/
 117          $sql['orderby'] = 'ORDER BY date_modified';
 118          $sql['order']   = 'DESC';
 119  
 120          /** LIMIT clause ******************************************************/
 121          $this->group_member_ids = $wpdb->get_col( "{$sql['select']} {$sql['where']} {$sql['orderby']} {$sql['order']} {$sql['limit']}" );
 122  
 123          return array_merge( $this->group_member_ids, $pending_invites );
 124      }
 125  
 126      /**
 127       * @since 3.0.0
 128       */
 129  	public function build_meta_query( BP_User_Query $bp_user_query ) {
 130          if ( isset( $this->query_vars['scope'] ) && 'members' === $this->query_vars['scope'] && isset( $this->query_vars['meta_query'] ) ) {
 131  
 132              $invites_meta_query = new WP_Meta_Query( $this->query_vars['meta_query'] );
 133              $meta_sql           = $invites_meta_query->get_sql( 'user', 'u', 'ID' );
 134  
 135              if ( empty( $meta_sql['join'] ) || empty( $meta_sql['where'] ) ) {
 136                  return;
 137              }
 138  
 139              $bp_user_query->uid_clauses['select'] .= ' ' . $meta_sql['join'];
 140              $bp_user_query->uid_clauses['where']  .= ' ' . $meta_sql['where'];
 141          }
 142      }
 143  
 144      /**
 145       * @since 3.0.0
 146       */
 147  	public static function get_inviter_ids( $user_id = 0, $group_id = 0 ) {
 148          global $wpdb;
 149  
 150          if ( empty( $group_id ) || empty( $user_id ) ) {
 151              return array();
 152          }
 153  
 154          return groups_get_invites( array(
 155              'user_id'     => $user_id,
 156              'item_id'     => $group_id,
 157              'invite_sent' => 'sent',
 158              'fields'      => 'inviter_ids'
 159          ) );
 160      }
 161  }
 162  
 163  /**
 164   * A specific Group Nav class to make it possible to set new positions for
 165   * buddypress()->groups->nav.
 166   *
 167   * @since 3.0.0
 168   */
 169  class BP_Nouveau_Customizer_Group_Nav extends BP_Core_Nav {
 170      /**
 171       * Constructor
 172       *
 173       * @param int $object_id Optional. The random group ID used to generate the nav.
 174       */
 175  	public function __construct( $object_id = 0 ) {
 176          $error = new WP_Error( 'missing_parameter' );
 177  
 178          if ( empty( $object_id ) || ! bp_current_user_can( 'bp_moderate' ) || ! did_action( 'admin_init' ) ) {
 179              return $error;
 180          }
 181  
 182          $group = groups_get_group( array( 'group_id' => $object_id ) );
 183          if ( empty( $group->id ) ) {
 184              return $error;
 185          }
 186  
 187          $this->group = $group;
 188  
 189          parent::__construct( $group->id );
 190          $this->setup_nav();
 191      }
 192  
 193      /**
 194       * Checks whether a property is set.
 195       *
 196       * Overrides BP_Core_Nav::__isset() to avoid looking into its nav property.
 197       *
 198       * @since 3.0.0
 199       *
 200       * @param string $key The property.
 201       *
 202       * @return bool True if the property is set, false otherwise.
 203       */
 204  	public function __isset( $key ) {
 205          return isset( $this->{$key} );
 206      }
 207  
 208      /**
 209       * Gets a property.
 210       *
 211       * Overrides BP_Core_Nav::__isset() to avoid looking into its nav property.
 212       *
 213       * @since 3.0.0
 214       *
 215       * @param string $key The property.
 216       *
 217       * @return mixed The value corresponding to the property.
 218       */
 219  	public function __get( $key ) {
 220          if ( ! isset( $this->{$key} ) ) {
 221              $this->{$key} = null;
 222          }
 223  
 224          return $this->{$key};
 225      }
 226  
 227      /**
 228       * Sets a property.
 229       *
 230       * Overrides BP_Core_Nav::__isset() to avoid adding a value to its nav property.
 231       *
 232       * @since 3.0.0
 233       *
 234       * @param string $key The property.
 235       *
 236       * @param mixed $value The value of the property.
 237       */
 238  	public function __set( $key, $value ) {
 239          $this->{$key} = $value;
 240      }
 241  
 242      /**
 243       * Setup a temporary nav with only the needed parameters.
 244       *
 245       * @since 3.0.0
 246       */
 247  	protected function setup_nav() {
 248          $nav_items = array(
 249              'root'    => array(
 250                  'name'                => __( 'Memberships', 'buddypress' ),
 251                  'slug'                => $this->group->slug,
 252                  'position'            => -1,
 253                  /** This filter is documented in bp-groups/classes/class-bp-groups-component.php. */
 254                  'default_subnav_slug' => apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' ),
 255              ),
 256              'home'    => array(
 257                  'name'        => _x( 'Home', 'Group screen navigation title', 'buddypress' ),
 258                  'slug'        => 'home',
 259                  'parent_slug' => $this->group->slug,
 260                  'position'    => 10,
 261              ),
 262              'invites' => array(
 263                  'name'        => _x( 'Invite', 'My Group screen nav', 'buddypress' ),
 264                  'slug'        => 'send-invites',
 265                  'parent_slug' => $this->group->slug,
 266                  'position'    => 70,
 267              ),
 268              'manage'  => array(
 269                  'name'        => _x( 'Manage', 'My Group screen nav', 'buddypress' ),
 270                  'slug'        => 'admin',
 271                  'parent_slug' => $this->group->slug,
 272                  'position'    => 1000,
 273              ),
 274          );
 275  
 276          // Make sure only global front.php will be checked.
 277          add_filter( '_bp_nouveau_group_reset_front_template', array( $this, 'all_groups_fronts' ), 10, 1 );
 278  
 279          $front_template = bp_groups_get_front_template( $this->group );
 280  
 281          remove_filter( '_bp_nouveau_group_reset_front_template', array( $this, 'all_groups_fronts' ), 10, 1 );
 282  
 283          if ( ! $front_template ) {
 284              if ( bp_is_active( 'activity' ) ) {
 285                  $nav_items['home']['name'] = _x( 'Home (Activity)', 'Group screen navigation title', 'buddypress' );
 286              } else {
 287                  $nav_items['home']['name'] = _x( 'Home (Members)', 'Group screen navigation title', 'buddypress' );
 288              }
 289          } else {
 290              if ( bp_is_active( 'activity' ) ) {
 291                  $nav_items['activity'] = array(
 292                      'name'        => _x( 'Activity', 'My Group screen nav', 'buddypress' ),
 293                      'slug'        => 'activity',
 294                      'parent_slug' => $this->group->slug,
 295                      'position'    => 11,
 296                  );
 297              }
 298  
 299              // Add the members one
 300              $nav_items['members'] = array(
 301                  'name'        => _x( 'Members', 'My Group screen nav', 'buddypress' ),
 302                  'slug'        => 'members',
 303                  'parent_slug' => $this->group->slug,
 304                  'position'    => 60,
 305              );
 306          }
 307  
 308          // Required params
 309          $required_params = array(
 310              'slug'              => true,
 311              'name'              => true,
 312              'nav_item_position' => true,
 313          );
 314  
 315          // Now find nav items plugins are creating within their Group extensions!
 316          foreach ( get_declared_classes() as $class ) {
 317              if ( is_subclass_of( $class, 'BP_Group_Extension' ) ) {
 318                  $extension = new $class;
 319  
 320                  if ( ! empty( $extension->params ) && ! array_diff_key( $required_params, $extension->params ) ) {
 321                      $nav_items[ $extension->params['slug'] ] = array(
 322                          'name'        => $extension->params['name'],
 323                          'slug'        => $extension->params['slug'],
 324                          'parent_slug' => $this->group->slug,
 325                          'position'    => $extension->params['nav_item_position'],
 326                      );
 327                  }
 328              }
 329          }
 330  
 331          // Now we got all, create the temporary nav.
 332          foreach ( $nav_items as $nav_item ) {
 333              $this->add_nav( $nav_item );
 334          }
 335      }
 336  
 337      /**
 338       * Front template: do not look into group's template hierarchy.
 339       *
 340       * @since 3.0.0
 341       *
 342       * @param array $templates The list of possible group front templates.
 343       *
 344       * @return array The list of "global" group front templates.
 345       */
 346  	public function all_groups_fronts( $templates = array() ) {
 347          return array_intersect( array(
 348              'groups/single/front.php',
 349              'groups/single/default-front.php',
 350          ), $templates );
 351      }
 352  
 353      /**
 354       * Get the original order for the group navigation.
 355       *
 356       * @since 3.0.0
 357       *
 358       * @return array a list of nav items slugs ordered.
 359       */
 360  	public function get_default_value() {
 361          $default_nav = $this->get_secondary( array( 'parent_slug' => $this->group->slug ) );
 362          return wp_list_pluck( $default_nav, 'slug' );
 363      }
 364  
 365      /**
 366       * Get the list of nav items ordered according to the Site owner preferences.
 367       *
 368       * @since 3.0.0
 369       *
 370       * @return array the nav items ordered.
 371       */
 372  	public function get_group_nav() {
 373          // Eventually reset the order
 374          bp_nouveau_set_nav_item_order( $this, bp_nouveau_get_appearance_settings( 'group_nav_order' ), $this->group->slug );
 375  
 376          return $this->get_secondary( array( 'parent_slug' => $this->group->slug ) );
 377      }
 378  }


Generated: Sat Sep 21 01:01:46 2019 Cross-referenced by PHPXref 0.7.1