tainacan/docs/exposers.md

260 lines
6.4 KiB
Markdown
Raw Normal View History

2019-05-13 19:31:56 +00:00
# How to create an Exposer
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
Exposers are classes that implement a new Exposer that can be used by Tainacan to expose the content of a repository via API.
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
In order to create a new exposer you basically have to create an Exposer class and register it.
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
## Creating an Exposer class
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
Create a class that extends `\Tainacan\Exposers\Exposer`.
2018-08-08 06:00:02 +00:00
2019-05-13 19:31:56 +00:00
```PHP
<?php
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
class MyExposer extends \Tainacan\Exposers\Exposer {
}
```
In this class you will have to set up some attributes and methods:
### Attributes
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
#### Slug
**String $slug**
2018-08-08 06:00:02 +00:00
2018-03-15 21:35:09 +00:00
A URL friendly version of the Exposer name, to be used as a parameter to the API request informing you want to get the data using this exposer.
2019-05-13 19:31:56 +00:00
```PHP
<?php
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
class MyExposer extends \Tainacan\Exposers\Exposer {
public $slug = 'my-exposer';
}
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
```
2018-08-08 06:00:02 +00:00
2019-05-13 19:31:56 +00:00
#### Supported Mapping Standard
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
**Array or true $mappers**
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
A list of mapping standards that is exposer supports. This means that whenever someone makes a request to receive data via this exposer, he/she will also be able to choose in which mapping standard they want the content to be served. If set to `true` the exposer will accept all mapping standards.
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
```PHP
<?php
2018-08-08 06:00:02 +00:00
2019-05-13 19:31:56 +00:00
class MyExposer extends \Tainacan\Exposers\Exposer {
public $slug = 'my-exposer';
public $mappers = true;
}
2018-08-08 06:00:02 +00:00
2019-05-13 19:31:56 +00:00
```
2018-08-08 06:00:02 +00:00
2019-05-13 19:31:56 +00:00
or
2018-03-15 21:35:09 +00:00
2019-05-13 19:31:56 +00:00
```PHP
<?php
2018-08-09 15:24:34 +00:00
2019-05-13 19:31:56 +00:00
class MyExposer extends \Tainacan\Exposers\Exposer {
public $slug = 'my-exposer';
public $mappers = ['dublin-core']; // indicates that this exposer will only serve data mapped to dublin core mapper
}
2018-08-09 15:54:05 +00:00
```
2019-05-13 19:31:56 +00:00
#### Accept no Mapping Standards
**Bool $accept_no_mapper**
Indicates whether this exposer accept to serve data in its native form, without any mapping standards.
```PHP
<?php
class MyExposer extends \Tainacan\Exposers\Exposer {
public $slug = 'my-exposer';
public $mappers = ['dublin-core']; // indicates that this exposer will only serve data mapped to dublin core mapper
public $accept_no_mapper = true;
}
```
### Methods
Now that you have declared the basic attributes of your Exposer, there are two methods you must implement.
#### __construct()
In this method you must call `set_name()` and `set_description()` to identify your exposer.
```PHP
<?php
class MyExposer extends \Tainacan\Exposers\Exposer {
public $slug = 'my-exposer';
public $mappers = ['dublin-core']; // indicates that this exposer will only serve data mapped to dublin core mapper
public $accept_no_mapper = true;
public function __construct() {
$this->set_name( __('My Exposer', 'my-test-plugin-namespace') );
$this->set_description( __('This exposer server the data in a very different way', 'my-test-plugin-namespace') );
2018-08-09 15:54:05 +00:00
}
2019-05-13 19:31:56 +00:00
}
2018-08-09 15:54:05 +00:00
```
2019-05-13 19:31:56 +00:00
**Note**: The reason Name and Description are declared this way, and not as attributes, is to give you the opportunity to localize your strings to different languages. Please refer to the WordPress documentation to learn how to internationalize your plugin.
#### rest_request_after_callbacks()
Now this is where all the magic happens!
2018-08-09 15:24:34 +00:00
2019-05-13 19:31:56 +00:00
This method will be called right before the API returns the data to the client.
It will give you all the items it received, in the way they were about to be served in the default JSON format, and give you the opportunity to transform it.
It receives 3 parameters:
* $response: an instance of the `\WP_REST_Response` object
* $response: an instance of the `\WP_REST_Server` object
* $response: an instance of the `\WP_REST_Request` object
This method have to return the modified version of the `\WP_REST_Response` object.
```PHP
<?php
class MyExposer extends \Tainacan\Exposers\Exposer {
public $slug = 'my-exposer';
public $mappers = ['dublin-core']; // indicates that this exposer will only serve data mapped to dublin core mapper
public $accept_no_mapper = true;
public function __construct() {
$this->set_name( __('My Exposer', 'my-test-plugin-namespace') );
$this->set_description( __('This exposer server the data in a very different way', 'my-test-plugin-namespace') );
}
2018-08-09 15:24:34 +00:00
2019-05-13 19:31:56 +00:00
public function rest_request_after_callbacks( $response, $handler, $request ) {
// Set the headers to another content type, if applicable
$response->set_headers( ['Content-Type: text/plain; charset=' . get_option( 'blog_charset' )] );
$items = $response->get_data();
// Transform the items somehow ...
// ...
$response->set_data($items);
return $response;
}
2018-08-09 15:24:34 +00:00
2019-05-13 19:31:56 +00:00
}
```
### Registering a new exposer
To register a new exposer, the action need to be added to the `init` hook, like:
```PHP
<?php
function registerMyExposer() {
$exposers = \Tainacan\Exposers_Handler::get_instance();
$exposers->register_exposer_type('MyExposer');
}
add_action('init', 'registerMyExposer');
```
### Full Example
This is a full example of a plugin that implements a simple text exposer
```PHP
<?php
/*
Plugin Name: Tainacan TXT Exposer
Description: This is a sample exposer class
*/
function myNewExposer($exposers) {
class TxtExposer extends \Tainacan\Exposers\Exposer {
2018-08-09 15:24:34 +00:00
public $slug = 'txt'; // type slug for url safe
2018-08-09 15:54:05 +00:00
public $name = 'TXT';
2019-05-13 19:31:56 +00:00
protected $mappers = true;
public $accept_no_mapper = true;
private $identation = '';
function __construct() {
$this->set_name( 'TXT' );
$this->set_description( __('A simple TXT table', 'my-test-plugin-namespace') );
}
2018-08-09 15:24:34 +00:00
/**
2019-05-13 19:31:56 +00:00
*
* {@inheritDoc}
* @see \Tainacan\Exposers\Types\Type::rest_request_after_callbacks()
*/
2018-08-09 15:24:34 +00:00
public function rest_request_after_callbacks( $response, $handler, $request ) {
$response->set_headers( ['Content-Type: text/plain; charset=' . get_option( 'blog_charset' )] );
2019-05-13 19:31:56 +00:00
$txt = $this->array_to_txt($response->get_data(), false);
2018-08-09 15:24:34 +00:00
$response->set_data($txt);
return $response;
}
/**
2019-05-13 19:31:56 +00:00
* Convert Array to Txt
* @param array $data
* @param string $txt
* @return string
*/
protected function array_to_txt( $data, $addlevel = true ) {
$return = '';
if ($addlevel) {
$this->identation .= ' ';
}
2018-08-09 15:24:34 +00:00
foreach( $data as $key => $value ) {
2019-05-13 19:31:56 +00:00
$return .= $this->identation . $key . ': ';
if (is_array($value)) {
$return .= "\n" . $this->array_to_txt($value);
2018-08-09 15:24:34 +00:00
} else {
2019-05-13 19:31:56 +00:00
$return .= $value . "\n";
2018-08-09 15:24:34 +00:00
}
2019-05-13 19:31:56 +00:00
2018-08-09 15:24:34 +00:00
}
2019-05-13 19:31:56 +00:00
if ($addlevel) {
$this->identation = substr($this->identation, 0, strlen($this->identation) - 4 );
}
return $return;
2018-08-09 15:24:34 +00:00
}
}
2019-05-13 19:31:56 +00:00
$exposers = \Tainacan\Exposers_Handler::get_instance();
$exposers->register_exposer('TxtExposer');
}
add_action('init', 'myNewExposer');
```