[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Core component classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage Core
   7   * @since 2.7.0
   8   */
   9  
  10  /**
  11   * Generate markup for an HTML element.
  12   *
  13   * @since 2.7.0
  14   */
  15  class BP_Core_HTML_Element {
  16      /**
  17       * Open tag for an element.
  18       *
  19       * This would include attributes if applicable. eg. '<a href="" class="">'
  20       *
  21       * @since 2.7.0
  22       *
  23       * @var string
  24       */
  25      public $open_tag   = '';
  26  
  27      /**
  28       * Inner HTML for an element.
  29       *
  30       * For example, this could be anchor text within an <a> element.
  31       *
  32       * @since 2.7.0
  33       *
  34       * @var string
  35       */
  36      public $inner_html = '';
  37  
  38      /**
  39       * Closing tag for an element.
  40       *
  41       * For example, "</a>".
  42       *
  43       * @since 2.7.0
  44       *
  45       * @var string
  46       */
  47      public $close_tag  = '';
  48  
  49      /**
  50       * Constructor.
  51       *
  52       * @since 2.7.0
  53       *
  54       * @param array $r {
  55       *     An array of arguments.
  56       *     @type string $element    The element to render. eg. 'a' for the anchor element.
  57       *     @type array  $attr       Optional. The element's attributes set as key/value pairs. eg.
  58       *                              array( 'href' => 'http://example.com', 'class' => 'my-class' )
  59       *     @type string $inner_html Optional. The inner HTML for the element if applicable. Please note that
  60       *                              this isn't sanitized, so you should use your own sanitization routine
  61       *                              before using this parameter.
  62       * }
  63       */
  64  	public function __construct( $r = array() ) {
  65          $elem = sanitize_html_class( $r['element'] );
  66          if ( empty( $elem ) ) {
  67              return;
  68          }
  69  
  70          // Render attributes.
  71          $attributes = '';
  72          foreach( (array) $r['attr'] as $attr => $val ) {
  73              // If attribute is empty, skip.
  74              if ( empty( $val ) ) {
  75                  continue;
  76              }
  77  
  78              if ( 'href' === $attr || 'formaction' === $attr || 'src' === $attr ) {
  79                  $val = esc_url( $val );
  80              } elseif ( 'id' === $attr ) {
  81                  $val = sanitize_html_class( $val );
  82              } else {
  83                  $val = esc_attr( $val );
  84              }
  85  
  86              $attributes .= sprintf( '%s="%s" ', sanitize_html_class( $attr ), $val );
  87          }
  88  
  89          // <input> / <img> is self-closing.
  90          if ( 'input' === $elem || 'img' === $elem ) {
  91              $this->open_tag = sprintf( '<%1$s %2$s />', $elem, $attributes );
  92  
  93          // All other elements.
  94          } else {
  95              $this->open_tag   = sprintf( '<%1$s %2$s>', $elem, $attributes );
  96              $this->inner_html = ! empty( $r['inner_html'] ) ? $r['inner_html'] : '';
  97              $this->close_tag  = sprintf( '</%1$s>',    $elem );
  98          }
  99      }
 100  
 101      /**
 102       * Returns a property from this class.
 103       *
 104       * @since 2.7.0
 105       *
 106       * @param  string $prop Property name. Either 'open_tag', 'inner_html', 'close_tag'.
 107       * @return string
 108       */
 109  	public function get( $prop = '' ) {
 110          if ( ! isset( $this->{$prop} ) ) {
 111              return '';
 112          }
 113  
 114          return $this->{$prop};
 115      }
 116  
 117      /**
 118       * Returns full contents of HTML element.
 119       *
 120       * @since 2.7.0
 121       *
 122       * @return string
 123       */
 124  	public function contents() {
 125          return $this->open_tag . $this->inner_html . $this->close_tag;
 126      }
 127  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1