Download the PHP package cmatosbc/wp-coderpress-endpoints without Composer
On this page you can find all versions of the php package cmatosbc/wp-coderpress-endpoints. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cmatosbc/wp-coderpress-endpoints
More information about cmatosbc/wp-coderpress-endpoints
Files in cmatosbc/wp-coderpress-endpoints
Package wp-coderpress-endpoints
Short Description Endpoints builder and manager for CoderPress
License gpl-3.0-or-later
Informations about the package wp-coderpress-endpoints
WP CoderPress Endpoints
This package intends to provide a more straightforward way to add custom endpoints to Wordpress Rest API (added to mu-plugins with no hooks, as a suggestion), also using a Facade pattern which allows:
- To attach PSR-16 compliant cache drivers of any nature
- To attach as many middlewares as you want to modify the request or provide a particular response
- Interfere in the way cached responses are stored and retrieved (JSON, serialize(), igbinary_serialize())
- Provide a port to add multiple custom endpoints without repeating code and sharing cache, middleware and variables within, including a dynamic endpoint generation
- Offer common middlewares to handle common scenarios in REST API development
Installation
Easily install the package using composer:
Basic Example
The following example creates a very simple example endpoint to the rest API that comes with two middlewares attached (as \Closure objects). The first one modifies any passed parameter to "id = 123", which triggers the second middleware and returns a WP_REST_Response - this also prevents the cache to be done.
If the second middleware is removed, the endpoint is no longer short-circuited, so the results are now serialized and cached in a file within wp-content/cache/. In the example, the cache is managed by FileCache, but this package also includes usable drivers for Redis (using redis-php extension) and MySQL cache, using a custom DB table.
Middlewares
The package includes several built-in middlewares to handle common scenarios in REST API development:
CORS Middleware
The CORS (Cross-Origin Resource Sharing) middleware allows you to configure cross-origin requests to your API endpoints. Here's how to use it:
The CORS middleware provides:
- Origin validation against a whitelist
- Configurable HTTP methods
- Customizable allowed headers
- Preflight request handling
- Configurable cache duration for preflight responses
- Credentials support
All parameters are optional and come with sensible defaults for typical API usage.
Sanitization Middleware
The Sanitization middleware provides comprehensive input sanitization and validation for your API endpoints. It helps prevent XSS attacks, SQL injection, and ensures data consistency:
The middleware provides:
- Field-specific sanitization rules using callbacks
- HTML tag stripping with configurable allowed tags
- Special character encoding
- UTF-8 validation
- Recursive array sanitization
- WordPress-specific sanitization functions integration
- XSS and SQL injection protection
All parameters are optional and come with secure defaults. Use the rules
parameter to define custom sanitization logic for specific fields.