| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | /**
|
|---|
| 4 | * If you want to go a step further, you can create your own custom WordPress loop for your component.
|
|---|
| 5 | * By doing this you could output a number of items within a loop, just as you would output a number
|
|---|
| 6 | * of blog posts within a standard WordPress loop.
|
|---|
| 7 | *
|
|---|
| 8 | * The example template class below would allow you do the following in the template file:
|
|---|
| 9 | *
|
|---|
| 10 | * <?php if ( bp_get_plans_has_items() ) : ?>
|
|---|
| 11 | *
|
|---|
| 12 | * <?php while ( bp_get_plans_items() ) : bp_get_plans_the_item(); ?>
|
|---|
| 13 | *
|
|---|
| 14 | * <p><?php bp_get_plans_item_name() ?></p>
|
|---|
| 15 | *
|
|---|
| 16 | * <?php endwhile; ?>
|
|---|
| 17 | *
|
|---|
| 18 | * <?php else : ?>
|
|---|
| 19 | *
|
|---|
| 20 | * <p class="error">No items!</p>
|
|---|
| 21 | *
|
|---|
| 22 | * <?php endif; ?>
|
|---|
| 23 | *
|
|---|
| 24 | * Obviously, you'd want to be more specific than the word 'item'.
|
|---|
| 25 | *
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | abstract class BP_Loop_Template {
|
|---|
| 29 | protected $current_item = -1;
|
|---|
| 30 | public $item_count;
|
|---|
| 31 | public $items;
|
|---|
| 32 | public $item;
|
|---|
| 33 |
|
|---|
| 34 | protected $in_the_loop;
|
|---|
| 35 |
|
|---|
| 36 | public $pag_page;
|
|---|
| 37 | public $pag_num;
|
|---|
| 38 | public $pag_links;
|
|---|
| 39 |
|
|---|
| 40 | /*
|
|---|
| 41 | * Functions to be called by the constructor
|
|---|
| 42 | */
|
|---|
| 43 | protected function set_user_id($user_id) {
|
|---|
| 44 | global $bp;
|
|---|
| 45 |
|
|---|
| 46 | if (empty($user_id)) $user_id = $bp->displayed_user->id;
|
|---|
| 47 | $this->user_id = $user_id;
|
|---|
| 48 |
|
|---|
| 49 | return $this->user_id;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | protected function set_per_page($per_page) {
|
|---|
| 53 | /***
|
|---|
| 54 | * If you want to make parameters that can be passed, then append a
|
|---|
| 55 | * character or two to "page" like this: $_REQUEST['xpage']
|
|---|
| 56 | * You can add more than a single letter.
|
|---|
| 57 | *
|
|---|
| 58 | * The "x" in "xpage" should be changed to something unique so as not to conflict with
|
|---|
| 59 | * BuddyPress core components which use the unique characters "b", "g", "u", "w",
|
|---|
| 60 | * "ac", "fr", "gr", "ml", "mr" with "page".
|
|---|
| 61 | */
|
|---|
| 62 |
|
|---|
| 63 | $this->pag_page = isset( $_REQUEST['bp_page'] ) ? intval( $_REQUEST['bp_page'] ) : 1;
|
|---|
| 64 | $this->pag_num = isset( $_GET['num'] ) ? intval( $_GET['num'] ) : $per_page;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | protected function set_max($max) {
|
|---|
| 68 | // Item Requests
|
|---|
| 69 | if ( $max && ($max < $this->item_count ) )
|
|---|
| 70 | $this->item_count = $max;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | protected function set_page_links() {
|
|---|
| 74 | /* Remember to change the "x" in "bp_page" to match whatever character(s) you're using above */
|
|---|
| 75 | $this->pag_links = paginate_links( array(
|
|---|
| 76 | 'base' => add_query_arg( 'bp_page', '%#%' ),
|
|---|
| 77 | 'format' => '',
|
|---|
| 78 | 'total' => ceil( (int) $this->total_item_count / (int) $this->pag_num ),
|
|---|
| 79 | 'current' => (int) $this->pag_page,
|
|---|
| 80 | 'prev_text' => '«',
|
|---|
| 81 | 'next_text' => '»',
|
|---|
| 82 | 'mid_size' => 1
|
|---|
| 83 | ));
|
|---|
| 84 |
|
|---|
| 85 | $this->from_num = intval( ( $this->pag_page - 1 ) * $this->pag_num ) + 1;
|
|---|
| 86 | $this->to_num = ( $this->from_num + ( $this->pag_num - 1 ) > $this->total_item_count ) ? $this->total_item_count : $this->from_num + ( $this->pag_num - 1) ;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /*
|
|---|
| 90 | * Functions to be called by template tags
|
|---|
| 91 | */
|
|---|
| 92 |
|
|---|
| 93 | public function has_items() {
|
|---|
| 94 | if ( $this->item_count )
|
|---|
| 95 | return true;
|
|---|
| 96 |
|
|---|
| 97 | return false;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | public function next_item() {
|
|---|
| 101 | $this->current_item++;
|
|---|
| 102 | $this->item = $this->items[$this->current_item];
|
|---|
| 103 |
|
|---|
| 104 | return $this->item;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | public function rewind_items() {
|
|---|
| 108 | $this->current_item = -1;
|
|---|
| 109 | if ( $this->item_count > 0 ) {
|
|---|
| 110 | $this->item = $this->items[0];
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | public function items() {
|
|---|
| 115 | if ( $this->current_item + 1 < $this->item_count ) {
|
|---|
| 116 | return true;
|
|---|
| 117 | } elseif ( $this->current_item + 1 == $this->item_count ) {
|
|---|
| 118 | do_action('loop_end');
|
|---|
| 119 | // Do some cleaning up after the loop
|
|---|
| 120 | $this->rewind_items();
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | $this->in_the_loop = false;
|
|---|
| 124 | return false;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | public function the_item() {
|
|---|
| 128 | $this->in_the_loop = true;
|
|---|
| 129 | $this->item = $this->next_item();
|
|---|
| 130 |
|
|---|
| 131 | if ( 0 == $this->current_item ) // loop has just started
|
|---|
| 132 | do_action('loop_start');
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | ?>
|
|---|