BP_Core_HTML_Element
Generate markup for an HTML element.
Description
Source
File: bp-core/classes/class-bp-core-html-element.php
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 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 | class BP_Core_HTML_Element { /** * Open tag for an element. * * This would include attributes if applicable. eg. '<a href="" class="">' * * @since BuddyPress 2.7.0 * * @var string */ public $open_tag = '' ; /** * Inner HTML for an element. * * For example, this could be anchor text within an <a> element. * * @since BuddyPress 2.7.0 * * @var string */ public $inner_html = '' ; /** * Closing tag for an element. * * For example, "</a>". * * @since BuddyPress 2.7.0 * * @var string */ public $close_tag = '' ; /** * Constructor. * * @since BuddyPress 2.7.0 * * @param array $r { * An array of arguments. * @type string $element The element to render. eg. 'a' for the anchor element. * @type array $attr Optional. The element's attributes set as key/value pairs. eg. * array( 'href' => 'http://example.com', 'class' => 'my-class' ) * @type string $inner_html Optional. The inner HTML for the element if applicable. Please note that * this isn't sanitized, so you should use your own sanitization routine * before using this parameter. * } */ public function __construct( $r = array () ) { $elem = sanitize_html_class( $r [ 'element' ] ); if ( empty ( $elem ) ) { return ; } // Render attributes. $attributes = '' ; foreach ( ( array ) $r [ 'attr' ] as $attr => $val ) { // If attribute is empty, skip. if ( empty ( $val ) ) { continue ; } if ( 'href' === $attr || 'formaction' === $attr || 'src' === $attr ) { $val = esc_url( $val ); } elseif ( 'id' === $attr ) { $val = sanitize_html_class( $val ); } else { $val = esc_attr( $val ); } $attributes .= sprintf( '%s="%s" ' , sanitize_html_class( $attr ), $val ); } // <input> / <img> is self-closing. if ( 'input' === $elem || 'img' === $elem ) { $this ->open_tag = sprintf( '<%1$s %2$s />' , $elem , $attributes ); // All other elements. } else { $this ->open_tag = sprintf( '<%1$s %2$s>' , $elem , $attributes ); $this ->inner_html = ! empty ( $r [ 'inner_html' ] ) ? $r [ 'inner_html' ] : '' ; $this ->close_tag = sprintf( '</%1$s>' , $elem ); } } /** * Returns a property from this class. * * @since BuddyPress 2.7.0 * * @param string $prop Property name. Either 'open_tag', 'inner_html', 'close_tag'. * @return string */ public function get( $prop = '' ) { if ( ! isset( $this ->{ $prop } ) ) { return '' ; } return $this ->{ $prop }; } /** * Returns full contents of HTML element. * * @since BuddyPress 2.7.0 * * @return string */ public function contents() { return $this ->open_tag . $this ->inner_html . $this ->close_tag; } } |
Changelog
Version | Description |
---|---|
BuddyPress 2.7.0 | Introduced. |
Methods
- __construct — Constructor.
- contents — Returns full contents of HTML element.
- get — Returns a property from this class.
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.