BP_Core_HTML_Element::__construct( array $r = array() )

Constructor.


Description Description


Parameters Parameters

$r

(Optional) An array of arguments.

  • 'element'
    (string) The element to render. eg. 'a' for the anchor element.
  • 'attr'
    (array) Optional. The element's attributes set as key/value pairs. eg. array( 'href' => <a href="http://example.com">http://example.com</a>, 'class' => 'my-class' )
  • 'inner_html'
    (string) 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.

Default value: array()


Top ↑

Source Source

File: bp-core/classes/class-bp-core-html-element.php

	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 );
		}
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.