Skip to content

Commit

Permalink
Fix missing terms count update
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-sakal authored Jul 8, 2022
1 parent 0fc7355 commit 778b5e6
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions inc/OneClickDemoImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class OneClickDemoImport {
*/
private $plugin_page_setup = array();

/**
* Imported terms.
*
* @var array
*/
private $imported_terms = array();

/**
* Returns the *Singleton* instance of this class.
*
Expand Down Expand Up @@ -120,6 +127,7 @@ protected function __construct() {
add_action( 'admin_notices', array( $this, 'start_notice_output_capturing' ), 0 );
add_action( 'all_admin_notices', array( $this, 'finish_notice_output_capturing' ), PHP_INT_MAX );
add_action( 'admin_init', array( $this, 'redirect_from_old_default_admin_page' ) );
add_action( 'set_object_terms', array( $this, 'add_imported_terms' ), 10, 6 );
}


Expand Down Expand Up @@ -390,6 +398,9 @@ public function import_demo_data_ajax_callback() {
wp_send_json( array( 'status' => 'afterAllImportAJAX' ) );
}

// Update terms count.
$this->update_terms_count();

// Send a JSON response with final report.
$this->final_response();
}
Expand Down Expand Up @@ -444,6 +455,9 @@ public function after_all_import_data_ajax_callback() {
Helpers::do_action( 'ocdi/after_all_import_execution', $this->selected_import_files, $this->import_files, $this->selected_index );
}

// Update terms count.
$this->update_terms_count();

// Send a JSON response with final report.
$this->final_response();
}
Expand Down Expand Up @@ -498,6 +512,7 @@ private function use_existing_importer_data() {
$this->selected_import_files = empty( $data['selected_import_files'] ) ? array() : $data['selected_import_files'];
$this->import_files = empty( $data['import_files'] ) ? array() : $data['import_files'];
$this->before_import_executed = empty( $data['before_import_executed'] ) ? false : $data['before_import_executed'];
$this->imported_terms = empty( $data['imported_terms'] ) ? [] : $data['imported_terms'];
$this->importer->set_importer_data( $data );

return true;
Expand All @@ -519,6 +534,7 @@ public function get_current_importer_data() {
'selected_import_files' => $this->selected_import_files,
'import_files' => $this->import_files,
'before_import_executed' => $this->before_import_executed,
'imported_terms' => $this->imported_terms,
);
}

Expand Down Expand Up @@ -691,4 +707,30 @@ public function redirect_from_old_default_admin_page() {
exit;
}
}

/**
* Add imported terms.
*
* Mainly it's needed for saving all imported terms and trigger terms count updates.
* WP core term defer counting is not working, since import split to chunks and we are losing `$_deffered` array
* items between ajax calls.
*/
public function add_imported_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ){

if ( ! isset( $this->imported_terms[ $taxonomy ] ) ) {
$this->imported_terms[ $taxonomy ] = array();
}

$this->imported_terms[ $taxonomy ] = array_unique( array_merge( $this->imported_terms[ $taxonomy ], $tt_ids ) );
}

/**
* Update imported terms count.
*/
private function update_terms_count() {

foreach ( $this->imported_terms as $tax => $terms ) {
wp_update_term_count_now( $terms, $tax );
}
}
}

0 comments on commit 778b5e6

Please sign in to comment.