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


Generated: Thu Jul 18 01:01:42 2019 Cross-referenced by PHPXref 0.7.1