class-show-custom-taxonomy-filters.php
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for Show Custom Taxonomy Filters module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class Show_Custom_Taxonomy_Filters { |
| 11 | // Excluded taxonomies |
| 12 | private $inapplicable_taxonomies = array( |
| 13 | 'category', |
| 14 | // Post Categories |
| 15 | 'product_cat', |
| 16 | // WooCommerce Product Categories |
| 17 | 'asenha_code_snippet_category', |
| 18 | // ASE Code Snippets Categories |
| 19 | 'asenha-media-category', |
| 20 | ); |
| 21 | |
| 22 | /** |
| 23 | * Show custom (hierarchical) taxonomy filter(s) for all post types. |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | public function show_custom_taxonomy_filters( $post_type ) { |
| 28 | $object_taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 29 | array_walk( $object_taxonomies, [$this, 'output_taxonomy_filter'] ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Output filter on the post type's list table for a taxonomy |
| 34 | * |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | public function output_taxonomy_filter( $taxonomy ) { |
| 38 | // Show filter if taxonomy is hierarchical |
| 39 | if ( true === $taxonomy->hierarchical && !in_array( $taxonomy->name, $this->inapplicable_taxonomies ) ) { |
| 40 | $this->render_additional_filter( $taxonomy ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Render additional filter |
| 46 | * |
| 47 | * @since 6.9.7 |
| 48 | */ |
| 49 | public function render_additional_filter( $taxonomy ) { |
| 50 | $show_option_all_label = sprintf( 'All %s', ucwords( $taxonomy->label ) ); |
| 51 | if ( property_exists( $taxonomy, 'labels' ) ) { |
| 52 | $taxonomy_labels = $taxonomy->labels; |
| 53 | if ( property_exists( $taxonomy_labels, 'all_items' ) && !empty( $taxonomy_labels->all_items ) ) { |
| 54 | $show_option_all_label = $taxonomy->labels->all_items; |
| 55 | } |
| 56 | } |
| 57 | wp_dropdown_categories( array( |
| 58 | 'show_option_all' => $show_option_all_label, |
| 59 | 'orderby' => 'name', |
| 60 | 'order' => 'ASC', |
| 61 | 'hide_empty' => false, |
| 62 | 'hide_if_empty' => true, |
| 63 | 'selected' => sanitize_text_field( ( isset( $_GET[$taxonomy->query_var] ) && !empty( $_GET[$taxonomy->query_var] ) ? sanitize_text_field( $_GET[$taxonomy->query_var] ) : '' ) ), |
| 64 | 'hierarchical' => true, |
| 65 | 'name' => $taxonomy->query_var, |
| 66 | 'taxonomy' => $taxonomy->name, |
| 67 | 'value_field' => 'slug', |
| 68 | ) ); |
| 69 | } |
| 70 | |
| 71 | } |
| 72 |