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

This commit is contained in:
mateuswetah 2018-03-21 08:10:28 -03:00
commit 73319097f6
13 changed files with 1046 additions and 748 deletions

View File

@ -4,8 +4,16 @@
echo 'Watching changes on src/'
while inotifywait -qqr src -e create,move,modify,delete; do
current_OS=`uname`
# For macOS (Darwin) is needed fsevents-tools installed (you can use homebrew install fsevents-tools)
if [ $current_OS == "Darwin" ]; then
echo
notifyloop src ./build.sh
else
while inotifywait -qqr src -e create,move,modify,delete; do
echo
echo 'Change detected, running build'
./build.sh
done
done
fi

View File

@ -113,6 +113,8 @@ return [
'label_collection_items' => __('Collection Items', 'tainacan'),
'label_collection_fields' => __('Collection Fields', 'tainacan'),
'label_collection_filters' => __('Collection Filters', 'tainacan'),
'label_parent_term' => __('Parent Term', 'tainacan'),
'label_add_new_term' => __('Add New Term', 'tainacan'),
// Instructions. More complex sentences to guide user and placeholders
'instruction_dragndrop_fields_collection' => __('Drag and drop Fields here to Collection.', 'tainacan'),
@ -126,6 +128,7 @@ return [
'instruction_select_a_filter_type' => __('Select a filter type:', 'tainacan'),
// Info. Other feedback to user.
'info_name_is_required' => __('Name is required.', 'tainacan'),
'info_no_collection_created' => __('No collection was created in this repository.', 'tainacan'),
'info_no_item_created' => __('No item was created in this collection.', 'tainacan'),
'info_error_deleting_collection' => __('Error on deleting collection.', 'tainacan'),

View File

@ -0,0 +1,126 @@
<template>
<div>
<span>
<a
class="button"
@click="showForm = !showForm"><b-icon size="is-small" icon="plus"></b-icon>&nbsp;{{ $i18n.get('label_add_new_term') }}</a>
</span>
<div class="columns">
<transition name="fade">
<section
v-if="showForm"
class="column is-one-third"
style="padding-left: 0px;">
<b-field :label="$i18n.get('label_name')">
<b-input v-model="name"></b-input>
</b-field>
<b-field :label="$i18n.get('label_parent_term')">
<b-select
v-model="parent">
<option :value="0" selected> ---{{ $i18n.get('label_parent_term') }}--- </option>
<option
v-for="option,index in options"
:key="index"
:value="option.term_id"
v-html="setSpaces( option.level ) + option.name"></option>
</b-select>
</b-field>
<a
class="button is-primary"
@click="save">{{ $i18n.get('save') }}</a>
</section>
</transition>
</div>
</div>
</template>
<script>
import { tainacan as axios } from '../../../js/axios/axios'
export default {
data(){
return {
name: '',
parent: 0,
showForm: false,
field_id: this.field.field.id
}
},
props: {
id: String,
item_id: [Number,String],
field: [Number,String],
taxonomy_id: [Number,String],
value:[ Array, Boolean, Number ],
options: {
type: Array
}
},
methods: {
setSpaces( level ){
let result = '';
let space = '&nbsp;&nbsp;'
for(let i = 0;i < level; i++)
result += space;
return result;
},
save(){
if( this.name.trim() === ''){
this.$toast.open({
duration: 2000,
message: this.$i18n.get('info_name_is_required'),
position: 'is-bottom',
type: 'is-danger'
})
} else {
const instance = this;
axios.post(`/taxonomy/${this.taxonomy_id}/terms`, {
name: this.name,
parent: this.parent
})
.then( res => {
instance.name = '';
instance.parent = 0;
if( res.data && res.data.term_id || res.term_id ){
let term_id = ( res.term_id ) ? res.term_id : res.data.term_id;
let val = this.value;
if( !Array.isArray( val ) && this.field.field.multiple === 'no' ){
axios.patch(`/item/${this.item_id}/metadata/${this.field_id}`, {
values: term_id,
}).then( res => {
instance.$emit('newTerm', term_id);
})
} else {
val = ( val ) ? val : [];
val.push( term_id );
axios.patch(`/item/${this.item_id}/metadata/${this.field_id}`, {
values: val,
}).then( res => {
instance.$emit('newTerm', val);
})
}
}
});
}
}
}
}
</script>
<style scoped>
button{
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
}
</style>

View File

@ -1,16 +1,29 @@
<template>
<div>
<component
:is="getComponent()"
v-model="valueComponent"
:allowNew="allowNew"
:terms="terms"
:options="getOptions(0)"></component>
<add-new-term
class="add-new-term"
v-if="getComponent() !== 'tainacan-category-tag-input' && allowNew"
:taxonomy_id="taxonomy"
:field="field"
:item_id="field.item.id"
:value="valueComponent"
:options="getOptions(0)"
@newTerm="reload"></add-new-term>
</div>
</template>
<script>
import { tainacan as axios } from '../../../js/axios/axios'
import TainacanCategoryRadio from './CategoryRadio.vue'
import TainacanCategoryCheckbox from './CategoryCheckbox.vue'
import TainacanCategoryTagInput from './CategoryTaginput.vue'
import TainacanCategorySelectbox from './CategorySelectbox.vue'
import AddNewTerm from './AddNewTerm.vue'
export default {
created(){
@ -30,7 +43,9 @@
components: {
TainacanCategoryRadio,
TainacanCategoryCheckbox,
TainacanCategoryTagInput
TainacanCategoryTagInput,
TainacanCategorySelectbox,
AddNewTerm
},
data(){
return {
@ -109,7 +124,21 @@
this.inputValue = $event;
this.$emit('input', this.inputValue);
this.$emit('blur');
},
reload( val ){
this.valueComponent = val;
this.terms = [];
this.getTermsFromTaxonomy();
this.getTermsId();
}
}
}
</script>
<style scoped>
.add-new-term{
margin-top: 15px;
margin-bottom: 30px;
}
</style>

View File

@ -1,6 +1,6 @@
<template>
<div>
<div class="block" v-for="option,index in options">
<div v-for="option,index in options">
<b-checkbox
:id="id"
:style="{ paddingLeft: (option.level * 30) + 'px' }"
@ -11,6 +11,7 @@
border>
{{ option.name }}
</b-checkbox>
<br>
</div>
</div>
</template>
@ -27,6 +28,11 @@
checked: []
}
},
watch: {
value( val ){
this.checked = val;
}
},
props: {
options: {
type: Array

View File

@ -1,6 +1,6 @@
<template>
<div>
<div class="block" v-for="option,index in options">
<div v-for="option,index in options">
<b-radio
:id="id"
:style="{ paddingLeft: (option.level * 30) + 'px' }"
@ -11,6 +11,7 @@
border>
{{ option.name }}
</b-radio>
<br>
</div>
</div>
</template>
@ -18,13 +19,14 @@
<script>
export default {
created(){
if( this.value )
this.checked = this.value;
},
data(){
return {
checked:''
checked: ( this.value ) ? this.value : ''
}
},
watch: {
value( val ){
this.checked = val;
}
},
props: {

View File

@ -0,0 +1,59 @@
<template>
<div>
<div class="block">
<b-select
:id="id"
v-model="selected"
@input="emitChange()"
:placeholder="$i18n.get('label_select_category')" expanded>
<option
v-for="option,index in options"
:key="index"
:value="option.term_id"
v-html="setSpaces( option.level ) + option.name"></option>
</b-select>
</div>
</div>
</template>
<script>
export default {
created(){
if( this.value )
this.selected = this.value;
},
data(){
return {
selected: ''
}
},
watch: {
value( val ){
this.selected = val;
}
},
props: {
id: String,
options: {
type: Array
},
value: [ Number, String, Array ]
},
methods: {
emitChange() {
this.$emit('input', this.selected);
this.$emit('blur');
},
setSpaces( level ){
let result = '';
let space = '&nbsp;&nbsp;'
for(let i = 0;i < level; i++)
result += space;
return result;
}
}
}
</script>

View File

@ -5,6 +5,7 @@
rounded
icon="magnify"
:allowNew="allowNew"
@input="emitChange"
v-model="selected"
:data="labels"
field="label"
@ -22,19 +23,6 @@
}
},
watch: {
selected( val ){
this.selected = val;
let results = [];
for( let term of val ){
if( term.value ){
results.push( term.value );
} else {
results.push( term );
}
}
this.$emit('input', results);
this.$emit('blur');
},
terms( val ){
this.selectedValues();
}
@ -74,6 +62,19 @@
}
this.selected = selected;
}
},
emitChange(){
let val = this.selected;
let results = [];
for( let term of val ){
if( term.value ){
results.push( term.value );
} else {
results.push( term );
}
}
this.$emit('input', results);
this.$emit('blur');
}
}
}

