From 1a365ad0b7cbf24439d5b82eb8db6f49133e0c1a Mon Sep 17 00:00:00 2001 From: ren <18050944+renintw@users.noreply.github.com> Date: Mon, 9 Oct 2023 04:14:14 +0900 Subject: [PATCH 1/2] Query Filters: Add taxonomy term counts --- .../wporg-showcase-2022/inc/block-config.php | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php b/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php index 44d77f74..d58261d6 100644 --- a/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php +++ b/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php @@ -70,12 +70,19 @@ function ( $a, $b ) { _n( 'Popular tags %s', 'Popular tags %s', $count, 'wporg' ), $count ); + $option_labels = array_map( + function( $name, $count ) { + return $name . " ($count)"; + }, + wp_list_pluck( $tags, 'name' ), + wp_list_pluck( $tags, 'count' ) + ); return array( 'label' => $label, 'title' => __( 'Popular tags', 'wporg' ), 'key' => 'tag', 'action' => home_url( '/archives/' ), - 'options' => array_combine( wp_list_pluck( $tags, 'slug' ), wp_list_pluck( $tags, 'name' ) ), + 'options' => array_combine( wp_list_pluck( $tags, 'slug' ), $option_labels ), 'selected' => $selected, ); } @@ -101,12 +108,20 @@ function get_flavor_options( $options ) { _n( 'Flavors %s', 'Flavors %s', $count, 'wporg' ), $count ); + $option_labels = array_map( + function( $name, $count ) { + return $name . " ($count)"; + }, + wp_list_pluck( $flavors, 'name' ), + wp_list_pluck( $flavors, 'count' ) + ); + return array( 'label' => $label, 'title' => __( 'Flavors', 'wporg' ), 'key' => 'flavor', 'action' => home_url( '/archives/' ), - 'options' => array_combine( wp_list_pluck( $flavors, 'slug' ), wp_list_pluck( $flavors, 'name' ) ), + 'options' => array_combine( wp_list_pluck( $flavors, 'slug' ), $option_labels ), 'selected' => $selected, ); } @@ -147,12 +162,19 @@ function get_category_options( $options ) { _n( 'Categories %s', 'Categories %s', $count, 'wporg' ), $count ); + $option_labels = array_map( + function( $name, $count ) { + return $name . " ($count)"; + }, + wp_list_pluck( $categories, 'name' ), + wp_list_pluck( $categories, 'count' ) + ); return array( 'label' => $label, 'title' => __( 'Categories', 'wporg' ), 'key' => 'cat', 'action' => home_url( '/archives/' ), - 'options' => array_combine( wp_list_pluck( $categories, 'term_id' ), wp_list_pluck( $categories, 'name' ) ), + 'options' => array_combine( wp_list_pluck( $categories, 'term_id' ), $option_labels ), 'selected' => $selected, ); } From 468d05315ea3096cd25e30c1e9006403fc0169d2 Mon Sep 17 00:00:00 2001 From: ren <18050944+renintw@users.noreply.github.com> Date: Tue, 10 Oct 2023 12:30:38 +0900 Subject: [PATCH 2/2] Extract get_option_labels --- .../wporg-showcase-2022/inc/block-config.php | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php b/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php index d58261d6..7198b843 100644 --- a/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php +++ b/source/wp-content/themes/wporg-showcase-2022/inc/block-config.php @@ -70,13 +70,7 @@ function ( $a, $b ) { _n( 'Popular tags %s', 'Popular tags %s', $count, 'wporg' ), $count ); - $option_labels = array_map( - function( $name, $count ) { - return $name . " ($count)"; - }, - wp_list_pluck( $tags, 'name' ), - wp_list_pluck( $tags, 'count' ) - ); + $option_labels = get_option_labels( $tags ); return array( 'label' => $label, 'title' => __( 'Popular tags', 'wporg' ), @@ -108,14 +102,7 @@ function get_flavor_options( $options ) { _n( 'Flavors %s', 'Flavors %s', $count, 'wporg' ), $count ); - $option_labels = array_map( - function( $name, $count ) { - return $name . " ($count)"; - }, - wp_list_pluck( $flavors, 'name' ), - wp_list_pluck( $flavors, 'count' ) - ); - + $option_labels = get_option_labels( $flavors ); return array( 'label' => $label, 'title' => __( 'Flavors', 'wporg' ), @@ -162,13 +149,8 @@ function get_category_options( $options ) { _n( 'Categories %s', 'Categories %s', $count, 'wporg' ), $count ); - $option_labels = array_map( - function( $name, $count ) { - return $name . " ($count)"; - }, - wp_list_pluck( $categories, 'name' ), - wp_list_pluck( $categories, 'count' ) - ); + $option_labels = get_option_labels( $categories ); + return array( 'label' => $label, 'title' => __( 'Categories', 'wporg' ), @@ -421,3 +403,22 @@ function update_site_breadcrumbs( $breadcrumbs ) { return $breadcrumbs; } + +/** + * Get an array of option labels. + * Each label is a string formatted as 'name (count)'. e.g, Business (30). + * + * @param array $taxonomy An arrays with taxonomy terms. e.g., Categories = [ Business, Store, etc. ]. + * @return array An array of formatted option label strings. + */ +function get_option_labels( $taxonomy ) { + $option_labels = array_map( + function( $name, $count ) { + return $name . " ($count)"; + }, + wp_list_pluck( $taxonomy, 'name' ), + wp_list_pluck( $taxonomy, 'count' ) + ); + + return $option_labels; +}