WordPress API EndPoint Create: Generate classes to create WordPress API endpoints

Recommend this page to a friend!
  Info   View files Documentation   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2021-03-18 (7 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 27 All time: 10,340 This week: 233Up
Version License PHP version Categories
wp-endpoint-gen 1.0BSD License7PHP 5, Web services, Code Generation, B...
Description Author

This package can generate classes to create WordPress API endpoints.

It provides a configuration class that can be used to add new calls to the WordPress API by specifying new functions that will be called when specific HTTP methods are used to call the WordPress API.

A separate class can be used to generate code for a class with the specified functions using the configuration class.

The generated class should be edited to add the necessary code to implement the actual functionality that it is intended to be added to the WordPress API.

Innovation Award
PHP Programming Innovation award nominee
March 2021
Number 10
WordPress is a popular content management system used by many sites to publish blog posts and other types of content.

Its capabilities can be expanded by the means of plugins that enhance the list of features that WordPress can provide to its users.

WordPress also provides an API that can be called by external applications to integrate with WordPress.

This package can be used by plugin developers to improve the WordPress API with new function endpoints that can be called by external applications to integrate with new plugins that use the package.

Manuel Lemos
Picture of Carlos Artur Curvelo da Matos
  Performance   Level  
Name: Carlos Artur Curvelo da ... is available for providing paid consulting. Contact Carlos Artur Curvelo da ... .
Classes: 17 packages by
Country: Portugal Portugal
Age: 43
All time rank: 321241 in Portugal Portugal
Week rank: 36 Up2 in Portugal Portugal Up
Innovation award
Innovation award
Nominee: 11x

Winner: 2x

Details

wp-endpoint-gen

Wordpress custom endpoints helper and class generator. Wordpress engine to add new custom endpoints to the WP API is not that hard to understand. However, manipulate dozens of new endpoints and the respective callbacks and permission callbacks can be troublesome depending on the complexity of the plugin or extension. Thus, this library helps you not only by creating an OOP approach to add new endpoints, but also for managing their namespaces, arguments and callbacks. Besides, we thought that'd be much simpler and organized if we concentrated callbacks and permissions for each of the new endpoints in a particular controller class - and, of course, automatically generate them depending on how you customize your new endpoint. Well, nothing else to add by now - let's check how it works within the following code samples.

Installation

Usage

The first step involves creating a new instance of the class WPH\Endpoints\Config in your code. Do not forget to make it instantiable using Composer's autoload. The Config class can either be instantiated within the Generator class, or before it, everytime you desire to change the default values for the config details.

Configuring what?

By default, we considered all custom endpoints to be positioned within the namespace `wph(for obvious reasons). Generated classes will be created under the namespace or Psr "EndpointGroup", and will bring "v1" as the current version. The base path for saving the generated classes will be located onto a new folder named "endpoints", inside thewp-content` directory. Brilliant, ain't it? No? Don't worry, by instantiating the Config class, you are able to customize all of those variables.

use WPH\Endpoints\Config;

require __DIR__ . '/vendor/autoload.php';

$config = new Config(); // Config class instance.
$config->setPsr('Foo'); // Sets generated classes' namespaces as Foo
$config->setPath( WP_CONTENT_DIR . '/foo'); // Creates and sets the generated classes' path 
$config->setVersion('v1.2'); // Sets a new version for all endpoints
$config->setNamespace('foo-api'); // Sets a new namespace for the WP API endpoints (not the classes' namespace)

I loved your defaults - don't need to change it

Brilliant again! In this case, just ignore the Config class set up, and go to the next part of this tutorial. You'll still need to use the Config class, but if you don't need to change anything, you should just inject a new instance of it within the Generator class. The next example will clarify.

use WPH\Endpoints\Generator;

require __DIR__ . '/vendor/autoload.php';

// If you instantiated the Config class and set up new values for the variables, just use the generator including three arguments: the endpoint name, an array of all http methods to be allowed on it and, finally, the object of the Config class:
$gen = new Generator('wph-endpoint', ['GET', 'PUT'], $config); // That's it. The new endpoints have been created inside your WP API. 
$gen->generate(); // Now the magic: check the configured path or, if you just used ours, look inside the `wp-content/endpoints` directory.
$gen->autoload(); // Once callbacks and permission callbacks have been set up as external classes, we need to autoload them.

All custom endpoints will find the respective callbacks and permission callbacks inside their own classes, located in the path you indicated. It's more than just thinking the WP endpoints in an OOP way - that means you DON'T NEED to write dozens of repetitive functions and callbacks, nor pollute your code with huge anonymous functions or similars.

Ok, but you guys forgot the endpoint arguments...

Uhhh... no, actually we did not. By the way, you can add args to the endpoints using two different methods. 1. If all endpoint methods shall be accepting the same arguments, add them as an array, while instantiating the Generator class (underway) 2. However, if you need to add arguments just for the GET method, for instance, you can apply the chain method addArgs().

Let's start with the method 2, which is already functional. All you need to do is using the chained method addArgs(), with two paramets: the http method (GET, for instance), and an array of the desired arguments. Like this:

$gen = new Generator('wph-endpoint', ['GET', 'PUT'], $config); 
$gen->addArgs('PUT', ['id' => ['description' => 'Id of something']]);
$gen->addArgs('GET', ['name' => ['description' => 'Name of someone']]);

How generated classes look like?

So now you are probably asking yourself how those generated controllers we mentioned look like. Of course they are basically skeletons in which you can create your own processes and conditions, but they are already functional from the first moment, returning a simple instance of WP_Rest_Response class. Let's consider an endpoint added under the name 'boxes', and dealing with GET, POST and HEAD calls. In this case, the generated controller class would appear like this:


namespace Foo\Routes;
/
 * Controller class for callbacks and permissions.
 * Route --> Foo\Routes\Boxes
 * @since 1.0.0
 */
class Boxes
{
	/
	 * Handles GET requests to the endpoint.
	 * @return \WP_Rest_Response
	 */
	public function getBoxes(\WP_Rest_Request $request)
	{
		return new \WP_Rest_Response();
	}


	/
	 * Handles POST requests to the endpoint.
	 * @return \WP_Rest_Response
	 */
	public function postBoxes(\WP_Rest_Request $request)
	{
		return new \WP_Rest_Response();
	}


	/
	 * Handles HEAD requests to the endpoint.
	 * @return \WP_Rest_Response
	 */
	public function headBoxes(\WP_Rest_Request $request)
	{
		return new \WP_Rest_Response();
	}


	/
	 * Authenticate or limitate requests to the endpoint.
	 * @return bool
	 */
	public function permissions(\WP_Rest_Request $request)
	{
		// Your conditions.
		return true;
	}
}

All classes are already annotated and bring a callback for each HTTP method created for the respective endpoint, as well as a permissions() function, which return `true` by default. Callbacks, as shown, initially return a simple instance of the WP_Rest_Response class.

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file Config.php Class Class source
  Plain text file Generator.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:27
This week:0
All time:10,340
This week:233Up
For more information send a message to info at phpclasses dot org.