-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add page regarding plugins as dependencies
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
One of the main strengths of WordPress is the wide range of plugins available | ||
which allow us to fully customise a website in a short time period. However, every time a plugin | ||
is added it can negatively impact the performance of our API endpoints, because even | ||
though those endpoints might not need some of the activated plugins to work properly, they will | ||
still be loaded. | ||
|
||
To address this issue [WP-FastEndpoints Depends](https://github.com/matapatos/wp-fastendpoints-depends) | ||
was created to enable us to treat plugins as REST endpoint dependencies. | ||
|
||
## Adding another plugin?? 😱 | ||
|
||
Yes, this is a plugin! It could seem counterintuitive that adding another plugin could | ||
positively impact our API endpoints. However, given that in most cases our API | ||
endpoints don't need all the plugins that are active e.g. BuddyPress, Elementor | ||
it can actually improve your API endpoints. | ||
|
||
## How it works? | ||
|
||
Given this plugin needs to be setup as a MU-plugin it will always run before any regular plugin | ||
which allow us to decide which plugins are necessary for a given REST endpoint before loading them. | ||
|
||
## How to use it? | ||
|
||
Currently, we support both native WP endpoints and FastEndpoints 😊 | ||
|
||
=== "With FastEndpoints" | ||
|
||
```php | ||
$router->get('/example/all-plugins', function () { | ||
return "Loads all active plugins"; | ||
}); | ||
|
||
$router->get('/example/buddypress', function () { | ||
return "Only MyPlugin and BuddyPress plugins are loaded"; | ||
})->depends(['my-plugin', 'buddypress']); | ||
``` | ||
|
||
=== "Native WP endpoints" | ||
|
||
```php | ||
// Loads all active plugins | ||
register_rest_route('native/v1', 'example/all-plugins', [ | ||
'methods' => 'GET', | ||
(...) | ||
]); | ||
|
||
// Only MyPlugin and BuddyPress plugins are loaded | ||
register_rest_route('native/v1', 'example/buddypress', [ | ||
'methods' => 'GET', | ||
'depends' => ['my-plugin', 'buddypress'], | ||
(...) | ||
]); | ||
}); | ||
``` | ||
|
||
???+ tip | ||
By default, if no dependencies are specified in an endpoint it assumes that all active plugins needs | ||
to be loaded. This behaviour could be overridden for a given set of WP-FastEndpoint's by setting | ||
router dependencies e.g. `$router->depends(['my-plugin'])` | ||
|
||
### Router vs Endpoint dependencies | ||
|
||
With WP-FastEndpoint's we are able to either define _global_ endpoint dependencies via router dependencies | ||
or specific endpoint dependencies. | ||
|
||
One common scenario where router dependencies might be useful is when we want to change the default behaviour | ||
of loading all active plugins per endpoint. | ||
|
||
```php | ||
$router = new \Wp\FastEndpoints\Router('my-api', 'v1'); | ||
$router->depends(['my-plugin']); // All endpoints and sub-routers would have this dependency | ||
``` | ||
|
||
!!! danger | ||
When adding dependencies to endpoints, make sure to at least include the given plugin that holds those endpoints. | ||
For instance, if your endpoints reside inside a plugin with a slug `my-plugin` you have to set the dependencies | ||
to `['my-plugin']` otherwise when a request is received for that endpoint `my-plugin` will not be loaded. |