From d0b78be46b506a62de0d6f2e9078f9e7003acbd1 Mon Sep 17 00:00:00 2001 From: Egidio Corica Date: Mon, 18 Sep 2023 15:13:23 +0200 Subject: [PATCH] Add FacetWP services --- inc/Framework.php | 2 + inc/Services/Facet_WP.php | 165 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 inc/Services/Facet_WP.php diff --git a/inc/Framework.php b/inc/Framework.php index 95267cc0..810d3c06 100644 --- a/inc/Framework.php +++ b/inc/Framework.php @@ -4,6 +4,7 @@ use BEA\Theme\Framework\Services\Acf; use BEA\Theme\Framework\Services\Assets; +use BEA\Theme\Framework\Services\Facet_WP; use BEA\Theme\Framework\Services\Performance; use BEA\Theme\Framework\Services\Assets_JS_Async; use BEA\Theme\Framework\Services\Editor; @@ -40,6 +41,7 @@ class Framework { Svg::class, Acf::class, Menu::class, + Facet_WP::class, // Services as Tools Body_Class::class, diff --git a/inc/Services/Facet_WP.php b/inc/Services/Facet_WP.php new file mode 100644 index 00000000..c7eeb287 --- /dev/null +++ b/inc/Services/Facet_WP.php @@ -0,0 +1,165 @@ + 1, + 'per_page' => 10, + 'total_rows' => 1, + ]; + $params = array_merge( $defaults, $params ); + $output = ''; + $page = (int) $params['page']; + $total_pages = (int) $params['total_pages']; + + // Only show pagination when > 1 page + if ( 1 < $total_pages ) { + + $text_page = esc_html__( 'Page', 'fwp-front' ); + $text_of = esc_html__( 'of', 'fwp-front' ); + $step = 10; + + $output .= '' . "$text_page $page $text_of $total_pages"; + + if ( $page > 1 ) { + $output .= ''; + } else { + $output .= ''; + } + + if ( 3 < $page ) { + $output .= ' + Première page + + '; + } + if ( 1 < ( $page - $step ) ) { + $output .= ''; + } + + for ( $i = 2; $i > 0; $i -- ) { + if ( 0 < ( $page - $i ) ) { + $output .= 'Page ' . ( $page - $i ) . ''; + } + } + + // Current page + $output .= 'Page courante ' . $page . ''; + + for ( $i = 1; $i <= 2; $i ++ ) { + if ( $total_pages >= ( $page + $i ) ) { + $output .= 'Page ' . ( $page + $i ) . ''; + } + } + + if ( $total_pages > ( $page + $step ) ) { + $output .= ''; + } + + if ( $total_pages > ( $page + 2 ) ) { + $output .= ' + Dernière page + + '; + } + + if ( $page < $total_pages && $total_pages > 1 ) { + $output .= ''; + } else { + $output .= ''; + } + } + + return $output; + } + + /** + * Fix Labels for supported facets. + * Put in show_label_not_empty the facets that only need to be visible in they have results. + * + * @param string $html + * @param array $args + * + * @return string + */ + public function accessible_facetwp_labels( string $html, array $args ): string { + $show_label_not_empty = [ + 'checkboxes', + 'radio', + ]; + + 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 ) ) { + $html = sprintf( '%s', esc_attr( $args['facet']['name'] ), esc_html( $args['facet']['label'] ), $html ); + } + + return $html; + } +}