These are chips.
;
+const Edit = ( props: EditProps ): JSX.Element => {
+ const colorGradientSettings = useMultipleOriginColorsAndGradients();
+ const {
+ context,
+ clientId,
+ attributes,
+ setAttributes,
+ chipText,
+ setChipText,
+ chipBackground,
+ setChipBackground,
+ chipBorder,
+ setChipBorder,
+ selectedChipText,
+ setSelectedChipText,
+ selectedChipBackground,
+ setSelectedChipBackground,
+ selectedChipBorder,
+ setSelectedChipBorder,
+ } = props;
+ const {
+ customChipText,
+ customChipBackground,
+ customChipBorder,
+ customSelectedChipText,
+ customSelectedChipBackground,
+ customSelectedChipBorder,
+ } = attributes;
+ const { filterData } = context;
+ const { isLoading, items } = filterData;
+
+ const blockProps = useBlockProps( {
+ className: clsx( 'wc-block-product-filter-chips', {
+ 'is-loading': isLoading,
+ ...getColorClasses( attributes ),
+ } ),
+ style: getColorVars( attributes ),
+ } );
+
+ const loadingState = useMemo( () => {
+ return [ ...Array( 10 ) ].map( ( _, i ) => (
+
+
+ { isLoading && loadingState }
+ { ! isLoading &&
+ ( isLongList
+ ? items.slice( 0, threshold )
+ : items
+ ).map( ( item, index ) => (
+
+
+ { item.label }
+
+
+ ) ) }
+
+ { ! isLoading && isLongList && (
+
+ ) }
+
+ >
+
+
+
+
+
+
+ $show_initially ) : ?>
+
+
+
+ false,
'hasPageWithWordPressAdminBar' => false,
+ 'params' => $this->get_filter_query_params( 0 ),
+ 'originalParams' => $this->get_filter_query_params( 0 ),
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
)
);
$tags->set_attribute( 'data-wc-navigation-id', $this->generate_navigation_id( $block ) );
+ $tags->set_attribute( 'data-wc-watch', 'callbacks.maybeNavigate' );
if (
'always' === $attributes['overlay'] ||
@@ -171,4 +174,45 @@ class ProductFilters extends AbstractBlock {
md5( wp_json_encode( $block->parsed_block['innerBlocks'] ) )
);
}
+
+ /**
+ * Parse the filter parameters from the URL.
+ * For now we only get the global query params from the URL. In the future,
+ * we should get the query params based on $query_id.
+ *
+ * @param int $query_id Query ID.
+ * @return array Parsed filter params.
+ */
+ private function get_filter_query_params( $query_id ) {
+ // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
+ $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
+
+ $parsed_url = wp_parse_url( esc_url_raw( $request_uri ) );
+
+ if ( empty( $parsed_url['query'] ) ) {
+ return array();
+ }
+
+ parse_str( $parsed_url['query'], $url_query_params );
+
+ /**
+ * Filters the active filter data provided by filter blocks.
+ *
+ * @since 11.7.0
+ *
+ * @param array $filter_param_keys The active filters data
+ * @param array $url_param_keys The query param parsed from the URL.
+ *
+ * @return array Active filters params.
+ */
+ $filter_param_keys = array_unique( apply_filters( 'collection_filter_query_param_keys', array(), array_keys( $url_query_params ) ) );
+
+ return array_filter(
+ $url_query_params,
+ function ( $key ) use ( $filter_param_keys ) {
+ return in_array( $key, $filter_param_keys, true );
+ },
+ ARRAY_FILTER_USE_KEY
+ );
+ }
}