Fixes validation, Item repository now uses insert from parent, created actions for deletions, added tag style for attachment mime type

This commit is contained in:
weryques 2018-04-19 16:12:30 -03:00
parent 7410840520
commit c8b6c67ffe
19 changed files with 153 additions and 161 deletions

View File

@ -33,7 +33,7 @@
<div class="media-content">
<div class="content">
<p>
<strong class="is-capitalized">{{ o.title }}</strong> <small>{{ o.mime_type }}</small>
<strong class="is-capitalized">{{ o.title }}</strong> <small class="tag is-light">{{ o.mime_type }}</small>
<br>
{{ o.description }}
</p>
@ -99,7 +99,7 @@
<div class="media-content">
<div class="content">
<p>
<strong class="is-capitalized">{{ d.title }}</strong> <small>{{ d.mime_type }}</small>
<strong class="is-capitalized">{{ d.title }}</strong> <small class="tag is-light">{{ d.mime_type }}</small>
<br>
{{ d.description }}
</p>

View File

@ -35,7 +35,7 @@
<div class="media-content">
<div class="content">
<p>
<strong class="is-capitalized">{{ o.title }}</strong> <small>{{ o.mime_type }}</small>
<strong class="is-capitalized">{{ o.title }}</strong> <small class="tag is-light">{{ o.mime_type }}</small>
<br>
{{ o.description }}
</p>
@ -96,7 +96,7 @@
<div class="media-content">
<div class="content">
<p>
<strong class="is-capitalized">{{ d.title }}</strong> <small>{{ d.mime_type }}</small>
<strong class="is-capitalized">{{ d.title }}</strong> <small class="tag is-light">{{ d.mime_type }}</small>
<br>
{{ d.description }}
</p>

View File

@ -189,7 +189,10 @@ class Entity {
}
}
$this->set_validated($is_valid);
if($is_valid){
$this->set_as_valid();
}
return $is_valid;
}

View File

@ -432,11 +432,15 @@ class Field extends Entity {
$is_valid = $fto->validate_options($this);
}
if (true === $is_valid)
return true;
if (true === $is_valid) {
$this->set_as_valid();
return true;
}
if (!is_array($is_valid))
throw new \Exception("Return of validate_options field type method should be an Array in case of error");
if (!is_array($is_valid)) {
throw new \Exception( "Return of validate_options field type method should be an Array in case of error" );
}
$this->add_error('field_type_options', $is_valid);

View File

@ -205,11 +205,14 @@ class Filter extends Entity {
$is_valid = $fto->validate_options( $this );
}
if (true === $is_valid)
return true;
if (true === $is_valid) {
$this->set_as_valid();
return true;
}
if (!is_array($is_valid))
throw new \Exception("Return of validate_options field type method should be an Array in case of error");
if (!is_array($is_valid)) {
throw new \Exception( "Return of validate_options field type method should be an Array in case of error" );
}
foreach ($is_valid as $field => $message) {
$this->add_error($field, $message);

View File

@ -388,6 +388,10 @@ class Item extends Entity {
}
}
if($is_valid){
$this->set_as_valid();
}
return $is_valid;
}

View File

@ -221,6 +221,7 @@ class Term extends Entity {
return false;
}
$this->set_as_valid();
return true;
}

View File

