-
Couldn't load subscription status.
- Fork 32
Add Filtering Capabilities for Registered Abilities using collections (POC) #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
Public constants are converted to private static properties Additionally, validation methods are updated to use guard clauses, and some boolean logic is simplified.
WP_Abilities_Query already returns all abilities when args is empty, making the backward compatibility check unnecessary.
Updates type hints to specify arrays of strings for 'category' and 'namespace' parameters.
Updates method names and related docblocks to clarify that query argument processing focuses on sanitization rather than validation.
Improves documentation to specify use of the query class for retrieving and filtering abilities.
Updates the `WP_Abilities_Collection::pluck()` method to support dot notation for accessing nested properties. This allows for more flexible data extraction, such as retrieving values from `meta` properties like `meta.show_in_rest`. Both the value and key parameters now support this syntax. The implementation is updated to handle nested data retrieval, and comprehensive unit tests and documentation are added to reflect this enhancement. Also corrects a heading number in the advanced filtering documentation.
Update documentation for advanced filtering and sorting to improve clarity and prevent common mistakes. - Clarify that the operator in the `where()` method is optional and defaults to an equality check. - Document that `all()` is an alias for `to_array()`. - Add a prominent note for `where_meta()` to specify that the 'meta.' prefix should be omitted from keys.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #119 +/- ##
============================================
- Coverage 86.65% 85.11% -1.55%
- Complexity 148 210 +62
============================================
Files 18 19 +1
Lines 982 1162 +180
Branches 92 90 -2
============================================
+ Hits 851 989 +138
- Misses 131 173 +42
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Proof of Concept: Alternative implementation to #115 using a Collection-based approach for filtering and sorting abilities.
This PR introduces
WP_Abilities_Collection- a fluent, chainable collection class that provides Laravel-inspired filtering, sorting, and transformation methods for working with registered abilities. Instead of the query-based approach in #115, this implementation leverages in-memory collection operations for a more developer-friendly API.Key Differences from #115
#115 Approach: Uses
WP_Abilities_Queryclass with array-based filtering arguments (similar toWP_Query)This PR Approach: Uses
WP_Abilities_Collectionclass with fluent, chainable method calls (similar to Laravel Collections)Example Comparison
Query Approach (#115):
Collection Approach (This PR):
What's New
Core Collection Class
WP_Abilities_Collection- New collection class for filtering, sorting, and querying abilitiesIteratorAggregateandCountablefor native PHP iterationFiltering Methods
where($key, $value)orwhere($key, $operator, $value)- Generic property filtering with operators (=,!=,!==,>,<,>=,<=)where_in($key, $values)- Filter where property is in array of valueswhere_not_in($key, $values)- Filter where property is NOT in array of valueswhere_category($categories)- Filter by category (single string or array)where_namespace($namespaces)- Filter by namespace (single string or array)where_meta($filters)- Filter by metadata properties with dot notation supportfilter($callback)- Custom callback filteringsearch($term)- Full-text search across name, label, and descriptionSorting Methods
sort_by($property, $descending)- Sort by property name or custom callbacksort_by_desc($property)- Shorthand for descending sortreverse()- Reverse collection orderRetrieval & Utility Methods
all()/to_array()- Get all abilities as arrayfirst($callback, $default)- Get first ability (optionally with filter)last($callback, $default)- Get last ability (optionally with filter)get($name, $default)- Get ability by namepluck($value, $key)- Extract property values (supports dot notation:meta.annotations.readonly)keys()- Get all ability namesvalues()- Re-index collection with sequential keyscount()- Count abilities in collectionis_empty()/is_not_empty()- Check if collection is emptyChanges by Component
PHP Core
includes/abilities-api/class-wp-abilities-collection.php(577 lines)includes/abilities-api.php-wp_get_abilities()now returnsWP_Abilities_CollectionDocumentation
docs/8.advanced-filtering-and-sorting.md(441 lines) - Comprehensive guidedocs/4.using-abilities.md- Collection examplesJavaScript Client
Tests
tests/unit/abilities-api/wpAbilitiesCollection.phpDocumentation Highlights
The new
docs/8.advanced-filtering-and-sorting.mdincludes:Backward Compatibility
✅ Fully backward compatible
wp_get_abilities()without arguments still worksIteratorAggregate- can be used inforeachloopsto_array()method converts collection back to arrayPerformance Considerations
This is an in-memory collection approach, which means:
Discussion Points
Related