|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BEA\Theme\Framework\Services; |
| 4 | + |
| 5 | +use BEA\Theme\Framework\Service; |
| 6 | +use BEA\Theme\Framework\Service_Container; |
| 7 | + |
| 8 | +class Facet_WP implements Service { |
| 9 | + public function register( Service_Container $container ): void { |
| 10 | + } |
| 11 | + |
| 12 | + public function boot( Service_Container $container ): void { |
| 13 | + add_filter( 'facetwp_load_assets', '__return_true' ); |
| 14 | + add_filter( 'facetwp_load_a11y', '__return_true' ); |
| 15 | + add_filter( 'facetwp_facets', [ $this, 'register_facets' ], 40 ); |
| 16 | + add_filter( 'facetwp_pager_html', [ $this, 'accessible_facetwp_pager_html' ], 10, 2 ); |
| 17 | + add_filter( 'facetwp_facet_html', [ $this, 'accessible_facetwp_labels' ], 10, 2 ); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Get service name |
| 22 | + * |
| 23 | + * @return string |
| 24 | + */ |
| 25 | + public function get_service_name(): string { |
| 26 | + return 'facetwp'; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Register facets from config file. |
| 31 | + * |
| 32 | + * @param array $facets |
| 33 | + * |
| 34 | + * @return array |
| 35 | + * @author Egidio CORICA |
| 36 | + */ |
| 37 | + public function register_facets( array $facets ): array { |
| 38 | + |
| 39 | + $filename = get_theme_file_path( 'assets/facetwp/config.json' ); |
| 40 | + if ( ! is_readable( $filename ) ) { |
| 41 | + return $facets; |
| 42 | + } |
| 43 | + |
| 44 | + $facet_content = file_get_contents( $filename ); // phpcs:ignore |
| 45 | + if ( empty( $facet_content ) ) { |
| 46 | + return $facets; |
| 47 | + } |
| 48 | + |
| 49 | + $facet_content = \json_decode( $facet_content, true ); |
| 50 | + if ( |
| 51 | + JSON_ERROR_NONE !== \json_last_error() |
| 52 | + || ! is_array( $facet_content ) |
| 53 | + || empty( $facet_content['facets'] ) |
| 54 | + ) { |
| 55 | + return $facets; |
| 56 | + } |
| 57 | + |
| 58 | + return wp_parse_args( $facet_content['facets'], $facets ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Add custom and accessible FacetWP pagination. |
| 63 | + * |
| 64 | + * @param $output |
| 65 | + * @param $params |
| 66 | + * |
| 67 | + * @return string |
| 68 | + * |
| 69 | + * @author Egidio CORICA |
| 70 | + * |
| 71 | + */ |
| 72 | + public function accessible_facetwp_pager_html( $output, $params ): string { |
| 73 | + $defaults = [ |
| 74 | + 'page' => 1, |
| 75 | + 'per_page' => 10, |
| 76 | + 'total_rows' => 1, |
| 77 | + ]; |
| 78 | + $params = array_merge( $defaults, $params ); |
| 79 | + $output = ''; |
| 80 | + $page = (int) $params['page']; |
| 81 | + $total_pages = (int) $params['total_pages']; |
| 82 | + |
| 83 | + // Only show pagination when > 1 page |
| 84 | + if ( 1 < $total_pages ) { |
| 85 | + |
| 86 | + $text_page = esc_html__( 'Page', 'fwp-front' ); |
| 87 | + $text_of = esc_html__( 'of', 'fwp-front' ); |
| 88 | + $step = 10; |
| 89 | + |
| 90 | + $output .= '<span class="facetwp-pager-label sr-only">' . "$text_page $page $text_of $total_pages</span>"; |
| 91 | + |
| 92 | + if ( $page > 1 ) { |
| 93 | + $output .= '<a href="#" class="facetwp-page previouspostslink" data-page="' . ( $page - 1 ) . '">Précédent</a>'; |
| 94 | + } else { |
| 95 | + $output .= '<span class="facetwp-page previouspostslink" aria-hidden="true" tabindex="-1" style="visibility: hidden"></span>'; |
| 96 | + } |
| 97 | + |
| 98 | + if ( 3 < $page ) { |
| 99 | + $output .= '<a href="#" class="facetwp-page first-page" data-page="1"> |
| 100 | + <span class="sr-only">Première page</span> |
| 101 | + <span aria-hidden="true">1</span> |
| 102 | + </a>'; |
| 103 | + } |
| 104 | + if ( 1 < ( $page - $step ) ) { |
| 105 | + $output .= '<span class="facetwp-page-more" aria-hidden="true">...</span>'; |
| 106 | + } |
| 107 | + |
| 108 | + for ( $i = 2; $i > 0; $i -- ) { |
| 109 | + if ( 0 < ( $page - $i ) ) { |
| 110 | + $output .= '<a href="#" class="facetwp-page" data-page="' . ( $page - $i ) . '"><span class="sr-only">Page</span> ' . ( $page - $i ) . '</a>'; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Current page |
| 115 | + $output .= '<a href="#" class="facetwp-page active" aria-current="true" data-page="' . $page . '"><span class="sr-only">Page courante</span> ' . $page . '</a>'; |
| 116 | + |
| 117 | + for ( $i = 1; $i <= 2; $i ++ ) { |
| 118 | + if ( $total_pages >= ( $page + $i ) ) { |
| 119 | + $output .= '<a href="#" class="facetwp-page" data-page="' . ( $page + $i ) . '"><span class="sr-only">Page</span> ' . ( $page + $i ) . '</a>'; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if ( $total_pages > ( $page + $step ) ) { |
| 124 | + $output .= '<span class="facetwp-page-more" aria-hidden="true">...</span>'; |
| 125 | + } |
| 126 | + |
| 127 | + if ( $total_pages > ( $page + 2 ) ) { |
| 128 | + $output .= '<a href="#" class="facetwp-page last-page" data-page="' . $total_pages . '"> |
| 129 | + <span class="sr-only">Dernière page</span> |
| 130 | + <span aria-hidden="true">' . $total_pages . '</span> |
| 131 | + </a>'; |
| 132 | + } |
| 133 | + |
| 134 | + if ( $page < $total_pages && $total_pages > 1 ) { |
| 135 | + $output .= '<a href="#" class="facetwp-page nextpostslink" data-page="' . ( $page + 1 ) . '">Suivant</a>'; |
| 136 | + } else { |
| 137 | + $output .= '<span class="facetwp-page nextpostslink" aria-hidden="true" style="visibility: hidden;" tabindex="-1"></span>'; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return $output; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Fix Labels for supported facets. |
| 146 | + * Put in show_label_not_empty the facets that only need to be visible in they have results. |
| 147 | + * |
| 148 | + * @param string $html |
| 149 | + * @param array $args |
| 150 | + * |
| 151 | + * @return string |
| 152 | + */ |
| 153 | + public function accessible_facetwp_labels( string $html, array $args ): string { |
| 154 | + $show_label_not_empty = [ |
| 155 | + 'checkboxes', |
| 156 | + 'radio', |
| 157 | + ]; |
| 158 | + |
| 159 | + if ( ( true === in_array( $args['facet']['type'], $show_label_not_empty, true ) && ! empty( $args['values'] ) ) || false === in_array( $args['facet']['type'], $show_label_not_empty, true ) ) { |
| 160 | + $html = sprintf( '<label class="facetwp-label" for="%s">%s</label>%s', esc_attr( $args['facet']['name'] ), esc_html( $args['facet']['label'] ), $html ); |
| 161 | + } |
| 162 | + |
| 163 | + return $html; |
| 164 | + } |
| 165 | +} |
0 commit comments