[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Groups classes 4 * 5 * @since 3.0.0 6 * @version 7.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 'manage' => array( 263 'name' => _x( 'Manage', 'My Group screen nav', 'buddypress' ), 264 'slug' => 'admin', 265 'parent_slug' => $this->group->slug, 266 'position' => 1000, 267 ), 268 ); 269 270 if ( bp_is_active( 'groups', 'invitations' ) ) { 271 $nav_items['invites'] = array( 272 'name' => _x( 'Invite', 'My Group screen nav', 'buddypress' ), 273 'slug' => 'send-invites', 274 'parent_slug' => $this->group->slug, 275 'position' => 70, 276 ); 277 } 278 279 // Make sure only global front.php will be checked. 280 add_filter( '_bp_nouveau_group_reset_front_template', array( $this, 'all_groups_fronts' ), 10, 1 ); 281 282 $front_template = bp_groups_get_front_template( $this->group ); 283 284 remove_filter( '_bp_nouveau_group_reset_front_template', array( $this, 'all_groups_fronts' ), 10, 1 ); 285 286 $members_nav = array( 287 'name' => _x( 'Members', 'My Group screen nav', 'buddypress' ), 288 'slug' => 'members', 289 'parent_slug' => $this->group->slug, 290 'position' => 60, 291 ); 292 293 if ( ! $front_template ) { 294 if ( bp_is_active( 'activity' ) ) { 295 $nav_items['home']['name'] = _x( 'Home (Activity)', 'Group screen navigation title', 'buddypress' ); 296 297 // Add the members nav. 298 $nav_items['members'] = $members_nav; 299 } else { 300 $nav_items['home']['name'] = _x( 'Home (Members)', 'Group screen navigation title', 'buddypress' ); 301 } 302 } else { 303 if ( bp_is_active( 'activity' ) ) { 304 $nav_items['activity'] = array( 305 'name' => _x( 'Activity', 'My Group screen nav', 'buddypress' ), 306 'slug' => 'activity', 307 'parent_slug' => $this->group->slug, 308 'position' => 11, 309 ); 310 } 311 312 // Add the members nav. 313 $nav_items['members'] = $members_nav; 314 } 315 316 // Required params 317 $required_params = array( 318 'slug' => true, 319 'name' => true, 320 'nav_item_position' => true, 321 ); 322 323 // Now find nav items plugins are creating within their Group extensions! 324 foreach ( get_declared_classes() as $class ) { 325 if ( is_subclass_of( $class, 'BP_Group_Extension' ) ) { 326 $extension = new $class; 327 328 if ( ! empty( $extension->params ) && ! array_diff_key( $required_params, $extension->params ) ) { 329 $nav_items[ $extension->params['slug'] ] = array( 330 'name' => $extension->params['name'], 331 'slug' => $extension->params['slug'], 332 'parent_slug' => $this->group->slug, 333 'position' => $extension->params['nav_item_position'], 334 ); 335 } 336 } 337 } 338 339 // Now we got all, create the temporary nav. 340 foreach ( $nav_items as $nav_item ) { 341 $this->add_nav( $nav_item ); 342 } 343 } 344 345 /** 346 * Front template: do not look into group's template hierarchy. 347 * 348 * @since 3.0.0 349 * 350 * @param array $templates The list of possible group front templates. 351 * 352 * @return array The list of "global" group front templates. 353 */ 354 public function all_groups_fronts( $templates = array() ) { 355 return array_intersect( array( 356 'groups/single/front.php', 357 'groups/single/default-front.php', 358 ), $templates ); 359 } 360 361 /** 362 * Get the original order for the group navigation. 363 * 364 * @since 3.0.0 365 * 366 * @return array a list of nav items slugs ordered. 367 */ 368 public function get_default_value() { 369 $default_nav = $this->get_secondary( array( 'parent_slug' => $this->group->slug ) ); 370 return wp_list_pluck( $default_nav, 'slug' ); 371 } 372 373 /** 374 * Get the list of nav items ordered according to the Site owner preferences. 375 * 376 * @since 3.0.0 377 * 378 * @return array the nav items ordered. 379 */ 380 public function get_group_nav() { 381 // Eventually reset the order 382 bp_nouveau_set_nav_item_order( $this, bp_nouveau_get_appearance_settings( 'group_nav_order' ), $this->group->slug ); 383 384 return $this->get_secondary( array( 'parent_slug' => $this->group->slug ) ); 385 } 386 } 387 388 /** 389 * Group template meta backwards compatibility class. 390 * 391 * @since 7.0.0 392 */ 393 class BP_Nouveau_Group_Meta { 394 /** 395 * Used to get the template meta used in Groups loop. 396 * 397 * @since 7.0.0 398 * @var string $meta The template meta used in Groups loop. 399 */ 400 public $meta = ''; 401 402 /** 403 * Magic getter. 404 * 405 * This exists specifically for supporting deprecated object vars. 406 * 407 * @since 7.0.0 408 * 409 * @param string $key 410 * @return string 411 */ 412 public function __get( $key = '' ) { 413 /* translators: %s is the name of the function to use instead of the deprecated one */ 414 _doing_it_wrong( 'bp_nouveau_group_meta', sprintf( __( 'Please use %s instead', 'buddypress' ), 'bp_nouveau_the_group_meta( array( \'keys\' => \'' . $key . '\' ) )' ) , '7.0.0' ); 415 416 // Backwards compatibility. 417 return bp_nouveau_the_group_meta( array( 'keys' => $key, 'echo' => false ) ); 418 } 419 420 /** 421 * Constructor 422 * 423 * @since 7.0.0 424 */ 425 public function __construct() { 426 // Backwards compatibility. 427 $this->meta = bp_nouveau_the_group_meta( array( 'echo' => false ) ); 428 } 429 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 26 01:01:04 2024 | Cross-referenced by PHPXref 0.7.1 |