allow scripts to disable creation of logs. useful for importers

This commit is contained in:
Leo Germani 2018-07-27 16:54:01 -03:00
parent 5aa6a65dd6
commit e823a4d6b7
2 changed files with 59 additions and 29 deletions

View File

@ -13,6 +13,29 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
abstract class Repository { abstract class Repository {
public $entities_type = '\Tainacan\Entities\Entity'; 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 * Register hooks
*/ */
@ -81,6 +104,8 @@ abstract class Repository {
$old = ''; $old = '';
$diffs = []; $diffs = [];
if ($this->use_logs) {
if ( $obj->get_id() ) { if ( $obj->get_id() ) {
$old = $obj->get_repository()->fetch( $obj->get_id() ); $old = $obj->get_repository()->fetch( $obj->get_id() );
@ -93,6 +118,7 @@ abstract class Repository {
$diffs = $this->diff( $old, $obj ); $diffs = $this->diff( $old, $obj );
} }
}
$map = $this->get_map(); $map = $this->get_map();
@ -137,9 +163,10 @@ abstract class Repository {
} }
// TODO: Logs for header image insert and update // TODO: Logs for header image insert and update
if ($this->use_logs) {
do_action( 'tainacan-insert', $obj, $diffs, $is_update ); do_action( 'tainacan-insert', $obj, $diffs, $is_update );
do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj ); do_action( 'tainacan-insert-' . $obj->get_post_type(), $obj );
}
// return a brand new object // return a brand new object
return new $this->entities_type( $obj->WP_Post ); return new $this->entities_type( $obj->WP_Post );
} }

View File

@ -770,6 +770,9 @@ abstract class Importer {
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_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(); $item = new Entities\Item();
$itemMetadataArray = []; $itemMetadataArray = [];