Merge branch 'develop' of https://github.com/tainacan/tainacan into develop

This commit is contained in:
mateuswetah 2018-02-26 15:52:37 -03:00
commit 0b6ba4e88e
14 changed files with 310 additions and 69 deletions

View File

@ -87,7 +87,8 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API.
"title": "string", "title": "string",
"description": "string", "description": "string",
"status": "string", "status": "string",
}            "terms": ["integer", "integer", ...]
     }
``` ```
2. Route `wp-json/tainacan/v2/items/(?P<item_id>[\d]+)` 2. Route `wp-json/tainacan/v2/items/(?P<item_id>[\d]+)`
@ -108,7 +109,8 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API.
{ {
"title": "string", "title": "string",
"description": "string", "description": "string",
...            "terms": ["integer", "integer", ...]
           ...
} }
``` ```
@ -170,10 +172,7 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API.
```javascript ```javascript
{ {
"values": [            "values": ["any", "type"]
{ "new": "any_type", "prev": "any_type"},
{ "new": "any_type", "prev": ""}
]
} }
``` ```

View File

@ -92,7 +92,7 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
'offset' => 'offset', 'offset' => 'offset',
'metaquery' => 'meta_query', 'metaquery' => 'meta_query',
'datequery' => 'date_query', 'datequery' => 'date_query',
'taxquery' => 'taxquery', 'taxquery' => 'tax_query',
'order' => 'order', 'order' => 'order',
'orderby' => 'orderby', 'orderby' => 'orderby',
'metakey' => 'meta_key', 'metakey' => 'meta_key',

View File

@ -78,6 +78,21 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
)); ));
} }
private function add_terms_to_item($item_object, $item_array){
$item_terms = $item_object->get_terms();
foreach ($item_terms as $index => $term){
$term_id = $term['term_id'];
$item_array['terms'][$term_id]['name'] = $term['name'];
$item_array['terms'][$term_id]['description'] = $term['description'];
$item_array['terms'][$term_id]['taxonomy'] = $term['taxonomy'];
}
return $item_array;
}
private function add_metadata_to_item($item_object, $item_array){ private function add_metadata_to_item($item_object, $item_array){
$item_metadata = $item_object->get_fields(); $item_metadata = $item_object->get_fields();
@ -107,7 +122,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$item_arr['current_user_can_edit'] = $item->can_edit(); $item_arr['current_user_can_edit'] = $item->can_edit();
} }
return $this->add_metadata_to_item($item, $item_arr); $prep = $this->add_metadata_to_item($item, $item_arr);
return $this->add_terms_to_item($item, $prep);
} }
return $item; return $item;
@ -150,6 +166,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$limited_item = $this->get_only_needed_attributes($item, $map, $request['context']); $limited_item = $this->get_only_needed_attributes($item, $map, $request['context']);
$limited_item = $this->add_metadata_to_item($item, $limited_item); $limited_item = $this->add_metadata_to_item($item, $limited_item);
$limited_item = $this->add_terms_to_item($item, $limited_item);
array_push($response, $limited_item); array_push($response, $limited_item);
} }

View File

@ -28,6 +28,20 @@ class Item extends Entity {
return 'Hello, my name is '. $this->get_title(); return 'Hello, my name is '. $this->get_title();
} }
/**
* @param $value
*/
function set_terms($value){
$this->set_mapped_property('terms', $value);
}
/**
* @return mixed|null
*/
function get_terms(){
return $this->get_mapped_property('terms');
}
/** /**
* @return mixed|null * @return mixed|null
*/ */

View File

@ -8,9 +8,21 @@ abstract class Filter_Type {
private $supported_types = []; private $supported_types = [];
public $options; public $options;
public $component;
public function __construct(){
add_action('register_filter_types', array(&$this, 'register_filter_type'));
}
abstract function render( $field ); abstract function render( $field );
/**
* generate the fields for this field type
*/
public function form(){
}
/** /**
* @return array Supported types by the filter * @return array Supported types by the filter
*/ */
@ -27,6 +39,25 @@ abstract class Filter_Type {
$this->supported_types = $supported_types; $this->supported_types = $supported_types;
} }
/**
* @return mixed
*/
public function get_component() {
return $this->component;
}
/**
* @return array
*/
public function __toArray(){
$attributes = [];
$attributes['className'] = get_class($this);
$attributes['component'] = $this->get_component();
$attributes['supported_types'] = $this->get_supported_types();
return $attributes;
}
/** /**
* @param $options * @param $options
*/ */

