Skip to content

Commit

Permalink
docs: Add page regarding plugins as dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
matapatos committed Nov 20, 2024
1 parent eb5d9be commit b85a6c8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- IDE auto completion support
- No magic router. It uses WordPress [`register_rest_route`](https://developer.wordpress.org/reference/functions/register_rest_route/)
- Support for newer JSON schema drafts thanks to [opis/json-schema](https://opis.io/json-schema/2.x/)
- Able to treat plugins as dependencies via [WP-FastEndpoints Depends](https://github.com/matapatos/wp-fastendpoints-depends)

## Requirements

Expand Down
77 changes: 77 additions & 0 deletions docs/docs/advanced-user-guide/plugins-as-dependencies.md
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.

0 comments on commit b85a6c8

Please sign in to comment.