@ -262,10 +262,22 @@ class Collections extends Repository {
*/
public function delete( $args ) {
if ( ! empty( $args[1] ) && $args[1] === true ) {
return new Entities\Collection( wp_delete_post( $args[0], $args[1] ) );
$deleted = new Entities\Collection( wp_delete_post( $args[0], $args[1] ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true );
}
return $deleted;
}
return new Entities\Collection( wp_trash_post( $args[0] ) );
$trashed = new Entities\Collection( wp_trash_post( $args[0] ) );
if($trashed) {
do_action( 'tainacan-trashed', $trashed, $is_update = false, $is_delete_permanently = false );
}
return $trashed;
}
/**

View File

@ -430,8 +430,6 @@ class Fields extends Repository {
* @see \Tainacan\Repositories\Repository::insert()
*/
public function insert($field){
$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance();
$this->pre_update_category_field($field);
$new_field = parent::insert($field);
@ -452,7 +450,14 @@ class Fields extends Repository {
public function delete($field_id){
$this->delete_category_field($field_id);
return new Entities\Field( wp_trash_post( $field_id ) );
$deleted = new Entities\Field( wp_trash_post( $field_id ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true );
}
return $deleted;
}
/**

View File

@ -186,10 +186,24 @@ class Filters extends Repository {
*/
public function delete($args){
if(!empty($args[1]) && $args[1] === true){
return new Entities\Filter(wp_delete_post($args[0], $args[1]));
$deleted = new Entities\Filter(wp_delete_post($args[0], $args[1]));
if($deleted){
do_action('tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true);
}
return $deleted;
}
return new Entities\Filter(wp_trash_post($args[0]));
$trashed = new Entities\Filter(wp_trash_post($args[0]));
if($trashed){
do_action('tainacan-trashed', $trashed, $is_update = false, $is_delete_permanently = false);
}
return $trashed;
}
public function update($object, $new_values = null){

View File

@ -13,18 +13,15 @@ class Items extends Repository {
private static $instance = null;
public static function get_instance()
{
if(!isset(self::$instance))
{
public static function get_instance() {
if(!isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
protected function __construct()
{
protected function __construct() {
parent::__construct();
add_filter( 'posts_where', array(&$this, 'title_in_posts_where'), 10, 2 );
add_filter( 'posts_where', array(&$this, 'content_in_posts_where'), 10, 2 );
@ -167,108 +164,7 @@ class Items extends Repository {
}
public function insert( $item ) {
$is_update = false;
$diffs = [];
if ( $item->get_id() ) {
$old = $item->get_repository()->fetch( $item->get_id() );
if($old->get_status() === 'auto-draft') {
$is_update = false;
} else {
$is_update = true;
}
$diffs = $this->diff($old, $item);
}
$map = $this->get_map();
// get collection to determine post type
$collection = $item->get_collection();
if ( !$collection ) {
return false;
}
$cpt = $collection->get_db_identifier();
// iterate through the native post properties
foreach ( $map as $prop => $mapped ) {
if ( $mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms' ) {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property( $prop );
}
}
// save post and get its ID
$item->WP_Post->post_type = $cpt;
//$item->WP_Post->post_status = 'publish';
$id = wp_insert_post( $item->WP_Post );
$item->WP_Post = get_post( $id );
// Now run through properties stored as postmeta
foreach ( $map as $prop => $mapped ) {
if ( $mapped['map'] == 'meta' ) {
update_post_meta( $id, $prop, wp_slash( $item->get_mapped_property( $prop ) ) );
} elseif ( $mapped['map'] == 'meta_multi' ) {
$values = $item->get_mapped_property( $prop );
delete_post_meta( $id, $prop );
if ( is_array( $values ) ) {
foreach ( $values as $value ) {
add_post_meta( $id, $prop, wp_slash( $value ) );
}
}
}
}
if ( method_exists( $item, 'get_featured_img_id' ) ) {
if ( ! get_post_thumbnail_id( $item->WP_Post->ID ) ) {
// was added a thumbnail
$settled = set_post_thumbnail( $item->WP_Post, $item->get_featured_img_id( $item->WP_Post->ID ) );
if ( $settled ) {
$thumbnail_url = get_the_post_thumbnail_url( $item->WP_Post->ID );
$diffs['featured_image'] = [
'new' => $thumbnail_url,
'old' => '',
'diff_with_index' => 0,
];
}
} else {
// was update a thumbnail
$old_thumbnail = get_the_post_thumbnail_url( $item->WP_Post->ID );
$settled = set_post_thumbnail( $item->WP_Post, $item->get_featured_img_id( $item->WP_Post->ID ) );
if ( $settled ) {
$thumbnail_url = get_the_post_thumbnail_url( $item->WP_Post->ID );
$diffs['featured_image'] = [
'new' => $thumbnail_url,
'old' => $old_thumbnail,
'diff_with_index' => 0,
];
}
}
}
do_action( 'tainacan-insert', $item, $diffs, $is_update );
do_action( 'tainacan-insert-Item', $item );
// return a brand new object
return new Entities\Item( $item->WP_Post );
return parent::insert($item);
}
/**
@ -395,10 +291,23 @@ class Items extends Repository {
*/
public function delete( $args ) {
if ( ! empty( $args[1] ) && $args[1] === true ) {
return new Entities\Item( wp_delete_post( $args[0], $args[1] ) );
$deleted = new Entities\Item( wp_delete_post( $args[0], $args[1] ) );
if($deleted) {
do_action( 'tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true );
}
return $deleted;
}
return new Entities\Item( wp_trash_post( $args[0] ) );
$trashed = new Entities\Item( wp_trash_post( $args[0] ) );
if($trashed) {
do_action( 'tainacan-trashed', $trashed, $is_update = false, $is_delete_permanently = false );
}
return $trashed;
}
/**

View File

@ -28,10 +28,12 @@ class Logs extends Repository {
protected function __construct() {
parent::__construct();
add_action( 'tainacan-insert', array( $this, 'insert_log' ), 10, 3 );
add_action( 'tainacan-insert', array( $this, 'insert_log' ), 10, 4 );
add_action( 'tainacan-deleted', array( $this, 'insert_log'), 10, 4 );
add_action( 'tainacan-trashed', array( $this, 'insert_log'), 10, 4 );
add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10, 3 );
// add_action( 'attachment_updated', array( $this, 'prepare_attachment_log_before_insert' ), 10, 3);
add_action( 'add_attachment', array( $this, 'prepare_attachment_log_before_insert' ), 10 );
add_action( 'attachment_updated', array( $this, 'prepare_attachment_log_before_insert' ), 10, 3);
}
public function get_map() {
@ -263,6 +265,8 @@ class Logs extends Repository {
}
}
} else {
// TODO: Save a log when a attachment is updated
}
}
@ -270,16 +274,16 @@ class Logs extends Repository {
* Insert a log when a new entity is inserted
*
* @param Entity $new_value
* @param Entity $old_value
*
* @param array $diffs
* @param null $is_update
*
* @return Entities\Log new created log
*/
public function insert_log( $new_value, $diffs, $is_update = null ) {
public function insert_log( $new_value, $diffs = [], $is_update = null, $is_delete_permanently = null ) {
$msn = "";
$description = "";
// TODO: Continue with is_delete_permanently
if ( is_object( $new_value ) ) {
// do not log a log
if ( ( method_exists( $new_value, 'get_post_type' ) && $new_value->get_post_type() === 'tainacan-log' ) || $new_value->get_status() === 'auto-draft' ) {

View File

@ -108,6 +108,19 @@ abstract class Repository {
$obj->WP_Post->post_status = 'publish';
}
if( $obj instanceof Entities\Item ){
// get collection to determine post type
$collection = $obj->get_collection();
if ( !$collection ) {
return false;
}
$post_t = $collection->get_db_identifier();
$obj->WP_Post->post_type = $post_t;
}
// TODO verificar se salvou mesmo
$id = wp_insert_post( $obj->WP_Post );

View File

@ -193,22 +193,26 @@ class Taxonomies extends Repository {
return $unregistered;
}
$deleted = wp_delete_post($taxonomy_id, true);
$deleted = new Entities\Taxonomy(wp_delete_post($taxonomy_id, true));
if(!$deleted){
return $deleted;
}
return new Entities\Taxonomy($deleted);
do_action('tainacan-deleted', $deleted, $is_update = false, $is_delete_permanently = true);
return $deleted;
}
$trashed = wp_trash_post($taxonomy_id);
$trashed = new Entities\Taxonomy(wp_trash_post($taxonomy_id));
if(!$trashed){
return $trashed;
}
return new Entities\Taxonomy($trashed);
do_action('tainacan-trashed', $trashed, $diffs = [], $is_update = false, $is_delete_permanently = false );
return $trashed;
}

View File

@ -225,7 +225,15 @@ class Terms extends Repository {
}
public function delete($args){
return wp_delete_term($args[0], $args[1]);
$deleted = wp_delete_term($args[0], $args[1]);
if($deleted) {
$deleted_term_tainacan = new Entities\Term($args[0], $args[1]);
do_action( 'tainacan-deleted', $deleted_term_tainacan, $is_update = false, $is_delete_permanently = true );
}
return $deleted;
}
public function register_post_type() { }

View File

@ -52,7 +52,7 @@ class Html extends Type {
$html .= '<td>'.htmlspecialchars($value).'</td>';
}
}
if(count($data > 0)) $html = '<th>'.implode('</th><th>', $heads).'</th><tr>'.$html.'</tr>';
if(count($data) > 0) $html = '<th>'.implode('</th><th>', $heads).'</th><tr>'.$html.'</tr>';
return $html;
}
}

View File

@ -406,7 +406,13 @@ abstract class Importer {
// inserted the id on processed item with its index as array index
$this->processed_items[ $index ] = $item->get_id();
$Tainacan_Items->update( $item );
if($item->validate()) {
$Tainacan_Items->update( $item );
} else {
$this->add_log( 'error', 'Item ' . $index . ': ' ); // TODO add the $item->get_errors() array
return false;
}
return $item;
} else {
$this->add_log( 'error', 'Collection not set');

View File

@ -26,6 +26,8 @@ class Entity_Factory {
* @param array $args
* @param bool $is_validated_and_in_db
*
* @param bool $publish
*
* @return mixed
* @throws \ErrorException
*/

View File

@ -70,7 +70,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->field->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
@ -79,13 +79,13 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$data = $response->get_data();
$this->assertEquals($item->get_id(), $data['item']['id']);
$this->assertEquals($this->item->get_id(), $data['item']['id']);
$this->assertEquals('TestValues_exposers', $data['value']);
$item_exposer_json = json_encode([
'exposer-map' => 'Value',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->field->get_id() );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -93,7 +93,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('TestValues_exposers', $data['teste_Expose']);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -116,7 +116,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->field->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
@ -125,14 +125,14 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$data = $response->get_data();
$this->assertEquals($item->get_id(), $data['item']['id']);
$this->assertEquals($this->item->get_id(), $data['item']['id']);
$this->assertEquals('TestValues_exposers', $data['value']);
$item_exposer_json = json_encode([
'exposer-type' => 'Xml',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->field->get_id() );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -143,7 +143,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$item_exposer_json = json_encode([
'exposer-map' => 'Dublin Core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $field->get_id() );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->field->get_id() );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -154,7 +154,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
'exposer-type' => 'Xml',
'exposer-map' => 'Dublin Core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -181,7 +181,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$item_exposer_json = json_encode([
'exposer-type' => 'OAI-PMH',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -205,7 +205,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
'exposer-type' => 'Html',
'exposer-map' => 'Value'
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -254,7 +254,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->field->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
@ -264,7 +264,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$item_exposer_json = json_encode([
'exposer-type' => 'Csv',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' );
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
@ -300,7 +300,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
'values' => 'TestValues_exposers',
]);
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $field->get_id() );
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->field->get_id() );
$request->set_body($item__metadata_json);
$response = $this->server->dispatch($request);
@ -309,7 +309,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$data = $response->get_data();
$this->assertEquals($item->get_id(), $data['item']['id']);
$this->assertEquals($this->item->get_id(), $data['item']['id']);
$this->assertEquals('TestValues_exposers', $data['value']);
$item2 = $this->tainacan_entity_factory->create_entity(
@ -317,7 +317,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
array(
'title' => 'item_teste_Expose2',
'description' => 'adasdasdsa2',
'collection' => $collection
'collection' => $this->collection
),
true,
true
@ -328,7 +328,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
array(
'title' => 'item_teste_Expose3',
'description' => 'adasdasdsa3',
'collection' => $collection
'collection' => $this->collection
),
true,
true
@ -338,7 +338,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
'exposer-map' => 'Value',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/items/' . $item->get_id() );
$request = new \WP_REST_Request('GET', $this->namespace . '/items/' . $this->item->get_id() );
$request->set_body($item_exposer_json);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());