do not get thumb from entity with no post ID, add diff function to entities and repositories, simple collection diff test
This commit is contained in:
parent
b1cf8c385c
commit
6e13222ad1
|
@ -361,4 +361,14 @@ class Entity {
|
||||||
return get_post_type_capabilities((object) $args);
|
return get_post_type_capabilities((object) $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compare this entity props with self old values or with $which other entity
|
||||||
|
* @param Entity|integer|\WP_Post $which default ($which = 0) to self compare with stored entity
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function diff($which = 0) {
|
||||||
|
global ${$this->repository};
|
||||||
|
return ${$this->repository}->diff($which, $this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -285,7 +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 = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full');
|
$property = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null;
|
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null;
|
||||||
}
|
}
|
||||||
|
@ -317,7 +319,7 @@ abstract class Repository {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param integer|\WP_Post $post
|
* @param integer|\WP_Post $post|Entity
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
* @return \Tainacan\Entities\Entity|boolean
|
* @return \Tainacan\Entities\Entity|boolean
|
||||||
*/
|
*/
|
||||||
|
@ -503,6 +505,45 @@ abstract class Repository {
|
||||||
return user_can($user, $entity_cap->publish_posts, $entity->get_id());
|
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
|
||||||
|
*/
|
||||||
|
public function diff($old = 0, $new) {
|
||||||
|
$old_entity = null;
|
||||||
|
if($old === 0) { // self diff or other entity?
|
||||||
|
$id = $new->get_id();
|
||||||
|
if(!empty($id)) { // there is a repository entity?
|
||||||
|
$old_entity = $this->get_entity_by_post($new->WP_Post->ID);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$entity_type = get_class($new);
|
||||||
|
$old_entity = new $entity_type; // there is no saved entity, let compare with a new empty one
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // get entity from repository
|
||||||
|
$old_entity = $this->get_entity_by_post($old);
|
||||||
|
}
|
||||||
|
$new_entity = $this->get_entity_by_post($new);
|
||||||
|
|
||||||
|
$map = $this->get_map();
|
||||||
|
|
||||||
|
$diff = [];
|
||||||
|
|
||||||
|
foreach ($map as $prop => $mapped) {
|
||||||
|
if($old_entity->get_mapped_property($prop) != $new_entity->get_mapped_property($prop) ) {
|
||||||
|
$diff[$prop] = ['new' => $new_entity->get_mapped_property($prop), 'old' => $old_entity->get_mapped_property($prop)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$diff = apply_filters('tainacan-entity-diff', $diff, $new, $old);
|
||||||
|
|
||||||
|
return $diff;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -219,4 +219,29 @@ class Collections extends TAINACAN_UnitTestCase {
|
||||||
global $Tainacan_Collections;
|
global $Tainacan_Collections;
|
||||||
$this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!');
|
$this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group diff
|
||||||
|
*/
|
||||||
|
function test_diff() {
|
||||||
|
$x = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'testeDiff',
|
||||||
|
'description' => 'adasdasdsa',
|
||||||
|
'default_order' => 'DESC'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$x->set_name('OtherValue');
|
||||||
|
$x->set_description('testeDiff2');
|
||||||
|
|
||||||
|
$diff = $x->diff();
|
||||||
|
$this->assertEquals(2, count($diff));
|
||||||
|
$this->assertEquals($diff['name']['new'], 'OtherValue');
|
||||||
|
$this->assertEquals($diff['name']['old'], 'testeDiff');
|
||||||
|
$this->assertEquals($diff['description']['new'], 'testeDiff2');
|
||||||
|
$this->assertEquals($diff['description']['old'], 'adasdasdsa');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue