Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Mar 23, 2023
1 parent 744eb8d commit fc228b3
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions includes/classes/IndexHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,56 +686,43 @@ function( $item ) {
}

/**
* Filter the number of errors of a sync that should be stored.
* Filter the number of errors of a current sync that should be stored.
*
* @since 4.2.0
* @hook ep_sync_number_of_errors_stored
* @since 5.0.0
* @hook ep_current_sync_number_of_errors_stored
* @param {int} $number Number of errors to be logged.
* @return {int} New value
*/
$logged_errors = (int) apply_filters( 'ep_sync_number_of_errors_stored', 50 );
$logged_errors = (int) apply_filters( 'ep_current_sync_number_of_errors_stored', 50 );

$error_store_msg = 'Reached maximum number of errors to store';
$error_store_msg = __( 'Reached maximum number of errors to store', 'elasticpress' );

if ( is_wp_error( $return ) ) {
$this->index_meta['current_sync_item']['failed'] += count( $queued_items );

$wp_error_messages = $return->get_error_messages();

if ( count( $this->index_meta['current_sync_item']['errors'] ) + count( $wp_error_messages ) > $logged_errors ) {
$diff = $logged_errors - count( $this->index_meta['current_sync_item']['errors'] );
if ( $diff > 0 ) {
$wp_error_messages = array_slice( $wp_error_messages, 0, $diff );
} else {
$wp_error_messages = [];
if ( end( $this->index_meta['current_sync_item']['errors'] ) !== $error_store_msg ) {
$this->index_meta['current_sync_item']['errors'][] = $error_store_msg;
}
}
}

$this->index_meta['current_sync_item']['errors'] = array_merge( $this->index_meta['current_sync_item']['errors'], $wp_error_messages );
$this->process_error_limit(
count( $this->index_meta['current_sync_item']['errors'] ) + count( $wp_error_messages ),
count( $this->index_meta['current_sync_item']['errors'] ),
$wp_error_messages,
$logged_errors,
);

$this->output( implode( "\n", $wp_error_messages ), 'warning' );
} elseif ( count( $failed_objects ) ) {
$errors_output = $this->output_index_errors( $failed_objects );

$this->index_meta['current_sync_item']['synced'] += count( $queued_items ) - count( $failed_objects );

if ( $this->index_meta['current_sync_item']['failed'] + count( $failed_objects ) > $logged_errors ) {
$diff = $logged_errors - $this->index_meta['current_sync_item']['failed'];
if ( $diff > 0 ) {
$errors_output = array_slice( $errors_output, 0, $diff );
} else {
$errors_output = [];
if ( end( $this->index_meta['current_sync_item']['errors'] ) !== $error_store_msg ) {
$this->index_meta['current_sync_item']['errors'][] = $error_store_msg;
}
}
}
$this->maybe_process_error_limit(
$this->index_meta['current_sync_item']['failed'] + count( $failed_objects ),
$this->index_meta['current_sync_item']['failed'],
$errors_output,
$logged_errors,
);

$this->index_meta['current_sync_item']['failed'] += count( $failed_objects );
$this->index_meta['current_sync_item']['errors'] = array_merge( $this->index_meta['current_sync_item']['errors'], $errors_output );

$this->output( $errors_output, 'warning' );
} else {
Expand All @@ -760,6 +747,33 @@ function( $item ) {
);
}

/**
* If the number of errors is greater than the limit, slice the array to the limit.
* If the number of errors is less than or equal the limit, add the error message to the array (if it's not there).
*
* @param int $count Number of errors.
* @param int $num Number of errors to subtract from $limit.
* @param array $errors Array of errors.
* @param int $limit Limit of errors.
*/
protected function maybe_process_error_limit( $count, $num, $errors, $limit ) {
$error_store_msg = __( 'Reached maximum number of errors to store', 'elasticpress' );

if ( $limit > 0 && $count > $limit ) {
$diff = $limit - $num;
if ( $diff > 0 ) {
$errors = array_slice( $errors, 0, $diff );
} else {
$errors = [];
if ( end( $this->index_meta['current_sync_item']['errors'] ) !== $error_store_msg ) {
$this->index_meta['current_sync_item']['errors'][] = $error_store_msg;
}
}
}

$this->index_meta['current_sync_item']['errors'] = array_merge( $this->index_meta['current_sync_item']['errors'], $errors );
}

/**
* Update the sync info with the totals from the last sync item.
*
Expand Down

0 comments on commit fc228b3

Please sign in to comment.