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",
"description": "string",
"status": "string",
}
           "terms": ["integer", "integer", ...]
     }
```
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",
"description": "string",
...
           "terms": ["integer", "integer", ...]
           ...
}
```
@ -170,10 +172,7 @@ A REST API for Tainacan Plugin. This API uses the Wordpress REST API.
```javascript
{
"values": [
{ "new": "any_type", "prev": "any_type"},
{ "new": "any_type", "prev": ""}
]
           "values": ["any", "type"]
}
```

View File

@ -92,7 +92,7 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
'offset' => 'offset',
'metaquery' => 'meta_query',
'datequery' => 'date_query',
'taxquery' => 'taxquery',
'taxquery' => 'tax_query',
'order' => 'order',
'orderby' => 'orderby',
'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){
$item_metadata = $item_object->get_fields();
@ -85,8 +100,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$field = $me->get_field();
$slug = $field->get_slug();
$item_array['metadata'][$slug]['name'] = $field->get_name();
$item_array['metadata'][$slug]['value'] = $me->get_value();
$item_array['metadata'][$slug]['name'] = $field->get_name();
$item_array['metadata'][$slug]['value'] = $me->get_value();
$item_array['metadata'][$slug]['multiple'] = $field->get_multiple();
}
@ -107,7 +122,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
$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;
@ -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->add_metadata_to_item($item, $limited_item);
$limited_item = $this->add_terms_to_item($item, $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();
}
/**
* @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
*/

View File

@ -8,9 +8,21 @@ abstract class Filter_Type {
private $supported_types = [];
public $options;
public $component;
public function __construct(){
add_action('register_filter_types', array(&$this, 'register_filter_type'));
}
abstract function render( $field );
/**
* generate the fields for this field type
*/
public function form(){
}
/**
* @return array Supported types by the filter
*/
@ -27,6 +39,25 @@ abstract class Filter_Type {
$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
*/

View File

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

View File

@ -11,6 +11,7 @@ class Range extends Filter_Type {
function __construct(){
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;
class Filters extends Repository {
public $entities_type = '\Tainacan\Entities\Filter';
public $filters_types = [];
public function get_map() {
return apply_filters('tainacan-get-map-'.$this->get_name(), [
@ -87,6 +88,7 @@ class Filters extends Repository {
'parent_item_colon' => __('Parent Filter:', 'tainacan'),
'menu_name' => __('Filters', 'tainacan')
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
@ -104,7 +106,7 @@ class Filters extends Repository {
'can_export' => true,
'rewrite' => true,
'map_meta_cap' => true,
'capability_type' => 'tainacan-filter',
'capability_type' => Entities\Field::get_post_type(),
'supports' => [
'title',
'editor',
@ -209,21 +211,62 @@ class Filters extends Repository {
}
}
/**
* fetch all declared filter type classes
*
* @return Array of Entities\Filter_Types\Filter_Type objects
*/
public function fetch_filter_types(){
$filters = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){
$filters[] = new $class();
}
/**
* register field types class on array of types
*
* @param $class_name string | object The class name or the instance
*/
public function register_filter_type( $class_name ){
if( is_object( $class_name ) ){
$class_name = get_class( $class_name );
}
return $filters;
if(!in_array( $class_name, $this->filters_types)){
$this->filters_types[] = $class_name;
}
}
/**
* 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',
'description' => __('The item attachments')
],
'terms' => [
'map' => 'terms',
'title' => __('Term IDs', 'tainacan'),
'type' => 'array',
'description' => __('The item term IDs', 'tainacan'),
],
//'collection' => 'relation...',
// field .. field...
]);
@ -127,7 +133,7 @@ class Items extends Repository {
// iterate through the native post properties
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);
}
}
@ -153,7 +159,22 @@ class Items extends Repository {
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 );
}
}
}
}
do_action('tainacan-insert', $item);

View File

@ -69,12 +69,12 @@ abstract class Repository {
throw new \Exception('Entities must be validated before you can save them');
// TODO: Throw Warning saying you must validate object before insert()
}
$map = $this->get_map();
// First iterate through the native post properties
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);
}
}
@ -92,7 +92,6 @@ abstract class Repository {
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') {
$this->insert_metadata($obj, $prop);
}
@ -286,11 +285,9 @@ abstract class Repository {
$property = get_term_meta($entity->WP_Term->term_id, $prop, true);
} elseif ( isset( $entity->WP_Post )) {
if($mapped == 'thumbnail'){
if(isset($entity->WP_Post->ID)) {
$property = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full');
}
$property = isset($entity->WP_Post->ID) ? get_the_post_thumbnail_url($entity->WP_Post->ID, 'full') : null;
} elseif($mapped == 'attachments'){
if(isset($entity->WP_Post) && isset($entity->WP_Post->ID)){
if(isset($entity->WP_Post->ID)){
$attachments_query = [
'post_type' => 'attachment',
'post_per_page' => -1,
@ -317,6 +314,24 @@ abstract class Repository {
$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 {
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null;
}
@ -532,15 +547,16 @@ abstract class Repository {
}
return user_can($user, $entity_cap->publish_posts, $entity->get_id());
}
/**
* Compare two repository entities
*
* @param Entity|integer|\WP_Post $old default ($which = 0) to self compare with stored entity
* @param Entity|integer|\WP_Post $new
*
* @return array List of diff values
*/
/**
* Compare two repository entities
*
* @param Entity|integer|\WP_Post $old default ($which = 0) to self compare with stored entity
* @param Entity|integer|\WP_Post $new
*
* @return array List of diff values
* @throws \Exception
*/
public function diff($old = 0, $new) {
$old_entity = null;
if($old === 0) { // self diff or other entity?

View File

@ -93,6 +93,10 @@ $Tainacan_Fields->register_field_type('Tainacan\Field_Types\Checkbox');
global $Tainacan_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;
$Tainacan_Taxonomies = new \Tainacan\Repositories\Taxonomies();

View File

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

View File

@ -186,10 +186,12 @@ class DevInterface {
<?php Helpers\HtmlHelpers::collections_dropdown( $value ); ?>
<?php elseif ($prop == 'collections_ids'): ?>
<?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 elseif ($prop == 'field_type'): ?>
<?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: ?>
<textarea name="tnc_prop_<?php echo $prop; ?>"><?php echo htmlspecialchars($value); ?></textarea>
<?php endif; ?>
@ -378,6 +380,33 @@ class DevInterface {
?>
<?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) {
global $Tainacan_Collections;
@ -432,10 +461,14 @@ class DevInterface {
$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', wp_slash( get_class( new $class() ) ) );
} elseif($prop == 'field_type_options') {
} elseif($prop == 'field_type_options' || $prop == 'filter_type_options') {
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') {
$repo->insert_metadata($entity, $prop);
}

View File

@ -3,13 +3,34 @@
namespace Tainacan\Tests;
/**
* @group api
* @group queries
* **/
class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
public function test_queries(){
// 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(
'collection',
[
@ -20,6 +41,35 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
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
$itemA1 = $this->tainacan_entity_factory->create_entity(
'item',
@ -27,7 +77,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
'title' => 'Item A-1',
'description' => 'Item in collection A',
'status' => 'publish',
'collection' => $collectionA
'collection' => $collectionA,
'terms' => [$termA]
],
true
);
@ -38,7 +89,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
'title' => 'Item A-2',
'description' => 'Item in collection A',
'status' => 'private',
'collection' => $collectionA
'collection' => $collectionA,
'terms' => [$termA]
],
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
$name_query = ['name' => 'B'];
@ -237,6 +268,36 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
$data5 = $date_query_response_collections->get_data();
$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);
}
}