Ticket #1269: 1269.01.patch
File 1269.01.patch, 18.9 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 838f54f..c72912f 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. 114 * @since BuddyPress (2.3.0) 121 115 * 122 * @access public 123 * @var int 124 */ 125 public $blog_count = 0; 126 127 /** 128 * Array of blogs located by the query.. 129 * 130 * @access public 116 * @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 $args = 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 ) ); 253 } 165 $this->set_loop( $args ); 254 166 } 255 167 256 168 /** 257 * Whether there are blogs available in the loop. 258 * 259 * @see bp_has_blogs() 169 * Get Blogs. 260 170 * 261 * @return bool True if there are items in the loop, otherwise false. 262 */ 263 public function has_blogs() { 264 return (bool) ! empty( $this->blog_count ); 265 } 266 267 /** 268 * Set up the next blog and iterate index. 269 * 270 * @return object The next blog to iterate over. 271 */ 272 public function next_blog() { 273 $this->current_blog++; 274 $this->blog = $this->blogs[ $this->current_blog ]; 275 276 return $this->blog; 277 } 278 279 /** 280 * Rewind the blogs and reset blog index. 281 */ 282 public function rewind_blogs() { 283 $this->current_blog = -1; 284 if ( $this->blog_count > 0 ) { 285 $this->blog = $this->blogs[0]; 286 } 287 } 288 289 /** 290 * Whether there are blogs left in the loop to iterate over. 171 * @since BuddyPress (2.3.0) 291 172 * 292 * This method is used by {@link bp_blogs()} as part of the while loop 293 * that controls iteration inside the blogs loop, eg: 294 * while ( bp_blogs() ) { ... 295 * 296 * @see bp_blogs() 297 * 298 * @return bool True if there are more blogs to show, otherwise false. 173 * @access public 299 174 */ 300 public function blogs() {301 if ( ( $this->current_blog + 1 ) < $this->blog_count ) {302 return true;303 } 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 ); 304 179 305 /** 306 * Fires right before the rewinding of blogs listing after all are shown. 307 * 308 * @since BuddyPress (1.5.0) 309 */ 310 do_action( 'blog_loop_end' ); 311 // Do some cleaning up after the loop 312 $this->rewind_blogs(); 180 // Typical blogs query 181 } else { 182 $blogs = bp_blogs_get_blogs( $this->args['params'] ); 313 183 } 314 184 315 $this->in_the_loop = false; 316 return false; 317 } 318 319 /** 320 * Set up the current blog inside the loop. 321 * 322 * Used by {@link bp_the_blog()} to set up the current blog data while 323 * looping, so that template tags used during that iteration make 324 * reference to the current blog. 325 * 326 * @see bp_the_blog() 327 */ 328 public function the_blog() { 329 330 $this->in_the_loop = true; 331 $this->blog = $this->next_blog(); 332 333 // loop has just started 334 if ( 0 === $this->current_blog ) { 335 336 /** 337 * Fires if on the first blog in the loop. 338 * 339 * @since BuddyPress (1.5.0) 340 */ 341 do_action( 'blog_loop_start' ); 342 } 185 return $blogs; 343 186 } 344 187 } 345 188 … … class BP_Blogs_Template { 349 192 function bp_rewind_blogs() { 350 193 global $blogs_template; 351 194 352 $blogs_template->rewind_ blogs();195 $blogs_template->rewind_items(); 353 196 } 354 197 355 198 /** … … function bp_has_blogs( $args = '' ) { 428 271 * @param BP_Blogs_Template $blogs_template Current blogs template object. 429 272 * @param array $r Parsed arguments used in blogs template query. 430 273 */ 431 return apply_filters( 'bp_has_blogs', $blogs_template->has_ blogs(), $blogs_template, $r );274 return apply_filters( 'bp_has_blogs', $blogs_template->has_items(), $blogs_template, $r ); 432 275 } 433 276 434 277 /** … … function bp_has_blogs( $args = '' ) { 441 284 function bp_blogs() { 442 285 global $blogs_template; 443 286 444 return $blogs_template-> blogs();287 return $blogs_template->items(); 445 288 } 446 289 447 290 /** … … function bp_blogs() { 454 297 function bp_the_blog() { 455 298 global $blogs_template; 456 299 457 return $blogs_template->the_ blog();300 return $blogs_template->the_item(); 458 301 } 459 302 460 303 /** … … function bp_the_blog() { 465 308 function bp_blogs_pagination_count() { 466 309 global $blogs_template; 467 310 468 $start_num = intval( ( $blogs_template->pag _page - 1 ) * $blogs_template->pag_num) + 1;311 $start_num = intval( ( $blogs_template->page - 1 ) * $blogs_template->per_page ) + 1; 469 312 $from_num = bp_core_number_format( $start_num ); 470 $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 ) );313 $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 ) ); 471 314 $total = bp_core_number_format( $blogs_template->total_blog_count ); 472 315 473 316 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 bc3afdb..19ed833 100644
class BP_Members_Suggestions extends BP_Suggestions { 2754 2754 return apply_filters( 'bp_members_suggestions_get_suggestions', $results, $this ); 2755 2755 } 2756 2756 } 2757 2758 /** 2759 * Base class for components template loops. 2760 * 2761 * @since 2.3.0 2762 */ 2763 abstract class BP_Template_Loop { 2764 2765 /** 2766 * Default arguments common to all components loop. 2767 * 2768 * @since BuddyPress (2.3.0) 2769 * 2770 * @access protected 2771 * @var array 2772 */ 2773 protected $default_args = array( 2774 'labels' => array( 2775 'item' => 'item', 2776 'item_plural' => 'items', 2777 'item_total_count' => 'total_item_count', 2778 'item_count' => 'item_count', 2779 'current_item' => 'current_item', 2780 ), 2781 'params' => array( 2782 'page_arg' => 'page', 2783 'page' => 1, 2784 'per_page' => 20, 2785 'max' => 0, 2786 ) 2787 ); 2788 2789 /** 2790 * Holds the arguments to customize the loop labels and the component's items query. 2791 * 2792 * This includes `$default_args`, as well as the user-supplied values. 2793 * 2794 * @since BuddyPress (2.3.0) 2795 * 2796 * @access protected 2797 * @var array 2798 */ 2799 protected $args = array(); 2800 2801 /** 2802 * A flag for whether the loop is currently being iterated. 2803 * 2804 * @since BuddyPress (2.3.0) 2805 * 2806 * @access public 2807 * @var bool 2808 */ 2809 public $in_the_loop; 2810 2811 /** 2812 * An HTML string containing pagination links. 2813 * 2814 * @since BuddyPress (2.3.0) 2815 * 2816 * @access public 2817 * @var string 2818 */ 2819 public $pag_links; 2820 2821 /** 2822 * Set the loop 2823 * 2824 * @since BuddyPress (2.3.0) 2825 * 2826 * @access public 2827 * @param array $args component's loop arguments 2828 */ 2829 public function set_loop( $args = array() ) { 2830 $this->args = wp_parse_args( $args, $this->default_args ); 2831 $this->{$this->args['labels']['current_item']} = -1; 2832 2833 // Setting loop parameters 2834 if ( ! empty( $this->args['params'] ) ) { 2835 foreach ( (array) $this->args['params'] as $key => $value ) { 2836 $this->{$key} = $value; 2837 } 2838 2839 if ( ! empty( $_REQUEST[ $this->page_arg ] ) ) { 2840 $this->page = intval( $_REQUEST[ $this->page_arg ] ); 2841 } 2842 2843 if ( ! empty( $_REQUEST['num'] ) ) { 2844 $this->per_page = intval( $_REQUEST['num'] ); 2845 } 2846 } 2847 2848 // Setup the Items to loop through 2849 $items = $this->get_items(); 2850 2851 if ( ! isset( $items[ $this->args['labels']['item_plural'] ] ) || ! isset( $items['total'] ) ) { 2852 return false; 2853 } 2854 2855 // Set items 2856 $this->{$this->args['labels']['item_plural']} = $items[ $this->args['labels']['item_plural'] ]; 2857 2858 if ( empty( $this->{$this->args['labels']['item_plural']} ) ) { 2859 $this->{$this->args['labels']['item_count']} = 0; 2860 $this->{$this->args['labels']['item_total_count']} = 0; 2861 2862 } else { 2863 // Set the total items count 2864 $this->{$this->args['labels']['item_total_count']} = (int) $items['total']; 2865 2866 if ( ! empty( $this->max ) && ( $this->max < (int) $items['total'] ) ) { 2867 $this->{$this->args['labels']['item_total_count']} = (int) $this->max; 2868 } 2869 2870 // Set current items count 2871 $this->{$this->args['labels']['item_count']} = count( $this->{$this->args['labels']['item_plural']} ); 2872 2873 if ( ! empty( $this->max ) && ( $this->max < (int) $this->{$this->args['labels']['item_count']} ) ) { 2874 $this->{$this->args['labels']['item_count']} = (int) $this->max; 2875 } 2876 } 2877 2878 if ( ! empty( $this->{$this->args['labels']['item_total_count']} ) && ! empty( $this->per_page ) && ! empty( $this->page ) ) { 2879 $this->set_pag_links(); 2880 } 2881 } 2882 2883 /** 2884 * Get the component's items 2885 * 2886 * @since BuddyPress (2.3.0) 2887 * 2888 * @access public 2889 */ 2890 abstract public function get_items(); 2891 2892 /** 2893 * Set the pagination links 2894 * 2895 * @since BuddyPress (2.3.0) 2896 * 2897 * @access public 2898 */ 2899 public function set_pag_links() { 2900 $this->pag_links = paginate_links( array( 2901 'base' => add_query_arg( $this->page_arg, '%#%' ), 2902 'format' => '', 2903 'total' => ceil( (int) $this->{$this->args['labels']['item_total_count']} / (int) $this->per_page ), 2904 'current' => (int) $this->page, 2905 'prev_text' => _x( '←', 'pagination previous text', 'buddypress' ), 2906 'next_text' => _x( '→', 'pagination next text', 'buddypress' ), 2907 'mid_size' => 1, 2908 ) ); 2909 } 2910 2911 /** 2912 * Whether there are Items available in the loop. 2913 * 2914 * @since BuddyPress (2.3.0) 2915 * 2916 * @return bool True if there are items in the loop, otherwise false. 2917 */ 2918 public function has_items() { 2919 if ( ! empty( $this->{$this->args['labels']['item_count']} ) ) { 2920 return true; 2921 } 2922 2923 return false; 2924 } 2925 2926 /** 2927 * Set up the next item and iterate index. 2928 * 2929 * @since BuddyPress (2.3.0) 2930 * 2931 * @return object The next item to iterate over. 2932 */ 2933 public function next_item() { 2934 2935 $this->{$this->args['labels']['current_item']}++; 2936 2937 $this->{$this->args['labels']['item']} = $this->{$this->args['labels']['item_plural']}[ $this->{$this->args['labels']['current_item']} ]; 2938 2939 return $this->{$this->args['labels']['item']}; 2940 } 2941 2942 /** 2943 * Rewind the items and reset items index. 2944 * 2945 * @since BuddyPress (2.3.0) 2946 */ 2947 public function rewind_items() { 2948 2949 $this->{$this->args['labels']['current_item']} = -1; 2950 2951 if ( $this->{$this->args['labels']['item_count']} > 0 ) { 2952 $this->{$this->args['labels']['item']} = $this->{$this->args['labels']['item_plural']}[0]; 2953 } 2954 } 2955 2956 /** 2957 * Whether there are items left in the loop to iterate over. 2958 * 2959 * @since BuddyPress (2.3.0) 2960 * 2961 * @return bool True if there are more items to show, 2962 * otherwise false. 2963 */ 2964 public function items() { 2965 2966 if ( $this->{$this->args['labels']['current_item']} + 1 < $this->{$this->args['labels']['item_count']} ) { 2967 return true; 2968 2969 } elseif ( $this->{$this->args['labels']['current_item']} + 1 == $this->{$this->args['labels']['item_count']} ) { 2970 do_action( $this->args['labels']['item'] . '_loop_end' ); 2971 2972 $this->rewind_items(); 2973 } 2974 2975 $this->in_the_loop = false; 2976 return false; 2977 } 2978 2979 /** 2980 * Set up the current item inside the loop. 2981 * 2982 * @since BuddyPress (2.3.0) 2983 */ 2984 public function the_item() { 2985 $this->in_the_loop = true; 2986 $this->{$this->args['labels']['item']} = $this->next_item(); 2987 2988 // loop has just started 2989 if ( 0 === $this->{$this->args['labels']['current_item']} ) { 2990 do_action( $this->args['labels']['item'] . '_loop_start' ); 2991 } 2992 } 2993 } -
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..df90241 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 /** 16 * @group BP_Blogs_Template 17 */ 18 public function test_bp_blogs_template() { 19 if ( ! is_multisite() ) { 20 return; 21 } 22 23 // Parse arguments 24 $r = array( 25 'type' => 'active', 26 'page_arg' => 'bpage', 27 'page' => 1, 28 'per_page' => 1, 29 'max' => false, 30 'user_id' => 0, 31 'include_blog_ids' => false, 32 'search_terms' => '', 33 'update_meta_cache' => true 34 ); 35 36 // Get the blogs 37 $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'] ); 38 39 $this->assertEquals( 0, count( $blogs_empty->blogs ) ); 40 $this->assertEquals( 0, $blogs_empty->blog_count ); 41 $this->assertEquals( 0, $blogs_empty->total_blog_count ); 42 43 $blogs = array( 44 'test_loop1' => array( 'blogname' => 'TestLoop1', 'blogdescription' => 'TestLoop1 Description', 'public' => 1 ), 45 'test_loop2' => array( 'blogname' => 'TestLoop2', 'blogdescription' => 'TestLoop2 Description', 'public' => 1 ), 46 ); 47 48 $users = array( 49 'test_loop1' => 0, 50 'test_loop2' => 0, 51 ); 52 53 $test_loop = array(); 54 $expected_loop = array(); 55 $r['include_blog_ids'] = array(); 56 57 foreach( $blogs as $path => $meta ) { 58 $users[ $path ] = $this->factory->user->create( array( 'role' => '' ) ); 59 60 $blogs[ $path ]['id'] = $this->factory->blog->create( array( 61 'path' => '/' . $path, 62 'meta' => $meta, 63 'user_id' => $users[ $path ], 64 ) ); 65 66 $r['include_blog_ids'][] = $blogs[ $path ]['id']; 67 68 $expected_loop[] = array( 69 'blog_id' => $blogs[ $path ]['id'], 70 'admin_user_id' => $users[ $path ], 71 'name' => $meta['blogname'], 72 'description' => $meta['blogdescription'], 73 ); 74 } 75 76 bp_blogs_record_existing_blogs(); 77 78 $blogs_q1 = 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'] ); 79 $the_blogs = $blogs_q1->blogs; 80 81 $this->assertEquals( 1, count( $blogs_q1->blogs ) ); 82 $this->assertEquals( 1, $blogs_q1->blog_count ); 83 $this->assertEquals( 2, $blogs_q1->total_blog_count ); 84 85 preg_match( '/\?bpage=(.*)\'/', $blogs_q1->pag_links, $matches ); 86 $r['page'] = intval( $matches[1] ); 87 88 $blogs_q2 = 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'] ); 89 $the_blogs[] = $blogs_q2->blogs[0]; 90 91 foreach ( $the_blogs as $blog ) { 92 $test_loop[] = array( 93 'blog_id' => $blog->blog_id, 94 'admin_user_id' => $blog->admin_user_id, 95 'name' => $blog->name, 96 'description' => $blog->description, 97 ); 98 } 99 100 $this->assertEqualSets( $expected_loop, $test_loop ); 101 } 102 }