PHP code example of carloswph / wp-endpoint-gen

1. Go to this page and download the library: Download carloswph/wp-endpoint-gen library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carloswph / wp-endpoint-gen example snippets


use WPH\Endpoints\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)


use WPH\Endpoints\Generator;

onfig 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.


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



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;
	}
}