BP_Button
API to create BuddyPress buttons.
Description
Parameters
- $args
-
(Required) Array of arguments.
- 'id'
(string) String describing the button type. - 'component'
(string) The name of the component the button belongs to. Default: 'core'. - 'must_be_logged_in'
(bool) Optional. Does the user need to be logged in to see this button? Default: true. - 'block_self'
(bool) Optional. True if the button should be hidden when a user is viewing his own profile. Default: true. - 'parent_element'
(string) Optional. Parent element to wrap button around. Default: 'div'. - 'parent_attr'
(array) Optional. Element attributes for parent element. Set whatever attributes like 'id', 'class' as array keys. - 'button_element'
(string) Optional. Button element. Default: 'a'. - 'button_attr'
(array) Optional. Button attributes. Set whatever attributes like 'id', 'class' as array keys. - 'link_text'
(string) Optional. Text to appear on the button. Default: ''. - 'wrapper'
(string|bool) Deprecated. Use $parent_element instead. - 'wrapper_id'
(string) Deprecated. Use $parent_attr and set 'id' as array key. - 'wrapper_class'
(string) Deprecated. Use $parent_attr and set 'class' as array key. - 'link_href'
(string) Deprecated. Use $button_attr and set 'href' as array key. - 'link_class'
(string) Deprecated. Use $button_attr and set 'class' as array key. - 'link_id'
(string) Deprecated. Use $button_attr and set 'id' as array key. - 'link_rel'
(string) Deprecated. Use $button_attr and set 'rel' as array key. - 'link_title'
(string) Deprecated. Use $button_attr and set 'title' as array key.
- 'id'
Source
File: bp-core/classes/class-bp-button.php
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | class BP_Button { /** Button properties *****************************************************/ /** * The button ID. * * @since BuddyPress 1.2.6 * * @var string */ public $id = '' ; /** * The name of the component that the button belongs to. * * @since BuddyPress 1.2.6 * * @var string */ public $component = 'core' ; /** * Does the user need to be logged in to see this button? * * @since BuddyPress 1.2.6 * * @var bool */ public $must_be_logged_in = true; /** * Whether the button should be hidden when viewing your own profile. * * @since BuddyPress 1.2.6 * * @var bool */ public $block_self = true; /** Wrapper ***************************************************************/ /** * Parent element to wrap button around. * * @since BuddyPress 2.7.0 * * @var string Default: 'div'. */ public $parent_element = '' ; /** * Element attributes for parent element. * * @since BuddyPress 2.7.0 * * @var array Set whatever attributes like 'id', 'class' as array key. */ public $parent_attr = array (); /** Button ****************************************************************/ /** * Button element. * * @since BuddyPress 2.7.0 * * @var string Default: 'a'. */ public $button_element = 'a' ; /** * Button attributes. * * @since BuddyPress 2.7.0 * * @var array Set whatever attributes like 'id', 'href' as array key. */ public $button_attr = array (); /** * The contents of the button link. * * @since BuddyPress 1.2.6 * * @var string */ public $link_text = '' ; /** * HTML result. * * @since BuddyPress 1.2.6 * * @var string */ public $contents = '' ; /** Deprecated ***********************************************************/ /** * The type of DOM element to use for a wrapper. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Use $parent_element instead. * * @var string|bool */ public $wrapper = 'div' ; /** * The DOM class of the button wrapper. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Set 'class' key in $parent_attr instead. * * @var string */ public $wrapper_class = '' ; /** * The DOM ID of the button wrapper. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Set 'id' key in $parent_attr instead. * * @var string */ public $wrapper_id = '' ; /** * The destination link of the button. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Set 'href' key in $button_attr instead. * * @var string */ public $link_href = '' ; /** * The DOM class of the button link. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Set 'class' key in $button_attr instead. * * @var string */ public $link_class = '' ; /** * The DOM ID of the button link. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Set 'id' key in $button_attr instead. * * @var string */ public $link_id = '' ; /** * The DOM rel value of the button link. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Set 'rel' key in $button_attr instead. * * @var string */ public $link_rel = '' ; /** * Title of the button link. * * @since BuddyPress 1.2.6 * @deprecated 2.7.0 Set 'title' key in $button_attr instead. * * @var string */ public $link_title = '' ; /** Methods ***************************************************************/ /** * Builds the button based on class parameters. * * @since BuddyPress 1.2.6 * * @param array|string $args See {@BP_Button}. */ public function __construct( $args = '' ) { $r = wp_parse_args( $args , get_class_vars( __CLASS__ ) ); // Backward compatibility with deprecated parameters. $r = $this ->backward_compatibility_args( $r ); // Deprecated. Subject to removal in a future release. $this ->wrapper = $r [ 'wrapper' ]; if ( ! empty ( $r [ 'link_id' ] ) ) $this ->link_id = ' id="' . $r [ 'link_id' ] . '"' ; if ( ! empty ( $r [ 'link_href' ] ) ) $this ->link_href = ' href="' . $r [ 'link_href' ] . '"' ; if ( ! empty ( $r [ 'link_title' ] ) ) $this ->link_title = ' title="' . $r [ 'link_title' ] . '"' ; if ( ! empty ( $r [ 'link_rel' ] ) ) $this ->link_rel = ' rel="' . $r [ 'link_rel' ] . '"' ; if ( ! empty ( $r [ 'link_class' ] ) ) $this ->link_class = ' class="' . $r [ 'link_class' ] . '"' ; if ( ! empty ( $r [ 'link_text' ] ) ) $this ->link_text = $r [ 'link_text' ]; // Required button properties. $this ->id = $r [ 'id' ]; $this ->component = $r [ 'component' ]; $this ->must_be_logged_in = (bool) $r [ 'must_be_logged_in' ]; $this ->block_self = (bool) $r [ 'block_self' ]; // $id and $component are required and component must be active. if ( empty ( $r [ 'id' ] ) || empty ( $r [ 'component' ] ) || ! bp_is_active( $this ->component ) ) { return false; } // No button for guests if must be logged in. if ( true == $this ->must_be_logged_in && ! is_user_logged_in() ) { return false; } // The block_self property. if ( true == $this ->block_self ) { /* * No button if you are the current user in a members loop. * * This condition takes precedence, because members loops can be found on user * profiles. */ if ( bp_get_member_user_id() ) { if ( is_user_logged_in() && bp_loggedin_user_id() == bp_get_member_user_id() ) { return false; } // No button if viewing your own profile (and not in a members loop). } elseif ( bp_is_my_profile() ) { return false; } } // Should we use a parent element? if ( ! empty ( $r [ 'parent_element' ] ) ) { if ( ! isset( $r [ 'parent_attr' ][ 'class' ] ) ) { $r [ 'parent_attr' ][ 'class' ] = '' ; } // Always add 'generic-button' class. if ( false === strpos ( $r [ 'parent_attr' ][ 'class' ], 'generic-button' ) ) { if ( ! is_array ( $r [ 'parent_attr' ] ) ) { $r [ 'parent_attr' ] = array (); } if ( ! empty ( $r [ 'parent_attr' ][ 'class' ] ) ) { $r [ 'parent_attr' ][ 'class' ] .= ' ' ; } $r [ 'parent_attr' ][ 'class' ] .= 'generic-button' ; } // Set parent element props. $this ->parent_element = $r [ 'parent_element' ]; $this ->parent_attr = $r [ 'parent_attr' ]; // Render parent element attributes. $parent_elem = new BP_Core_HTML_Element( array ( 'element' => $r [ 'parent_element' ], 'attr' => $r [ 'parent_attr' ] ) ); // Set before and after. $before = $parent_elem ->get( 'open_tag' ); $after = $parent_elem ->get( 'close_tag' ); // No parent element. } else { $before = $after = '' ; } // Button properties. $button = '' ; if ( ! empty ( $r [ 'button_element' ] ) ) { $button = new BP_Core_HTML_Element( array ( 'element' => $r [ 'button_element' ], 'attr' => $r [ 'button_attr' ], 'inner_html' => ! empty ( $r [ 'link_text' ] ) ? $r [ 'link_text' ] : '' ) ); $button = $button ->contents(); } // Build the button. $this ->contents = $before . $button . $after ; /** * Filters the button based on class parameters. * * This filter is a dynamic filter based on component and component ID and * allows button to be manipulated externally. * * @since BuddyPress 1.2.6 * @since BuddyPress 2.7.0 Added $r as a parameter. * * @param string $contents HTML being used for the button. * @param BP_Button $this Current BP_Button instance. * @param string $before HTML appended before the actual button. * @param string $after HTML appended after the actual button. * @param array $r Parsed button arguments. */ $this ->contents = apply_filters( 'bp_button_' . $this ->component . '_' . $this ->id, $this ->contents, $this , $before , $after , $r ); } /** * Provide backward compatibility for deprecated button arguments. * * @since BuddyPress 2.7.0. * * @param array $r See {@link BP_Button} class for full documentation. * @return array */ protected function backward_compatibility_args( $r = array () ) { // Array of deprecated arguments. $backpat_args = array ( 'wrapper' , 'wrapper_class' , 'wrapper_id' , 'link_href' , 'link_class' , 'link_id' , 'link_rel' , 'link_title' ); foreach ( $backpat_args as $prop ) { if ( empty ( $r [ $prop ] ) ) { continue ; } $parent = $child = false; $sep = strpos ( $prop , '_' ); // Check if this is an attribute. if ( false !== $sep ) { $child = true; $parent = substr ( $prop , 0, $sep ); } else { $parent = $prop ; } if ( 'wrapper' === $parent ) { $parent = 'parent' ; } else { $parent = 'button' ; } // Set element. if ( false === $child && empty ( $r [ "{$parent}_element" ] ) ) { $r [ "{$parent}_element" ] = $r [ $prop ]; // Set attributes. } elseif ( true === $child ) { $new_prop = substr ( $prop , strpos ( $prop , '_' ) +1 ); if ( empty ( $r [ "{$parent}_attr" ] ) ) { $r [ "{$parent}_attr" ] = array (); } if ( empty ( $r [ "{$parent}_attr" ][ $new_prop ] ) ) { $r [ "{$parent}_attr" ][ $new_prop ] = $r [ $prop ]; } } } return $r ; } /** * Return the markup for the generated button. * * @since BuddyPress 1.2.6 * * @return string Button markup. */ public function contents() { return $this ->contents; } /** * Output the markup of button. * * @since BuddyPress 1.2.6 */ public function display() { if ( ! empty ( $this ->contents ) ) echo $this ->contents; } } |
Changelog
Version | Description |
---|---|
BuddyPress 1.2.6 | Introduced. |
Methods
- __construct — Builds the button based on class parameters.
- backward_compatibility_args — Provide backward compatibility for deprecated button arguments.
- contents — Return the markup for the generated button.
- display — Output the markup of button.
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.