allow scripts to disable creation of logs. useful for importers
This commit is contained in:
parent
5aa6a65dd6
commit
e823a4d6b7
|
@ -13,6 +13,29 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
abstract class Repository {
|
||||
public $entities_type = '\Tainacan\Entities\Entity';
|
||||
|
||||
/**
|
||||
* If set to false, no logs will be generated upon insertion or update
|
||||
*
|
||||
* use enable_logs() and disable_logs() to set the values
|
||||
* @var bool
|
||||
*/
|
||||
private $use_logs = true;
|
||||
|
||||
/**
|
||||
* Disable creation of logs while inerting and updating entities
|
||||
*/
|
||||
public function disable_logs() {
|
||||
$this->use_logs = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable creation of logs while inerting and updating entities
|
||||
* if it was disabled
|
||||
*/
|
||||
public function enable_logs() {
|
||||
$this->use_logs = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register hooks
|
||||
*/
|
||||
|
@ -81,19 +104,22 @@ abstract class Repository {
|
|||
$old = '';
|
||||
|
||||
$diffs = [];
|
||||
if ( $obj->get_id() ) {
|
||||
|
||||
$old = $obj->get_repository()->fetch( $obj->get_id() );
|
||||
|
||||
if ( method_exists( $old, 'get_status' ) && $old->get_status() === 'auto-draft' ) {
|
||||
$is_update = false;
|
||||
} else {
|
||||
$is_update = true;
|
||||
|
||||
if ($this->use_logs) {
|
||||
if ( $obj->get_id() ) {
|
||||
|
||||
$old = $obj->get_repository()->fetch( $obj->get_id() );
|
||||
|
||||
if ( method_exists( $old, 'get_status' ) && $old->get_status() === 'auto-draft' ) {
|
||||
$is_update = false;
|
||||
} else {
|
||||
$is_update = true;
|
||||
}
|
||||
|
||||
$diffs = $this->diff( $old, $obj );
|
||||
}
|
||||
|
||||
$diffs = $this->diff( $old, $obj );
|
||||
}
|
||||
|
||||
|
||||
$map = $this->get_map();
|
||||
|
||||
// First iterate through the native post properties
|
||||
|
@ -137,9 +163,10 @@ abstract class Repository {
|
|||
}
|
||||
|
||||
// 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 );
|
||||
|
||||
if ($this->use_logs) {
|
||||
do_action( 'tainacan-insert', $obj, $diffs, $is_update );
|
||||
do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj );
|
||||
}
|
||||
// return a brand new object
|
||||
return new $this->entities_type( $obj->WP_Post );
|
||||
}
|
||||
|
|
|
@ -757,7 +757,7 @@ abstract class Importer {
|
|||
*/
|
||||
public function insert( $processed_item, $collection_index ) {
|
||||
|
||||
$collections = $this->get_collections();
|
||||
$collections = $this->get_collections();
|
||||
$collection_definition = isset($collections[$collection_index]) ? $collections[$collection_index] : false;
|
||||
if ( !$collection_definition || !is_array($collection_definition) || !isset($collection_definition['id']) || !isset($collection_definition['mapping']) ) {
|
||||
$this->add_error_log('Collection misconfigured');
|
||||
|
@ -768,11 +768,14 @@ abstract class Importer {
|
|||
|
||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
||||
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
|
||||
|
||||
$Tainacan_Items->disable_logs();
|
||||
$Tainacan_Metadata->disable_logs();
|
||||
|
||||
$item = new Entities\Item();
|
||||
$itemMetadataArray = [];
|
||||
|
||||
$itemMetadataArray = [];
|
||||
|
||||
if( is_array( $processed_item ) ){
|
||||
foreach ( $processed_item as $metadatum_source => $values ){
|
||||
$tainacan_metadatum_id = array_search( $metadatum_source, $collection_definition['mapping'] );
|
||||
|
@ -788,23 +791,23 @@ abstract class Importer {
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( !empty( $itemMetadataArray ) && $collection instanceof Entities\Collection ){
|
||||
$item->set_collection( $collection );
|
||||
|
||||
if( $item->validate() ){
|
||||
$insertedItem = $Tainacan_Items->insert( $item );
|
||||
$insertedItem = $Tainacan_Items->insert( $item );
|
||||
} else {
|
||||
$this->add_error_log( 'Error inserting item' );
|
||||
$this->add_error_log( $item->get_errors() );
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
foreach ( $itemMetadataArray as $itemMetadata ) {
|
||||
$itemMetadata->set_item( $insertedItem ); // *I told you
|
||||
|
||||
if( $itemMetadata->validate() ){
|
||||
$result = $Tainacan_Item_Metadata->insert( $itemMetadata );
|
||||
$result = $Tainacan_Item_Metadata->insert( $itemMetadata );
|
||||
} else {
|
||||
$this->add_error_log('Error saving value for ' . $itemMetadata->get_metadatum()->get_name());
|
||||
$this->add_error_log($itemMetadata->get_errors());
|
||||
|
@ -818,18 +821,18 @@ abstract class Importer {
|
|||
//} else {
|
||||
// $this->add_error_log( 'Item ' . $insertedItem->get_id() . ' has an error' );
|
||||
//}
|
||||
}
|
||||
|
||||
$insertedItem->set_status('publish' );
|
||||
|
||||
}
|
||||
|
||||
$insertedItem->set_status('publish' );
|
||||
|
||||
if($insertedItem->validate()) {
|
||||
$insertedItem = $Tainacan_Items->update( $insertedItem );
|
||||
$insertedItem = $Tainacan_Items->update( $insertedItem );
|
||||
} else {
|
||||
$this->add_error_log( 'Error publishing Item' );
|
||||
$this->add_error_log( $insertedItem->get_errors() );
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $insertedItem;
|
||||
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue