Ticket #4785: 4785.10.patch
File 4785.10.patch, 37.6 KB (added by , 10 years ago) |
---|
-
src/bp-groups/bp-groups-actions.php
diff --git src/bp-groups/bp-groups-actions.php src/bp-groups/bp-groups-actions.php index 2a0ea8b..52cf952 100644
15 15 if ( !defined( 'ABSPATH' ) ) exit; 16 16 17 17 /** 18 * Protect access to single groups. 19 * 20 * @since BuddyPress (2.1.0) 21 */ 22 function bp_groups_group_access_protection() { 23 if ( ! bp_is_group() ) { 24 return; 25 } 26 27 $current_group = groups_get_current_group(); 28 $user_has_access = $current_group->user_has_access; 29 $no_access_args = array(); 30 31 if ( ! $user_has_access && 'hidden' !== $current_group->status ) { 32 // Always allow access to home and request-membership 33 if ( bp_is_current_action( 'home' ) || bp_is_current_action( 'request-membership' ) ) { 34 $user_has_access = true; 35 36 // User doesn't have access, so set up redirect args 37 } else if ( is_user_logged_in() ) { 38 $no_access_args = array( 39 'message' => __( 'You do not have access to this group.', 'buddypress' ), 40 'root' => bp_get_group_permalink( $current_group ) . 'home/', 41 'redirect' => false 42 ); 43 } 44 } 45 46 // Protect the admin tab from non-admins 47 if ( bp_is_current_action( 'admin' ) && ! bp_is_item_admin() ) { 48 $user_has_access = false; 49 $no_access_args = array( 50 'message' => __( 'You are not an admin of this group.', 'buddypress' ), 51 'root' => bp_get_group_permalink( $current_group ), 52 'redirect' => false 53 ); 54 } 55 56 /** 57 * Allow plugins to filter whether the current user has access to this group content. 58 * 59 * Note that if a plugin sets $user_has_access to false, it may also 60 * want to change the $no_access_args, to avoid problems such as 61 * logged-in users being redirected to wp-login.php. 62 * 63 * @since BuddyPress (2.1.0) 64 * 65 * @param bool $user_has_access True if the user has access to the 66 * content, otherwise false. 67 * @param array $no_access_args Arguments to be passed to 68 * bp_core_no_access() in case of no access. Note that this 69 * value is passed by reference, so it can be modified by the 70 * filter callback. 71 */ 72 $user_has_access = apply_filters_ref_array( 'bp_group_user_has_access', array( $user_has_access, &$no_access_args ) ); 73 74 // If user has access, we return rather than redirect 75 if ( $user_has_access ) { 76 return; 77 } 78 79 // Hidden groups should return a 404 for non-members. 80 // Unset the current group so that you're not redirected 81 // to the default group tab 82 if ( 'hidden' == $current_group->status ) { 83 buddypress()->groups->current_group = 0; 84 buddypress()->is_single_item = false; 85 bp_do_404(); 86 return; 87 } else { 88 bp_core_no_access( $no_access_args ); 89 } 90 91 } 92 add_action( 'bp_actions', 'bp_groups_group_access_protection' ); 93 94 /** 18 95 * Catch and process group creation form submissions. 19 96 */ 20 97 function groups_action_create_group() { -
src/bp-groups/bp-groups-classes.php
diff --git src/bp-groups/bp-groups-classes.php src/bp-groups/bp-groups-classes.php index 2290660..3613554 100644
class BP_Group_Extension { 2880 2880 public $params = array(); 2881 2881 2882 2882 /** 2883 * Raw config params, as passed by the extending class. 2884 * 2885 * @since BuddyPress (2.1.0) 2886 * @var array 2887 */ 2888 public $params_raw = array(); 2889 2890 /** 2883 2891 * The ID of the current group. 2884 2892 * 2885 2893 * @since BuddyPress (1.8.0) … … class BP_Group_Extension { 2923 2931 public $enable_nav_item = true; 2924 2932 2925 2933 /** 2934 * Whether the current user should see the navigation item. 2935 * 2936 * @since BuddyPress (2.1.0) 2937 * @var bool 2938 */ 2939 public $user_can_see_nav_item; 2940 2941 /** 2942 * Whether the current user can visit the tab. 2943 * 2944 * @since BuddyPress (2.1.0) 2945 * @var bool 2946 */ 2947 public $user_can_visit; 2948 2949 /** 2926 2950 * The text of the nav item. Defaults to self::name. 2927 2951 * 2928 2952 * @var string … … class BP_Group_Extension { 3060 3084 * } 3061 3085 */ 3062 3086 public function init( $args = array() ) { 3087 // Store the raw arguments 3088 $this->params_raw = $args; 3063 3089 3064 3090 // Before this init() method was introduced, plugins were 3065 3091 // encouraged to set their config directly. For backward … … class BP_Group_Extension { 3080 3106 'display_hook' => $this->display_hook, 3081 3107 'template_file' => $this->template_file, 3082 3108 'screens' => $this->get_default_screens(), 3109 'access' => null, 3110 'show_tab' => null, 3083 3111 ) ); 3084 3112 3085 3113 $this->initialized = true; … … class BP_Group_Extension { 3123 3151 // Configure 'screens': create, admin, and edit contexts 3124 3152 $this->setup_screens(); 3125 3153 3154 // Configure access-related settings 3155 $this->setup_access_settings(); 3156 3126 3157 // Mirror configuration data so it's accessible to plugins 3127 3158 // that look for it in its old locations 3128 3159 $this->setup_legacy_properties(); … … class BP_Group_Extension { 3241 3272 } 3242 3273 } 3243 3274 3275 /** 3276 * Set up access-related settings for this extension. 3277 * 3278 * @since BuddyPress (2.1.0) 3279 */ 3280 protected function setup_access_settings() { 3281 // Backward compatibility 3282 if ( isset( $this->params['enable_nav_item'] ) ) { 3283 $this->enable_nav_item = (bool) $this->params['enable_nav_item']; 3284 } 3285 3286 // Tab Access 3287 $this->user_can_visit = false; 3288 3289 // Backward compatibility for components that do not provide 3290 // explicit 'access' parameter 3291 if ( empty( $this->params['access'] ) ) { 3292 if ( false === $this->enable_nav_item ) { 3293 $this->params['access'] = 'noone'; 3294 } else { 3295 $group = groups_get_group( array( 3296 'group_id' => $this->group_id, 3297 ) ); 3298 3299 if ( ! empty( $group->status ) && 'public' === $group->status ) { 3300 // Tabs in public groups are accessible to anyone by default 3301 $this->params['access'] = 'anyone'; 3302 } else { 3303 // All other groups have members-only as the default 3304 $this->params['access'] = 'member'; 3305 } 3306 } 3307 } 3308 3309 // Parse multiple access conditions into an array 3310 $access_conditions = $this->params['access']; 3311 if ( ! is_array( $access_conditions ) ) { 3312 $access_conditions = explode( ',', $access_conditions ); 3313 } 3314 3315 // If the current user meets at least one condition, the 3316 // get access 3317 foreach ( $access_conditions as $access_condition ) { 3318 if ( $this->user_meets_access_condition( $access_condition ) ) { 3319 $this->user_can_visit = true; 3320 break; 3321 } 3322 } 3323 3324 // Tab Visibility 3325 $this->user_can_see_nav_item = false; 3326 3327 // Backward compatibility for components that do not provide 3328 // explicit 'show_tab' parameter 3329 if ( empty( $this->params['show_tab'] ) ) { 3330 if ( false === $this->params['enable_nav_item'] ) { 3331 // enable_nav_item is only false if it's been 3332 // defined explicitly as such in the 3333 // constructor. So we always trust this value 3334 $this->params['show_tab'] = 'noone'; 3335 3336 } else if ( isset( $this->params_raw['enable_nav_item'] ) || isset( $this->params_raw['visibility'] ) ) { 3337 // If enable_nav_item or visibility is passed, 3338 // we assume this is a legacy extension. 3339 // Legacy behavior is that enable_nav_item=true + 3340 // visibility=private implies members-only 3341 if ( 'public' !== $this->visibility ) { 3342 $this->params['show_tab'] = 'member'; 3343 } else { 3344 $this->params['show_tab'] = 'anyone'; 3345 } 3346 3347 } else { 3348 // No show_tab or enable_nav_item value is 3349 // available, so match the value of 'access' 3350 $this->params['show_tab'] = $this->params['access']; 3351 } 3352 } 3353 3354 // Parse multiple access conditions into an array 3355 $access_conditions = $this->params['show_tab']; 3356 if ( ! is_array( $access_conditions ) ) { 3357 $access_conditions = explode( ',', $access_conditions ); 3358 } 3359 3360 // If the current user meets at least one condition, the 3361 // get access 3362 foreach ( $access_conditions as $access_condition ) { 3363 if ( $this->user_meets_access_condition( $access_condition ) ) { 3364 $this->user_can_see_nav_item = true; 3365 break; 3366 } 3367 } 3368 } 3369 3370 /** 3371 * Check whether the current user meets an access condition. 3372 * 3373 * @param string $access_condition 'anyone', 'loggedin', 'member', 3374 * 'mod', 'admin' or 'noone'. 3375 * @return bool 3376 */ 3377 protected function user_meets_access_condition( $access_condition ) { 3378 $group = groups_get_group( array( 3379 'group_id' => $this->group_id, 3380 ) ); 3381 3382 switch ( $access_condition ) { 3383 case 'admin' : 3384 $meets_condition = groups_is_user_admin( bp_loggedin_user_id(), $this->group_id ); 3385 break; 3386 3387 case 'mod' : 3388 $meets_condition = groups_is_user_mod( bp_loggedin_user_id(), $this->group_id ); 3389 break; 3390 3391 case 'member' : 3392 $meets_condition = groups_is_user_member( bp_loggedin_user_id(), $this->group_id ); 3393 break; 3394 3395 case 'loggedin' : 3396 $meets_condition = is_user_logged_in(); 3397 break; 3398 3399 case 'noone' : 3400 $meets_condition = false; 3401 break; 3402 3403 case 'anyone' : 3404 default : 3405 $meets_condition = true; 3406 break; 3407 } 3408 3409 return $meets_condition; 3410 } 3411 3244 3412 /** Display ***********************************************************/ 3245 3413 3246 3414 /** … … class BP_Group_Extension { 3255 3423 return; 3256 3424 } 3257 3425 3258 // Ba il if the current user doesn't have access3426 // Backward compatibility only 3259 3427 if ( ( 'public' !== $this->visibility ) && ! buddypress()->groups->current_group->user_has_access ) { 3260 3428 return; 3261 3429 } 3262 3430 3263 if ( true === $this->enable_nav_item ) { 3431 $user_can_see_nav_item = $this->user_can_see_nav_item(); 3432 3433 if ( $user_can_see_nav_item ) { 3434 $group_permalink = bp_get_group_permalink( groups_get_current_group() ); 3435 3264 3436 bp_core_new_subnav_item( array( 3265 3437 'name' => ! $this->nav_item_name ? $this->name : $this->nav_item_name, 3266 3438 'slug' => $this->slug, 3267 3439 'parent_slug' => bp_get_current_group_slug(), 3268 'parent_url' => bp_get_group_permalink( groups_get_current_group() ),3440 'parent_url' => $group_permalink, 3269 3441 'position' => $this->nav_item_position, 3270 3442 'item_css_id' => 'nav-' . $this->slug, 3271 3443 'screen_function' => array( &$this, '_display_hook' ), 3272 'user_has_access' => $this->enable_nav_item 3444 'user_has_access' => $user_can_see_nav_item, 3445 'no_access_url' => $group_permalink, 3273 3446 ) ); 3274 3447 3275 3448 // When we are viewing the extension display page, set the title and options title 3276 3449 if ( bp_is_current_action( $this->slug ) ) { 3450 add_filter( 'bp_group_user_has_access', array( $this, 'group_access_protection' ), 10, 2 ); 3277 3451 add_action( 'bp_template_content_header', create_function( '', 'echo "' . esc_attr( $this->name ) . '";' ) ); 3278 3452 add_action( 'bp_template_title', create_function( '', 'echo "' . esc_attr( $this->name ) . '";' ) ); 3279 3453 } … … class BP_Group_Extension { 3293 3467 bp_core_load_template( apply_filters( 'bp_core_template_plugin', $this->template_file ) ); 3294 3468 } 3295 3469 3470 /** 3471 * Determine whether the current user should see this nav tab. 3472 * 3473 * Note that this controls only the display of the navigation item. 3474 * Access to the tab is controlled by the user_can_visit() check. 3475 * 3476 * @since BuddyPress (2.1.0) 3477 * 3478 * @return bool 3479 */ 3480 public function user_can_see_nav_item( $user_can_see_nav_item = false ) { 3481 if ( 'noone' !== $this->params['show_tab'] && current_user_can( 'bp_moderate' ) ) { 3482 return true; 3483 } 3484 3485 return $this->user_can_see_nav_item; 3486 } 3487 3488 /** 3489 * Determine whether the current user has access to visit this tab. 3490 * 3491 * @since BuddyPress (2.1.0) 3492 * 3493 * @return bool 3494 */ 3495 public function user_can_visit( $user_can_visit = false ) { 3496 if ( 'noone' !== $this->params['access'] && current_user_can( 'bp_moderate' ) ) { 3497 return true; 3498 } 3499 3500 return $this->user_can_visit; 3501 } 3502 3503 /** 3504 * Filter the access check in bp_groups_group_access_protection() for this extension. 3505 * 3506 * Note that $no_access_args is passed by reference, as there are some 3507 * circumstances where the bp_core_no_access() arguments need to be 3508 * modified before the redirect takes place. 3509 * 3510 * @since BuddyPress (2.1.0) 3511 * 3512 * @param bool $user_can_visit 3513 * @param array $no_access_args 3514 * @return bool 3515 */ 3516 public function group_access_protection( $user_can_visit, &$no_access_args ) { 3517 $user_can_visit = $this->user_can_visit(); 3518 3519 if ( ! $user_can_visit && is_user_logged_in() ) { 3520 $current_group = groups_get_group( array( 3521 'group_id' => $this->group_id, 3522 ) ); 3523 3524 $no_access_args['message'] = __( 'You do not have access to this content.', 'buddypress' ); 3525 $no_access_args['root'] = bp_get_group_permalink( $current_group ) . 'home/'; 3526 $no_access_args['redirect'] = false; 3527 } 3528 3529 return $user_can_visit; 3530 } 3531 3532 3296 3533 /** Create ************************************************************/ 3297 3534 3298 3535 /** -
src/bp-groups/bp-groups-loader.php
diff --git src/bp-groups/bp-groups-loader.php src/bp-groups/bp-groups-loader.php index 0086696..0348781 100644
class BP_Groups_Component extends BP_Component { 269 269 270 270 } 271 271 272 // Group access control273 if ( bp_is_groups_component() && !empty( $this->current_group ) ) {274 if ( !$this->current_group->user_has_access ) {275 276 // Hidden groups should return a 404 for non-members.277 // Unset the current group so that you're not redirected278 // to the default group tab279 if ( 'hidden' == $this->current_group->status ) {280 $this->current_group = 0;281 $bp->is_single_item = false;282 bp_do_404();283 return;284 285 // Skip the no_access check on home and membership request pages286 } elseif ( !bp_is_current_action( 'home' ) && !bp_is_current_action( 'request-membership' ) ) {287 288 // Off-limits to this user. Throw an error and redirect to the group's home page289 if ( is_user_logged_in() ) {290 bp_core_no_access( array(291 'message' => __( 'You do not have access to this group.', 'buddypress' ),292 'root' => bp_get_group_permalink( $bp->groups->current_group ) . 'home/',293 'redirect' => false294 ) );295 296 // User does not have access, and does not get a message297 } else {298 bp_core_no_access();299 }300 }301 }302 303 // Protect the admin tab from non-admins304 if ( bp_is_current_action( 'admin' ) && !bp_is_item_admin() ) {305 bp_core_no_access( array(306 'message' => __( 'You are not an admin of this group.', 'buddypress' ),307 'root' => bp_get_group_permalink( $bp->groups->current_group ),308 'redirect' => false309 ) );310 }311 }312 313 272 // Preconfigured group creation steps 314 273 $this->group_creation_steps = apply_filters( 'groups_create_group_steps', array( 315 274 'group-details' => array( … … class BP_Groups_Component extends BP_Component { 482 441 'screen_function' => 'groups_screen_group_members', 483 442 'position' => 60, 484 443 'user_has_access' => $this->current_group->user_has_access, 485 'item_css_id' => 'members' 444 'item_css_id' => 'members', 445 'no_access_url' => $group_link, 486 446 ); 487 447 488 448 if ( bp_is_active( 'friends' ) && bp_groups_user_can_send_invites() ) { … … class BP_Groups_Component extends BP_Component { 494 454 'screen_function' => 'groups_screen_group_invite', 495 455 'item_css_id' => 'invite', 496 456 'position' => 70, 497 'user_has_access' => $this->current_group->user_has_access 457 'user_has_access' => $this->current_group->user_has_access, 458 'no_access_url' => $group_link, 498 459 ); 499 460 } 500 461 … … class BP_Groups_Component extends BP_Component { 508 469 'screen_function' => 'groups_screen_group_admin', 509 470 'position' => 1000, 510 471 'user_has_access' => true, 511 'item_css_id' => 'admin' 472 'item_css_id' => 'admin', 473 'no_access_url' => $group_link, 512 474 ); 513 475 } 514 476 -
src/bp-templates/bp-legacy/buddypress/groups/single/home.php
diff --git src/bp-templates/bp-legacy/buddypress/groups/single/home.php src/bp-templates/bp-legacy/buddypress/groups/single/home.php index 11dfb2e..f22e339 100644
33 33 * @todo A real template hierarchy? Gasp! 34 34 */ 35 35 36 // Group is visible37 if ( bp_group_is_visible() ) :38 39 36 // Looking at home location 40 37 if ( bp_is_group_home() ) : 41 38 42 // Use custom front if one exists 43 $custom_front = bp_locate_template( array( 'groups/single/front.php' ), false, true ); 44 if ( ! empty( $custom_front ) ) : load_template( $custom_front, true ); 39 if ( bp_group_is_visible() ) { 45 40 46 // Default to activity 47 elseif ( bp_is_active( 'activity' ) ) : bp_get_template_part( 'groups/single/activity' ); 41 // Use custom front if one exists 42 $custom_front = bp_locate_template( array( 'groups/single/front.php' ), false, true ); 43 if ( ! empty( $custom_front ) ) : load_template( $custom_front, true ); 48 44 49 // Otherwise show members 50 elseif ( bp_is_active( 'members' ) ) : bp_groups_members_template_part(); 45 // Default to activity 46 elseif ( bp_is_active( 'activity' ) ) : bp_get_template_part( 'groups/single/activity' ); 47 48 // Otherwise show members 49 elseif ( bp_is_active( 'members' ) ) : bp_groups_members_template_part(); 50 51 endif; 52 53 } else { 54 55 do_action( 'bp_before_group_status_message' ); ?> 56 57 <div id="message" class="info"> 58 <p><?php bp_group_status_message(); ?></p> 59 </div> 60 61 <?php do_action( 'bp_after_group_status_message' ); 62 63 } 51 64 52 endif;53 54 65 // Not looking at home 55 66 else : 56 67 … … 76 87 else : bp_get_template_part( 'groups/single/plugins' ); 77 88 78 89 endif; 79 endif;80 81 // Group is not visible82 elseif ( ! bp_group_is_visible() ) :83 84 // Membership request85 if ( bp_is_group_membership_request() ) :86 bp_get_template_part( 'groups/single/request-membership' );87 88 // The group is not visible, show the status message89 else :90 91 do_action( 'bp_before_group_status_message' ); ?>92 93 <div id="message" class="info">94 <p><?php bp_group_status_message(); ?></p>95 </div>96 97 <?php do_action( 'bp_after_group_status_message' );98 90 99 91 endif; 100 endif;101 92 102 93 do_action( 'bp_after_group_body' ); ?> 103 94 -
tests/phpunit/assets/group-extensions.php
diff --git tests/phpunit/assets/group-extensions.php tests/phpunit/assets/group-extensions.php index 3f6ea36..4f2ed87 100644
class BPTest_Group_Extension_Visibility_Public extends BP_Group_Extension { 202 202 parent::init( $args ); 203 203 } 204 204 } 205 206 class BPTest_Group_Extension_Inferred_Access_Settings_EnableNavItem_True extends BP_Group_Extension { 207 public function __construct() { 208 $class_name = get_class( $this ); 209 210 $args = array( 211 'name' => $class_name, 212 'slug' => sanitize_title( $class_name ), 213 'enable_nav_item' => true, 214 ); 215 216 parent::init( $args ); 217 } 218 } 219 220 class BPTest_Group_Extension_Inferred_Access_Settings_EnableNavItem_False extends BP_Group_Extension { 221 public function __construct() { 222 $class_name = get_class( $this ); 223 224 $args = array( 225 'name' => $class_name, 226 'slug' => sanitize_title( $class_name ), 227 'enable_nav_item' => false, 228 ); 229 230 parent::init( $args ); 231 } 232 } 233 class BPTest_Group_Extension_Access_Anyone extends BP_Group_Extension { 234 public function __construct() { 235 $class_name = get_class( $this ); 236 237 $args = array( 238 'name' => $class_name, 239 'slug' => sanitize_title( $class_name ), 240 'access' => 'anyone', 241 ); 242 243 parent::init( $args ); 244 } 245 } 246 247 class BPTest_Group_Extension_Access_Loggedin extends BP_Group_Extension { 248 public function __construct() { 249 $class_name = get_class( $this ); 250 251 $args = array( 252 'name' => $class_name, 253 'slug' => sanitize_title( $class_name ), 254 'access' => 'loggedin', 255 ); 256 257 parent::init( $args ); 258 } 259 } 260 261 class BPTest_Group_Extension_Access_Member extends BP_Group_Extension { 262 public function __construct() { 263 $class_name = get_class( $this ); 264 265 $args = array( 266 'name' => $class_name, 267 'slug' => sanitize_title( $class_name ), 268 'access' => 'member', 269 ); 270 271 parent::init( $args ); 272 } 273 } 274 275 class BPTest_Group_Extension_Access_AdminMod extends BP_Group_Extension { 276 public function __construct() { 277 $class_name = get_class( $this ); 278 279 $args = array( 280 'name' => $class_name, 281 'slug' => sanitize_title( $class_name ), 282 'access' => array( 283 'mod', 284 'admin', 285 ), 286 ); 287 288 parent::init( $args ); 289 } 290 } 291 292 class BPTest_Group_Extension_Access_Admin extends BP_Group_Extension { 293 public function __construct() { 294 $class_name = get_class( $this ); 295 296 $args = array( 297 'name' => $class_name, 298 'slug' => sanitize_title( $class_name ), 299 'access' => 'admin', 300 ); 301 302 parent::init( $args ); 303 } 304 } 305 306 class BPTest_Group_Extension_Access_Noone extends BP_Group_Extension { 307 public function __construct() { 308 $class_name = get_class( $this ); 309 310 $args = array( 311 'name' => $class_name, 312 'slug' => sanitize_title( $class_name ), 313 'access' => 'noone', 314 ); 315 316 parent::init( $args ); 317 } 318 } 319 320 class BPTest_Group_Extension_ShowTab_Anyone extends BP_Group_Extension { 321 public function __construct() { 322 $class_name = get_class( $this ); 323 324 $args = array( 325 'name' => $class_name, 326 'slug' => sanitize_title( $class_name ), 327 'access' => 'admin', 328 'show_tab' => 'anyone', 329 ); 330 331 parent::init( $args ); 332 } 333 } 334 335 class BPTest_Group_Extension_ShowTab_Loggedin extends BP_Group_Extension { 336 public function __construct() { 337 $class_name = get_class( $this ); 338 339 $args = array( 340 'name' => $class_name, 341 'slug' => sanitize_title( $class_name ), 342 'access' => 'admin', 343 'show_tab' => 'loggedin', 344 ); 345 346 parent::init( $args ); 347 } 348 } 349 350 class BPTest_Group_Extension_ShowTab_Member extends BP_Group_Extension { 351 public function __construct() { 352 $class_name = get_class( $this ); 353 354 $args = array( 355 'name' => $class_name, 356 'slug' => sanitize_title( $class_name ), 357 'access' => 'admin', 358 'show_tab' => 'member', 359 ); 360 361 parent::init( $args ); 362 } 363 } 364 365 class BPTest_Group_Extension_ShowTab_AdminMod extends BP_Group_Extension { 366 public function __construct() { 367 $class_name = get_class( $this ); 368 369 $args = array( 370 'name' => $class_name, 371 'slug' => sanitize_title( $class_name ), 372 'access' => 'admin', 373 'show_tab' => array( 374 'mod', 375 'admin', 376 ), 377 ); 378 379 parent::init( $args ); 380 } 381 } 382 383 class BPTest_Group_Extension_ShowTab_Admin extends BP_Group_Extension { 384 public function __construct() { 385 $class_name = get_class( $this ); 386 387 $args = array( 388 'name' => $class_name, 389 'slug' => sanitize_title( $class_name ), 390 'access' => 'admin', 391 'show_tab' => 'admin', 392 ); 393 394 parent::init( $args ); 395 } 396 } 397 398 class BPTest_Group_Extension_ShowTab_Noone extends BP_Group_Extension { 399 public function __construct() { 400 $class_name = get_class( $this ); 401 402 $args = array( 403 'name' => $class_name, 404 'slug' => sanitize_title( $class_name ), 405 'access' => 'noone', 406 'show_tab' => 'noone', 407 ); 408 409 parent::init( $args ); 410 } 411 } -
tests/phpunit/testcases/groups/class-bp-group-extension.php
diff --git tests/phpunit/testcases/groups/class-bp-group-extension.php tests/phpunit/testcases/groups/class-bp-group-extension.php index b6ef89b..00cb10f 100644
class BP_Tests_Group_Extension_TestCases extends BP_UnitTestCase { 346 346 buddypress()->bp_options_nav = $old_options_nav; 347 347 $this->set_current_user( $old_current_user ); 348 348 } 349 350 /** 351 * @group user_can_visit 352 */ 353 public function test_user_can_visit_inferred_from_enable_nav_item() { 354 $old_current_user = get_current_user_id(); 355 356 $g = $this->factory->group->create( array( 357 'status' => 'public', 358 ) ); 359 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 360 $this->go_to( bp_get_group_permalink( $g_obj ) ); 361 362 $this->set_current_user( 0 ); 363 364 $e = new BPTest_Group_Extension_Inferred_Access_Settings_EnableNavItem_True(); 365 $e->_register(); 366 $this->assertTrue( $e->user_can_visit() ); 367 368 $e2 = new BPTest_Group_Extension_Inferred_Access_Settings_EnableNavItem_False(); 369 $e2->_register(); 370 $this->assertFalse( $e2->user_can_visit() ); 371 372 $this->set_current_user( $old_current_user ); 373 } 374 375 /** 376 * @group user_can_visit 377 */ 378 public function test_user_can_visit_explicit_for_logged_out_user() { 379 $old_current_user = get_current_user_id(); 380 $this->set_current_user( 0 ); 381 382 $g = $this->factory->group->create( array( 383 'status' => 'public', 384 ) ); 385 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 386 $this->go_to( bp_get_group_permalink( $g_obj ) ); 387 388 $e1 = new BPTest_Group_Extension_Access_Anyone(); 389 $e1->_register(); 390 $this->assertTrue( $e1->user_can_visit() ); 391 392 $e2 = new BPTest_Group_Extension_Access_Loggedin(); 393 $e2->_register(); 394 $this->assertFalse( $e2->user_can_visit() ); 395 396 $e3 = new BPTest_Group_Extension_Access_Member(); 397 $e3->_register(); 398 $this->assertFalse( $e3->user_can_visit() ); 399 400 $e4 = new BPTest_Group_Extension_Access_AdminMod(); 401 $e4->_register(); 402 $this->assertFalse( $e4->user_can_visit() ); 403 404 $e5 = new BPTest_Group_Extension_Access_Admin(); 405 $e5->_register(); 406 $this->assertFalse( $e5->user_can_visit() ); 407 408 $e6 = new BPTest_Group_Extension_Access_Noone(); 409 $e6->_register(); 410 $this->assertFalse( $e6->user_can_visit() ); 411 412 $this->set_current_user( $old_current_user ); 413 } 414 415 /** 416 * @group user_can_visit 417 */ 418 public function test_user_can_visit_explicit_for_logged_in_user() { 419 $g = $this->factory->group->create( array( 420 'status' => 'public', 421 ) ); 422 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 423 424 $u = $this->create_user(); 425 $old_current_user = get_current_user_id(); 426 $this->set_current_user( $u ); 427 428 $this->go_to( bp_get_group_permalink( $g_obj ) ); 429 430 $e1 = new BPTest_Group_Extension_Access_Anyone(); 431 $e1->_register(); 432 $this->assertTrue( $e1->user_can_visit() ); 433 434 $e2 = new BPTest_Group_Extension_Access_Loggedin(); 435 $e2->_register(); 436 $this->assertTrue( $e2->user_can_visit() ); 437 438 $e3 = new BPTest_Group_Extension_Access_Member(); 439 $e3->_register(); 440 $this->assertFalse( $e3->user_can_visit() ); 441 442 $e4 = new BPTest_Group_Extension_Access_AdminMod(); 443 $e4->_register(); 444 $this->assertFalse( $e4->user_can_visit() ); 445 446 $e5 = new BPTest_Group_Extension_Access_Admin(); 447 $e5->_register(); 448 $this->assertFalse( $e5->user_can_visit() ); 449 450 $e6 = new BPTest_Group_Extension_Access_Noone(); 451 $e6->_register(); 452 $this->assertFalse( $e6->user_can_visit() ); 453 454 $this->set_current_user( $old_current_user ); 455 } 456 457 /** 458 * @group user_can_visit 459 */ 460 public function test_user_can_visit_explicit_for_group_member() { 461 $g = $this->factory->group->create( array( 462 'status' => 'public', 463 ) ); 464 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 465 466 $u = $this->create_user(); 467 $old_current_user = get_current_user_id(); 468 $this->set_current_user( $u ); 469 470 $this->add_user_to_group( $u, $g ); 471 472 $this->go_to( bp_get_group_permalink( $g_obj ) ); 473 474 $e1 = new BPTest_Group_Extension_Access_Anyone(); 475 $e1->_register(); 476 $this->assertTrue( $e1->user_can_visit() ); 477 478 $e2 = new BPTest_Group_Extension_Access_Loggedin(); 479 $e2->_register(); 480 $this->assertTrue( $e2->user_can_visit() ); 481 482 $e3 = new BPTest_Group_Extension_Access_Member(); 483 $e3->_register(); 484 $this->assertTrue( $e3->user_can_visit() ); 485 486 $e4 = new BPTest_Group_Extension_Access_AdminMod(); 487 $e4->_register(); 488 $this->assertFalse( $e4->user_can_visit() ); 489 490 $e5 = new BPTest_Group_Extension_Access_Admin(); 491 $e5->_register(); 492 $this->assertFalse( $e5->user_can_visit() ); 493 494 $e6 = new BPTest_Group_Extension_Access_Noone(); 495 $e6->_register(); 496 $this->assertFalse( $e6->user_can_visit() ); 497 498 $this->set_current_user( $old_current_user ); 499 } 500 501 /** 502 * @group user_can_visit 503 */ 504 public function test_user_can_visit_explicit_for_group_mod() { 505 $g = $this->factory->group->create( array( 506 'status' => 'public', 507 ) ); 508 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 509 510 $u = $this->create_user(); 511 $old_current_user = get_current_user_id(); 512 $this->set_current_user( $u ); 513 514 $m = $this->add_user_to_group( $u, $g ); 515 $gm = new BP_Groups_Member( $u, $g ); 516 $gm->promote( 'mod' ); 517 518 $this->go_to( bp_get_group_permalink( $g_obj ) ); 519 520 $e1 = new BPTest_Group_Extension_Access_Anyone(); 521 $e1->_register(); 522 $this->assertTrue( $e1->user_can_visit() ); 523 524 $e2 = new BPTest_Group_Extension_Access_Loggedin(); 525 $e2->_register(); 526 $this->assertTrue( $e2->user_can_visit() ); 527 528 $e3 = new BPTest_Group_Extension_Access_Member(); 529 $e3->_register(); 530 $this->assertTrue( $e3->user_can_visit() ); 531 532 $e4 = new BPTest_Group_Extension_Access_AdminMod(); 533 $e4->_register(); 534 $this->assertTrue( $e4->user_can_visit() ); 535 536 $e5 = new BPTest_Group_Extension_Access_Admin(); 537 $e5->_register(); 538 $this->assertFalse( $e5->user_can_visit() ); 539 540 $e6 = new BPTest_Group_Extension_Access_Noone(); 541 $e6->_register(); 542 $this->assertFalse( $e6->user_can_visit() ); 543 544 $this->set_current_user( $old_current_user ); 545 } 546 547 /** 548 * @group user_can_visit 549 */ 550 public function test_user_can_visit_explicit_for_group_admin() { 551 $g = $this->factory->group->create( array( 552 'status' => 'public', 553 ) ); 554 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 555 556 $u = $this->create_user(); 557 $old_current_user = get_current_user_id(); 558 $this->set_current_user( $u ); 559 560 $m = $this->add_user_to_group( $u, $g ); 561 $gm = new BP_Groups_Member( $u, $g ); 562 $gm->promote( 'admin' ); 563 564 $this->go_to( bp_get_group_permalink( $g_obj ) ); 565 566 $e1 = new BPTest_Group_Extension_Access_Anyone(); 567 $e1->_register(); 568 $this->assertTrue( $e1->user_can_visit() ); 569 570 $e2 = new BPTest_Group_Extension_Access_Loggedin(); 571 $e2->_register(); 572 $this->assertTrue( $e2->user_can_visit() ); 573 574 $e3 = new BPTest_Group_Extension_Access_Member(); 575 $e3->_register(); 576 $this->assertTrue( $e3->user_can_visit() ); 577 578 $e4 = new BPTest_Group_Extension_Access_AdminMod(); 579 $e4->_register(); 580 $this->assertTrue( $e4->user_can_visit() ); 581 582 $e5 = new BPTest_Group_Extension_Access_Admin(); 583 $e5->_register(); 584 $this->assertTrue( $e5->user_can_visit() ); 585 586 $e6 = new BPTest_Group_Extension_Access_Noone(); 587 $e6->_register(); 588 $this->assertFalse( $e6->user_can_visit() ); 589 590 $this->set_current_user( $old_current_user ); 591 } 592 593 /** 594 * @group user_can_see_nav_item 595 */ 596 public function test_user_can_see_nav_item_implied() { 597 $g = $this->factory->group->create( array( 598 'status' => 'public', 599 ) ); 600 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 601 602 $old_current_user = get_current_user_id(); 603 $this->set_current_user( 0 ); 604 605 $this->go_to( bp_get_group_permalink( $g_obj ) ); 606 607 $e1 = new BPTest_Group_Extension_Access_Anyone(); 608 $e1->_register(); 609 $this->assertTrue( $e1->user_can_see_nav_item() ); 610 611 $e2 = new BPTest_Group_Extension_Access_Loggedin(); 612 $e2->_register(); 613 $this->assertFalse( $e2->user_can_see_nav_item() ); 614 615 $e3 = new BPTest_Group_Extension_Access_Member(); 616 $e3->_register(); 617 $this->assertFalse( $e3->user_can_see_nav_item() ); 618 619 $e4 = new BPTest_Group_Extension_Access_AdminMod(); 620 $e4->_register(); 621 $this->assertFalse( $e4->user_can_see_nav_item() ); 622 623 $e5 = new BPTest_Group_Extension_Access_Admin(); 624 $e5->_register(); 625 $this->assertFalse( $e5->user_can_see_nav_item() ); 626 627 $e6 = new BPTest_Group_Extension_Access_Noone(); 628 $e6->_register(); 629 $this->assertFalse( $e6->user_can_visit() ); 630 631 $this->set_current_user( $old_current_user ); 632 } 633 634 /** 635 * @group user_can_see_nav_item 636 */ 637 public function test_user_can_see_nav_item_explicit_for_logged_out_user() { 638 $g = $this->factory->group->create( array( 639 'status' => 'public', 640 ) ); 641 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 642 643 $old_current_user = get_current_user_id(); 644 $this->set_current_user( 0 ); 645 646 $this->go_to( bp_get_group_permalink( $g_obj ) ); 647 648 $e1 = new BPTest_Group_Extension_ShowTab_Anyone(); 649 $e1->_register(); 650 $this->assertTrue( $e1->user_can_see_nav_item() ); 651 652 $e2 = new BPTest_Group_Extension_ShowTab_Loggedin(); 653 $e2->_register(); 654 $this->assertFalse( $e2->user_can_see_nav_item() ); 655 656 $e3 = new BPTest_Group_Extension_ShowTab_Member(); 657 $e3->_register(); 658 $this->assertFalse( $e3->user_can_see_nav_item() ); 659 660 $e4 = new BPTest_Group_Extension_ShowTab_AdminMod(); 661 $e4->_register(); 662 $this->assertFalse( $e4->user_can_see_nav_item() ); 663 664 $e5 = new BPTest_Group_Extension_ShowTab_Admin(); 665 $e5->_register(); 666 $this->assertFalse( $e5->user_can_see_nav_item() ); 667 668 $e6 = new BPTest_Group_Extension_ShowTab_Noone(); 669 $e6->_register(); 670 $this->assertFalse( $e6->user_can_see_nav_item() ); 671 672 $this->set_current_user( $old_current_user ); 673 } 674 675 /** 676 * @group user_can_see_nav_item 677 */ 678 public function test_user_can_see_nav_item_explicit_for_logged_in_user() { 679 $g = $this->factory->group->create( array( 680 'status' => 'public', 681 ) ); 682 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 683 684 $u = $this->create_user(); 685 $old_current_user = get_current_user_id(); 686 $this->set_current_user( $u ); 687 688 $this->go_to( bp_get_group_permalink( $g_obj ) ); 689 690 $e1 = new BPTest_Group_Extension_ShowTab_Anyone(); 691 $e1->_register(); 692 $this->assertTrue( $e1->user_can_see_nav_item() ); 693 694 $e2 = new BPTest_Group_Extension_ShowTab_Loggedin(); 695 $e2->_register(); 696 $this->assertTrue( $e2->user_can_see_nav_item() ); 697 698 $e3 = new BPTest_Group_Extension_ShowTab_Member(); 699 $e3->_register(); 700 $this->assertFalse( $e3->user_can_see_nav_item() ); 701 702 $e4 = new BPTest_Group_Extension_ShowTab_AdminMod(); 703 $e4->_register(); 704 $this->assertFalse( $e4->user_can_see_nav_item() ); 705 706 $e5 = new BPTest_Group_Extension_ShowTab_Admin(); 707 $e5->_register(); 708 $this->assertFalse( $e5->user_can_see_nav_item() ); 709 710 $e6 = new BPTest_Group_Extension_ShowTab_Noone(); 711 $e6->_register(); 712 $this->assertFalse( $e6->user_can_see_nav_item() ); 713 714 $this->set_current_user( $old_current_user ); 715 } 716 717 /** 718 * @group user_can_see_nav_item 719 */ 720 public function test_user_can_see_nav_item_explicit_for_group_member() { 721 $g = $this->factory->group->create( array( 722 'status' => 'public', 723 ) ); 724 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 725 726 $u = $this->create_user(); 727 $old_current_user = get_current_user_id(); 728 $this->set_current_user( $u ); 729 730 $this->add_user_to_group( $u, $g ); 731 732 $this->go_to( bp_get_group_permalink( $g_obj ) ); 733 734 $e1 = new BPTest_Group_Extension_ShowTab_Anyone(); 735 $e1->_register(); 736 $this->assertTrue( $e1->user_can_see_nav_item() ); 737 738 $e2 = new BPTest_Group_Extension_ShowTab_Loggedin(); 739 $e2->_register(); 740 $this->assertTrue( $e2->user_can_see_nav_item() ); 741 742 $e3 = new BPTest_Group_Extension_ShowTab_Member(); 743 $e3->_register(); 744 $this->assertTrue( $e3->user_can_see_nav_item() ); 745 746 $e4 = new BPTest_Group_Extension_ShowTab_AdminMod(); 747 $e4->_register(); 748 $this->assertFalse( $e4->user_can_see_nav_item() ); 749 750 $e5 = new BPTest_Group_Extension_ShowTab_Admin(); 751 $e5->_register(); 752 $this->assertFalse( $e5->user_can_see_nav_item() ); 753 754 $e6 = new BPTest_Group_Extension_ShowTab_Noone(); 755 $e6->_register(); 756 $this->assertFalse( $e6->user_can_see_nav_item() ); 757 758 $this->set_current_user( $old_current_user ); 759 } 760 761 /** 762 * @group user_can_see_nav_item 763 */ 764 public function test_user_can_see_nav_item_explicit_for_group_mod() { 765 $g = $this->factory->group->create( array( 766 'status' => 'public', 767 ) ); 768 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 769 770 $u = $this->create_user(); 771 $old_current_user = get_current_user_id(); 772 $this->set_current_user( $u ); 773 774 $this->add_user_to_group( $u, $g ); 775 $gm = new BP_Groups_Member( $u, $g ); 776 $gm->promote( 'mod' ); 777 778 $this->go_to( bp_get_group_permalink( $g_obj ) ); 779 780 $e1 = new BPTest_Group_Extension_ShowTab_Anyone(); 781 $e1->_register(); 782 $this->assertTrue( $e1->user_can_see_nav_item() ); 783 784 $e2 = new BPTest_Group_Extension_ShowTab_Loggedin(); 785 $e2->_register(); 786 $this->assertTrue( $e2->user_can_see_nav_item() ); 787 788 $e3 = new BPTest_Group_Extension_ShowTab_Member(); 789 $e3->_register(); 790 $this->assertTrue( $e3->user_can_see_nav_item() ); 791 792 $e4 = new BPTest_Group_Extension_ShowTab_AdminMod(); 793 $e4->_register(); 794 $this->assertTrue( $e4->user_can_see_nav_item() ); 795 796 $e5 = new BPTest_Group_Extension_ShowTab_Admin(); 797 $e5->_register(); 798 $this->assertFalse( $e5->user_can_see_nav_item() ); 799 800 $e6 = new BPTest_Group_Extension_ShowTab_Noone(); 801 $e6->_register(); 802 $this->assertFalse( $e6->user_can_see_nav_item() ); 803 804 $this->set_current_user( $old_current_user ); 805 } 806 807 /** 808 * @group user_can_see_nav_item 809 */ 810 public function test_user_can_see_nav_item_explicit_for_group_admin() { 811 $g = $this->factory->group->create( array( 812 'status' => 'public', 813 ) ); 814 $g_obj = groups_get_group( array( 'group_id' => $g ) ); 815 816 $u = $this->create_user(); 817 $old_current_user = get_current_user_id(); 818 $this->set_current_user( $u ); 819 820 $this->add_user_to_group( $u, $g ); 821 $gm = new BP_Groups_Member( $u, $g ); 822 $gm->promote( 'admin' ); 823 824 $this->go_to( bp_get_group_permalink( $g_obj ) ); 825 826 $e1 = new BPTest_Group_Extension_ShowTab_Anyone(); 827 $e1->_register(); 828 $this->assertTrue( $e1->user_can_see_nav_item() ); 829 830 $e2 = new BPTest_Group_Extension_ShowTab_Loggedin(); 831 $e2->_register(); 832 $this->assertTrue( $e2->user_can_see_nav_item() ); 833 834 $e3 = new BPTest_Group_Extension_ShowTab_Member(); 835 $e3->_register(); 836 $this->assertTrue( $e3->user_can_see_nav_item() ); 837 838 $e4 = new BPTest_Group_Extension_ShowTab_AdminMod(); 839 $e4->_register(); 840 $this->assertTrue( $e4->user_can_see_nav_item() ); 841 842 $e5 = new BPTest_Group_Extension_ShowTab_Admin(); 843 $e5->_register(); 844 $this->assertTrue( $e5->user_can_see_nav_item() ); 845 846 $e6 = new BPTest_Group_Extension_ShowTab_Noone(); 847 $e6->_register(); 848 $this->assertFalse( $e6->user_can_see_nav_item() ); 849 850 $this->set_current_user( $old_current_user ); 851 } 349 852 }