Set up abilities API abstraction layers#803
Draft
agibson-godaddy wants to merge 7 commits intomasterfrom
Draft
Conversation
Contributor
|
Neat! |
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
This sets up Abilities API abstraction layers for plugins to:
Details
Core resources
HasAbilitiesContract-- to be used in classes (probably main plugin files) that provide abilities.JsonSerializable-- interface that can be used on classes that serve as models that have abilities (e.g. aMembershipPlanclass). This creates definitions for JSON serialized data, and the schema that corresponds to that data.Ability-- DTO representation of an ability. Largely follows the WordPress input args, but with additional properties that would enable certain features (such as SV framework REST integration, or in the future a CLI integration, etc.)Basic implementation
Example implementation: https://github.com/gdcorp-partners/woocommerce-memberships/pull/1272
Core steps are:
1. Create abilities
Create a class (e.g.
GetPlanthat implementsMakesAbilityContract. This defines a public methodmakeAbility() : Ability. Build the Ability DTO as desired.2. Create a provider
Create a provider class that extends
AbstractAbilitiesProvider.$abilitiesproperty. Each item is the name of your abilities class. I chose the class name approach to avoid having one file that defines 30 abilities and getting unwieldy.getCategories()method. I chose a method here instead of the class-per-category approach since category definitions take up less space and you'll have less of them.Example:
3. Implement
HasAbilitiesContractIn the main plugin class file, update the class to implement
HasAbilitiesContract. This will give you agetAbilitiesProvider()method to implement. Return an instance of your created provider.That's pretty much it for bootstrapping.
JsonSerializableThe purpose of the
JsonSerializableinterface is to standardize return values for objects. If you have an ability that returns an object, that object must be JSON serializable. You cannot return aMembership_Planobject on its own, unless that object can be JSON serialized.WordPress core checks for the base PHP
JsonSerializableinterface and accepts that. I took this a step further by having the framework define its own version that extends that. This ensures:JsonSerializableinterface)JsonSerializableinterface)The overall purpose of this is to keep code DRY and ensure that our returned data is standardized whether we're doing a "get plan" call or a "list plans" call.
QA
Before merge