[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-button.php (source)

   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 = bp_parse_args(
 240              $args,
 241              get_class_vars( __CLASS__ )
 242          );
 243  
 244          // Backward compatibility with deprecated parameters.
 245          $r = $this->backward_compatibility_args( $r );
 246  
 247          // Deprecated. Subject to removal in a future release.
 248          $this->wrapper = $r['wrapper'];
 249          if ( !empty( $r['link_id']    ) ) $this->link_id    = ' id="' .    $r['link_id']    . '"';
 250          if ( !empty( $r['link_href']  ) ) $this->link_href  = ' href="' .  $r['link_href']  . '"';
 251          if ( !empty( $r['link_title'] ) ) $this->link_title = ' title="' . $r['link_title'] . '"';
 252          if ( !empty( $r['link_rel']   ) ) $this->link_rel   = ' rel="' .   $r['link_rel']   . '"';
 253          if ( !empty( $r['link_class'] ) ) $this->link_class = ' class="' . $r['link_class'] . '"';
 254          if ( !empty( $r['link_text']  ) ) $this->link_text  =              $r['link_text'];
 255  
 256          // Required button properties.
 257          $this->id                = $r['id'];
 258          $this->component         = $r['component'];
 259          $this->must_be_logged_in = (bool) $r['must_be_logged_in'];
 260          $this->block_self        = (bool) $r['block_self'];
 261  
 262          // $id and $component are required and component must be active.
 263          if ( empty( $r['id'] ) || empty( $r['component'] ) || ! bp_is_active( $this->component ) ) {
 264              return false;
 265          }
 266  
 267          // No button for guests if must be logged in.
 268          if ( true == $this->must_be_logged_in && ! is_user_logged_in() ) {
 269              return false;
 270          }
 271  
 272          // The block_self property.
 273          if ( true == $this->block_self ) {
 274              /*
 275               * No button if you are the current user in a members loop.
 276               *
 277               * This condition takes precedence, because members loops can be found on user
 278               * profiles.
 279               */
 280              if ( bp_get_member_user_id() ) {
 281                  if ( is_user_logged_in() && bp_loggedin_user_id() == bp_get_member_user_id() ) {
 282                      return false;
 283                  }
 284  
 285              // No button if viewing your own profile (and not in a members loop).
 286              } elseif ( bp_is_my_profile() ) {
 287                  return false;
 288              }
 289          }
 290  
 291          // Should we use a parent element?
 292          if ( ! empty( $r['parent_element'] ) ) {
 293              if ( ! isset( $r['parent_attr']['class'] ) ) {
 294                  $r['parent_attr']['class'] = '';
 295              }
 296  
 297              // Always add 'generic-button' class.
 298              if ( false === strpos( $r['parent_attr']['class'], 'generic-button' ) ) {
 299                  if ( ! is_array( $r['parent_attr'] ) ) {
 300                      $r['parent_attr'] = array();
 301                  }
 302                  if ( ! empty( $r['parent_attr']['class'] ) ) {
 303                      $r['parent_attr']['class'] .= ' ';
 304                  }
 305                  $r['parent_attr']['class'] .= 'generic-button';
 306              }
 307  
 308              // Set parent element props.
 309              $this->parent_element = $r['parent_element'];
 310              $this->parent_attr    = $r['parent_attr'];
 311  
 312              // Render parent element attributes.
 313              $parent_elem = new BP_Core_HTML_Element( array(
 314                  'element' => $r['parent_element'],
 315                  'attr'    => $r['parent_attr']
 316              ) );
 317  
 318              // Set before and after.
 319              $before = $parent_elem->get( 'open_tag' );
 320              $after  = $parent_elem->get( 'close_tag' );
 321  
 322          // No parent element.
 323          } else {
 324              $before = $after = '';
 325          }
 326  
 327          // Button properties.
 328          $button = '';
 329          if ( ! empty( $r['button_element'] ) ) {
 330              // Ensure the `href` attribute is not wrongly used inside buttons.
 331              if ( 'button' === $r['button_element'] && ! empty( $r['button_attr']['href'] ) ) {
 332                  _doing_it_wrong(
 333                      __CLASS__,
 334                      __( 'The `href` attribute is not available inside the `&lt;button&gt;` tag. Please use a `data-*` attribute to transport a link into this tag.', 'buddypress' ),
 335                      '9.0.0'
 336                  );
 337  
 338                  unset( $r['button_attr']['href'] );
 339              }
 340  
 341              $button = new BP_Core_HTML_Element( array(
 342                  'element'    => $r['button_element'],
 343                  'attr'       => $r['button_attr'],
 344                  'inner_html' => ! empty( $r['link_text'] ) ? $r['link_text'] : ''
 345              ) );
 346              $button = $button->contents();
 347          }
 348  
 349          // Build the button.
 350          $this->contents = $before . $button . $after;
 351  
 352          /**
 353           * Filters the button based on class parameters.
 354           *
 355           * This filter is a dynamic filter based on component and component ID and
 356           * allows button to be manipulated externally.
 357           *
 358           * @since 1.2.6
 359           * @since 2.7.0 Added $r as a parameter.
 360           *
 361           * @param string    $contents HTML being used for the button.
 362           * @param BP_Button $this     Current BP_Button instance.
 363           * @param string    $before   HTML appended before the actual button.
 364           * @param string    $after    HTML appended after the actual button.
 365           * @param array     $r        Parsed button arguments.
 366           */
 367          $this->contents = apply_filters( 'bp_button_' . $this->component . '_' . $this->id, $this->contents, $this, $before, $after, $r );
 368      }
 369  
 370  
 371      /**
 372       * Provide backward compatibility for deprecated button arguments.
 373       *
 374       * @since 2.7.0.
 375       *
 376       * @param  array $r See {@link BP_Button} class for full documentation.
 377       * @return array
 378       */
 379  	protected function backward_compatibility_args( $r = array() ) {
 380          // Array of deprecated arguments.
 381          $backpat_args = array(
 382              'wrapper', 'wrapper_class', 'wrapper_id',
 383              'link_href', 'link_class', 'link_id', 'link_rel', 'link_title'
 384          );
 385  
 386          foreach ( $backpat_args as $prop ) {
 387              if ( empty( $r[ $prop ] ) ) {
 388                  continue;
 389              }
 390  
 391              $parent = $child = false;
 392              $sep    = strpos( $prop, '_' );
 393  
 394              // Check if this is an attribute.
 395              if ( false !== $sep ) {
 396                  $child  = true;
 397                  $parent = substr( $prop, 0, $sep );
 398              } else {
 399                  $parent = $prop;
 400              }
 401  
 402              if ( 'wrapper' === $parent ) {
 403                  $parent = 'parent';
 404              } else {
 405                  $parent = 'button';
 406              }
 407  
 408              // Set element.
 409              if ( false === $child && empty( $r[ "{$parent}_element" ] ) ) {
 410                  $r[ "{$parent}_element" ] = $r[ $prop ];
 411  
 412              // Set attributes.
 413              } elseif ( true === $child ) {
 414                  $new_prop = substr( $prop, strpos( $prop, '_' ) +1 );
 415                  if ( empty( $r[ "{$parent}_attr" ] ) ) {
 416                      $r[ "{$parent}_attr" ] = array();
 417                  }
 418  
 419                  if ( empty( $r[ "{$parent}_attr" ][ $new_prop ] ) ) {
 420                      $r[ "{$parent}_attr" ][ $new_prop ] = $r[ $prop ];
 421                  }
 422              }
 423          }
 424  
 425          return $r;
 426      }
 427  
 428  
 429      /**
 430       * Return the markup for the generated button.
 431       *
 432       * @since 1.2.6
 433       *
 434       * @return string Button markup.
 435       */
 436  	public function contents() {
 437          return $this->contents;
 438      }
 439  
 440      /**
 441       * Output the markup of button.
 442       *
 443       * @since 1.2.6
 444       */
 445  	public function display() {
 446          if ( !empty( $this->contents ) )
 447              echo $this->contents;
 448      }
 449  }


Generated: Sun Oct 24 01:00:54 2021 Cross-referenced by PHPXref 0.7.1