1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<?php
namespace SIW\Elements;
class Taxonomy_Filter {
const ISOTOPE_VERSION = '3.0.6';
public function __construct() {
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_script' ] );
}
public function enqueue_script(){
wp_register_script( 'siw-taxonomy-filter', SIW_ASSETS_URL . 'js/elements/siw-taxonomy-filter.js', [], SIW_PLUGIN_VERSION, true );
wp_enqueue_script( 'siw-taxonomy-filter' );
}
public function generate( string $taxonomy ) {
$terms = $this->get_terms( $taxonomy );
$name = 'continent';
$output = sprintf( '<div class="filter-button-group" data-filter-group="%s">', $taxonomy );
$output .= '<h5>' . sprintf( esc_html__( 'Filter op %s', 'siw' ), $name ) . '</h5>';
$output .= sprintf ( '<button class="kad-btn is-checked" data-filter="">%s</button>', esc_html__( 'Alle', 'siw' ) );
foreach ( $terms as $term ) {
$output .= sprintf( '<button class="kad-btn" data-filter=".%s">%s</button>', esc_attr( $term->slug ), esc_html( $term->name ) );
}
$output .= '</div>';
return $output;
}
protected function get_terms( string $taxonomy ) {
$terms = get_terms( [
'taxonomy' => $taxonomy,
'hide_empty' => true,
] );
return $terms;
}
}