[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BP Buttons Group class. 4 * 5 * @since 3.0.0 6 * @version 10.0.0 7 */ 8 9 // Exit if accessed directly. 10 defined( 'ABSPATH' ) || exit; 11 12 /** 13 * Builds a group of BP_Button 14 * 15 * @since 3.0.0 16 */ 17 class BP_Buttons_Group { 18 19 /** 20 * The parameters of the Group of buttons 21 * 22 * @var array 23 */ 24 protected $group = array(); 25 26 /** 27 * Constructor 28 * 29 * @since 3.0.0 30 * 31 * @param array $args Optional array having the following parameters { 32 * @type string $id A string to use as the unique ID for the button. Required. 33 * @type int $position Where to insert the Button. Defaults to 99. 34 * @type string $component The Component's the button is build for (eg: Activity, Groups..). Required. 35 * @type bool $must_be_logged_in Whether the button should only be displayed to logged in users. Defaults to True. 36 * @type bool $block_self Optional. True if the button should be hidden when a user is viewing his own profile. 37 * Defaults to False. 38 * @type string $parent_element Whether to use a wrapper. Defaults to false. 39 * @type string $parent_attr set an array of attributes for the parent element. 40 * @type string $button_element Set this to 'button', 'img', or 'a', defaults to anchor. 41 * @type string $button_attr Any attributes required for the button_element 42 * @type string $link_text The text of the link. Required. 43 * } 44 */ 45 public function __construct( $args = array() ) { 46 foreach ( $args as $arg ) { 47 $this->add( $arg ); 48 } 49 } 50 51 52 /** 53 * Sort the Buttons of the group according to their position attribute 54 * 55 * @since 3.0.0 56 * 57 * @param array the list of buttons to sort. 58 * 59 * @return array the list of buttons sorted. 60 */ 61 public function sort( $buttons ) { 62 $sorted = array(); 63 64 foreach ( $buttons as $button ) { 65 $position = 99; 66 67 if ( isset( $button['position'] ) ) { 68 $position = (int) $button['position']; 69 } 70 71 // If position is already taken, move to the first next available 72 if ( isset( $sorted[ $position ] ) ) { 73 $sorted_keys = array_keys( $sorted ); 74 75 do { 76 $position += 1; 77 } while ( in_array( $position, $sorted_keys, true ) ); 78 } 79 80 $sorted[ $position ] = $button; 81 } 82 83 ksort( $sorted ); 84 return $sorted; 85 } 86 87 /** 88 * Get the BuddyPress buttons for the group 89 * 90 * @since 3.0.0 91 * 92 * @param bool $sort whether to sort the buttons or not. 93 * 94 * @return array An array of HTML links. 95 */ 96 public function get( $sort = true ) { 97 $buttons = array(); 98 99 if ( empty( $this->group ) ) { 100 return $buttons; 101 } 102 103 if ( true === $sort ) { 104 $this->group = $this->sort( $this->group ); 105 } 106 107 foreach ( $this->group as $key_button => $button ) { 108 // Reindex with ids. 109 if ( true === $sort ) { 110 $this->group[ $button['id'] ] = $button; 111 unset( $this->group[ $key_button ] ); 112 } 113 114 $buttons[ $button['id'] ] = bp_get_button( $button ); 115 } 116 117 return $buttons; 118 } 119 120 /** 121 * Update the group of buttons 122 * 123 * @since 3.0.0 124 * 125 * @param array $args Optional. See the __constructor for a description of this argument. 126 */ 127 public function update( $args = array() ) { 128 foreach ( $args as $id => $params ) { 129 if ( isset( $this->group[ $id ] ) ) { 130 $this->group[ $id ] = bp_parse_args( 131 $params, 132 $this->group[ $id ], 133 'buttons_group_update' 134 ); 135 } else { 136 $this->add( $params ); 137 } 138 } 139 } 140 141 /** 142 * Adds a button. 143 * 144 * @since 9.0.0 145 * 146 * @param array $args Required. See the __constructor for a description of this argument. 147 * @return bool true on success, false on failure to add. 148 */ 149 private function add( $args ) { 150 $r = bp_parse_args( 151 (array) $args, 152 array( 153 'id' => '', 154 'position' => 99, 155 'component' => '', 156 'must_be_logged_in' => true, 157 'block_self' => false, 158 'parent_element' => false, 159 'parent_attr' => array(), 160 'button_element' => 'a', 161 'button_attr' => array(), 162 'link_text' => '', 163 ), 164 'buttons_group_constructor' 165 ); 166 167 // Just don't set the button if a param is missing 168 if ( empty( $r['id'] ) || empty( $r['component'] ) || empty( $r['link_text'] ) ) { 169 return false; 170 } 171 172 $r['id'] = sanitize_key( $r['id'] ); 173 174 // If the button already exist don't add it 175 if ( isset( $this->group[ $r['id'] ] ) ) { 176 return false; 177 } 178 179 /* 180 * If, in bp_nouveau_get_*_buttons(), we pass through a false value for 'parent_element' 181 * but we have attributtes for it in the array, let's default to setting a div. 182 * 183 * Otherwise, the original false value will be passed through to BP buttons. 184 * @todo: this needs review, probably trying to be too clever 185 */ 186 if ( ( ! empty( $r['parent_attr'] ) ) && false === $r['parent_element'] ) { 187 $r['parent_element'] = 'div'; 188 } 189 190 $this->group[ $r['id'] ] = $r; 191 return true; 192 } 193 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Oct 13 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |