Skip to content
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

GBFS/Commons-API: Add filter hook for item processing #1549

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/API/GBFS/BaseRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,39 @@ public function get_items( $request ): WP_REST_Response {
/**
* Returns item data array.
*
* @param $request
* @since 2.9.3 includes filter hooks for location get-args and for-each item skip.
*
* @param WP_REST_Request $request inbound rest request.
*
* @return array
*/
public function getItemData( $request ): array {
$data = [];
$locations = Location::get();
$data = array();

/**
* Lets you customize the args for database query. Affects all endpoints, because gbfs is based on location model.
*
* @param array $args request args array
* @param WP_REST_Request $request
*/
$args = apply_filters( 'commonsbooking_gbfs_location_getargs', array(), $request );

/** @var \CommonsBooking\Model\Location[] $locations */
$locations = Location::get( $args );

foreach ( $locations as $location ) {
/**
* You can compute if an item should be skipped or not for the api response, based on $item data.
*
* Example: add_filter( '...', function ( Location $location ) {
* return 'my_category' in $location->term;
* })
*
* @param \CommonsBooking\Model\Location $location bookable post type.
*/
if ( true === apply_filters( 'commonsbooking_gbfs_location_skipitem', $location ) ) {
continue;
}
try {
$itemdata = $this->prepare_item_for_response( $location, $request );
$data[] = $itemdata;
Expand Down
28 changes: 27 additions & 1 deletion src/API/ItemsRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ItemsRoute extends BaseRoute {
/**
* Returns raw data collection.
*
* @param $request
* @param WP_REST_Request $request inbound request object.
*
* @return stdClass
*/
Expand All @@ -49,8 +49,34 @@ public function getItemData( $request ): stdClass {
];
}

/**
* Lets you customize the database query.
*
* Example: add_filter( '...', function( array $args ) {
* $args['category_slug'] = 'my_category';
* })
*
* @param array $args from the {@see Item::get} args.
*/
$args = apply_filters( 'commonsbooking_commonsapi_itemsroute_getargs', $args );

/** @var \CommonsBooking\Model\Item[] $items */
$items = Item::get( $args );
foreach ( $items as $item ) {

/**
* You can compute if an item should be skipped or not for the api response, based on $item data.
*
* Example: add_filter( '...', function ( Item $item ) {
* return 'my_category' in $item->term;
* })
*
* @param \CommonsBooking\Model\Item $item bookable post type.
*/
if ( true === apply_filters( 'commonsbooking_commonsapi_itemsroute_skipitem', $item ) ) {
continue;
}

$itemdata = $this->prepare_item_for_response( $item, $request );
$data->items[] = $this->prepare_response_for_collection( $itemdata );
}
Expand Down
Loading