Skip to content

Commit

Permalink
Pass additional info to some filters
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Aug 24, 2022
1 parent 2626fb4 commit f9ae942
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
18 changes: 11 additions & 7 deletions includes/classes/Feature/Facets/Types/Meta/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class Block {
public function setup() {
add_action( 'init', [ $this, 'register_block' ] );
add_action( 'rest_api_init', [ $this, 'setup_endpoints' ] );

/** This filter is documented in includes/classes/Feature/Facets/Types/Taxonomy/Block.php */
$renderer_class = apply_filters( 'ep_facet_renderer_class', __NAMESPACE__ . '\Renderer', 'meta', 'block' );

$this->renderer = new $renderer_class();
}

/**
Expand Down Expand Up @@ -119,10 +114,15 @@ public function register_block() {
*/
public function render_block( $attributes ) {
$attributes = $this->parse_attributes( $attributes );

/** This filter is documented in includes/classes/Feature/Facets/Types/Taxonomy/Block.php */
$renderer_class = apply_filters( 'ep_facet_renderer_class', __NAMESPACE__ . '\Renderer', 'meta', 'block', $attributes );
$renderer = new $renderer_class();

ob_start();
?>
<div class="wp-block-elasticpress-facet">
<?php $this->renderer->render( [], $attributes ); ?>
<?php $renderer->render( [], $attributes ); ?>
</div>
<?php
return ob_get_clean();
Expand Down Expand Up @@ -165,8 +165,12 @@ function ( $meta_fields ) use ( $attributes ) {
]
);

/** This filter is documented in includes/classes/Feature/Facets/Types/Taxonomy/Block.php */
$renderer_class = apply_filters( 'ep_facet_renderer_class', __NAMESPACE__ . '\Renderer', 'meta', 'block', $attributes );
$renderer = new $renderer_class();

ob_start();
$this->renderer->render( [], $attributes );
$renderer->render( [], $attributes );
$block_content = ob_get_clean();

if ( empty( $block_content ) ) {
Expand Down
41 changes: 21 additions & 20 deletions includes/classes/Feature/Facets/Types/Meta/FacetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,28 @@ public function get_filter_type() : string {
public function set_wp_query_aggs( $facet_aggs ) {
$facets_meta_fields = $this->get_facets_meta_fields();

/**
* Retrieve aggregations based on a custom field. This field must exist on the mapping.
* Values available out-of-the-box are:
* - raw (default)
* - long
* - double
* - boolean
* - date
* - datetime
* - time
*
* `meta.<field>.value` is *not* available, as that throws a `Fielddata is disabled on text fields by default` error.
*
* @since 4.3.0
* @hook ep_facet_meta_use_field
* @param {string} $field The Elasticsearch field to use for this meta field
* @return {string} The chosen ES field
*/
$facet_field = apply_filters( 'ep_facet_meta_use_field', 'raw' );

foreach ( $facets_meta_fields as $meta_field ) {
/**
* Retrieve aggregations based on a custom field. This field must exist on the mapping.
* Values available out-of-the-box are:
* - raw (default)
* - long
* - double
* - boolean
* - date
* - datetime
* - time
*
* `meta.<field>.value` is *not* available, as that throws a `Fielddata is disabled on text fields by default` error.
*
* @since 4.3.0
* @hook ep_facet_meta_use_field
* @param {string} $es_field The Elasticsearch field to use for this meta field
* @param {string} $meta_field The meta field key
* @return {string} The chosen ES field
*/
$facet_field = apply_filters( 'ep_facet_meta_use_field', 'raw', $meta_field );

$facet_aggs[ $this->get_filter_name() . $meta_field ] = array(
'terms' => array(
/**
Expand Down
14 changes: 8 additions & 6 deletions includes/classes/Feature/Facets/Types/Meta/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ public function render( $args, $instance ) {
*
* @hook ep_facet_meta_values_with_count
* @since 4.3.0
* @param {array} $values Values with names and counts.
* @param {array} $values Values with names and counts
* @param {string} $meta_field Meta field
* @param {array} $instance Block info
* @return {array} New values
*/
$values = apply_filters( 'ep_facet_meta_all_values', $values, $this->meta_field );
$values = apply_filters( 'ep_facet_meta_all_values', $values, $this->meta_field, $instance );

echo wp_kses_post( $args['before_widget'] );

Expand All @@ -121,12 +122,13 @@ public function render( $args, $instance ) {
* Filter facet search threshold
*
* @hook ep_facet_search_threshold
* @param {int} $search_threshold Search threshold
* @param {string} $taxonomy Current taxonomy
* @param {string} $context Hint about where the value will be used
* @param {int} $search_threshold Search threshold
* @param {string} $taxonomy Current taxonomy
* @param {string} $context Hint about where the value will be used
* @param {array} $instance Block instance
* @return {int} New threshold
*/
$search_threshold = apply_filters( 'ep_facet_search_threshold', 15, $this->meta_field, 'meta' );
$search_threshold = apply_filters( 'ep_facet_search_threshold', 15, $this->meta_field, 'meta', $instance );
?>
<div class="terms <?php if ( count( $values ) > $search_threshold ) : ?>searchable<?php endif; ?>">
<?php if ( count( $values ) > $search_threshold ) : ?>
Expand Down

0 comments on commit f9ae942

Please sign in to comment.