BP_Core_HTML_Element

Generate markup for an HTML element.

Defined (1)

The class is defined in the following location(s).

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