Ticket #1269: 1269.02.patch
File 1269.02.patch, 40.7 KB (added by , 9 years ago) |
---|
-
src/bp-blogs/bp-blogs-template.php
diff --git src/bp-blogs/bp-blogs-template.php src/bp-blogs/bp-blogs-template.php index 21fdb7c..fadb4aa 100644
function bp_blogs_directory_permalink() { 106 106 * 107 107 * Responsible for loading a group of blogs into a loop for display. 108 108 */ 109 class BP_Blogs_Template {109 class BP_Blogs_Template extends BP_Template_Loop { 110 110 111 111 /** 112 * The loop iterator.112 * Default arguments for Blogs loop. 113 113 * 114 * @access public 115 * @var int 116 */ 117 public $current_blog = -1; 118 119 /** 120 * The number of blogs returned by the paged query. 121 * 122 * @access public 123 * @var int 124 */ 125 public $blog_count = 0; 126 127 /** 128 * Array of blogs located by the query.. 114 * @since BuddyPress (2.3.0) 129 115 * 130 * @access p ublic116 * @access protected 131 117 * @var array 132 118 */ 133 public $blogs = array(); 134 135 /** 136 * The blog object currently being iterated on. 137 * 138 * @access public 139 * @var object 140 */ 141 public $blog; 142 143 /** 144 * A flag for whether the loop is currently being iterated. 145 * 146 * @access public 147 * @var bool 148 */ 149 public $in_the_loop = false; 150 151 /** 152 * The page number being requested. 153 * 154 * @access public 155 * @var public 156 */ 157 public $pag_page = 1; 158 159 /** 160 * The number of items being requested per page. 161 * 162 * @access public 163 * @var public 164 */ 165 public $pag_num = 20; 166 167 /** 168 * An HTML string containing pagination links. 169 * 170 * @access public 171 * @var string 172 */ 173 public $pag_links = ''; 174 175 /** 176 * The total number of blogs matching the query parameters. 177 * 178 * @access public 179 * @var int 180 */ 181 public $total_blog_count = 0; 119 protected $default_args = array( 120 'labels' => array( 121 'item' => 'blog', 122 'item_plural' => 'blogs', 123 'item_total_count' => 'total_blog_count', 124 'item_count' => 'blog_count', 125 'current_item' => 'current_blog', 126 ), 127 'params' => array( 128 'page_arg' => 'bpage', 129 'page' => 1, 130 'per_page' => 20, 131 'max' => 0, 132 ) 133 ); 182 134 183 135 /** 184 136 * Constructor method. … … class BP_Blogs_Template { 198 150 * @param array $include_blog_ids Array of blog IDs to include. 199 151 */ 200 152 public function __construct( $type, $page, $per_page, $max, $user_id, $search_terms, $page_arg = 'bpage', $update_meta_cache = true, $include_blog_ids = false ) { 153 $params = array( 'params' => array( 154 'type' => $type, 155 'page' => $page, 156 'per_page' => $per_page, 157 'max' => $max, 158 'user_id' => $user_id, 159 'search_terms' => $search_terms, 160 'page_arg' => $page_arg, 161 'update_meta_cache' => $update_meta_cache, 162 'include_blog_ids' => $include_blog_ids, 163 ) ); 201 164 202 $this->pag_page = isset( $_REQUEST[ $page_arg ] ) ? intval( $_REQUEST[ $page_arg ] ) : $page; 203 $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page; 204 205 // Backwards compatibility support for blogs by first letter 206 if ( ! empty( $_REQUEST['letter'] ) ) { 207 $this->blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page ); 208 209 // Typical blogs query 210 } else { 211 $this->blogs = bp_blogs_get_blogs( array( 212 'type' => $type, 213 'per_page' => $this->pag_num, 214 'page' => $this->pag_page, 215 'user_id' => $user_id, 216 'search_terms' => $search_terms, 217 'update_meta_cache' => $update_meta_cache, 218 'include_blog_ids' => $include_blog_ids, 219 ) ); 220 } 221 222 // Set the total blog count 223 if ( empty( $max ) || ( $max >= (int) $this->blogs['total'] ) ) { 224 $this->total_blog_count = (int) $this->blogs['total']; 225 } else { 226 $this->total_blog_count = (int) $max; 227 } 228 229 // Set the blogs array (to loop through later 230 $this->blogs = $this->blogs['blogs']; 231 232 // Get the current blog count to compare maximum against 233 $blog_count = count( $this->blogs ); 234 235 // Set the current blog count 236 if ( empty( $max ) || ( $max >= (int) $blog_count ) ) { 237 $this->blog_count = (int) $blog_count; 238 } else { 239 $this->blog_count = (int) $max; 240 } 241 242 // Build pagination links based on total blogs and current page number 243 if ( ! empty( $this->total_blog_count ) && ! empty( $this->pag_num ) ) { 244 $this->pag_links = paginate_links( array( 245 'base' => add_query_arg( $page_arg, '%#%' ), 246 'format' => '', 247 'total' => ceil( (int) $this->total_blog_count / (int) $this->pag_num ), 248 'current' => (int) $this->pag_page, 249 'prev_text' => _x( '←', 'Blog pagination previous text', 'buddypress' ), 250 'next_text' => _x( '→', 'Blog pagination next text', 'buddypress' ), 251 'mid_size' => 1, 252 'add_args' => array(), 253 ) ); 254 } 255 } 256 257 /** 258 * Whether there are blogs available in the loop. 259 * 260 * @see bp_has_blogs() 261 * 262 * @return bool True if there are items in the loop, otherwise false. 263 */ 264 public function has_blogs() { 265 return (bool) ! empty( $this->blog_count ); 266 } 267 268 /** 269 * Set up the next blog and iterate index. 270 * 271 * @return object The next blog to iterate over. 272 */ 273 public function next_blog() { 274 $this->current_blog++; 275 $this->blog = $this->blogs[ $this->current_blog ]; 276 277 return $this->blog; 278 } 279 280 /** 281 * Rewind the blogs and reset blog index. 282 */ 283 public function rewind_blogs() { 284 $this->current_blog = -1; 285 if ( $this->blog_count > 0 ) { 286 $this->blog = $this->blogs[0]; 287 } 165 $this->set_loop( $params ); 288 166 } 289 167 290 168 /** 291 * Whether there are blogs left in the loop to iterate over. 292 * 293 * This method is used by {@link bp_blogs()} as part of the while loop 294 * that controls iteration inside the blogs loop, eg: 295 * while ( bp_blogs() ) { ... 169 * Get Blogs. 296 170 * 297 * @s ee bp_blogs()171 * @since BuddyPress (2.3.0) 298 172 * 299 * @ return bool True if there are more blogs to show, otherwise false.173 * @access public 300 174 */ 301 public function blogs() {302 if ( ( $this->current_blog + 1 ) < $this->blog_count ) {303 return true;304 } elseif ( ( $this->current_blog + 1 ) === $this->blog_count ) {175 public function get_items() { 176 // Backwards compatibility support for blogs by first letter 177 if ( ! empty( $_REQUEST['letter'] ) ) { 178 $blogs = BP_Blogs_Blog::get_by_letter( $_REQUEST['letter'], $this->per_page, $this->page ); 305 179 306 /** 307 * Fires right before the rewinding of blogs listing after all are shown. 308 * 309 * @since BuddyPress (1.5.0) 310 */ 311 do_action( 'blog_loop_end' ); 312 // Do some cleaning up after the loop 313 $this->rewind_blogs(); 180 // Typical blogs query 181 } else { 182 $blogs = bp_blogs_get_blogs( $this->args['params'] ); 314 183 } 315 184 316 $this->in_the_loop = false; 317 return false; 185 return $blogs; 318 186 } 319 187 320 188 /** 321 * Set up the current blog inside the loop. 322 * 323 * Used by {@link bp_the_blog()} to set up the current blog data while 324 * looping, so that template tags used during that iteration make 325 * reference to the current blog. 189 * Rewind Blogs is an alias of rewind_items() 326 190 * 327 * @ see bp_the_blog()191 * @access public 328 192 */ 329 public function the_blog() { 330 331 $this->in_the_loop = true; 332 $this->blog = $this->next_blog(); 333 334 // loop has just started 335 if ( 0 === $this->current_blog ) { 336 337 /** 338 * Fires if on the first blog in the loop. 339 * 340 * @since BuddyPress (1.5.0) 341 */ 342 do_action( 'blog_loop_start' ); 343 } 193 public function rewind_blogs() { 194 _doing_it_wrong( __FUNCTION__, __( 'BP_Blogs_Template->rewind_blogs() should not be used directly, please use bp_rewind_blogs()', 'buddypress' ), '2.3.0' ); 195 $this->rewind_items(); 344 196 } 345 197 } 346 198 … … class BP_Blogs_Template { 350 202 function bp_rewind_blogs() { 351 203 global $blogs_template; 352 204 353 $blogs_template->rewind_ blogs();205 $blogs_template->rewind_items(); 354 206 } 355 207 356 208 /** … … function bp_has_blogs( $args = '' ) { 429 281 * @param BP_Blogs_Template $blogs_template Current blogs template object. 430 282 * @param array $r Parsed arguments used in blogs template query. 431 283 */ 432 return apply_filters( 'bp_has_blogs', $blogs_template->has_ blogs(), $blogs_template, $r );284 return apply_filters( 'bp_has_blogs', $blogs_template->has_items(), $blogs_template, $r ); 433 285 } 434 286 435 287 /** … … function bp_has_blogs( $args = '' ) { 442 294 function bp_blogs() { 443 295 global $blogs_template; 444 296 445 return $blogs_template-> blogs();297 return $blogs_template->items(); 446 298 } 447 299 448 300 /** … … function bp_blogs() { 455 307 function bp_the_blog() { 456 308 global $blogs_template; 457 309 458 return $blogs_template->the_ blog();310 return $blogs_template->the_item(); 459 311 } 460 312 461 313 /** … … function bp_the_blog() { 466 318 function bp_blogs_pagination_count() { 467 319 global $blogs_template; 468 320 469 $start_num = intval( ( $blogs_template->pag _page - 1 ) * $blogs_template->pag_num) + 1;321 $start_num = intval( ( $blogs_template->page - 1 ) * $blogs_template->per_page ) + 1; 470 322 $from_num = bp_core_number_format( $start_num ); 471 $to_num = bp_core_number_format( ( $start_num + ( $blogs_template->p ag_num - 1 ) > $blogs_template->total_blog_count ) ? $blogs_template->total_blog_count : $start_num + ( $blogs_template->pag_num- 1 ) );323 $to_num = bp_core_number_format( ( $start_num + ( $blogs_template->per_page - 1 ) > $blogs_template->total_blog_count ) ? $blogs_template->total_blog_count : $start_num + ( $blogs_template->per_page - 1 ) ); 472 324 $total = bp_core_number_format( $blogs_template->total_blog_count ); 473 325 474 326 echo sprintf( _n( 'Viewing 1 site', 'Viewing %1$s - %2$s of %3$s sites', $total, 'buddypress' ), $from_num, $to_num, $total ); -
src/bp-core/bp-core-classes.php
diff --git src/bp-core/bp-core-classes.php src/bp-core/bp-core-classes.php index f47f0b8..e4ea96e 100644
abstract class BP_Recursive_Query { 2978 2978 * @return bool 2979 2979 */ 2980 2980 abstract protected function is_first_order_clause( $query ); 2981 } 2982 No newline at end of file 2981 } 2982 2983 /** 2984 * Base class for components template loops. 2985 * 2986 * Each public method can be overriden from a custom template class 2987 * For instance, you can customize the default paginate links within 2988 * your class. 2989 * @see BP_Groups_Template->set_pag_links() for an example of use 2990 * 2991 * the abstract method get_items() is required. This is where you define 2992 * an associative array having your custom 'item_plural' and 'total' keys 2993 * 2994 * @since 2.3.0 2995 */ 2996 abstract class BP_Template_Loop { 2997 2998 /** 2999 * Default arguments common to all components loop. 3000 * 3001 * You can add the custom item default args within the custom template loop class 3002 * @see BP_Groups_Template->default_args for an example of use. 3003 * 3004 * @since BuddyPress (2.3.0) 3005 * 3006 * @access protected 3007 * @var array $default_args { 3008 * Array describing the default argument for the item loop. 3009 * @type array $labels list of custom var names for the loop 3010 * - @type string $item the singular item var name 3011 * for instance 'group' for the groups loop 3012 * - @type string $item_plural the plural item var name 3013 * for instance 'groups' for the groups loop 3014 * - @type string $item_total_count the total count var name 3015 * for instance 'total_group_count' for the groups loop 3016 * - @type string $item_count the current items count var name 3017 * for instance 'group_count' for the groups loop 3018 * - @type string $current_item the current item var name 3019 * for instance 'current_group' for the groups loop 3020 * @type array $params list of default arguments for the loop 3021 * - @type string $page_arg the var name to get the page value. 3022 * for instance 'grpage' for the groups loop 3023 * - @type int $page default value for the page argument 3024 * - @type int $per_page default value for the per page argument 3025 * - @type int $max default value for the max argument 3026 * } 3027 */ 3028 protected $default_args = array( 3029 'labels' => array( 3030 'item' => 'item', 3031 'item_plural' => 'items', 3032 'item_total_count' => 'total_item_count', 3033 'item_count' => 'item_count', 3034 'current_item' => 'current_item', 3035 ), 3036 'params' => array( 3037 'page_arg' => 'page', 3038 'page' => 1, 3039 'per_page' => 20, 3040 'max' => 0, 3041 ) 3042 ); 3043 3044 /** 3045 * Holds the arguments to customize the loop labels and the component's items query. 3046 * 3047 * This includes `$default_args`, as well as the user-supplied values. 3048 * 3049 * @since BuddyPress (2.3.0) 3050 * 3051 * @access protected 3052 * @var array 3053 */ 3054 protected $args = array(); 3055 3056 /** 3057 * A flag for whether the loop is currently being iterated. 3058 * 3059 * @since BuddyPress (2.3.0) 3060 * 3061 * @access public 3062 * @var bool 3063 */ 3064 public $in_the_loop; 3065 3066 /** 3067 * An HTML string containing pagination links. 3068 * 3069 * @since BuddyPress (2.3.0) 3070 * 3071 * @access public 3072 * @var string 3073 */ 3074 public $pag_links; 3075 3076 /** 3077 * Set the loop 3078 * 3079 * @since BuddyPress (2.3.0) 3080 * 3081 * @access public 3082 * @param array $args component's loop arguments 3083 */ 3084 public function set_loop( $args = array() ) { 3085 $this->args = wp_parse_args( $args, $this->default_args ); 3086 $this->{$this->args['labels']['current_item']} = -1; 3087 3088 // Setting loop parameters 3089 if ( ! empty( $this->args['params'] ) ) { 3090 foreach ( (array) $this->args['params'] as $key => $value ) { 3091 $this->{$key} = $value; 3092 } 3093 3094 if ( ! empty( $_REQUEST[ $this->page_arg ] ) ) { 3095 $this->page = intval( $_REQUEST[ $this->page_arg ] ); 3096 } 3097 3098 if ( ! empty( $_REQUEST['num'] ) ) { 3099 $this->per_page = intval( $_REQUEST['num'] ); 3100 } 3101 } 3102 3103 // Setup the Items to loop through 3104 $items = $this->get_items(); 3105 3106 if ( ! isset( $items[ $this->args['labels']['item_plural'] ] ) || ! isset( $items['total'] ) ) { 3107 return false; 3108 } 3109 3110 // Set items 3111 $this->{$this->args['labels']['item_plural']} = $items[ $this->args['labels']['item_plural'] ]; 3112 3113 if ( empty( $this->{$this->args['labels']['item_plural']} ) ) { 3114 $this->{$this->args['labels']['item_count']} = 0; 3115 $this->{$this->args['labels']['item_total_count']} = 0; 3116 3117 } else { 3118 // Set the total items count 3119 $this->{$this->args['labels']['item_total_count']} = (int) $items['total']; 3120 3121 if ( ! empty( $this->max ) && ( $this->max < (int) $items['total'] ) ) { 3122 $this->{$this->args['labels']['item_total_count']} = (int) $this->max; 3123 } 3124 3125 // Set current items count 3126 $this->{$this->args['labels']['item_count']} = count( $this->{$this->args['labels']['item_plural']} ); 3127 3128 if ( ! empty( $this->max ) && ( $this->max < (int) $this->{$this->args['labels']['item_count']} ) ) { 3129 $this->{$this->args['labels']['item_count']} = (int) $this->max; 3130 } 3131 } 3132 3133 if ( ! empty( $this->{$this->args['labels']['item_total_count']} ) && ! empty( $this->per_page ) && ! empty( $this->page ) ) { 3134 $this->set_pag_links(); 3135 } 3136 } 3137 3138 /** 3139 * Get the component's items 3140 * 3141 * This method needs to be set within the custom Template class 3142 * @see BP_Blogs_Template->get_items() for an example of use 3143 * 3144 * @since BuddyPress (2.3.0) 3145 * 3146 * @access public 3147 */ 3148 abstract public function get_items(); 3149 3150 /** 3151 * Set the pagination links 3152 * 3153 * Each item loop can override this and sets its own pagination links 3154 * within its custom template class. 3155 * @see BP_Groups_Template->set_pag_links() for an example of use 3156 * 3157 * @since BuddyPress (2.3.0) 3158 * 3159 * @access public 3160 */ 3161 public function set_pag_links() { 3162 $this->pag_links = paginate_links( array( 3163 'base' => add_query_arg( $this->page_arg, '%#%' ), 3164 'format' => '', 3165 'total' => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ), 3166 'current' => (int) $this->page, 3167 'prev_text' => _x( '←', 'pagination previous text', 'buddypress' ), 3168 'next_text' => _x( '→', 'pagination next text', 'buddypress' ), 3169 'mid_size' => 1, 3170 'add_args' => array(), 3171 ) ); 3172 } 3173 3174 /** 3175 * Whether there are Items available in the loop. 3176 * 3177 * @since BuddyPress (2.3.0) 3178 * 3179 * @return bool True if there are items in the loop, otherwise false. 3180 */ 3181 public function has_items() { 3182 if ( ! empty( $this->{$this->args['labels']['item_count']} ) ) { 3183 return true; 3184 } 3185 3186 return false; 3187 } 3188 3189 /** 3190 * Set up the next item and iterate index. 3191 * 3192 * @since BuddyPress (2.3.0) 3193 * 3194 * @return object The next item to iterate over. 3195 */ 3196 public function next_item() { 3197 3198 $this->{$this->args['labels']['current_item']}++; 3199 3200 $this->{$this->args['labels']['item']} = $this->{$this->args['labels']['item_plural']}[ $this->{$this->args['labels']['current_item']} ]; 3201 3202 return $this->{$this->args['labels']['item']}; 3203 } 3204 3205 /** 3206 * Rewind the items and reset items index. 3207 * 3208 * @since BuddyPress (2.3.0) 3209 */ 3210 public function rewind_items() { 3211 3212 $this->{$this->args['labels']['current_item']} = -1; 3213 3214 if ( $this->{$this->args['labels']['item_count']} > 0 ) { 3215 $this->{$this->args['labels']['item']} = $this->{$this->args['labels']['item_plural']}[0]; 3216 } 3217 } 3218 3219 /** 3220 * Whether there are items left in the loop to iterate over. 3221 * 3222 * @since BuddyPress (2.3.0) 3223 * 3224 * @return bool True if there are more items to show, 3225 * otherwise false. 3226 */ 3227 public function items() { 3228 3229 if ( $this->{$this->args['labels']['current_item']} + 1 < $this->{$this->args['labels']['item_count']} ) { 3230 return true; 3231 3232 } elseif ( $this->{$this->args['labels']['current_item']} + 1 == $this->{$this->args['labels']['item_count']} ) { 3233 /** 3234 * Inform when the item loop ends. Dynamically built using the item name 3235 * For instance the groups loops is using 'group' as its item name, as 3236 * a result the action will be 'group_loop_end' 3237 */ 3238 do_action( $this->args['labels']['item'] . '_loop_end' ); 3239 3240 $this->rewind_items(); 3241 } 3242 3243 $this->in_the_loop = false; 3244 return false; 3245 } 3246 3247 /** 3248 * Set up the current item inside the loop. 3249 * 3250 * @since BuddyPress (2.3.0) 3251 */ 3252 public function the_item() { 3253 $this->in_the_loop = true; 3254 $this->{$this->args['labels']['item']} = $this->next_item(); 3255 3256 // loop has just started 3257 if ( 0 === $this->{$this->args['labels']['current_item']} ) { 3258 /** 3259 * Inform when the item loop starts. Dynamically built using the item name 3260 * For instance the groups loops is using 'group' as its item name, as 3261 * a result the action will be 'group_loop_start' 3262 */ 3263 do_action( $this->args['labels']['item'] . '_loop_start' ); 3264 } 3265 } 3266 } -
src/bp-groups/bp-groups-template.php
diff --git src/bp-groups/bp-groups-template.php src/bp-groups/bp-groups-template.php index a2e546b..6dd7bcf 100644
function bp_groups_directory_permalink() { 63 63 function bp_get_groups_directory_permalink() { 64 64 return apply_filters( 'bp_get_groups_directory_permalink', trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() ) ); 65 65 } 66 67 66 /** 68 * The main Groupstemplate loop class.67 * The main blog template loop class. 69 68 * 70 * Responsible for loading a group of groups into a loop for display.69 * Responsible for loading a group of blogs into a loop for display. 71 70 */ 72 class BP_Groups_Template {71 class BP_Groups_Template extends BP_Template_Loop { 73 72 74 73 /** 75 * The loop iterator.74 * Default arguments for Blogs loop. 76 75 * 77 * @access public 78 * @var int 79 */ 80 var $current_group = -1; 81 82 /** 83 * The number of groups returned by the paged query. 84 * 85 * @access public 86 * @var int 87 */ 88 var $group_count; 89 90 /** 91 * Array of groups located by the query. 76 * @since BuddyPress (2.3.0) 92 77 * 93 * @access p ublic78 * @access protected 94 79 * @var array 95 80 */ 96 var $groups; 97 98 /** 99 * The group object currently being iterated on. 100 * 101 * @access public 102 * @var object 103 */ 104 var $group; 105 106 /** 107 * A flag for whether the loop is currently being iterated. 108 * 109 * @access public 110 * @var bool 111 */ 112 var $in_the_loop; 113 114 /** 115 * The page number being requested. 116 * 117 * @access public 118 * @var public 119 */ 120 var $pag_page; 121 122 /** 123 * The number of items being requested per page. 124 * 125 * @access public 126 * @var public 127 */ 128 var $pag_num; 129 130 /** 131 * An HTML string containing pagination links. 132 * 133 * @access public 134 * @var string 135 */ 136 var $pag_links; 137 138 /** 139 * The total number of groups matching the query parameters. 140 * 141 * @access public 142 * @var int 143 */ 144 var $total_group_count; 145 146 /** 147 * Whether the template loop is for a single group page. 148 * 149 * @access public 150 * @var bool 151 */ 152 var $single_group = false; 153 154 /** 155 * Field to sort by. 156 * 157 * @access public 158 * @var string 159 */ 160 var $sort_by; 161 162 /** 163 * Sort order. 164 * 165 * @access public 166 * @var string 167 */ 168 var $order; 81 protected $default_args = array( 82 'labels' => array( 83 'item' => 'group', 84 'item_plural' => 'groups', 85 'item_total_count' => 'total_group_count', 86 'item_count' => 'group_count', 87 'current_item' => 'current_group', 88 ), 89 'params' => array( 90 'page_arg' => 'grpage', 91 'page' => 1, 92 'per_page' => 20, 93 'max' => 0, 94 ) 95 ); 169 96 170 97 /** 171 98 * Constructor method. … … class BP_Groups_Template { 180 107 * @type int $page Default: 1. 181 108 * } 182 109 */ 183 function __construct( $args = array() ){ 184 110 public function __construct( $args = array() ) { 185 111 // Backward compatibility with old method of passing arguments 186 112 if ( ! is_array( $args ) || func_num_args() > 1 ) { 187 113 _deprecated_argument( __METHOD__, '1.7', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); … … class BP_Groups_Template { 206 132 } 207 133 208 134 $defaults = array( 209 'type' => 'active',210 'page' => 1,211 'per_page' => 20,212 'max' => false,213 'show_hidden' => false,214 'page_arg' => 'grpage',215 'user_id' => 0,216 'slug' => false,217 'include' => false,218 'exclude' => false,219 'search_terms' => '',220 'meta_query' => false,221 'populate_extras' => true,135 'type' => 'active', 136 'page' => 1, 137 'per_page' => 20, 138 'max' => false, 139 'show_hidden' => false, 140 'page_arg' => 'grpage', 141 'user_id' => 0, 142 'slug' => false, 143 'include' => false, 144 'exclude' => false, 145 'search_terms' => '', 146 'meta_query' => false, 147 'populate_extras' => true, 222 148 'update_meta_cache' => true, 223 149 ); 224 150 225 151 $r = wp_parse_args( $args, $defaults ); 226 extract( $r );227 152 228 $this-> pag_page = isset( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : $page;229 $this->pag_num = isset( $_REQUEST['num'] ) ? intval( $_REQUEST['num'] ) : $per_page;153 $this->set_loop( array( 'params' => $r ) ); 154 } 230 155 231 if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) ) 232 $show_hidden = true; 156 /** 157 * Get Groups. 158 * 159 * @since BuddyPress (2.3.0) 160 * 161 * @access public 162 */ 163 public function get_items() { 164 if ( bp_current_user_can( 'bp_moderate' ) || ( is_user_logged_in() && $this->user_id == bp_loggedin_user_id() ) ) { 165 $this->args['params']['show_hidden'] = true; 166 } 167 168 if ( 'invites' == $this->type ) { 169 return groups_get_invites_for_user( $this->user_id, $this->per_page, $this->page, $this->exclude ); 233 170 234 if ( 'invites' == $type ) { 235 $this->groups = groups_get_invites_for_user( $user_id, $this->pag_num, $this->pag_page, $exclude ); 236 } else if ( 'single-group' == $type ) { 171 } else if ( 'single-group' == $this->type ) { 237 172 $this->single_group = true; 238 173 239 174 if ( groups_get_current_group() ) { … … class BP_Groups_Template { 241 176 242 177 } else { 243 178 $group = groups_get_group( array( 244 'group_id' => BP_Groups_Group::get_id_from_slug( $ r['slug']),245 'populate_extras' => $ r['populate_extras'],179 'group_id' => BP_Groups_Group::get_id_from_slug( $this->slug ), 180 'populate_extras' => $this->populate_extras, 246 181 ) ); 247 182 } 248 183 249 // backwards compatibility - the 'group_id' variable is not part of the250 // BP_Groups_Group object, but we add it here for devs doing checks against it251 //252 // @see https://buddypress.trac.wordpress.org/changeset/3540253 //254 // this is subject to removal in a future release; devs should check against255 // $group->id instead256 $group->group_id = $group->id;257 258 $this->groups = array( $group );259 260 } else {261 $this->groups = groups_get_groups( array(262 'type' => $type,263 'order' => $order,264 'orderby' => $orderby,265 'per_page' => $this->pag_num,266 'page' => $this->pag_page,267 'user_id' => $user_id,268 'search_terms' => $search_terms,269 'meta_query' => $meta_query,270 'include' => $include,271 'exclude' => $exclude,272 'populate_extras' => $populate_extras,273 'update_meta_cache' => $update_meta_cache,274 'show_hidden' => $show_hidden275 ) );276 }277 278 if ( 'invites' == $type ) {279 $this->total_group_count = (int) $this->groups['total'];280 $this->group_count = (int) $this->groups['total'];281 $this->groups = $this->groups['groups'];282 } else if ( 'single-group' == $type ) {283 184 if ( empty( $group->id ) ) { 284 $this->total_group_count = 0; 285 $this->group_count = 0; 286 } else { 287 $this->total_group_count = 1; 288 $this->group_count = 1; 289 } 290 } else { 291 if ( empty( $max ) || $max >= (int) $this->groups['total'] ) { 292 $this->total_group_count = (int) $this->groups['total']; 293 } else { 294 $this->total_group_count = (int) $max; 295 } 296 297 $this->groups = $this->groups['groups']; 298 299 if ( !empty( $max ) ) { 300 if ( $max >= count( $this->groups ) ) { 301 $this->group_count = count( $this->groups ); 302 } else { 303 $this->group_count = (int) $max; 304 } 305 } else { 306 $this->group_count = count( $this->groups ); 307 } 308 } 309 310 // Build pagination links 311 if ( (int) $this->total_group_count && (int) $this->pag_num ) { 312 $pag_args = array( 313 $page_arg => '%#%' 314 ); 315 316 if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { 317 $base = remove_query_arg( 's', wp_get_referer() ); 185 return array( 'groups' => array(), 'total' => 0 ); 318 186 } else { 319 $base = '';187 return array( 'groups' => array( $group ), 'total' => 1 ); 320 188 } 321 189 322 $add_args = array( 323 'num' => $this->pag_num, 324 'sortby' => $this->sort_by, 325 'order' => $this->order, 326 ); 327 328 if ( ! empty( $search_terms ) ) { 329 $add_args['s'] = urlencode( $search_terms ); 330 } 331 332 $this->pag_links = paginate_links( array( 333 'base' => add_query_arg( $pag_args, $base ), 334 'format' => '', 335 'total' => ceil( (int) $this->total_group_count / (int) $this->pag_num ), 336 'current' => $this->pag_page, 337 'prev_text' => _x( '←', 'Group pagination previous text', 'buddypress' ), 338 'next_text' => _x( '→', 'Group pagination next text', 'buddypress' ), 339 'mid_size' => 1, 340 'add_args' => $add_args, 341 ) ); 190 } else { 191 return groups_get_groups( $this->args['params'] ); 342 192 } 343 193 } 344 194 345 195 /** 346 * Whether there are groups available in the loop.196 * Set Groups pagination. 347 197 * 348 * @s ee bp_has_groups()198 * @since BuddyPress (2.3.0) 349 199 * 350 * @return bool True if there are items in the loop, otherwise false. 351 */ 352 function has_groups() { 353 if ( $this->group_count ) 354 return true; 355 356 return false; 357 } 358 359 /** 360 * Set up the next group and iterate index. 361 * 362 * @return object The next group to iterate over. 363 */ 364 function next_group() { 365 $this->current_group++; 366 $this->group = $this->groups[$this->current_group]; 367 368 return $this->group; 369 } 370 371 /** 372 * Rewind the groups and reset member index. 200 * @access public 373 201 */ 374 function rewind_groups() { 375 $this->current_group = -1; 376 if ( $this->group_count > 0 ) { 377 $this->group = $this->groups[0]; 378 } 379 } 202 public function set_pag_links() { 203 $pag_args = array( 204 $this->page_arg => '%#%' 205 ); 380 206 381 /** 382 * Whether there are groups left in the loop to iterate over. 383 * 384 * This method is used by {@link bp_groups()} as part of the while loop 385 * that controls iteration inside the groups loop, eg: 386 * while ( bp_groups() ) { ... 387 * 388 * @see bp_groups() 389 * 390 * @return bool True if there are more groups to show, otherwise false. 391 */ 392 function groups() { 393 if ( $this->current_group + 1 < $this->group_count ) { 394 return true; 395 } elseif ( $this->current_group + 1 == $this->group_count ) { 396 do_action('group_loop_end'); 397 // Do some cleaning up after the loop 398 $this->rewind_groups(); 207 if ( defined( 'DOING_AJAX' ) && true === (bool) DOING_AJAX ) { 208 $base = remove_query_arg( 's', wp_get_referer() ); 209 } else { 210 $base = ''; 399 211 } 400 212 401 $this->in_the_loop = false; 402 return false; 403 } 213 $add_args = array( 214 'num' => $this->per_page, 215 'sortby' => $this->orderby, 216 'order' => $this->order, 217 ); 404 218 405 /** 406 * Set up the current group inside the loop. 407 * 408 * Used by {@link bp_the_group()} to set up the current group data 409 * while looping, so that template tags used during that iteration make 410 * reference to the current member. 411 * 412 * @see bp_the_group() 413 */ 414 function the_group() { 415 $this->in_the_loop = true; 416 $this->group = $this->next_group(); 219 if ( ! empty( $this->search_terms ) ) { 220 $add_args['s'] = urlencode( $this->search_terms ); 221 } 417 222 418 if ( 0 == $this->current_group ) // loop has just started 419 do_action('group_loop_start'); 223 $this->pag_links = paginate_links( array( 224 'base' => add_query_arg( $pag_args, $base ), 225 'format' => '', 226 'total' => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ), 227 'current' => (int) $this->page, 228 'prev_text' => _x( '←', 'Group pagination previous text', 'buddypress' ), 229 'next_text' => _x( '→', 'Group pagination next text', 'buddypress' ), 230 'mid_size' => 1, 231 'add_args' => $add_args, 232 ) ); 420 233 } 421 234 } 422 235 … … function bp_has_groups( $args = '' ) { 539 352 ) ); 540 353 541 354 // Filter and return whether or not the groups loop has groups in it 542 return apply_filters( 'bp_has_groups', $groups_template->has_ groups(), $groups_template, $r );355 return apply_filters( 'bp_has_groups', $groups_template->has_items(), $groups_template, $r ); 543 356 } 544 357 545 358 /** … … function bp_has_groups( $args = '' ) { 549 362 */ 550 363 function bp_groups() { 551 364 global $groups_template; 552 return $groups_template-> groups();365 return $groups_template->items(); 553 366 } 554 367 555 368 /** … … function bp_groups() { 559 372 */ 560 373 function bp_the_group() { 561 374 global $groups_template; 562 return $groups_template->the_ group();375 return $groups_template->the_item(); 563 376 } 564 377 565 378 /** … … function bp_groups_pagination_count() { 1575 1388 function bp_get_groups_pagination_count() { 1576 1389 global $groups_template; 1577 1390 1578 $start_num = intval( ( $groups_template->pag _page - 1 ) * $groups_template->pag_num) + 1;1391 $start_num = intval( ( $groups_template->page - 1 ) * $groups_template->per_page ) + 1; 1579 1392 $from_num = bp_core_number_format( $start_num ); 1580 $to_num = bp_core_number_format( ( $start_num + ( $groups_template->p ag_num - 1 ) > $groups_template->total_group_count ) ? $groups_template->total_group_count : $start_num + ( $groups_template->pag_num- 1 ) );1393 $to_num = bp_core_number_format( ( $start_num + ( $groups_template->per_page - 1 ) > $groups_template->total_group_count ) ? $groups_template->total_group_count : $start_num + ( $groups_template->per_page - 1 ) ); 1581 1394 $total = bp_core_number_format( $groups_template->total_group_count ); 1582 1395 1583 1396 return apply_filters( 'bp_get_groups_pagination_count', sprintf( _n( 'Viewing 1 group', 'Viewing %1$s - %2$s of %3$s groups', $total, 'buddypress' ), $from_num, $to_num, $total ), $from_num, $to_num, $total ); -
tests/phpunit/testcases/blogs/class-bp-blogs-template.php
diff --git tests/phpunit/testcases/blogs/class-bp-blogs-template.php tests/phpunit/testcases/blogs/class-bp-blogs-template.php index e69de29..61c2632 100644
1 <?php 2 /** 3 * @group BP_Blogs_Template 4 */ 5 class BP_Tests_BP_Blogs_Template_TestCases extends BP_UnitTestCase { 6 7 public function setUp() { 8 parent::setUp(); 9 } 10 11 public function tearDown() { 12 parent::tearDown(); 13 } 14 15 protected function blogs_get_args( $args = array() ) { 16 return wp_parse_args( $args, array( 17 'type' => 'active', 18 'page_arg' => 'bpage', 19 'page' => 1, 20 'per_page' => 20, 21 'max' => false, 22 'user_id' => 0, 23 'include_blog_ids' => false, 24 'search_terms' => '', 25 'update_meta_cache' => true 26 ) ); 27 } 28 29 /** 30 * @group BP_Blogs_Template 31 */ 32 public function test_bp_blogs_template() { 33 if ( ! is_multisite() ) { 34 return; 35 } 36 37 // Default arguments 38 $r = $this->blogs_get_args(); 39 40 // Get the blogs 41 $blogs_empty = new BP_Blogs_Template( $r['type'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['search_terms'], $r['page_arg'], $r['update_meta_cache'], $r['include_blog_ids'] ); 42 43 $this->assertEquals( 0, count( $blogs_empty->blogs ) ); 44 $this->assertEquals( 0, $blogs_empty->blog_count ); 45 $this->assertEquals( 0, $blogs_empty->total_blog_count ); 46 47 $blogs = array( 48 'test_loop1' => array( 'blogname' => 'TestLoop1', 'blogdescription' => 'TestLoop1 Description', 'public' => 1 ), 49 'test_loop2' => array( 'blogname' => 'TestLoop2', 'blogdescription' => 'TestLoop2 Description', 'public' => 1 ), 50 ); 51 52 // Not setting user login or email generates errors in unit tests ? 53 $u_test_loop = array( 54 'test_loop1' => $this->factory->user->create( array( 'role' => '' ) ), 55 'test_loop2' => $this->factory->user->create( array( 'role' => '' ) ), 56 ); 57 58 $test_loop = array(); 59 $expected_loop = array(); 60 $include_blog_ids = array(); 61 62 foreach( $blogs as $path => $meta ) { 63 64 $blogs[ $path ]['id'] = $this->factory->blog->create( array( 65 'path' => '/' . $path, 66 'meta' => $meta, 67 'user_id' => $u_test_loop[ $path ], 68 ) ); 69 70 $include_blog_ids[] = $blogs[ $path ]['id']; 71 72 $expected_loop[] = array( 73 'blog_id' => $blogs[ $path ]['id'], 74 'admin_user_id' => $u_test_loop[ $path ], 75 'name' => $meta['blogname'], 76 'description' => $meta['blogdescription'], 77 ); 78 } 79 80 $r = $this->blogs_get_args( array( 81 'per_page' => 1, 82 'include_blog_ids' => $include_blog_ids, 83 ) ); 84 85 $blogs_page_1 = new BP_Blogs_Template( $r['type'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['search_terms'], $r['page_arg'], $r['update_meta_cache'], $r['include_blog_ids'] ); 86 $the_blogs = $blogs_page_1->blogs; 87 88 $this->assertEquals( 1, count( $blogs_page_1->blogs ) ); 89 $this->assertEquals( 1, $blogs_page_1->blog_count ); 90 $this->assertEquals( 2, $blogs_page_1->total_blog_count ); 91 92 preg_match( '/\?bpage=(.*)\'/', $blogs_page_1->pag_links, $matches ); 93 $r['page'] = intval( $matches[1] ); 94 95 $blogs_page_2 = new BP_Blogs_Template( $r['type'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['search_terms'], $r['page_arg'], $r['update_meta_cache'], $r['include_blog_ids'] ); 96 $the_blogs[] = $blogs_page_2->blogs[0]; 97 98 foreach ( $the_blogs as $blog ) { 99 $test_loop[] = array( 100 'blog_id' => $blog->blog_id, 101 'admin_user_id' => $blog->admin_user_id, 102 'name' => $blog->name, 103 'description' => $blog->description, 104 ); 105 } 106 107 $this->assertEqualSets( $expected_loop, $test_loop ); 108 } 109 } -
tests/phpunit/testcases/groups/class-bp-groups-template.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-template.php tests/phpunit/testcases/groups/class-bp-groups-template.php index e69de29..68d827f 100644
1 <?php 2 /** 3 * @group BP_Groups_Template 4 */ 5 class BP_Tests_BP_Groups_Template_TestCases extends BP_UnitTestCase { 6 7 public function setUp() { 8 parent::setUp(); 9 } 10 11 public function tearDown() { 12 parent::tearDown(); 13 } 14 15 protected function groups_get_args( $args = array() ) { 16 return wp_parse_args( $args, array( 17 'type' => '', 18 'order' => 'DESC', 19 'orderby' => 'last_activity', 20 'page' => 1, 21 'per_page' => 20, 22 'max' => false, 23 'show_hidden' => false, 24 'page_arg' => 'grpage', 25 'user_id' => 0, 26 'slug' => false, 27 'search_terms' => false, 28 'meta_query' => false, 29 'include' => false, 30 'exclude' => false, 31 'populate_extras' => true, 32 'update_meta_cache' => true, 33 ) ); 34 } 35 36 /** 37 * @group BP_Groups_Template 38 */ 39 public function test_bp_groups_template_directory() { 40 41 // Default arguments 42 $r = $this->groups_get_args(); 43 44 $groups_empty = new BP_Groups_Template( $r ); 45 46 $this->assertEquals( 0, count( $groups_empty->groups ) ); 47 $this->assertEquals( 0, $groups_empty->group_count ); 48 $this->assertEquals( 0, $groups_empty->total_group_count ); 49 50 $u1 = $this->factory->user->create(); 51 $u2 = $this->factory->user->create(); 52 $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) ); 53 $g2 = $this->factory->group->create( array( 'creator_id' => $u2, 'status' => 'private' ) ); 54 55 // Add a user to one of the group 56 groups_join_group( $g1, $u2 ); 57 58 // Update one of the group's last activity 59 groups_update_groupmeta( $g1, 'last_activity', date( 'Y-m-d H:i:s' , strtotime( '+1 hour' ) ) ); 60 61 // Set pagination 62 $r = $this->groups_get_args( array( 'per_page' => 1 ) ); 63 64 $groups_page_1 = new BP_Groups_Template( $r ); 65 $the_groups = $groups_page_1->groups; 66 67 $this->assertEquals( 1, count( $groups_page_1->groups ) ); 68 $this->assertEquals( 1, $groups_page_1->group_count ); 69 $this->assertEquals( 2, $groups_page_1->total_group_count ); 70 71 preg_match( '/\?grpage=(.*)\'/', $groups_page_1->pag_links, $matches ); 72 73 // Set page 74 $r = $this->groups_get_args( array( 75 'per_page' => 1, 76 'page' => intval( $matches[1] ) 77 ) ); 78 79 $groups_page_2 = new BP_Groups_Template( $r ); 80 $the_groups[] = $groups_page_2->groups[0]; 81 $expected = array(); 82 $groups_loop = array(); 83 84 foreach ( $the_groups as $group ) { 85 86 $groups_loop[] = array( 87 'id' => $group->id, 88 'creator_id' => $group->creator_id, 89 'status' => $group->status, 90 'total_member_count' => $group->total_member_count, 91 'last_activity' => $group->last_activity, 92 ); 93 94 $check_group = groups_get_group( array( 'group_id' => $group->id, 'populate_extras' => true ) ); 95 96 $expected[] = array( 97 'id' => $check_group->id, 98 'creator_id' => $check_group->creator_id, 99 'status' => $check_group->status, 100 'total_member_count' => $check_group->total_member_count, 101 'last_activity' => $check_group->last_activity, 102 ); 103 } 104 105 $this->assertEqualSets( $groups_loop, $expected ); 106 } 107 108 /** 109 * @group BP_Groups_Template 110 */ 111 public function test_bp_groups_template_invites() { 112 113 $u1 = $this->factory->user->create(); 114 $u2 = $this->factory->user->create(); 115 $g1 = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 116 $g2 = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'hidden' ) ); 117 118 groups_invite_user( array( 119 'user_id' => $u2, 120 'group_id' => $g1, 121 'inviter_id' => $u1, 122 ) ); 123 124 groups_send_invites( $u1, $g1 ); 125 126 groups_invite_user( array( 127 'user_id' => $u2, 128 'group_id' => $g2, 129 'inviter_id' => $u1, 130 ) ); 131 132 groups_send_invites( $u1, $g2 ); 133 134 // Invites arguments 135 $r = $this->groups_get_args( array( 136 'type' => 'invites', 137 'user_id' => $u2, 138 'show_hidden' => true, 139 ) ); 140 141 $groups_invites = new BP_Groups_Template( $r ); 142 143 $this->assertEquals( 2, $groups_invites->total_group_count ); 144 145 $this->assertEqualSets( array( $g1, $g2 ), wp_list_pluck( $groups_invites->groups, 'id' ) ); 146 } 147 148 /** 149 * @group BP_Groups_Template 150 */ 151 public function test_bp_groups_template_single_group() { 152 153 $u = $this->factory->user->create(); 154 $g = $this->factory->group->create( array( 'creator_id' => $u ) ); 155 156 $group = groups_get_group( array( 'group_id' => $g ) ); 157 158 // Single group arguments 159 $r = $this->groups_get_args( array( 160 'type' => 'single-group', 161 'slug' => $group->slug, 162 ) ); 163 164 $groups_single = new BP_Groups_Template( $r ); 165 166 $this->assertEquals( 1, $groups_single->total_group_count ); 167 $this->assertTrue( $groups_single->single_group ); 168 $this->assertEquals( $g, $groups_single->groups[0]->id ); 169 } 170 }