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