Creating Exposers API endpoint
This commit is contained in:
parent
dcc2519d95
commit
13e34c1082
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\API\EndPoints;
|
||||
|
||||
use Tainacan\Exposers_Handler;
|
||||
use Tainacan\Mappers_Handler;
|
||||
use \Tainacan\API\REST_Controller;
|
||||
|
||||
class REST_Exposers_Controller extends REST_Controller {
|
||||
|
||||
|
||||
private $metadatum_repository;
|
||||
|
||||
/**
|
||||
* REST_Facets_Controller constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->rest_base = 'exposers';
|
||||
parent::__construct();
|
||||
add_action('init', array(&$this, 'init_objects'), 11);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize objects after post_type register
|
||||
*/
|
||||
public function init_objects() {
|
||||
$this->exposers = Exposers_Handler::get_instance();
|
||||
$this->mappers = Mappers_Handler::get_instance();
|
||||
}
|
||||
|
||||
public function register_routes() {
|
||||
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base, array(
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'callback' => array($this, 'get_items'),
|
||||
'permission_callback' => array($this, 'get_items_permissions_check')
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
*
|
||||
* @return \WP_Error|\WP_REST_Response
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
|
||||
$exposers = $this->exposers->get_exposers();
|
||||
|
||||
$response = [];
|
||||
|
||||
|
||||
foreach ($exposers as $exposer) {
|
||||
if ( class_exists($exposer) ) {
|
||||
$e = new $exposer();
|
||||
$response[] = $e->_toArray();
|
||||
}
|
||||
}
|
||||
|
||||
$rest_response = new \WP_REST_Response($response, 200);
|
||||
|
||||
$rest_response->header('X-WP-Total', count($response));
|
||||
|
||||
return $rest_response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \WP_REST_Request $request
|
||||
*
|
||||
* @return bool|\WP_Error
|
||||
*/
|
||||
public function get_items_permissions_check( $request ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -17,6 +17,7 @@ $rest_importers_controller = new \Tainacan\API\EndPoints\REST_Importers_Contr
|
|||
$rest_exporters_controller = new \Tainacan\API\EndPoints\REST_Exporters_Controller();
|
||||
$rest_background_processes_controller = new \Tainacan\API\EndPoints\REST_Background_Processes_Controller();
|
||||
$rest_bulkedit_controller = new \Tainacan\API\EndPoints\REST_Bulkedit_Controller();
|
||||
$rest_exposers_controller = new \Tainacan\API\EndPoints\REST_Exposers_Controller();
|
||||
new \Tainacan\API\EndPoints\REST_Export_Controller();
|
||||
new \Tainacan\API\EndPoints\REST_Metadatum_Mappers_Controller();
|
||||
$rest_facets_controller = new \Tainacan\API\EndPoints\REST_Facets_Controller();
|
||||
|
|
|
@ -8,13 +8,12 @@ namespace Tainacan\Exposers;
|
|||
*/
|
||||
class Csv extends Exposer {
|
||||
|
||||
/**
|
||||
* List of supported mappers
|
||||
* @var array
|
||||
*/
|
||||
public $mappers = ['Value'];
|
||||
public $slug = 'csv'; // type slug for url safe
|
||||
public $name = 'Comma-separated values';
|
||||
|
||||
function __construct() {
|
||||
$this->set_name( __('CSV', 'tainacan') );
|
||||
$this->set_description( __('Comma-separated values', 'tainacan') );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
|
@ -2,16 +2,72 @@
|
|||
|
||||
namespace Tainacan\Exposers;
|
||||
|
||||
use Tainacan\Mappers_Handler;
|
||||
|
||||
/**
|
||||
* abstract class for implement exposer types
|
||||
*
|
||||
*/
|
||||
abstract class Exposer {
|
||||
|
||||
protected $mappers = true; // List of supported mapper, leave true for all
|
||||
protected $extension = 'tnc'; // extension sufix for multi operation system compatibility
|
||||
protected $mappers = true; // List of supported mappers, leave true for all
|
||||
public $accept_no_mapper = true;
|
||||
public $slug = ''; // type slug for url safe
|
||||
public $name = ''; // User friend Name
|
||||
private $name = ''; // User friendly Name
|
||||
private $description = ''; // User friendly Description
|
||||
|
||||
/**
|
||||
* Returns i18n exposer name
|
||||
*
|
||||
* Must be implemented by Exposer class
|
||||
*
|
||||
* @param string Name
|
||||
* @return string
|
||||
*/
|
||||
protected function set_name($name) {
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets i18n exposer description
|
||||
*
|
||||
* @param string Description
|
||||
* @return string
|
||||
*/
|
||||
protected function set_description($description) {
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the exposer name
|
||||
* @return string exposer name
|
||||
*/
|
||||
public function get_name() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the exposer description
|
||||
* @return string exposer description
|
||||
*/
|
||||
public function get_description() {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* return exposer object as an array
|
||||
* @return array
|
||||
*/
|
||||
public function _toArray() {
|
||||
return [
|
||||
'slug' => $this->slug,
|
||||
'name' => $this->get_name(),
|
||||
'description' => $this->get_description(),
|
||||
'mappers' => $this->get_mappers(),
|
||||
'accept_no_mapper' => $this->accept_no_mapper,
|
||||
'class_name' => get_class($this)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Change response after api callbacks
|
||||
|
@ -26,10 +82,15 @@ abstract class Exposer {
|
|||
* Return list of supported mappers for this type
|
||||
*/
|
||||
public function get_mappers() {
|
||||
return apply_filters('tainacan-exporser-type-mappers', $this->mappers, $this);
|
||||
}
|
||||
|
||||
public function get_extension() {
|
||||
return $this->extension;
|
||||
$mappers = apply_filters('tainacan-exporser-type-mappers', $this->mappers, $this);
|
||||
if ( true === $mappers ) {
|
||||
$mappers_handler = Mappers_Handler::get_instance();
|
||||
$registered_mappers = $mappers_handler->get_mappers();
|
||||
return array_keys($registered_mappers);
|
||||
} elseif (is_array($mappers)) {
|
||||
return $mappers;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -37,7 +37,6 @@ class Exposers_Handler {
|
|||
do_action('tainacan-register-exposer', $this);
|
||||
|
||||
add_filter( 'rest_request_after_callbacks', [$this, 'rest_request_after_callbacks'], 10, 3 ); //exposer types
|
||||
// add_filter( 'tainacan-rest-response', [$this, 'rest_response'], 10, 2 ); // exposer mapper
|
||||
|
||||
}
|
||||
|
||||
|
@ -100,32 +99,6 @@ class Exposers_Handler {
|
|||
return ($root ? '\\' : '').$class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if rest response need mapper
|
||||
* @param array $item_arr
|
||||
* @param \WP_REST_Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function rest_response($item_arr, $request) {
|
||||
if($request->get_method() == 'GET' && $this->is_tainacan_request($request)) {
|
||||
if($exposer = $this->request_has_mapper($request)) {
|
||||
if(substr($request->get_route(), 0, strlen('/tainacan/v2/items')) == '/tainacan/v2/items') { //TODO do it at rest not here
|
||||
$repos_items = \Tainacan\Repositories\Items::get_instance();
|
||||
$item = $repos_items->fetch($item_arr['id']);
|
||||
$items_metadata = $item->get_metadata();
|
||||
$prepared_item = [];
|
||||
foreach ($items_metadata as $item_metadata){
|
||||
array_push($prepared_item, $item_metadata->_toArray());
|
||||
}
|
||||
$item_arr = $prepared_item;
|
||||
}
|
||||
return $this->map($item_arr, $exposer, $request); //TODO request -> args
|
||||
}
|
||||
}
|
||||
return $item_arr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* check if is a tainacan request
|
||||
|
|
|
@ -8,10 +8,14 @@ namespace Tainacan\Exposers;
|
|||
*/
|
||||
class Html extends Exposer {
|
||||
|
||||
public $mappers = ['Value'];
|
||||
public $slug = 'html'; // type slug for url safe
|
||||
public $name = 'HyperText Markup Language';
|
||||
|
||||
function __construct() {
|
||||
$this->set_name( 'HTML' );
|
||||
$this->set_description( __('A simple HTML table', 'tainacan') );
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* {@inheritDoc}
|
||||
|
|
|
@ -23,7 +23,7 @@ class Mappers_Handler {
|
|||
self::$instance = $this;
|
||||
|
||||
$this->register_mapper('Tainacan\Mappers\Dublin_Core');
|
||||
$this->register_mapper('Tainacan\Mappers\Value');
|
||||
//$this->register_mapper('Tainacan\Mappers\Value');
|
||||
do_action('tainacan-register-mappers', $this);
|
||||
|
||||
add_filter( 'tainacan-admin-i18n', [$this, 'mappers_i18n']);
|
||||
|
|
Loading…
Reference in New Issue