Add extensibility hooks for item/location lifecycle and scoped queries - #35
Open
datengraben wants to merge 4 commits into
Open
Add extensibility hooks for item/location lifecycle and scoped queries#35datengraben wants to merge 4 commits into
datengraben wants to merge 4 commits into
Conversation
Fires commonsbooking_booking_created, commonsbooking_booking_confirmed and commonsbooking_booking_cancelled at the existing single points where these state transitions already happen, plus commonsbooking_item_created and commonsbooking_location_created when those post types are first published. Previously the only custom hook in the plugin was commonsbooking_mail_sent, so external code had no reliable way to react to booking/item/location lifecycle events (e.g. to mirror them into another system's activity stream or notifications) without polling or hooking generic WP save_post.
The role-based booking eligibility check had no filter, unlike the neighbouring commonsbooking_isCurrentUserAdmin and commonsbooking_isCurrentUserCBManager checks. Restricting a timeframe to members of an external group/community (rather than a WordPress role) previously meant minting a dedicated WP role per group, which does not scale. External code can now layer additional eligibility logic on top of the existing role check via the commonsbooking_isCurrentUserAllowedToBook filter.
…ries BookablePost::get() (shared by Item::get() and Location::get(), and therefore also the map, the cb_items/cb_locations shortcodes and the REST API) previously only supported scoping results by post args or the built-in category taxonomy. External code had no way to scope results by an arbitrary external criterion, e.g. tagging items/locations with a postmeta value tying them to a group/community managed outside of CommonsBooking, without forking the query. The cache key is now derived from the filtered query args rather than the raw input args, so that context-dependent filtering (e.g. scoping by "current group") doesn't return stale results cached for a different context.
…12 The already-open (unmerged) PR #12 independently adds these same two hooks at the same call sites in handleBookingRequest()/postUpdated(), with a richer ($postId, $booking) signature. Keeping both would fire each hook twice per event with incompatible signatures once both PRs land. Deferring to PR #12 for booking_created/booking_confirmed; commonsbooking_booking_cancelled, commonsbooking_item_created and commonsbooking_location_created are unaffected since PR #12 doesn't touch Model/Booking.php, Item.php or Location.php.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Small, independent extensibility additions identified while assessing what it would take to integrate CommonsBooking with an external community/group system (e.g. BuddyPress) without forking core logic. Each is its own commit:
Lifecycle action hooks (
b1322fc, trimmed by4f5c5fc): addscommonsbooking_booking_cancelled,commonsbooking_item_createdandcommonsbooking_location_created. Previously the only custom hook in the plugin wascommonsbooking_mail_sent, so external code had no reliable way to react to these state transitions without hooking genericsave_post/post_updatedand re-deriving the "what changed" logic itself. Each hook fires at the single existing point in the codebase where that transition already happens (Model\Booking::cancel(),Item::savePost(),Location::savePost()).Filterable booking eligibility (
c79ea5f):commonsbooking_isCurrentUserAllowedToBook()now applies acommonsbooking_isCurrentUserAllowedToBookfilter to its result, matching the existing pattern used bycommonsbooking_isCurrentUserAdmin/commonsbooking_isCurrentUserCBManager. Today, restricting a timeframe to members of some external group means minting a dedicated WordPress role per group, which doesn't scale. The filter lets external code layer additional eligibility logic on top of the existing role check.Scoped Item/Location queries (
88187e4):BookablePost::get()(shared byItem::get()/Location::get(), and therefore also the map, thecb_items/cb_locationsshortcodes and the REST API) now applies acommonsbooking_repository_query_argsfilter to theWP_Queryargs before running the query. This lets external code scope results by an arbitrary criterion (e.g. a postmeta value tying a post to an externally-managed group) instead of only the built-in category taxonomy. The query cache key is derived from the filtered args rather than the raw input args, so context-dependent scoping (e.g. "current group") can't return stale results cached for a different context.None of these change default behavior — they only add hook points that no-op until something attaches a callback.
Test plan
php -lon all changed filesBooking.php+ 2 templates; my remaining hooks touchModel/Booking.php,Item.php,Location.php,includes/Users.php,Repository/BookablePost.php)Generated by Claude Code