View File

@ -1,12 +1,14 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
use Tainacan\Field_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use \Respect\Validation\Validator as v;
/**
* Class Fields
*/
@ -20,178 +22,183 @@ class Fields extends Repository {
'Tainacan\Field_Types\Core_Title',
'Tainacan\Field_Types\Core_Description'
];
/**
* Register specific hooks for field repository
*/
function __construct() {
parent::__construct();
add_filter('pre_trash_post', array( &$this, 'disable_delete_core_fields' ), 10, 2 );
add_filter('pre_delete_post', array( &$this, 'force_delete_core_fields' ), 10, 3 );
add_filter( 'pre_trash_post', array( &$this, 'disable_delete_core_fields' ), 10, 2 );
add_filter( 'pre_delete_post', array( &$this, 'force_delete_core_fields' ), 10, 3 );
}
public function get_map() {
return apply_filters('tainacan-get-map-'.$this->get_name(), [
return apply_filters( 'tainacan-get-map-' . $this->get_name(), [
'name' => [
'map' => 'post_title',
'title' => __('Name', 'tainacan'),
'title' => __( 'Name', 'tainacan' ),
'type' => 'string',
'description'=> __('Name of the field', 'tainacan'),
'on_error' => __('The name should be a text value and not empty', 'tainacan'),
'description' => __( 'Name of the field', 'tainacan' ),
'on_error' => __( 'The name should be a text value and not empty', 'tainacan' ),
'validation' => v::stringType()->notEmpty(),
],
'slug' => [
'map' => 'post_name',
'title' => __('Slug', 'tainacan'),
'title' => __( 'Slug', 'tainacan' ),
'type' => 'string',
'description'=> __('A unique and santized string representation of the field', 'tainacan'),
'description' => __( 'A unique and santized string representation of the field', 'tainacan' ),
//'validation' => v::stringType(),
],
'order' => [
'map' => 'menu_order',
'title' => __('Order', 'tainacan'),
'title' => __( 'Order', 'tainacan' ),
'type' => 'string/integer',
'description'=> __('Field order. Field used if collections are manually ordered', 'tainacan'),
'on_error' => __('The menu order should be a numeric value', 'tainacan'),
'description' => __( 'Field order. Field used if collections are manually ordered', 'tainacan' ),
'on_error' => __( 'The menu order should be a numeric value', 'tainacan' ),
//'validation' => v::numeric(),
],
'parent' => [
'map' => 'parent',
'title' => __('Parent', 'tainacan'),
'title' => __( 'Parent', 'tainacan' ),
'type' => 'integer',
'description'=> __('Parent field', 'tainacan'),
'description' => __( 'Parent field', 'tainacan' ),
'default' => 0
//'on_error' => __('The Parent should be numeric value', 'tainacan'),
//'validation' => v::numeric(),
],
'description' => [
'map' => 'post_content',
'title' => __('Description', 'tainacan'),
'title' => __( 'Description', 'tainacan' ),
'type' => 'string',
'description'=> __('The field description', 'tainacan'),
'description' => __( 'The field description', 'tainacan' ),
'default' => '',
//'on_error' => __('The description should be a text value', 'tainacan'),
//'validation' => v::stringType()->notEmpty(),
],
'field_type' => [
'map' => 'meta',
'title' => __('Type', 'tainacan'),
'title' => __( 'Type', 'tainacan' ),
'type' => 'string',
'description'=> __('The field type', 'tainacan'),
'on_error' => __('Field type is empty', 'tainacan'),
'description' => __( 'The field type', 'tainacan' ),
'on_error' => __( 'Field type is empty', 'tainacan' ),
'validation' => v::stringType()->notEmpty(),
],
'required' => [
'map' => 'meta',
'title' => __('Required', 'tainacan'),
'title' => __( 'Required', 'tainacan' ),
'type' => 'string',
'description'=> __('The field is required', 'tainacan'),
'on_error' => __('Field required field is invalid', 'tainacan'),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no
'description' => __( 'The field is required', 'tainacan' ),
'on_error' => __( 'Field required field is invalid', 'tainacan' ),
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
'default' => 'no'
],
'collection_key' => [
'map' => 'meta',
'title' => __('Collection key', 'tainacan'),
'title' => __( 'Collection key', 'tainacan' ),
'type' => 'string',
'description'=> __('Field value should not be repeated', 'tainacan'),
'on_error' => __('Collection key is invalid', 'tainacan'),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no
'description' => __( 'Field value should not be repeated', 'tainacan' ),
'on_error' => __( 'Collection key is invalid', 'tainacan' ),
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
'default' => 'no'
],
'multiple' => [
'map' => 'meta',
'title' => __('Multiple', 'tainacan'),
'title' => __( 'Multiple', 'tainacan' ),
'type' => 'string',
'description'=> __('Allow multiple fields for the field', 'tainacan'),
'on_error' => __('Multiple fields is invalid', 'tainacan'),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key
'description' => __( 'Allow multiple fields for the field', 'tainacan' ),
'on_error' => __( 'Multiple fields is invalid', 'tainacan' ),
'validation' => v::stringType()->in( [ 'yes', 'no' ] ),
// yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'cardinality' => [
'map' => 'meta',
'title' => __('Cardinality', 'tainacan'),
'title' => __( 'Cardinality', 'tainacan' ),
'type' => 'string/number',
'description'=> __('Number of multiples possible fields', 'tainacan'),
'on_error' => __('The number of fields not allowed', 'tainacan'),
'description' => __( 'Number of multiples possible fields', 'tainacan' ),
'on_error' => __( 'The number of fields not allowed', 'tainacan' ),
'validation' => v::numeric()->positive(),
'default' => 1
],
'privacy' => [
'map' => 'meta',
'title' => __('Privacy', 'tainacan'),
'title' => __( 'Privacy', 'tainacan' ),
'type' => 'string',
'description'=> __('The field should be omitted in item view', 'tainacan'),
'on_error' => __('Privacy is invalid', 'tainacan'),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key
'description' => __( 'The field should be omitted in item view', 'tainacan' ),
'on_error' => __( 'Privacy is invalid', 'tainacan' ),
'validation' => v::stringType()->in( [ 'yes', 'no' ] ),
// yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'mask' => [
'map' => 'meta',
'title' => __('Mask', 'tainacan'),
'title' => __( 'Mask', 'tainacan' ),
'type' => 'string',
'description'=> __('The mask to be used in the field', 'tainacan'),
'description' => __( 'The mask to be used in the field', 'tainacan' ),
//'on_error' => __('Mask is invalid', 'tainacan'),
//'validation' => ''
],
'default_value' => [
'map' => 'meta',
'title' => __('Default value', 'tainacan'),
'title' => __( 'Default value', 'tainacan' ),
'type' => 'string',
'description'=> __('The value default fot the field', 'tainacan'),
'description' => __( 'The value default fot the field', 'tainacan' ),
],
'field_type_options' => [ // not showed in form
'map' => 'meta',
'title' => __('Field Type options', 'tainacan'),
'title' => __( 'Field Type options', 'tainacan' ),
'type' => 'array/object/string',
'items' => ['type' => 'array/string/integer/object'],
'description'=> __('Options specific for field type', 'tainacan'),
'items' => [ 'type' => 'array/string/integer/object' ],
'description' => __( 'Options specific for field type', 'tainacan' ),
// 'validation' => ''
],
'collection_id' => [ // not showed in form
'map' => 'meta',
'title' => __('Collection', 'tainacan'),
'title' => __( 'Collection', 'tainacan' ),
'type' => 'integer/string',
'description'=> __('The collection ID', 'tainacan'),
'description' => __( 'The collection ID', 'tainacan' ),
//'validation' => ''
],
'accept_suggestion' => [
'map' => 'meta',
'title' => __('Field Value Accepts Suggestions', 'tainacan'),
'title' => __( 'Field Value Accepts Suggestions', 'tainacan' ),
'type' => 'bool',
'description'=> __('Allow the community suggest a different values for that field', 'tainacan'),
'description' => __( 'Allow the community suggest a different values for that field', 'tainacan' ),
'default' => false,
'validation' => v::boolType()
],
'can_delete' => [
'map' => 'meta',
'title' => __('Can delete', 'tainacan'),
'title' => __( 'Can delete', 'tainacan' ),
'type' => 'string',
'description'=> __('The field can be deleted', 'tainacan'),
'on_error' => __('Can delete is invalid', 'tainacan'),
'validation' => v::stringType()->in(['yes', 'no']), // yes or no. It cant be multiple if its collection_key
'description' => __( 'The field can be deleted', 'tainacan' ),
'on_error' => __( 'Can delete is invalid', 'tainacan' ),
'validation' => v::stringType()->in( [ 'yes', 'no' ] ),
// yes or no. It cant be multiple if its collection_key
'default' => 'yes'
],
]);
] );
}
/**
* Get the labels for the custom post type of this repository
*
* @return array Labels in the format expected by register_post_type()
*/
public function get_cpt_labels() {
return array(
'name' => __('Fields', 'tainacan'),
'singular_name' => __('Field', 'tainacan'),
'add_new' => __('Add new', 'tainacan'),
'add_new_item' => __('Add new Field', 'tainacan'),
'edit_item' => __('Edit Field', 'tainacan'),
'new_item' => __('New Field', 'tainacan'),
'view_item' => __('View Field', 'tainacan'),
'search_items' => __('Search Field', 'tainacan'),
'not_found' => __('No Field found ', 'tainacan'),
'not_found_in_trash' => __('No Field found in trash', 'tainacan'),
'parent_item_colon' => __('Parent Field:', 'tainacan'),
'menu_name' => __('Fields', 'tainacan')
'name' => __( 'Fields', 'tainacan' ),
'singular_name' => __( 'Field', 'tainacan' ),
'add_new' => __( 'Add new', 'tainacan' ),
'add_new_item' => __( 'Add new Field', 'tainacan' ),
'edit_item' => __( 'Edit Field', 'tainacan' ),
'new_item' => __( 'New Field', 'tainacan' ),
'view_item' => __( 'View Field', 'tainacan' ),
'search_items' => __( 'Search Field', 'tainacan' ),
'not_found' => __( 'No Field found ', 'tainacan' ),
'not_found_in_trash' => __( 'No Field found in trash', 'tainacan' ),
'parent_item_colon' => __( 'Parent Field:', 'tainacan' ),
'menu_name' => __( 'Fields', 'tainacan' )
);
}
@ -221,7 +228,7 @@ class Fields extends Repository {
'page-attributes'
]
);
register_post_type(Entities\Field::get_post_type(), $args);
register_post_type( Entities\Field::get_post_type(), $args );
}
/**
@ -229,7 +236,7 @@ class Fields extends Repository {
*
* @return string the value of constant
*/
public function get_default_metadata_attribute(){
public function get_default_metadata_attribute() {
return $this->default_metadata;
}
@ -238,12 +245,12 @@ class Fields extends Repository {
*
* @param $class_name string | object The class name or the instance
*/
public function register_field_type( $class_name ){
if( is_object( $class_name ) ){
public function register_field_type( $class_name ) {
if ( is_object( $class_name ) ) {
$class_name = get_class( $class_name );
}
if(!in_array( $class_name, $this->field_types)){
if ( ! in_array( $class_name, $this->field_types ) ) {
$this->field_types[] = $class_name;
}
}
@ -253,14 +260,14 @@ class Fields extends Repository {
*
* @param $class_name string | object The class name or the instance
*/
public function unregister_field_type( $class_name ){
if( is_object( $class_name ) ){
public function unregister_field_type( $class_name ) {
if ( is_object( $class_name ) ) {
$class_name = get_class( $class_name );
}
$key = array_search( $class_name, $this->field_types );
if($key !== false){
unset( $this->field_types[$key] );
if ( $key !== false ) {
unset( $this->field_types[ $key ] );
}
}
@ -281,25 +288,26 @@ class Fields extends Repository {
*/
public function fetch( $args, $output = null ) {
if( is_numeric($args) ){
$existing_post = get_post($args);
if ($existing_post instanceof \WP_Post) {
return new Entities\Field($existing_post);
if ( is_numeric( $args ) ) {
$existing_post = get_post( $args );
if ( $existing_post instanceof \WP_Post ) {
return new Entities\Field( $existing_post );
} else {
return [];
}
} elseif (is_array($args)) {
} elseif ( is_array( $args ) ) {
$args = array_merge([
'posts_per_page' => -1,
], $args);
$args = array_merge( [
'posts_per_page' => - 1,
], $args );
$args = $this->parse_fetch_args($args);
$args = $this->parse_fetch_args( $args );
$args['post_type'] = Entities\Field::get_post_type();
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
$wp_query = new \WP_Query( $args );
return $this->fetch_output( $wp_query, $output );
}
}
@ -313,7 +321,7 @@ class Fields extends Repository {
* @return array Entities\Field
* @throws \Exception
*/
public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){
public function fetch_by_collection( Entities\Collection $collection, $args = [], $output = null ) {
$collection_id = $collection->get_id();
//get parent collections
@ -331,9 +339,9 @@ class Fields extends Repository {
'compare' => 'IN',
);
if( isset( $args['meta_query'] ) ){
if ( isset( $args['meta_query'] ) ) {
$args['meta_query'][] = $meta_query;
} elseif(is_array($args)){
} elseif ( is_array( $args ) ) {
$args['meta_query'] = array( $meta_query );
}
@ -352,43 +360,43 @@ class Fields extends Repository {
* @param $result Response from method fetch
* @param Entities\Collection $collection
* @param bool $include_disabled Wether to include disabled fields in the results or not
*
* @return array or WP_Query ordinate
*/
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ){
public function order_result( $result, Entities\Collection $collection, $include_disabled = false ) {
$order = $collection->get_fields_order();
if($order) {
$order = ( is_array($order) ) ? $order : unserialize($order);
if ( $order ) {
$order = ( is_array( $order ) ) ? $order : unserialize( $order );
if ( is_array($result) ){
if ( is_array( $result ) ) {
$result_ordinate = [];
$not_ordinate = [];
foreach ( $result as $item ) {
$id = $item->WP_Post->ID;
$index = array_search ( $id , array_column( $order , 'id') );
$index = array_search( $id, array_column( $order, 'id' ) );
if( $index !== false ) {
if ( $index !== false ) {
// skipping fields disabled if the arg is set
if( !$include_disabled && isset( $order[$index]['enable'] ) && !$order[$index]['enable'] ) {
if ( ! $include_disabled && isset( $order[ $index ]['enable'] ) && ! $order[ $index ]['enable'] ) {
continue;
}
$enable = ( isset( $order[$index]['enable'] )) ? $order[$index]['enable'] : true;
$item->set_enabled_for_collection($enable);
$enable = ( isset( $order[ $index ]['enable'] ) ) ? $order[ $index ]['enable'] : true;
$item->set_enabled_for_collection( $enable );
$result_ordinate[$index] = $item;
$result_ordinate[ $index ] = $item;
} else {
$not_ordinate[] = $item;
}
}
ksort ( $result_ordinate );
ksort( $result_ordinate );
$result_ordinate = array_merge( $result_ordinate, $not_ordinate );
return $result_ordinate;
}
// if the result is a wp query object
} // if the result is a wp query object
else {
$posts = $result->posts;
$result_ordinate = [];
@ -396,38 +404,41 @@ class Fields extends Repository {
foreach ( $posts as $item ) {
$id = $item->ID;
$index = array_search ( $id , array_column( $order , 'id') );
$index = array_search( $id, array_column( $order, 'id' ) );
if( $index !== false ){
$result_ordinate[$index] = $item;
if ( $index !== false ) {
$result_ordinate[ $index ] = $item;
} else {
$not_ordinate[] = $item;
}
}
ksort ( $result_ordinate );
ksort( $result_ordinate );
$result->posts = $result_ordinate;
$result->posts = array_merge( $result->posts, $not_ordinate );
return $result;
}
}
return $result;
}
/**
* @param \Tainacan\Entities\Field $field
*
* @return \Tainacan\Entities\Field
* {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert()
*/
public function insert($field){
public function insert( $field ) {
global $Tainacan_Fields;
$this->pre_update_category_field($field);
$new_field = parent::insert($field);
$this->pre_update_category_field( $field );
$new_field = parent::insert( $field );
$this->update_category_field( $new_field );
$this->update_category_field($new_field);
return $new_field;
}
@ -438,12 +449,13 @@ class Fields extends Repository {
* @return mixed|string|Entities\Entity
* @throws \Exception
*/
public function update($object, $new_values = null){
return $this->insert($object);
public function update( $object, $new_values = null ) {
return $this->insert( $object );
}
public function delete($field_id){
$this->delete_category_field($field_id);
public function delete( $field_id ) {
$this->delete_category_field( $field_id );
return new Entities\Field( wp_trash_post( $field_id ) );
}
@ -455,16 +467,17 @@ class Fields extends Repository {
* NAME - return an Array of the names of field types registered
*
* @param $output string CLASS | NAME
*
* @return array of Entities\Field_Types\Field_Type classes path name
*/
public function fetch_field_types( $output = 'CLASS'){
public function fetch_field_types( $output = 'CLASS' ) {
$return = [];
do_action('register_field_types');
do_action( 'register_field_types' );
if( $output === 'NAME' ){
foreach ($this->field_types as $field_type) {
$return[] = str_replace('Tainacan\Field_Types\\','', $field_type);
if ( $output === 'NAME' ) {
foreach ( $this->field_types as $field_type ) {
$return[] = str_replace( 'Tainacan\Field_Types\\', '', $field_type );
}
return $return;
@ -480,7 +493,7 @@ class Fields extends Repository {
* @throws \ErrorException
* @throws \Exception
*/
public function register_core_fields( Entities\Collection $collection ){
public function register_core_fields( Entities\Collection $collection ) {
$fields = $this->get_core_fields( $collection );
@ -502,22 +515,22 @@ class Fields extends Repository {
]
];
if( $collection->get_parent() !== 0 ){
if ( $collection->get_parent() !== 0 ) {
return false;
}
foreach ( $data_core_fields as $index => $data_core_field ) {
if( empty( $fields ) ){
if ( empty( $fields ) ) {
$this->insert_array_field( $data_core_field );
} else {
$exists = false;
foreach ( $fields as $field ){
foreach ( $fields as $field ) {
if ( $field->get_field_type() === $data_core_field['field_type'] ) {
$exists = true;
}
}
if( !$exists ){
if ( ! $exists ) {
$this->insert_array_field( $data_core_field );
}
}
@ -533,10 +546,10 @@ class Fields extends Repository {
* @return null/bool
* @throws \Exception
*/
public function disable_delete_core_fields( $before, $post ){
public function disable_delete_core_fields( $before, $post ) {
$field = $this->fetch( $post->ID );
if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric($field->get_collection_id()) ) {
if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric( $field->get_collection_id() ) ) {
return false;
}
}
@ -552,10 +565,10 @@ class Fields extends Repository {
* @throws \Exception
* @internal param The $post_id post ID which is deleting
*/
public function force_delete_core_fields( $before, $post, $force_delete ){
public function force_delete_core_fields( $before, $post, $force_delete ) {
$field = $this->fetch( $post->ID );
if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric($field->get_collection_id()) ) {
if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric( $field->get_collection_id() ) ) {
return false;
}
}
@ -568,14 +581,14 @@ class Fields extends Repository {
* @return Array|\WP_Query
* @throws \Exception
*/
public function get_core_fields( Entities\Collection $collection ){
public function get_core_fields( Entities\Collection $collection ) {
$args = [];
$meta_query = array(
array(
'key' => 'collection_id',
'value' => $collection->get_id(),
'compare' => 'IN',
'compare' => '=',
),
array(
'key' => 'field_type',
@ -598,18 +611,19 @@ class Fields extends Repository {
* @throws \ErrorException
* @throws \Exception
*/
public function insert_array_field( $data ){
public function insert_array_field( $data ) {
$field = new Entities\Field();
foreach ( $data as $attribute => $value ) {
$set_ = 'set_' . $attribute;
$field->$set_( $value );
}
if ( $field->validate( )) {
if ( $field->validate() ) {
$field = $this->insert( $field );
return $field->get_id();
} else {
throw new \ErrorException('The entity wasn\'t validated.' . print_r( $field->get_errors(), true));
throw new \ErrorException( 'The entity wasn\'t validated.' . print_r( $field->get_errors(), true ) );
}
}
@ -621,7 +635,7 @@ class Fields extends Repository {
*
* @return array|null|object
*/
public function fetch_all_field_values($collection_id, $field_id){
public function fetch_all_field_values( $collection_id, $field_id ) {
global $wpdb;
// Clear the result cache
@ -629,13 +643,13 @@ class Fields extends Repository {
$item_post_type = "%%{$collection_id}_item";
$collection = new Entities\Collection($collection_id);
$collection = new Entities\Collection( $collection_id );
$capabilities = $collection->get_capabilities();
$results = [];
// If no has logged user or actual user can not read private posts
if(get_current_user_id() === 0 || !current_user_can( $capabilities->read_private_posts)) {
if ( get_current_user_id() === 0 || ! current_user_can( $capabilities->read_private_posts ) ) {
$args = [
'exclude_from_search' => false,
'public' => true,
@ -645,7 +659,7 @@ class Fields extends Repository {
$post_statuses = get_post_stati( $args, 'names', 'and' );
foreach ($post_statuses as $post_status) {
foreach ( $post_statuses as $post_status ) {
$sql_string = $wpdb->prepare(
"SELECT item_id, field_id, mvalue
FROM (
@ -662,18 +676,18 @@ class Fields extends Repository {
);
$pre_result = $wpdb->get_results( $sql_string, ARRAY_A );
if (!empty($pre_result)) {
if ( ! empty( $pre_result ) ) {
$results[] = $pre_result;
}
}
} elseif ( current_user_can( $capabilities->read_private_posts) ) {
} elseif ( current_user_can( $capabilities->read_private_posts ) ) {
$args = [
'exclude_from_search' => false,
];
$post_statuses = get_post_stati( $args, 'names', 'and' );
foreach ($post_statuses as $post_status) {
foreach ( $post_statuses as $post_status ) {
$sql_string = $wpdb->prepare(
"SELECT item_id, field_id, mvalue
FROM (
@ -691,7 +705,7 @@ class Fields extends Repository {
$pre_result = $wpdb->get_results( $sql_string, ARRAY_A );
if (!empty($pre_result)) {
if ( ! empty( $pre_result ) ) {
$results[] = $pre_result;
}
}
@ -704,14 +718,14 @@ class Fields extends Repository {
* Stores the value of the taxonomy_id option to use on update_category_field method.
*
*/
private function pre_update_category_field($field) {
private function pre_update_category_field( $field ) {
$field_type = $field->get_field_type_object();
$current_tax = '';
if ($field_type->get_primitive_type() == 'term') {
if ( $field_type->get_primitive_type() == 'term' ) {
$options = $this->get_mapped_property($field, 'field_type_options');
$field_type->set_options($options);
$current_tax = $field_type->get_option('taxonomy_id');
$options = $this->get_mapped_property( $field, 'field_type_options' );
$field_type->set_options( $options );
$current_tax = $field_type->get_option( 'taxonomy_id' );
}
$this->current_taxonomy = $current_tax;
}
@ -723,34 +737,36 @@ class Fields extends Repository {
* a field type category is inserted or removed
*
* @param [type] $field [description]
*
* @return [type] [description]
*/
private function update_category_field($field) {
private function update_category_field( $field ) {
$field_type = $field->get_field_type_object();
$new_tax = '';
if ($field_type->get_primitive_type() == 'term') {
$new_tax = $field_type->get_option('taxonomy_id');
if ( $field_type->get_primitive_type() == 'term' ) {
$new_tax = $field_type->get_option( 'taxonomy_id' );
}
if ($new_tax != $this->current_taxonomy) {
if (!empty($this->current_taxonomy)) {
do_action('tainacan-taxonomy-removed-from-collection', $this->current_taxonomy, $field->get_collection());
if ( $new_tax != $this->current_taxonomy ) {
if ( ! empty( $this->current_taxonomy ) ) {
do_action( 'tainacan-taxonomy-removed-from-collection', $this->current_taxonomy, $field->get_collection() );
}
if (!empty($new_tax)) {
do_action('tainacan-taxonomy-added-to-collection', $new_tax, $field->get_collection());
if ( ! empty( $new_tax ) ) {
do_action( 'tainacan-taxonomy-added-to-collection', $new_tax, $field->get_collection() );
}
}
}
private function delete_category_field($field_id) {
$field = $this->fetch($field_id);
private function delete_category_field( $field_id ) {
$field = $this->fetch( $field_id );
$field_type = $field->get_field_type_object();
if ($field_type->get_primitive_type() == 'term') {
$removed_tax = $field_type->get_option('taxonomy_id');
if (!empty($removed_tax))
do_action('tainacan-taxonomy-removed-from-collection', $removed_tax, $field->get_collection());
if ( $field_type->get_primitive_type() == 'term' ) {
$removed_tax = $field_type->get_option( 'taxonomy_id' );
if ( ! empty( $removed_tax ) ) {
do_action( 'tainacan-taxonomy-removed-from-collection', $removed_tax, $field->get_collection() );
}
}
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Tainacan\Repositories;
use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
@ -8,37 +9,36 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Item_Metadata extends Repository {
public $entities_type = '\Tainacan\Entities\Item_Metadata_Entity';
public function insert($item_metadata) {
public function insert( $item_metadata ) {
$unique = !$item_metadata->is_multiple();
$unique = ! $item_metadata->is_multiple();
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_core()) {
$this->save_core_field_value($item_metadata);
} elseif ($field_type->get_primitive_type() == 'term') {
$this->save_terms_field_value($item_metadata);
if ( $field_type->get_core() ) {
$this->save_core_field_value( $item_metadata );
} elseif ( $field_type->get_primitive_type() == 'term' ) {
$this->save_terms_field_value( $item_metadata );
} else {
if ($unique) {
update_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
if ( $unique ) {
update_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $item_metadata->get_value() ) );
} else {
delete_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id());
delete_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id() );
if (is_array($item_metadata->get_value())){
if ( is_array( $item_metadata->get_value() ) ) {
$values = $item_metadata->get_value();
foreach ($values as $value){
add_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value ));
foreach ( $values as $value ) {
add_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), wp_slash( $value ) );
}
}
}
}
do_action( 'tainacan-insert', $item_metadata );
do_action( 'tainacan-insert-Item_Metadata_Entity', $item_metadata );
do_action('tainacan-insert', $item_metadata);
do_action('tainacan-insert-Item_Metadata_Entity', $item_metadata);
return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_field());
return new Entities\Item_Metadata_Entity( $item_metadata->get_item(), $item_metadata->get_field() );
}
/**
@ -46,31 +46,32 @@ class Item_Metadata extends Repository {
*
* @return mixed|void
*/
public function delete($item_metadata){}
public function delete( $item_metadata ) {
}
public function save_core_field_value(\Tainacan\Entities\Item_Metadata_Entity $item_metadata) {
public function save_core_field_value( \Tainacan\Entities\Item_Metadata_Entity $item_metadata ) {
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_core()) {
if ( $field_type->get_core() ) {
$item = $item_metadata->get_item();
$set_method = 'set_' . $field_type->get_related_mapped_prop();
$value = $item_metadata->get_value();
$item->$set_method( is_array( $value ) ? $value[0] : $value );
if ($item->validate_core_fields()) {
if ( $item->validate_core_fields() ) {
global $Tainacan_Items;
$Tainacan_Items->insert($item);
$Tainacan_Items->update( $item );
} else {
throw new \Exception('Item metadata should be validated beforehand');
throw new \Exception( 'Item metadata should be validated beforehand' );
}
}
}
public function save_terms_field_value($item_metadata) {
public function save_terms_field_value( $item_metadata ) {
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_primitive_type() == 'term') {
if ( $field_type->get_primitive_type() == 'term' ) {
$new_terms = $item_metadata->get_value();
$taxonomy = new Entities\Taxonomy( $field_type->get_option('taxonomy_id') );
if( $taxonomy ){
wp_set_object_terms($item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() );
$taxonomy = new Entities\Taxonomy( $field_type->get_option( 'taxonomy_id' ) );
if ( $taxonomy ) {
wp_set_object_terms( $item_metadata->get_item()->get_id(), $new_terms, $taxonomy->get_db_identifier() );
}
@ -85,28 +86,28 @@ class Item_Metadata extends Repository {
* @return array
* @throws \Exception
*/
public function fetch($object, $output = null ){
if($object instanceof Entities\Item){
public function fetch( $object, $output = null ) {
if ( $object instanceof Entities\Item ) {
global $Tainacan_Items, $Tainacan_Fields;
$collection = $object->get_collection();
if (!$collection instanceof Entities\Collection){
if ( ! $collection instanceof Entities\Collection ) {
return [];
}
$meta_list = $Tainacan_Fields->fetch_by_collection($collection, [], 'OBJECT' );
$meta_list = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' );
$return = [];
if (is_array($meta_list)) {
foreach ($meta_list as $meta) {
$return[] = new Entities\Item_Metadata_Entity($object, $meta);
if ( is_array( $meta_list ) ) {
foreach ( $meta_list as $meta ) {
$return[] = new Entities\Item_Metadata_Entity( $object, $meta );
}
}
return $return;
}else{
} else {
return [];
}
}
@ -115,23 +116,25 @@ class Item_Metadata extends Repository {
* Get the value for a Item field.
*
* @param Entities\Item_Metadata_Entity $item_metadata
*
* @return mixed
*/
public function get_value(Entities\Item_Metadata_Entity $item_metadata) {
public function get_value( Entities\Item_Metadata_Entity $item_metadata ) {
$unique = ! $item_metadata->is_multiple();
$field_type = $item_metadata->get_field()->get_field_type_object();
if ($field_type->get_core()) {
if ( $field_type->get_core() ) {
$item = $item_metadata->get_item();
$get_method = 'get_' . $field_type->get_related_mapped_prop();
return $item->$get_method();
} elseif ($field_type->get_primitive_type() == 'term') {
} elseif ( $field_type->get_primitive_type() == 'term' ) {
if( is_numeric( $field_type->get_option('taxonomy_id') ) ){
$taxonomy = new Entities\Taxonomy( $field_type->get_option('taxonomy_id') );
if( $taxonomy ){
if ( is_numeric( $field_type->get_option( 'taxonomy_id' ) ) ) {
$taxonomy = new Entities\Taxonomy( $field_type->get_option( 'taxonomy_id' ) );
if ( $taxonomy ) {
$taxonomy_slug = $taxonomy->get_db_identifier();
} else {
return null;
@ -140,23 +143,30 @@ class Item_Metadata extends Repository {
return null;
}
$terms = wp_get_object_terms($item_metadata->get_item()->get_id(), $taxonomy_slug );
$terms = wp_get_object_terms( $item_metadata->get_item()->get_id(), $taxonomy_slug );
if ($unique)
$terms = reset($terms);
if ( $unique ) {
$terms = reset( $terms );
}
return $terms;
} else {
return get_post_meta($item_metadata->item->get_id(), $item_metadata->field->get_id(), $unique);
return get_post_meta( $item_metadata->item->get_id(), $item_metadata->field->get_id(), $unique );
}
}
public function register_post_type() { }
public function register_post_type() {
}
public function get_map() { return []; }
public function get_default_properties($map) { return []; }
public function get_map() {
return [];
}
public function get_default_properties( $map ) {
return [];
}
/**
* @param $object
@ -164,15 +174,17 @@ class Item_Metadata extends Repository {
* @return mixed
*/
public function update( $object, $new_values = null ) {
return $this->insert($object);
return $this->insert( $object );
}
/**
* Suggest a value to be inserted as a item Field value, return a pending log
*
* @param Entities\Item_Metadata_Entity $item_metadata
*
* @return Entities\Log
*/
public function suggest($item_metadata) {
return Entities\Log::create(false, '', $item_metadata, null, 'pending');
public function suggest( $item_metadata ) {
return Entities\Log::create( false, '', $item_metadata, null, 'pending' );
}
}

View File

@ -145,12 +145,12 @@ class Items extends Repository {
// iterate through the native post properties
foreach ( $map as $prop => $mapped ) {
if ( $mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms' && $mapped['map'] != 'thumbnail_id' ) {
if ( $mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms' ) {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property( $prop );
}
}
// save post and geet its ID
// save post and get its ID
$item->WP_Post->post_type = $cpt;
//$item->WP_Post->post_status = 'publish';

View File

@ -107,14 +107,6 @@ class DevInterface {
);
add_meta_box(
$col->get_db_identifier() . '_metadata_js',
__('Field Components', 'tainacan'),
array(&$this, 'metadata_components_metabox'),
$col->get_db_identifier(), //post type
'normal'
);
}

View File

@ -8,12 +8,7 @@ namespace Tainacan\Tests;
* @package Test_Tainacan
*/
use Tainacan\Entities;
/**
* Sample test case.
*/
class CoreFieldTypes extends TAINACAN_UnitTestCase {
class CoreFieldTypes extends TAINACAN_UnitApiTestCase {
function test_core_field_types() {
@ -62,33 +57,82 @@ class CoreFieldTypes extends TAINACAN_UnitTestCase {
);
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $field);
$item_metadata->set_value('changed title');
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $i, $field );
$item_metadata->set_value( 'changed title' );
$item_metadata->validate();
$Tainacan_Item_Metadata->insert($item_metadata);
$Tainacan_Item_Metadata->insert( $item_metadata );
$checkItem = $Tainacan_Items->fetch($i->get_id());
$checkItem = $Tainacan_Items->fetch( $i->get_id() );
$this->assertEquals('changed title', $checkItem->get_title());
$this->assertEquals( 'changed title', $checkItem->get_title() );
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $field);
$this->assertEquals('changed title', $check_item_metadata->get_value());
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $checkItem, $field );
$this->assertEquals( 'changed title', $check_item_metadata->get_value() );
// description
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $fieldDescription);
$item_metadata->set_value('changed description');
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $i, $fieldDescription );
$item_metadata->set_value( 'changed description' );
$item_metadata->validate();
$Tainacan_Item_Metadata->insert($item_metadata);
$Tainacan_Item_Metadata->insert( $item_metadata );
$checkItem = $Tainacan_Items->fetch($i->get_id());
$checkItem = $Tainacan_Items->fetch( $i->get_id() );
$this->assertEquals('changed description', $checkItem->get_description());
$this->assertEquals( 'changed description', $checkItem->get_description() );
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($checkItem, $fieldDescription);
$this->assertEquals('changed description', $check_item_metadata->get_value());
$check_item_metadata = new \Tainacan\Entities\Item_Metadata_Entity( $checkItem, $fieldDescription );
$this->assertEquals( 'changed description', $check_item_metadata->get_value() );
}
protected function get_fields( $collection ) {
$request_fields = new \WP_REST_Request( 'GET', $this->namespace . '/collection/' . $collection->get_id() . '/fields' );
$response = $this->server->dispatch( $request_fields );
$data = $response->get_data();
return $data;
}
public function test_update_core_fields_status() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
// GET
$data1 = self::get_fields( $collection );
// PATCH
$field_id = $data1[0]['id'];
$body = json_encode( array(
'status' => 'private'
) );
$request = new \WP_REST_Request( 'PATCH', $this->namespace . '/collection/' . $collection->get_id() . '/fields/' . $field_id );
$request->set_body( $body );
$response = $this->server->dispatch( $request );
$data2 = $response->get_data();
$this->assertEquals('private', $data2['status']);
// GET
$data3 = self::get_fields( $collection );
$this->assertCount(2, $data3);
$statuses = [$data3[0]['status'], $data3[1]['status']];
$this->assertContains('private', $statuses);
$this->assertContains('publish', $statuses);
}