refactor handling core metadata (refs #79)
This commit is contained in:
parent
c5f9705786
commit
38f1266855
|
@ -462,15 +462,8 @@ class Collection extends Entity {
|
|||
function get_core_metadata() {
|
||||
$repo = \Tainacan\Repositories\Metadata::get_instance();
|
||||
|
||||
return $repo->fetch_by_collection($this, [
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => 'metadata_type',
|
||||
'value' => ['Tainacan\Metadata_Types\Core_Title', 'Tainacan\Metadata_Types\Core_Description'],
|
||||
'compare' => 'IN'
|
||||
]
|
||||
]
|
||||
], 'OBJECT');
|
||||
return $repo->get_core_metadata($this);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -481,20 +474,7 @@ class Collection extends Entity {
|
|||
function get_core_title_metadatum() {
|
||||
$repo = \Tainacan\Repositories\Metadata::get_instance();
|
||||
|
||||
$results = $repo->fetch_by_collection($this, [
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => 'metadata_type',
|
||||
'value' => 'Tainacan\Metadata_Types\Core_Title',
|
||||
]
|
||||
],
|
||||
'posts_per_page' => 1
|
||||
], 'OBJECT');
|
||||
|
||||
if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum) {
|
||||
return $results[0];
|
||||
}
|
||||
return false;
|
||||
return $repo->get_core_title_metadatum($this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -505,20 +485,7 @@ class Collection extends Entity {
|
|||
function get_core_description_metadatum() {
|
||||
$repo = \Tainacan\Repositories\Metadata::get_instance();
|
||||
|
||||
$results = $repo->fetch_by_collection($this, [
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => 'metadata_type',
|
||||
'value' => 'Tainacan\Metadata_Types\Core_Description',
|
||||
]
|
||||
],
|
||||
'posts_per_page' => 1
|
||||
], 'OBJECT');
|
||||
|
||||
if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum) {
|
||||
return $results[0];
|
||||
}
|
||||
return false;
|
||||
return $repo->get_core_description_metadatum($this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -260,12 +260,13 @@ class Collections extends Repository {
|
|||
* @see \Tainacan\Repositories\Repository::insert()
|
||||
*/
|
||||
public function insert( $collection ) {
|
||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||
|
||||
$this->pre_update_moderators( $collection );
|
||||
$this->pre_process( $collection );
|
||||
|
||||
$new_collection = parent::insert( $collection );
|
||||
|
||||
//$Tainacan_Metadata->register_core_metadata( $new_collection );
|
||||
$this->handle_core_metadata( $new_collection );
|
||||
|
||||
$collection->register_collection_item_post_type();
|
||||
flush_rewrite_rules(false); // needed to activate items post type archive url
|
||||
$this->update_moderators( $new_collection );
|
||||
|
@ -358,11 +359,16 @@ class Collections extends Repository {
|
|||
return false;
|
||||
}
|
||||
|
||||
function pre_update_moderators( $collection ) {
|
||||
function pre_process( $collection ) {
|
||||
// make sure we get the current value from database
|
||||
$current_moderators = $this->get_mapped_property( $collection, 'moderators_ids' );
|
||||
$this->current_moderators = is_array( $current_moderators ) ? $current_moderators : [];
|
||||
|
||||
$this->old_collection = $this->fetch( $collection->get_id() );
|
||||
$this->old_core_title = $collection->get_core_title_metadatum();
|
||||
$this->old_core_description = $collection->get_core_description_metadatum();
|
||||
|
||||
|
||||
}
|
||||
|
||||
function update_moderators( $collection ) {
|
||||
|
@ -375,6 +381,16 @@ class Collections extends Repository {
|
|||
do_action( 'tainacan-remove-collection-moderators', $collection, $deleted );
|
||||
}
|
||||
|
||||
function handle_core_metadata ( $collection ) {
|
||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||
|
||||
$Tainacan_Metadata->register_core_metadata( $collection );
|
||||
|
||||
if ( $this->old_collection instanceof Entities\Collection && $this->old_collection->get_parent() != $collection->get_parent() ) {
|
||||
$Tainacan_Metadata->maybe_update_core_metadata_meta_keys( $collection, $this->old_collection, $this->old_core_title, $this->old_core_description );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter to handle special permissions
|
||||
*
|
||||
|
|
|
@ -38,7 +38,6 @@ class Metadata extends Repository {
|
|||
add_filter('pre_trash_post', array( &$this, 'disable_delete_core_metadata' ), 10, 2 );
|
||||
add_filter('pre_delete_post', array( &$this, 'force_delete_core_metadata' ), 10, 3 );
|
||||
|
||||
add_action('tainacan-collection-parent-updated', array(&$this, 'update_core_metadata_meta_keys'), 10, 2);
|
||||
}
|
||||
|
||||
public function get_map() {
|
||||
|
@ -508,110 +507,51 @@ class Metadata extends Repository {
|
|||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function update_core_metadata_meta_keys($collection_old, Entities\Collection $collection_new){
|
||||
public function maybe_update_core_metadata_meta_keys(Entities\Collection $collection_new, Entities\Collection $collection_old, Entities\Metadatum $old_title_metadatum, Entities\Metadatum $old_description_metadatum){
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->flush();
|
||||
|
||||
$item_post_type = "%%{$collection_new->get_id()}_item";
|
||||
$item_post_type = $collection_new->get_db_identifier();
|
||||
$parent_collection_id = $collection_new->get_parent();
|
||||
|
||||
if($parent_collection_id != 0){
|
||||
|
||||
$metadata = $this->get_core_metadata( $collection_new );
|
||||
|
||||
$data_core_metadata = $this->get_data_core_metadata($collection_new);
|
||||
|
||||
$parent_collection = new Entities\Collection($parent_collection_id);
|
||||
|
||||
$parent_cores = $this->get_core_metadata($parent_collection);
|
||||
|
||||
if(!empty($metadata)){
|
||||
|
||||
foreach ( $data_core_metadata as $index => $data_core_metadatum ) {
|
||||
foreach ( $metadata as $metadatum ){
|
||||
if ( $metadatum->get_metadata_type() === $data_core_metadatum['metadata_type'] ) {
|
||||
foreach ($parent_cores as $parent_core){
|
||||
if($metadatum->get_metadata_type() === $parent_core->get_metadata_type()){
|
||||
|
||||
$old_metadatum_id = $metadatum->get_id();
|
||||
$new_metadatum_id = $parent_core->get_id();
|
||||
|
||||
$sql_statement = $wpdb->prepare(
|
||||
"UPDATE $wpdb->postmeta
|
||||
SET meta_key = %s
|
||||
WHERE meta_key = %s AND post_id IN (
|
||||
SELECT ID
|
||||
FROM $wpdb->posts
|
||||
WHERE post_type LIKE %s
|
||||
)", $new_metadatum_id, $old_metadatum_id, $item_post_type
|
||||
);
|
||||
|
||||
$res = $wpdb->query($sql_statement);
|
||||
|
||||
wp_cache_flush();
|
||||
|
||||
if($res !== false) {
|
||||
update_post_meta( $metadatum->get_id(), 'metadata_type', 'to_delete', $data_core_metadatum['metadata_type'] );
|
||||
wp_delete_post( $metadatum->get_id(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
$old_parent = 0;
|
||||
|
||||
if (!$collection_old) {
|
||||
$this->register_core_metadata($collection_new);
|
||||
} else {
|
||||
$old_parent = $collection_old->get_parent();
|
||||
|
||||
$this->register_core_metadata($collection_new);
|
||||
}
|
||||
|
||||
if($old_parent != 0) {
|
||||
$metadata = $this->get_core_metadata( $collection_new );
|
||||
|
||||
$data_core_metadata = $this->get_data_core_metadata( $collection_new );
|
||||
|
||||
$parent_collection = new Entities\Collection( $old_parent );
|
||||
|
||||
$parent_cores = $this->get_core_metadata( $parent_collection );
|
||||
|
||||
foreach ( $data_core_metadata as $index => $data_core_metadatum ) {
|
||||
foreach ( $metadata as $metadatum ) {
|
||||
if ( $metadatum->get_metadata_type() === $data_core_metadatum['metadata_type'] ) {
|
||||
foreach ( $parent_cores as $parent_core ) {
|
||||
if ( $metadatum->get_metadata_type() === $parent_core->get_metadata_type() ) {
|
||||
|
||||
$old_metadatum_id = $parent_core->get_id();
|
||||
$new_metadatum_id = $metadatum->get_id();
|
||||
|
||||
$sql_statement = $wpdb->prepare(
|
||||
"UPDATE $wpdb->postmeta
|
||||
SET meta_key = %s
|
||||
WHERE meta_key = %s AND post_id IN (
|
||||
SELECT ID
|
||||
FROM $wpdb->posts
|
||||
WHERE post_type LIKE %s
|
||||
)", $new_metadatum_id, $old_metadatum_id, $item_post_type
|
||||
);
|
||||
|
||||
$res = $wpdb->query($sql_statement);
|
||||
|
||||
wp_cache_flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($parent_collection_id != 0 && $collection_old->get_parent() == 0) {
|
||||
update_post_meta( $old_description_metadatum->get_id(), 'metadata_type', 'to_delete', $old_description_metadatum->get_metadata_type() );
|
||||
wp_delete_post( $old_description_metadatum->get_id(), true);
|
||||
update_post_meta( $old_title_metadatum->get_id(), 'metadata_type', 'to_delete', $old_title_metadatum->get_metadata_type() );
|
||||
wp_delete_post( $old_title_metadatum->get_id(), true);
|
||||
}
|
||||
|
||||
$new_title_metadatum = $collection_new->get_core_title_metadatum();
|
||||
$new_description_metadatum = $collection_new->get_core_description_metadatum();
|
||||
|
||||
$sql_statement = $wpdb->prepare(
|
||||
"UPDATE $wpdb->postmeta
|
||||
SET meta_key = %s
|
||||
WHERE meta_key = %s AND post_id IN (
|
||||
SELECT ID
|
||||
FROM $wpdb->posts
|
||||
WHERE post_type = %s
|
||||
)", $new_title_metadatum->get_id(), $old_title_metadatum->get_id(), $item_post_type
|
||||
);
|
||||
|
||||
$res = $wpdb->query($sql_statement);
|
||||
|
||||
$sql_statement = $wpdb->prepare(
|
||||
"UPDATE $wpdb->postmeta
|
||||
SET meta_key = %s
|
||||
WHERE meta_key = %s AND post_id IN (
|
||||
SELECT ID
|
||||
FROM $wpdb->posts
|
||||
WHERE post_type = %s
|
||||
)", $new_description_metadatum->get_id(), $old_description_metadatum->get_id(), $item_post_type
|
||||
);
|
||||
|
||||
$res = $wpdb->query($sql_statement);
|
||||
|
||||
wp_cache_flush();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -655,7 +595,11 @@ class Metadata extends Repository {
|
|||
*/
|
||||
public function register_core_metadata( Entities\Collection $collection ){
|
||||
|
||||
$metadata = $this->get_core_metadata( $collection );
|
||||
if ($collection->get_status() == 'auto-draft') {
|
||||
return;
|
||||
}
|
||||
|
||||
$metadata = $collection->get_core_metadata();
|
||||
|
||||
$data_core_metadata = $this->get_data_core_metadata($collection);
|
||||
|
||||
|
@ -674,7 +618,7 @@ class Metadata extends Repository {
|
|||
$this->insert_array_metadatum( $data_core_metadatum );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -722,25 +666,68 @@ class Metadata extends Repository {
|
|||
* @throws \Exception
|
||||
*/
|
||||
public function get_core_metadata( Entities\Collection $collection ){
|
||||
$args = [];
|
||||
|
||||
return $this->fetch_by_collection($collection, [
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => 'metadata_type',
|
||||
'value' => $this->core_metadata,
|
||||
'compare' => 'IN'
|
||||
]
|
||||
]
|
||||
], 'OBJECT');
|
||||
|
||||
$meta_query = array(
|
||||
array(
|
||||
'key' => 'collection_id',
|
||||
'value' => $collection->get_id(),
|
||||
'compare' => 'IN',
|
||||
),
|
||||
array(
|
||||
'key' => 'metadata_type',
|
||||
'value' => $this->core_metadata,
|
||||
'compare' => 'IN',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Core Title Metadatum for a collection
|
||||
*
|
||||
* @param Entities\Collection $collection
|
||||
*
|
||||
* @return \Tainacan\Entities\Metadatum The Core Title Metadatum
|
||||
*/
|
||||
public function get_core_title_metadatum( Entities\Collection $collection ) {
|
||||
|
||||
$args['meta_query'] = $meta_query;
|
||||
$results = $this->fetch_by_collection($collection, [
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => 'metadata_type',
|
||||
'value' => 'Tainacan\Metadata_Types\Core_Title',
|
||||
]
|
||||
],
|
||||
'posts_per_page' => 1
|
||||
], 'OBJECT');
|
||||
|
||||
return $this->fetch( $args, 'OBJECT' );
|
||||
}
|
||||
if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum) {
|
||||
return $results[0];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Core Description Metadatum for a collection
|
||||
*
|
||||
* @param Entities\Collection $collection
|
||||
*
|
||||
* @return \Tainacan\Entities\Metadatum The Core Description Metadatum
|
||||
*/
|
||||
public function get_core_description_metadatum( Entities\Collection $collection ) {
|
||||
|
||||
$results = $this->fetch_by_collection($collection, [
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => 'metadata_type',
|
||||
'value' => 'Tainacan\Metadata_Types\Core_Description',
|
||||
]
|
||||
],
|
||||
'posts_per_page' => 1
|
||||
], 'OBJECT');
|
||||
|
||||
if (is_array($results) && sizeof($results) == 1 && $results[0] instanceof \Tainacan\Entities\Metadatum) {
|
||||
return $results[0];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* create a metadatum entity and insert by an associative array ( attribute => value )
|
||||
|
|
|
@ -78,13 +78,8 @@ abstract class Repository {
|
|||
}
|
||||
|
||||
$is_update = false;
|
||||
$is_updated_parent = false;
|
||||
$old = '';
|
||||
|
||||
if ( $obj instanceof Entities\Collection && $obj->get_status() != 'auto-draft' ) {
|
||||
$is_updated_parent = true;
|
||||
}
|
||||
|
||||
$diffs = [];
|
||||
if ( $obj->get_id() ) {
|
||||
|
||||
|
@ -141,10 +136,6 @@ abstract class Repository {
|
|||
}
|
||||
}
|
||||
|
||||
if($is_updated_parent){
|
||||
do_action('tainacan-collection-parent-updated', $old, $obj);
|
||||
}
|
||||
|
||||
// TODO: Logs for header image insert and update
|
||||
do_action( 'tainacan-insert', $obj, $diffs, $is_update );
|
||||
do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj );
|
||||
|
|
|
@ -210,7 +210,8 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase {
|
|||
}
|
||||
|
||||
function test_collection_parent_change_and_update_core_metadata(){
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
global $wpdb;
|
||||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
|
||||
$collection_parent2 = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
|
@ -219,7 +220,9 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase {
|
|||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
);
|
||||
|
||||
$core_metadata_parent2 = $collection_parent2->get_core_metadata();
|
||||
|
||||
$collection_parent = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
|
@ -230,26 +233,7 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase {
|
|||
true
|
||||
);
|
||||
|
||||
$collection_parent_item = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
'title' => 'Par',
|
||||
'description' => 'par',
|
||||
'status' => 'publish',
|
||||
'collection' => $collection_parent,
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$core_metadata_parent = $collection_parent->get_core_metadata();
|
||||
|
||||
$item_metadatum_title0 = $this->tainacan_item_metadata_factory->create_item_metadata(
|
||||
$collection_parent_item,
|
||||
$core_metadata_parent[1], 'Son of son');
|
||||
|
||||
$item_metadatum_desc0 = $this->tainacan_item_metadata_factory->create_item_metadata(
|
||||
$collection_parent_item, $core_metadata_parent[0],
|
||||
'Desc of son of son');
|
||||
$core_metadata_parent = $collection_parent->get_core_metadata();
|
||||
|
||||
$collection_son = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
|
@ -289,23 +273,30 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase {
|
|||
# When updated the parent, the item metadata key, have to be update too
|
||||
$collection_son->set_parent($collection_parent->get_id());
|
||||
|
||||
$collection_son->validate();
|
||||
$collection_son = $Tainacan_Collections->update($collection_son);
|
||||
$collection_son->validate();
|
||||
|
||||
$collection_son = $Tainacan_Collections->update($collection_son);
|
||||
|
||||
$check_deleted = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->posts WHERE ID = %d", $core_metadata_son[0]->get_id()) );
|
||||
$this->assertEquals(0, $check_deleted);
|
||||
$check_deleted = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->posts WHERE ID = %d", $core_metadata_son[1]->get_id()) );
|
||||
$this->assertEquals(0, $check_deleted);
|
||||
|
||||
$core_metadata_son = $collection_son->get_core_metadata();
|
||||
|
||||
$this->assertNotEquals($core_metadata_son[0]->get_id(), $item_metadatum_desc->get_metadatum()->get_id());
|
||||
$this->assertNotEquals($core_metadata_son[1]->get_id(), $item_metadatum_title->get_metadatum()->get_id());
|
||||
|
||||
$it = get_post_meta($collection_son_item->get_id());
|
||||
|
||||
$it = get_post_meta($collection_son_item->get_id());
|
||||
|
||||
$this->assertArrayHasKey($core_metadata_parent[0]->get_id(), $it);
|
||||
$this->assertArrayHasKey($core_metadata_parent[1]->get_id(), $it);
|
||||
|
||||
# Changes parent again
|
||||
$collection_son->set_parent(0);
|
||||
|
||||
$collection_son->validate();
|
||||
$collection_son->validate();
|
||||
|
||||
$collection_son = $Tainacan_Collections->update($collection_son);
|
||||
|
||||
$core_metadata_son2 = $collection_son->get_core_metadata();
|
||||
|
@ -321,18 +312,46 @@ class CoreMetadatumTypes extends TAINACAN_UnitTestCase {
|
|||
# Changes parent again
|
||||
$collection_son->set_parent($collection_parent2->get_id());
|
||||
|
||||
$collection_son->validate();
|
||||
$collection_son = $Tainacan_Collections->update($collection_son);
|
||||
$collection_son->validate();
|
||||
|
||||
$collection_son = $Tainacan_Collections->update($collection_son);
|
||||
|
||||
$check_deleted = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->posts WHERE ID = %d", $core_metadata_son2[0]->get_id()) );
|
||||
$this->assertEquals(0, $check_deleted);
|
||||
$check_deleted = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) FROM $wpdb->posts WHERE ID = %d", $core_metadata_son2[1]->get_id()) );
|
||||
$this->assertEquals(0, $check_deleted);
|
||||
|
||||
$core_metadata_son3 = $collection_son->get_core_metadata();
|
||||
$core_metadata_son3 = $collection_son->get_core_metadata();
|
||||
|
||||
$this->assertEquals( $core_metadata_son3[0]->get_id(), $core_metadata_parent2[0]->get_id() );
|
||||
|
||||
$it3 = get_post_meta($collection_son_item->get_id());
|
||||
$it3 = get_post_meta($collection_son_item->get_id());
|
||||
|
||||
$this->assertArrayHasKey($core_metadata_son3[0]->get_id(), $it3);
|
||||
$this->assertArrayHasKey($core_metadata_son3[1]->get_id(), $it3);
|
||||
|
||||
$this->assertArrayNotHasKey($core_metadata_parent[0]->get_id(), $it3);
|
||||
$this->assertArrayNotHasKey($core_metadata_parent[1]->get_id(), $it3);
|
||||
$this->assertArrayNotHasKey($core_metadata_parent[1]->get_id(), $it3);
|
||||
|
||||
|
||||
# Changes parent again to another parent directly
|
||||
$collection_son->set_parent($collection_parent->get_id());
|
||||
|
||||
$collection_son->validate();
|
||||
|
||||
$collection_son = $Tainacan_Collections->update($collection_son);
|
||||
|
||||
$core_metadata_son4 = $collection_son->get_core_metadata();
|
||||
|
||||
$this->assertEquals( $core_metadata_son4[0]->get_id(), $core_metadata_parent[0]->get_id() );
|
||||
|
||||
$it4 = get_post_meta($collection_son_item->get_id());
|
||||
|
||||
$this->assertArrayHasKey($core_metadata_son4[0]->get_id(), $it4);
|
||||
$this->assertArrayHasKey($core_metadata_son4[1]->get_id(), $it4);
|
||||
|
||||
$this->assertArrayNotHasKey($core_metadata_parent2[0]->get_id(), $it4);
|
||||
$this->assertArrayNotHasKey($core_metadata_parent2[1]->get_id(), $it4);
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue