From fbb7f3363bf1400bdb73a9b2ea374c35ad66fbc4 Mon Sep 17 00:00:00 2001 From: Gentyspun Date: Thu, 17 Oct 2024 10:22:46 +0100 Subject: [PATCH 1/4] Adds an option to the localisation theme options for forcing thousand separators on counter block --- .../includes/admin/theme-options/localisation.php | 15 +++++++++++++-- .../includes/blocks/stat-counter/render.php | 12 +++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php b/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php index 665cfa10..bee09dcf 100644 --- a/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php +++ b/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php @@ -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( @@ -117,7 +117,18 @@ 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, + ] ); do_action( 'amnesty_register_localisation_options', $localisation ); diff --git a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php index 9f3d8ae7..07888e6e 100644 --- a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php +++ b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php @@ -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, [ @@ -26,6 +28,14 @@ function render_stat_counter_block( array $attributes ): string { $duration = $attributes['duration']; $value = $attributes['value']; + if ( $options['force_thousands_separator'] === 'on') { + $value = number_format( intval($value) ); + + echo '
';
+			var_dump($value);
+			echo '
'; + } + $wrapper_attributes = get_block_wrapper_attributes( [ 'class' => $alignment, @@ -37,7 +47,7 @@ function render_stat_counter_block( array $attributes ): string { wp_kses_data( $wrapper_attributes ), esc_attr( $duration ), esc_attr( $value ), - wp_kses_post( $value ) + $value ); } } From eef1f2109f3299e8ec75130fe2813253b2acdd3f Mon Sep 17 00:00:00 2001 From: Gentyspun Date: Fri, 18 Oct 2024 16:31:20 +0100 Subject: [PATCH 2/4] Localises the localistion checkbox for use in JS, alters the render and module for stat counter block --- private/src/scripts/modules/counter.js | 13 +++++++++- .../styles/admin/_localisation-options.scss | 4 ++++ .../admin/theme-options/localisation.php | 1 + .../includes/blocks/stat-counter/render.php | 10 +++----- .../theme-setup/scripts-and-styles.php | 24 +++++++++++++++++++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/private/src/scripts/modules/counter.js b/private/src/scripts/modules/counter.js index ef2105e5..a5a51283 100644 --- a/private/src/scripts/modules/counter.js +++ b/private/src/scripts/modules/counter.js @@ -19,7 +19,18 @@ const toFormattedString = (value) => { } const { currentLocale = 'en-GB' } = window.amnestyCoreI18n; - const formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-')); + + const { forceTS } = window.amnestyForceTS; + + let formatted = ''; + + if (forceTS === 'on') { + formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-'), { + useGrouping: true, + }); + } else { + formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-')); + } return formatted; }; diff --git a/private/src/styles/admin/_localisation-options.scss b/private/src/styles/admin/_localisation-options.scss index e6e2bda8..66ee51d5 100644 --- a/private/src/styles/admin/_localisation-options.scss +++ b/private/src/styles/admin/_localisation-options.scss @@ -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; +} diff --git a/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php b/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php index bee09dcf..11cf7909 100644 --- a/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php +++ b/wp-content/themes/humanity-theme/includes/admin/theme-options/localisation.php @@ -128,6 +128,7 @@ function amnesty_register_localisation_options(): void { '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' ), ] ); diff --git a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php index 07888e6e..5efe5c8d 100644 --- a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php +++ b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php @@ -28,12 +28,8 @@ function render_stat_counter_block( array $attributes ): string { $duration = $attributes['duration']; $value = $attributes['value']; - if ( $options['force_thousands_separator'] === 'on') { - $value = number_format( intval($value) ); - - echo '
';
-			var_dump($value);
-			echo '
'; + if ( $options['force_thousands_separator'] === 'on' ) { + $value = number_format_i18n( $value ); } $wrapper_attributes = get_block_wrapper_attributes( @@ -47,7 +43,7 @@ function render_stat_counter_block( array $attributes ): string { wp_kses_data( $wrapper_attributes ), esc_attr( $duration ), esc_attr( $value ), - $value + wp_kses_post( $value ) ); } } diff --git a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php index 1c66333b..896e4986 100644 --- a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php +++ b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php @@ -347,3 +347,27 @@ 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 = [ + 'forceTS' => $force_thousands_separator, + ]; + + wp_localize_script( 'amnesty-theme', 'amnestyForceTS', $data ); + wp_localize_script( 'amnesty-core-blocks-js', 'amnestyForceTS', $data ); + } +} + +add_action( 'enqueue_block_editor_assets', 'amnesty_force_thousands_separator_localisation' ); +add_action( 'wp_loaded', 'amnesty_force_thousands_separator_localisation' ); From b68380646174a89089706b43574f1dec9410ee2c Mon Sep 17 00:00:00 2001 From: Gentyspun Date: Fri, 18 Oct 2024 16:42:56 +0100 Subject: [PATCH 3/4] Fix build errors --- .../humanity-theme/includes/blocks/stat-counter/render.php | 2 +- .../humanity-theme/includes/theme-setup/scripts-and-styles.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php index 5efe5c8d..2aea2315 100644 --- a/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php +++ b/wp-content/themes/humanity-theme/includes/blocks/stat-counter/render.php @@ -28,7 +28,7 @@ function render_stat_counter_block( array $attributes ): string { $duration = $attributes['duration']; $value = $attributes['value']; - if ( $options['force_thousands_separator'] === 'on' ) { + if ( 'on' === $options['force_thousands_separator'] ) { $value = number_format_i18n( $value ); } diff --git a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php index 896e4986..6fef92fc 100644 --- a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php +++ b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php @@ -357,7 +357,6 @@ function amnesty_disable_cart_fragments() { * @return void */ function amnesty_force_thousands_separator_localisation() { - $force_thousands_separator = get_option( 'amnesty_localisation_options_page' )['force_thousands_separator'] ?? 'off'; $data = [ From 5d95f52ee823d76ac4344a9d2cdb86fdda8a676b Mon Sep 17 00:00:00 2001 From: Gentyspun Date: Thu, 5 Dec 2024 13:54:21 +0000 Subject: [PATCH 4/4] Addresses PR feedback --- private/src/scripts/modules/counter.js | 14 ++++++-------- .../includes/theme-setup/scripts-and-styles.php | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/private/src/scripts/modules/counter.js b/private/src/scripts/modules/counter.js index a5a51283..96092c82 100644 --- a/private/src/scripts/modules/counter.js +++ b/private/src/scripts/modules/counter.js @@ -20,18 +20,16 @@ const toFormattedString = (value) => { const { currentLocale = 'en-GB' } = window.amnestyCoreI18n; - const { forceTS } = window.amnestyForceTS; + const { forceThousandSeparator } = window.amnestyForceThousandSeparator; - let formatted = ''; + let options = ''; - if (forceTS === 'on') { - formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-'), { - useGrouping: true, - }); - } else { - formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-')); + if (forceThousandSeparator) { + options = { useGrouping: true }; } + const formatted = toRawNumber(value).toLocaleString(currentLocale.replace('_', '-'), options); + return formatted; }; diff --git a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php index 6fef92fc..c8a7a03b 100644 --- a/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php +++ b/wp-content/themes/humanity-theme/includes/theme-setup/scripts-and-styles.php @@ -360,11 +360,11 @@ function amnesty_force_thousands_separator_localisation() { $force_thousands_separator = get_option( 'amnesty_localisation_options_page' )['force_thousands_separator'] ?? 'off'; $data = [ - 'forceTS' => $force_thousands_separator, + 'forceThousandSeparator' => amnesty_validate_boolish( $force_thousands_separator ), ]; - wp_localize_script( 'amnesty-theme', 'amnestyForceTS', $data ); - wp_localize_script( 'amnesty-core-blocks-js', 'amnestyForceTS', $data ); + wp_localize_script( 'amnesty-theme', 'amnestyForceThousandSeparator', $data ); + wp_localize_script( 'amnesty-core-blocks-js', 'amnestyForceThousandSeparator', $data ); } }