Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a non localized version of timer_stop() #2380

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,12 @@ function ( $prefix ) use ( $args ) {

$loop_counter++;
if ( ( $loop_counter % 10 ) === 0 ) {
$time_elapsed_diff = $time_elapsed > 0 ? ' (+' . (string) ( timer_stop( 0, 2 ) - $time_elapsed ) . ')' : '';
// Get a non-formatted version of the timer (1000.00 instead of 1,000.00)
add_filter( 'number_format_i18n', [ __CLASS__, 'skip_number_format_i18n' ], 10, 3 );
$time_elapsed_calc = timer_stop( 0, 2 ) * 1000;
remove_filter( 'number_format_i18n', [ __CLASS__, 'skip_number_format_i18n' ] );

$time_elapsed_diff = $time_elapsed > 0 ? ' (+' . (string) ( $time_elapsed_calc - $time_elapsed ) . ')' : '';
$time_elapsed = timer_stop( 0, 2 );
WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Time elapsed: ', 'elasticpress' ) . '%N' . $time_elapsed . $time_elapsed_diff ) );

Expand Down Expand Up @@ -1787,4 +1792,18 @@ protected function render_stats( $current_index, $body ) {
WP_CLI::warning( $current_index . ' is not currently indexed.' );
}
}

/**
* Utilitary function to skip i18n number formatting, so we can use it in calculations.
*
* This function is public because it is used by a WP filter and static so it is not considered a WP-CLI subcommand.
*
* @param string $formatted Formatted version of the number.
* @param float $number The number to return.
* @param int $decimals Precision of the number of decimal places.
* @return float
*/
public static function skip_number_format_i18n( $formatted, $number, $decimals ) {
return number_format( $number, absint( $decimals ), '.', '' );
}
}