Skip to content
New issue

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

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

Already on GitHub? # to your account

Add Option for forcing thousand separator for stats counter block #440

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion private/src/scripts/modules/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ const toFormattedString = (value) => {
}

const { currentLocale = 'en-GB' } = window.amnestyCoreI18n;
const formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-'));

const { forceThousandSeparator } = window.amnestyForceThousandSeparator;

let options = '';

if (forceThousandSeparator) {
options = { useGrouping: true };
}

const formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-'), options);

return formatted;
};
Expand Down
4 changes: 4 additions & 0 deletions private/src/styles/admin/_localisation-options.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
.cmb2-wrap .cmb2-id-ol-locale-option li {
margin-left: 25px;
}

.cmb2-id-force-thousands-separator .cmb-td label {
margin-left: 25px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function amnesty_register_localisation_options(): void {
'tab_title' => __( 'Localisation', 'amnesty' ),
'parent_slug' => 'amnesty_theme_options_page',
'display_cb' => 'amnesty_options_display_with_tabs',
]
]
);

$localisation->add_field(
Expand Down Expand Up @@ -117,7 +117,19 @@ function amnesty_register_localisation_options(): void {
/* translators: [admin] */
'upper-roman' => __( 'Uppercase Roman numerals.', 'amnesty' ),
],
]
]
);

// Add a checkbox for forcing thousands separator
$localisation->add_field(
[
/* translators: [admin] */
'name' => __( 'Force Thousands Separator', 'amnesty' ),
'id' => 'force_thousands_separator',
'type' => 'checkbox',
'default' => 0,
'desc' => __( 'Force the use of thousand separators for the Stat Counter block, for example for Spanish speaking countries "3.345"', 'amnesty' ),
]
);

do_action( 'amnesty_register_localisation_options', $localisation );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @return string
*/
function render_stat_counter_block( array $attributes ): string {
$options = get_option( 'amnesty_localisation_options_page' );

$attributes = wp_parse_args(
$attributes,
[
Expand All @@ -26,6 +28,10 @@ function render_stat_counter_block( array $attributes ): string {
$duration = $attributes['duration'];
$value = $attributes['value'];

if ( 'on' === $options['force_thousands_separator'] ) {
$value = number_format_i18n( $value );
}

$wrapper_attributes = get_block_wrapper_attributes(
[
'class' => $alignment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,26 @@ function amnesty_disable_cart_fragments() {
}

add_action( 'wp_enqueue_scripts', 'amnesty_disable_cart_fragments', 200 );

if ( ! function_exists( 'amnesty_force_thousands_separator_localisation' ) ) {
/**
* Localise the thousands separator option
*
* @package Amnesty\ThemeSetup
*
* @return void
*/
function amnesty_force_thousands_separator_localisation() {
$force_thousands_separator = get_option( 'amnesty_localisation_options_page' )['force_thousands_separator'] ?? 'off';

$data = [
'forceThousandSeparator' => amnesty_validate_boolish( $force_thousands_separator ),
];

wp_localize_script( 'amnesty-theme', 'amnestyForceThousandSeparator', $data );
wp_localize_script( 'amnesty-core-blocks-js', 'amnestyForceThousandSeparator', $data );
}
}

add_action( 'enqueue_block_editor_assets', 'amnesty_force_thousands_separator_localisation' );
add_action( 'wp_loaded', 'amnesty_force_thousands_separator_localisation' );