wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren
wp_get_abilities › WordPress Function
Seit6.9.0
Veraltetn/v
› wp_get_abilities ( $args = array() )
| Parameter: |
|
| Siehe: |
|
| Gibt zurück: |
|
| Definiert in: |
|
| Codex: | |
| Changelog: |
|
Retrieves registered abilities, optionally filtered by the given arguments.
When called without arguments, returns all registered abilities. When called with an $args array, returns only abilities that match every specified condition. Filtering pipeline (executed in order): 1. Declarative filters (category, namespace, meta) — per-item, AND logic between
arg types, OR logic within multi-value category arrays.
2. item_include_callback — per-item, caller-scoped. Return true to include, false to exclude.
3. wp_get_abilities_item_include filter — per-item, ecosystem-scoped. Plugins can enforce
universal inclusion rules regardless of what the caller passed.
4. result_callback — on the full matched array, caller-scoped. Sort, slice, or reshape.
5. wp_get_abilities_result filter — on the full array, ecosystem-scoped.
Steps 1–3 run inside a single loop over the registry — no extra iteration.
Examples:
// All abilities (unchanged behaviour).
$abilities = wp_get_abilities();
// Filter by category.
$abilities = wp_get_abilities( array( 'category' => 'content' ) );
// Filter by multiple categories (OR logic).
$abilities = wp_get_abilities( array( 'category' => array( 'content', 'settings' ) ) );
// Filter by namespace.
$abilities = wp_get_abilities( array( 'namespace' => 'woocommerce' ) );
// Filter by meta.
$abilities = wp_get_abilities( array( 'meta' => array( 'show_in_rest' => true ) ) );
// Combine filters (AND logic between arg types).
$abilities = wp_get_abilities( array(
'category' => 'content',
'namespace' => 'core',
'meta' => array( 'show_in_rest' => true ),
) );
// Caller-scoped per-item callback.
$abilities = wp_get_abilities( array(
'item_include_callback' => function ( WP_Ability $ability ) {
return current_user_can( 'manage_options' );
},
) );
// Caller-scoped result callback (sort + paginate).
$abilities = wp_get_abilities( array(
'result_callback' => function ( array $abilities ) {
usort( $abilities, fn( $a, $b ) => strcasecmp( $a->get_label(), $b->get_label() ) );
return array_slice( $abilities, 0, 10 );
},
) );
The pipeline always runs, even when called with no arguments. This ensures that the
wp_get_abilities_item_include and wp_get_abilities_result filters always fire,
giving plugins a reliable place to enforce universal inclusion or shaping rules.
For raw, unfiltered registry data that bypasses the filter pipeline entirely, use
WP_Abilities_Registry::get_all_registered directly.Ähnliche Funktionen: wp_get_ability, wp_get_sites, wp_get_ability_categories, _wp_get_abilities_match_meta, wp_get_ability_category
Quellcode
function wp_get_abilities( array $args = array() ): array {
$registry = WP_Abilities_Registry::get_instance();
if ( null === $registry ) {
return array();
}
$abilities = $registry->get_all_registered();
$category = isset( $args['category'] ) ? (array) $args['category'] : array();
$namespace = isset( $args['namespace'] ) && is_string( $args['namespace'] ) ? rtrim( $args['namespace'], '/' ) . '/' : '';
$meta = isset( $args['meta'] ) && is_array( $args['meta'] ) ? $args['meta'] : array();
$item_include_callback = isset( $args['item_include_callback'] ) && is_callable( $args['item_include_callback'] ) ? $args['item_include_callback'] : null;
$result_callback = isset( $args['result_callback'] ) && is_callable( $args['result_callback'] ) ? $args['result_callback'] : null;
$matched = array();
foreach ( $abilities as $name => $ability ) {
// Step 1a: Filter by category (OR logic within the arg).
if ( ! empty( $category ) && ! in_array( $ability->get_category(), $category, true ) ) {
continue;
}
// Step 1b: Filter by namespace prefix.
if ( '' !== $namespace && ! str_starts_with( $ability->get_name(), $namespace ) ) {
continue;
}
// Step 1c: Filter by meta key/value pairs (AND logic, supports nested arrays).
if ( ! empty( $meta ) && ! _wp_get_abilities_match_meta( $ability->get_meta(), $meta ) ) {
continue;
}
// Step 2: Caller-scoped per-item callback.
$include = true;
if ( null !== $item_include_callback ) {
$include = (bool) call_user_func( $item_include_callback, $ability );
}
/**
* Filters whether an individual ability should be included in the result set.
*
* Fires after the declarative filters and the caller-scoped item_include_callback.
* Plugins can use this to enforce universal inclusion rules regardless of
* what the caller passed in $args.
*
* @since 7.1.0
*
* @param bool $include Whether to include the ability. Default true (after declarative filters pass).
* @param WP_Ability $ability The ability instance being evaluated.
* @param array $args The full $args array passed to wp_get_abilities().
*/
$include = (bool) apply_filters( 'wp_get_abilities_item_include', $include, $ability, $args );
if ( $include ) {
$matched[ $name ] = $ability;
}
}
// Step 4: Caller-scoped result callback.
if ( null !== $result_callback ) {
$matched = (array) call_user_func( $result_callback, $matched );
}
/**
* Filters the full list of matched abilities after all per-item filtering is complete.
*
* Fires after the caller-scoped result_callback. Plugins can use this to sort,
* paginate, or reshape the final result set universally.
*
* @since 7.1.0
*
* @param WP_Ability[] $matched The matched abilities after all filtering.
* @param array $args The full $args array passed to wp_get_abilities().
*/
return (array) apply_filters( 'wp_get_abilities_result', $matched, $args );
}