beans_apply_filters

Call the functions added to a filter hook.

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

Sub-hooks must be set in square brackets as part of the filter id argument. Sub-hooks are cascaded in a similar way to CSS classes. Maximum 3 sub-hooks allowed.

beans_apply_filters( string $id, mixed $value )

Return: (mixed) The filtered value after all hooked functions are applied to it.

Parameters

NameTypeRequiredDefaultDescription
$idstringtrue-A unique string used as a reference. Sub-hook(s) must be set in square brackets. Each sub- hook will create a filter. For instance, 'hook[_sub_hook]' will create a 'hook_name' filter as well as a 'hook[_sub_hook]' filter. The id may contain multiple sub hooks such as 'hook[_sub_hook][_sub_sub_hook]'. In this case, four filters will be created 'hook', 'hook[_sub_hook]', 'hook[_sub_sub_hook]' and 'hook[_sub_hook][_sub_sub_hook]'. Sub-hooks always run the parent filter first, so a filter set to the parent will apply to all sub-hooks. Maximum 3 sub-hooks allowed.
$valuemixedtrue-The value on which the filters hooked to $id are applied to it.
$varmixedtrue-Additional variables passed to the functions hooked to $id.

Source

function beans_apply_filters( $id, $value ) {

	$args = func_get_args();

	// Return simple filter if no sub-hook is set.
	if ( ! preg_match_all( '#\[(.*?)\]#', $args[0], $matches ) ) {
		return call_user_func_array( 'apply_filters', $args );
	}

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

	// Base filter.
	$args[0] = $prefix . $suffix;
	$value = call_user_func_array( 'apply_filters', $args );

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

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

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

			if ( count( $matches[0] ) > 2 ) {
				$levels[] = str_replace( $subhook, '', $id );
			}

			$levels[] = $variable_prefix . $suffix;

		}

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

			$args[0] = $level;
			$args[1] = $value;
			$value = call_user_func_array( 'apply_filters', $args );

			// Apply filter whithout square brackets for backwards compatibility.
			$args[0] = preg_replace( '#(\[|\])#', '', $args[0] );
			$args[1] = $value;
			$value = call_user_func_array( 'apply_filters', $args );

		}
	}

	return $value;

}