WP_HTML_Tag_Processor::get_enqueued_attribute_value()privateWP 6.2.0

Return the enqueued value for a given attribute, if one exists.

Enqueued updates can take different data types:

  • If an update is enqueued and is boolean, the return will be true
  • If an update is otherwise enqueued, the return will be the string value of that update.
  • If an attribute is enqueued to be removed, the return will be null to indicate that.
  • If no updates are enqueued, the return will be false to differentiate from "removed."

Method of the class: WP_HTML_Tag_Processor{}

No Hooks.

Return

String|true|false|null. Value of enqueued update if present, otherwise false.

Usage

// private - for code of main (parent) class only
$result = $this->get_enqueued_attribute_value( $comparable_name );
$comparable_name(string) (required)
The attribute name in its comparable form.

Changelog

Since 6.2.0 Introduced.

WP_HTML_Tag_Processor::get_enqueued_attribute_value() code WP 6.6.2

private function get_enqueued_attribute_value( $comparable_name ) {
	if ( self::STATE_MATCHED_TAG !== $this->parser_state ) {
		return false;
	}

	if ( ! isset( $this->lexical_updates[ $comparable_name ] ) ) {
		return false;
	}

	$enqueued_text = $this->lexical_updates[ $comparable_name ]->text;

	// Removed attributes erase the entire span.
	if ( '' === $enqueued_text ) {
		return null;
	}

	/*
	 * Boolean attribute updates are just the attribute name without a corresponding value.
	 *
	 * This value might differ from the given comparable name in that there could be leading
	 * or trailing whitespace, and that the casing follows the name given in `set_attribute`.
	 *
	 * Example:
	 *
	 *     $p->set_attribute( 'data-TEST-id', 'update' );
	 *     'update' === $p->get_enqueued_attribute_value( 'data-test-id' );
	 *
	 * Detect this difference based on the absence of the `=`, which _must_ exist in any
	 * attribute containing a value, e.g. `<input type="text" enabled />`.
	 *                                            ¹           ²
	 *                                       1. Attribute with a string value.
	 *                                       2. Boolean attribute whose value is `true`.
	 */
	$equals_at = strpos( $enqueued_text, '=' );
	if ( false === $equals_at ) {
		return true;
	}

	/*
	 * Finally, a normal update's value will appear after the `=` and
	 * be double-quoted, as performed incidentally by `set_attribute`.
	 *
	 * e.g. `type="text"`
	 *           ¹²    ³
	 *        1. Equals is here.
	 *        2. Double-quoting starts one after the equals sign.
	 *        3. Double-quoting ends at the last character in the update.
	 */
	$enqueued_value = substr( $enqueued_text, $equals_at + 2, -1 );
	return WP_HTML_Decoder::decode_attribute( $enqueued_value );
}