View File

@ -11,6 +11,7 @@ class List_Filter extends Filter_Type {
function __construct(){ function __construct(){
parent::set_supported_types(['string']); parent::set_supported_types(['string']);
$this->component = 'tainacan-filter-list';
} }
/** /**

View File

@ -11,6 +11,7 @@ class Range extends Filter_Type {
function __construct(){ function __construct(){
parent::set_supported_types(['float','date']); parent::set_supported_types(['float','date']);
$this->component = 'tainacan-filter-range';
} }
/** /**

View File

@ -8,6 +8,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v; use \Respect\Validation\Validator as v;
class Filters extends Repository { class Filters extends Repository {
public $entities_type = '\Tainacan\Entities\Filter'; public $entities_type = '\Tainacan\Entities\Filter';
public $filters_types = [];
public function get_map() { public function get_map() {
return apply_filters('tainacan-get-map-'.$this->get_name(), [ return apply_filters('tainacan-get-map-'.$this->get_name(), [
@ -87,6 +88,7 @@ class Filters extends Repository {
'parent_item_colon' => __('Parent Filter:', 'tainacan'), 'parent_item_colon' => __('Parent Filter:', 'tainacan'),
'menu_name' => __('Filters', 'tainacan') 'menu_name' => __('Filters', 'tainacan')
); );
$args = array( $args = array(
'labels' => $labels, 'labels' => $labels,
'hierarchical' => true, 'hierarchical' => true,
@ -104,7 +106,7 @@ class Filters extends Repository {
'can_export' => true, 'can_export' => true,
'rewrite' => true, 'rewrite' => true,
'map_meta_cap' => true, 'map_meta_cap' => true,
'capability_type' => 'tainacan-filter', 'capability_type' => Entities\Field::get_post_type(),
'supports' => [ 'supports' => [
'title', 'title',
'editor', 'editor',
@ -209,21 +211,62 @@ class Filters extends Repository {
} }
} }
/** /**
* fetch all declared filter type classes * register field types class on array of types
* *
* @return Array of Entities\Filter_Types\Filter_Type objects * @param $class_name string | object The class name or the instance
*/ */
public function fetch_filter_types(){ public function register_filter_type( $class_name ){
$filters = array(); if( is_object( $class_name ) ){
$class_name = get_class( $class_name );
}
foreach (get_declared_classes() as $class) { if(!in_array( $class_name, $this->filters_types)){
if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){ $this->filters_types[] = $class_name;
$filters[] = new $class();
} }
} }
return $filters; /**
* register field types class on array of types
*
* @param $class_name string | object The class name or the instance
*/
public function deregister_filter_type( $class_name ){
if (is_object($class_name)) {
$class_name = get_class($class_name);
}
$key = array_search($class_name, $this->filters_types);
if ($key !== false) {
unset($this->filters_types[$key]);
}
}
/**
* fetch all registered filter type classes
*
* Possible outputs are:
* CLASS (default) - returns the Class name of of filter types registered
* NAME - return an Array of the names of filter types registered
*
* @param $output string CLASS | NAME
* @return array of Entities\Filter_Types\Filter_Type classes path name
*/
public function fetch_filter_types( $output = 'CLASS'){
$return = [];
do_action('register_filter_types');
if( $output === 'NAME' ){
foreach ($this->filters_types as $filter_type) {
$return[] = str_replace('Tainacan\Filter_Types\\','', $filter_type);
}
return $return;
}
return $this->filters_types;
} }
/** /**

View File

@ -76,6 +76,12 @@ class Items extends Repository {
'type' => 'array', 'type' => 'array',
'description' => __('The item attachments') 'description' => __('The item attachments')
], ],
'terms' => [
'map' => 'terms',
'title' => __('Term IDs', 'tainacan'),
'type' => 'array',
'description' => __('The item term IDs', 'tainacan'),
],
//'collection' => 'relation...', //'collection' => 'relation...',
// field .. field... // field .. field...
]); ]);
@ -127,7 +133,7 @@ class Items extends Repository {
// iterate through the native post properties // iterate through the native post properties
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms') {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop); $item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop);
} }
} }
@ -153,6 +159,21 @@ class Items extends Repository {
add_post_meta($id, $prop, wp_slash( $value )); add_post_meta($id, $prop, wp_slash( $value ));
} }
} }
} elseif($mapped['map'] == 'terms'){
$values = $item->get_mapped_property($prop);
if($values) {
$res = [];
foreach ($values as $value){
$taxonomy = get_term($value)->taxonomy;
$res[] = wp_set_post_terms( $item->WP_Post->ID, $value, $taxonomy );
}
if ( ! is_array( $res ) ) {
throw new \InvalidArgumentException( 'The id of post or taxonomy name or term name may be invalid. Response = ' . $res );
}
}
} }
} }

View File

@ -74,7 +74,7 @@ abstract class Repository {
// First iterate through the native post properties // First iterate through the native post properties
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms') {
$obj->WP_Post->{$mapped['map']} = $obj->get_mapped_property($prop); $obj->WP_Post->{$mapped['map']} = $obj->get_mapped_property($prop);
} }
} }
@ -92,7 +92,6 @@ abstract class Repository {
// Now run through properties stored as postmeta // Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') { if ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') {
$this->insert_metadata($obj, $prop); $this->insert_metadata($obj, $prop);
} }
@ -286,11 +285,9 @@ abstract class Repository {
$property = get_term_meta($entity->WP_Term->term_id, $prop, true); $property = get_term_meta($entity->WP_Term->term_id, $prop, true);
} elseif ( isset( $entity->WP_Post )) { } elseif ( isset( $entity->WP_Post )) {
if($mapped == 'thumbnail'){ if($mapped == 'thumbnail'){
if(isset($entity->WP_Post->ID)) { $property = isset($entity->WP_Post->ID) ? get_the_post_thumbnail_url($entity->WP_Post->ID, 'full') : null;
$property = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full');
}
} elseif($mapped == 'attachments'){ } elseif($mapped == 'attachments'){
if(isset($entity->WP_Post) && isset($entity->WP_Post->ID)){ if(isset($entity->WP_Post->ID)){
$attachments_query = [ $attachments_query = [
'post_type' => 'attachment', 'post_type' => 'attachment',
'post_per_page' => -1, 'post_per_page' => -1,
@ -317,6 +314,24 @@ abstract class Repository {
$property = $attachments_prepared; $property = $attachments_prepared;
} }
} elseif ($mapped === 'terms'){
$taxonomies = get_taxonomies('', 'names');
$terms_prepared = [];
foreach($taxonomies as $taxonomy){
if(stristr($taxonomy, 'tnc_tax_')) {
$terms = isset( $entity->WP_Post->ID ) ? wp_get_post_terms( $entity->WP_Post->ID, $taxonomy ) : null;
if ( is_array( $terms ) ) {
foreach ( $terms as $term ) {
$term = new Entities\Term( $term->term_id, $term->taxonomy );
array_push( $terms_prepared, $term->__toArray() );
}
}
}
}
$property = $terms_prepared;
} else { } else {
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null; $property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null;
} }
@ -540,6 +555,7 @@ abstract class Repository {
* @param Entity|integer|\WP_Post $new * @param Entity|integer|\WP_Post $new
* *
* @return array List of diff values * @return array List of diff values
* @throws \Exception
*/ */
public function diff($old = 0, $new) { public function diff($old = 0, $new) {
$old_entity = null; $old_entity = null;

View File

@ -93,6 +93,10 @@ $Tainacan_Fields->register_field_type('Tainacan\Field_Types\Checkbox');
global $Tainacan_Filters; global $Tainacan_Filters;
$Tainacan_Filters = new \Tainacan\Repositories\Filters(); $Tainacan_Filters = new \Tainacan\Repositories\Filters();
//register filter type
$Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\Range');
$Tainacan_Filters->register_filter_type('Tainacan\Filter_Types\List_Filter');
global $Tainacan_Taxonomies; global $Tainacan_Taxonomies;
$Tainacan_Taxonomies = new \Tainacan\Repositories\Taxonomies(); $Tainacan_Taxonomies = new \Tainacan\Repositories\Taxonomies();

View File

@ -33,7 +33,7 @@ trait Entity_Collections_Relation {
} }
public function set_collections_ids(Array $value) { public function set_collections_ids(Array $value) {
$this->set_mapped_property('collection_id', $value); $this->set_mapped_property('collections_ids', $value);
$this->collections = null; $this->collections = null;
} }

View File

@ -186,10 +186,12 @@ class DevInterface {
<?php Helpers\HtmlHelpers::collections_dropdown( $value ); ?> <?php Helpers\HtmlHelpers::collections_dropdown( $value ); ?>
<?php elseif ($prop == 'collections_ids'): ?> <?php elseif ($prop == 'collections_ids'): ?>
<?php Helpers\HtmlHelpers::collections_checkbox_list( $value ); ?> <?php Helpers\HtmlHelpers::collections_checkbox_list( $value ); ?>
<?php elseif ($prop == 'field_type_options'): ?> <?php elseif ($prop == 'field_type_options' || $prop == 'filter_type_options'): ?>
<?php echo $value; ?> <?php echo $value; ?>
<?php elseif ($prop == 'field_type'): ?> <?php elseif ($prop == 'field_type'): ?>
<?php echo $this->field_type_dropdown($post->ID,$value); ?> <?php echo $this->field_type_dropdown($post->ID,$value); ?>
<?php elseif ($prop == 'filter_type'): ?>
<?php echo $this->filter_type_dropdown($post->ID,$value); ?>
<?php else: ?> <?php else: ?>
<textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea> <textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea>
<?php endif; ?> <?php endif; ?>
@ -379,6 +381,33 @@ class DevInterface {
<?php <?php
} }
function filter_type_dropdown($id,$selected) {
global $Tainacan_Filters;
$class = ( class_exists( $selected ) ) ? new $selected() : '';
if(is_object( $class )){
$selected = str_replace('Tainacan\Filter_Types\\','', get_class( $class ) );
}
$types = $Tainacan_Filters->fetch_filter_types('NAME');
?>
<select name="tnc_prop_filter_type">
<?php foreach ($types as $type): ?>
<option value="<?php echo $type; ?>" <?php selected($type, $selected) ?>><?php echo $type; ?></option>
<?php endforeach; ?>
</select>
<?php
if( $class ){
$options = get_post_meta($id,'filter_type_options',true);
$class->set_options($options);
echo $class->form();
}
?>
<?php
}
function collections_checkbox_list($selected) { function collections_checkbox_list($selected) {
global $Tainacan_Collections; global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT'); $collections = $Tainacan_Collections->fetch([], 'OBJECT');
@ -432,8 +461,12 @@ class DevInterface {
$class = '\Tainacan\Field_Types\\'.$value; $class = '\Tainacan\Field_Types\\'.$value;
update_post_meta($post_id, 'field_type_options', $_POST['field_type_'.strtolower( $value ) ] ); update_post_meta($post_id, 'field_type_options', $_POST['field_type_'.strtolower( $value ) ] );
update_post_meta($post_id, 'field_type', wp_slash( get_class( new $class() ) ) ); update_post_meta($post_id, 'field_type', wp_slash( get_class( new $class() ) ) );
} elseif($prop == 'field_type_options') { } elseif($prop == 'field_type_options' || $prop == 'filter_type_options') {
continue; continue;
} elseif ($prop == 'filter_type') {
$class = '\Tainacan\Filter_Types\\'.$value;
update_post_meta($post_id, 'filter_type_options', $_POST['filter_type_'.strtolower( $value ) ] );
update_post_meta($post_id, 'filter_type', wp_slash( get_class( new $class() ) ) );
} elseif ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') { } elseif ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') {
$repo->insert_metadata($entity, $prop); $repo->insert_metadata($entity, $prop);

View File

@ -3,13 +3,34 @@
namespace Tainacan\Tests; namespace Tainacan\Tests;
/** /**
* @group api * @group queries
* **/ * **/
class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
public function test_queries(){ public function test_queries(){
// Populate the database // Populate the database
$collectionB = $this->tainacan_entity_factory->create_entity(
'collection',
[
'name' => 'B',
'description' => 'Collection B',
'status' => 'publish'
],
true
);
$collectionC = $this->tainacan_entity_factory->create_entity(
'collection',
[
'name' => 'C',
'description' => 'Collection C',
'status' => 'private'
],
true
);
$collectionA = $this->tainacan_entity_factory->create_entity( $collectionA = $this->tainacan_entity_factory->create_entity(
'collection', 'collection',
[ [
@ -20,6 +41,35 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
true true
); );
// Create Taxonomy
$taxonomyA = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'Tax A',
'description' => 'A taxonomy',
'allow_insert' => 'yes',
'status' => 'publish',
'collections_ids' => [
$collectionA->get_id(),
$collectionC->get_id(),
$collectionB->get_id()
]
),
true
);
// Create Term
$termA = $this->tainacan_entity_factory->create_entity(
'term',
array(
'taxonomy' => $taxonomyA->get_db_identifier(),
'name' => 'Term A',
'user' => get_current_user_id(),
),
true
);
//
// Create Items // Create Items
$itemA1 = $this->tainacan_entity_factory->create_entity( $itemA1 = $this->tainacan_entity_factory->create_entity(
'item', 'item',
@ -27,7 +77,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
'title' => 'Item A-1', 'title' => 'Item A-1',
'description' => 'Item in collection A', 'description' => 'Item in collection A',
'status' => 'publish', 'status' => 'publish',
'collection' => $collectionA 'collection' => $collectionA,
'terms' => [$termA]
], ],
true true
); );
@ -38,7 +89,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
'title' => 'Item A-2', 'title' => 'Item A-2',
'description' => 'Item in collection A', 'description' => 'Item in collection A',
'status' => 'private', 'status' => 'private',
'collection' => $collectionA 'collection' => $collectionA,
'terms' => [$termA]
], ],
true true
); );
@ -110,27 +162,6 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
// //
$collectionB = $this->tainacan_entity_factory->create_entity(
'collection',
[
'name' => 'B',
'description' => 'Collection B',
'status' => 'publish'
],
true
);
$collectionC = $this->tainacan_entity_factory->create_entity(
'collection',
[
'name' => 'C',
'description' => 'Collection C',
'status' => 'private'
],
true
);
// Fetch a collection with a specific name // Fetch a collection with a specific name
$name_query = ['name' => 'B']; $name_query = ['name' => 'B'];
@ -237,6 +268,36 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
$data5 = $date_query_response_collections->get_data(); $data5 = $date_query_response_collections->get_data();
$this->assertCount(0, $data5); $this->assertCount(0, $data5);
/* Tax Query
*
* Fetch items under a taxonomy with a specific term
*
* */
$tax_query = [
'taxquery' => [
[
'taxonomy' => $taxonomyA->get_db_identifier(),
'field' => 'slug',
'terms' => 'term-a'
]
]
];
$tax_query_request_collections = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collectionA->get_id() . '/items');
$tax_query_request_collections->set_query_params($tax_query);
$tax_query_response_collections = $this->server->dispatch($tax_query_request_collections);
$data6 = $tax_query_response_collections->get_data();
$this->assertCount(2, $data6);
$itemsA1_A2 = [$data6[0]['title'], $data6[1]['title']];
$this->assertContains('Item A-1', $itemsA1_A2);
$this->assertContains('Item A-2', $itemsA1_A2);
$this->assertNotContains('Item A-3', $itemsA1_A2);
} }
} }