beans_has_filters

Check if any filter has been registered for a hook.

This function is similar to has_filters() with the exception of checking sub-hooks if it is told to do so.

beans_has_filters( string $id, callback|bool $callback = false )

Return: (bool|int) If $callback is omitted, returns boolean for whether the hook has anything registered. When checking a specific function, the priority of that hook is returned, or false if the function is not attached. When using the $callback argument, this function may return a non-boolean value that evaluates to false (e.g. 0), so use the === operator for testing the return value.

Parameters

NameTypeRequiredDefaultDescription
$idstringtrue-The filter ID.
$callbackcallback|boolfalsefalseThe callback to check for. Default false.

Source

function beans_has_filters( $id, $callback = false ) {

	// Check simple filter if no subhook is set.
	if ( ! preg_match_all( '#\[(.*?)\]#', $id, $matches ) ) {
		return has_filter( $id, $callback );
	}

	$prefix = current( explode( '[', $id ) );
	$variable_prefix = $prefix;
	$suffix = preg_replace( '/^.*\]\s*/', '', $id );

	// Check base filter.
	if ( has_filter( $prefix . $suffix, $callback ) ) {
		return true;
	}

	foreach ( $matches[0] as $i => $subhook ) {

		$variable_prefix = $variable_prefix . $subhook;
		$levels = array( $prefix . $subhook . $suffix );

		// Cascade sub-hooks.
		if ( $i > 0 ) {

			$levels[] = str_replace( $subhook, '', $id );
			$levels[] = $variable_prefix . $suffix;

		}

		// Apply sub-hooks.
		foreach ( $levels as $level ) {

			if ( has_filter( $level, $callback ) ) {
				return true;
			}

			// Check filter whithout square brackets for backwards compatibility.
			if ( has_filter( preg_replace( '#(\[|\])#', '', $level ), $callback ) ) {
				return true;
			}
		}
	}

	return false;

}