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

This commit is contained in:
weryques 2018-02-16 15:40:05 -02:00
commit f2332ea16b
7 changed files with 136 additions and 74 deletions

View File

@ -153,14 +153,6 @@ class Field extends Entity {
return $this->get_mapped_property('field_type_options');
}
/**
* Return the if the field may be deleted
*
* @return string
*/
function get_can_delete(){
return $this->get_mapped_property('can_delete');
}
/**
* Set the field name
@ -281,16 +273,6 @@ class Field extends Entity {
$this->set_mapped_property('field_type', ( is_object( $value ) ) ? get_class( $value ) : $value ) ; // Encode to avoid backslaches removal
}
/**
* Set can delete
*
* @param [string] $value
* @return void
*/
function set_can_delete( $value ){
$this->set_mapped_property('can_delete', $value);
}
// helpers
/**

View File

@ -215,9 +215,12 @@ class Collections extends Repository {
* @see \Tainacan\Repositories\Repository::insert()
*/
public function insert($collection){
global $Tainacan_Fields;
$this->pre_update_moderators($collection);
$new_collection = parent::insert($collection);
$Tainacan_Fields->register_core_fields( $new_collection );
$collection->register_collection_item_post_type();
$this->update_moderators($new_collection);
return $new_collection;

View File

@ -2,6 +2,7 @@
namespace Tainacan\Repositories;
use Tainacan\Entities;
use Tainacan\Field_Types;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
@ -15,14 +16,17 @@ class Fields extends Repository {
public $field_types = [];
public $core_fields = [
'Tainacan\Field_Types\Core_Title',
'Tainacan\Field_Types\Core_Description'
];
/**
* Register specific hooks for field repository
*/
function __construct() {
parent::__construct();
add_action('tainacan_activated', array(&$this, 'register_core_fields'));
add_action('wp_trash_post', array( &$this, 'disable_delete_core_fields' ) );
add_action('before_delete_post', array( &$this, 'disable_delete_core_fields' ) );
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() {
@ -285,7 +289,7 @@ class Fields extends Repository {
* @throws \Exception
*/
public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = null){
$this->register_core_fields();
$this->register_core_fields( $collection );
$collection_id = $collection->get_id();
@ -414,71 +418,133 @@ class Fields extends Repository {
}
/**
* verify and, if is not registered, insert the default fields
* @param Entities\Collection $collection
* @return array
* @throws \ErrorException
*/
public function register_core_fields(){
$update_option = [];
$core_fields = get_option('tainacan_core_fields');
if( $core_fields ) {
return $core_fields;
}
public function register_core_fields( Entities\Collection $collection ){
$fields = $this->get_core_fields( $collection );
// TODO: create a better way to retrieve this data
$data_core_fields = [
'core_title' => [
'name' => 'Title',
'description' => 'title',
'collection_id' => 'default',
'collection_id' => $collection->get_id(),
'field_type' => 'Tainacan\Field_Types\Core_Title',
'can_delete' => 'no',
'status' => 'publish'
],
'core_description' => [
'name' => 'Description',
'description' => 'description',
'collection_id' => 'default',
'collection_id' => $collection->get_id(),
'field_type' => 'Tainacan\Field_Types\Core_Description',
'can_delete' => 'no',
'status' => 'publish'
]
];
if( $collection->get_parent() !== 0 ){
return false;
}
foreach ( $data_core_fields as $index => $data_core_field ) {
if( !$core_fields || !isset($core_fields[$index]) ){
$field = new Entities\Field();
foreach ($data_core_field as $attribute => $value) {
$set_ = 'set_' . $attribute;
$field->$set_( $value );
}
if ($field->validate()) {
$field = $this->insert($field);
$update_option[$index] = $field->get_id();
if( empty( $fields ) ){
$this->insert_array_field( $data_core_field );
} else {
throw new \ErrorException('The entity wasn\'t validated.' . print_r( $field->get_errors(), true));
}
} else if( isset($core_fields[$index]) ) {
$update_option[$index] = $core_fields[$index];
$exists = false;
foreach ( $fields as $field ){
if ( $field->get_field_type() === $data_core_field['field_type'] ) {
$exists = true;
}
}
update_option('tainacan_core_fields', $update_option);
return $update_option;
if( !$exists ){
$this->insert_array_field( $data_core_field );
}
}
}
}
/**
* block user from remove core fields
*
* @param $post_id The post ID which is deleting
* @param $before wordpress pass a null value
* @param $post the post which is moving to trash
* @return null/bool
* @throws \ErrorException
*/
public function disable_delete_core_fields( $post_id ){
$core_fields = get_option('tainacan_core_fields');
public function disable_delete_core_fields( $before, $post ){
$field = $this->fetch( $post->ID );
if ( $core_fields && in_array( $post_id, $core_fields ) ) {
throw new \ErrorException('Core fields cannot be deleted.');
if ( $field && in_array( $field->get_field_type(), $this->core_fields ) && is_numeric($field->get_collection_id()) ) {
return false;
}
}
/**
* block user from remove core fields ( if use wp_delete_post)
*
* @param $before wordpress pass a null value
* @param $post the post which is deleting
* @param $force_delete a boolean that force the deleting
* @return null /bool
* @internal param The $post_id post ID which is deleting
*/
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()) ) {
return false;
}
}
/**
* returns all core items from a specific collection
*
* @param Entities\Collection $collection
* @return Array|\WP_Query
*/
public function get_core_fields( Entities\Collection $collection ){
$args = [];
$meta_query = array(
array(
'key' => 'collection_id',
'value' => $collection->get_id(),
'compare' => 'IN',
),
array(
'key' => 'field_type',
'value' => $this->core_fields,
'compare' => 'IN',
)
);
$args['meta_query'] = $meta_query;
return $this->fetch( $args, 'OBJECT' );
}
/**
* create a field entity and insert by an associative array ( attribute => value )
*
* @param Array $data the array of attributes to insert a field
* @return int the field id inserted
* @throws \ErrorException
*/
public function insert_array_field( $data ){
$field = new Entities\Field();
foreach ( $data as $attribute => $value ) {
$set_ = 'set_' . $attribute;
$field->$set_( $value );
}
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));
}
}
}

View File

@ -25,13 +25,6 @@ function tnc_enable_dev_wp_interface() {
//return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
}
// fire actions right after plugin is activate
function tainacan_activate() {
do_action( 'tainacan_activated' );
}
register_activation_hook( __FILE__, 'tainacan_activate' );
// TODO move it somewhere else?
require_once('admin/class-tainacan-admin.php');
global $Tainacan_Admin;

View File

@ -171,12 +171,24 @@ class Fields extends TAINACAN_UnitTestCase {
function test_core_fields(){
global $Tainacan_Fields;
$core_fields_ids = $Tainacan_Fields->register_core_fields();
$collection_grandfather = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'collection field'
),
true
);
$core_fields = $Tainacan_Fields->get_core_fields( $collection_grandfather );
$this->expectException(\ErrorException::class);
if( $core_fields_ids ){
foreach( $core_fields_ids as $core_field_id ){
wp_trash_post( $core_field_id );
if( $core_fields ){
foreach( $core_fields as $core_field ){
wp_trash_post( $core_field->get_id() );
}
}
}

View File

@ -224,11 +224,17 @@ class Item_Metadata extends TAINACAN_UnitTestCase {
$item_metadatas = $Tainacan_Item_Metadata->fetch($i, 'OBJECT');
$names = [];
foreach ($item_metadatas as $item_metadata) {
$names[] = $item_metadata->get_field()->get_name();
}
$this->assertTrue(is_array($item_metadatas));
// notice for repository fields
$this->assertEquals(3, sizeof($item_metadatas));
$this->assertEquals('metadado', $item_metadatas[0]->get_field()->get_name());
//first 2 fields are repository fields
$this->assertTrue( in_array('metadado', $names) );
}
}

View File

@ -229,11 +229,11 @@ class Items extends TAINACAN_UnitTestCase {
'meta_query' => [
[
'key' => $field2->get_id(),
'value' => 'value_2'
'value' => 'value_3'
]
]
], $collection2);
$this->assertEquals(1, $test_query->post_count);
$this->assertEquals(2, $test_query->post_count);
// should return 2 items
$test_query = $Tainacan_Items->fetch([