WP_HTML_Tag_Processor::get_enqueued_attribute_value( string $comparable_name ): string|boolean|null

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

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

Description

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."

Parameters

$comparable_namestringrequired
The attribute name in its comparable form.

Return

string|boolean|null Value of enqueued update if present, otherwise false.

Source

private function get_enqueued_attribute_value( $comparable_name ) {
	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 html_entity_decode( $enqueued_value );
}

Changelog

VersionDescription
6.2.0Introduced.

User Contributed Notes

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