From 8ac1dc1ce56181efc5a2dd32fc24db9d85f64eda Mon Sep 17 00:00:00 2001 From: Marie Comet Date: Wed, 29 May 2024 16:52:43 +0200 Subject: [PATCH] FacetWP Service : add customization to facet pager links // remove custom labels --- inc/Services/Facet_WP.php | 76 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/inc/Services/Facet_WP.php b/inc/Services/Facet_WP.php index a79b6f31..9180e424 100644 --- a/inc/Services/Facet_WP.php +++ b/inc/Services/Facet_WP.php @@ -14,7 +14,7 @@ public function boot( Service_Container $container ): void { add_filter( 'facetwp_load_a11y', '__return_true' ); add_filter( 'facetwp_facets', [ $this, 'register_facets' ], 40 ); add_filter( 'facetwp_pager_html', [ $this, 'accessible_facetwp_pager_html' ], 10, 2 ); - add_filter( 'facetwp_facet_html', [ $this, 'accessible_facetwp_labels' ], 10, 2 ); + add_filter( 'facetwp_facet_pager_link', [ $this, 'facetwp_facet_pager_link' ], 10, 2 ); } /** @@ -60,6 +60,8 @@ public function register_facets( array $facets ): array { /** * Add custom and accessible FacetWP pagination. + * This function apply only with the old way to display a pager : facetwp_display( 'pager' ); + * For customization of the pagination facet, see facetwp_facet_pager_link function. * * @param $output * @param $params @@ -90,33 +92,37 @@ public function accessible_facetwp_pager_html( $output, $params ): string { $output .= '' . "$text_page $page $text_of $total_pages"; if ( $page > 1 ) { - $output .= ''; + $output .= sprintf( + '', + ( $page - 1 ) + ); } else { $output .= ''; } if ( 3 < $page ) { - $output .= ' - Première page - - '; + $output .= sprintf( + '', + __( 'First page', 'framework-textdomain' ) + ); } + if ( 1 < ( $page - $step ) ) { $output .= ''; } for ( $i = 2; $i > 0; $i -- ) { if ( 0 < ( $page - $i ) ) { - $output .= '' . __( 'Page', 'framework-textdomain' ) . ' ' . ( $page - $i ) . ''; + $output .= ''; } } // Current page - $output .= '' . __( 'Current page', 'framework-textdomain' ) . ' ' . $page . ''; + $output .= '' . __( 'Current page', 'framework-textdomain' ) . ' ' . $page . ''; for ( $i = 1; $i <= 2; $i ++ ) { if ( $total_pages >= ( $page + $i ) ) { - $output .= '' . __( 'Page', 'framework-textdomain' ) . ' ' . ( $page + $i ) . ''; + $output .= ''; } } @@ -125,14 +131,15 @@ public function accessible_facetwp_pager_html( $output, $params ): string { } if ( $total_pages > ( $page + 2 ) ) { - $output .= ' - ' . __( 'Last page', 'framework-textdomain' ) . ' - - '; + $output .= sprintf( + '', + $total_pages, + __( 'Last page', 'framework-textdomain' ) + ); } if ( $page < $total_pages && $total_pages > 1 ) { - $output .= ''; + $output .= ''; } else { $output .= ''; } @@ -142,32 +149,33 @@ public function accessible_facetwp_pager_html( $output, $params ): string { } /** - * Fix Labels for supported facets. - * Put in show_label_not_empty the facets that only need to be visible in they have results. + * Customize pagination facet output + * This function apply only on pagination facets : facetwp_display( 'facet', 'pagination' ); + * For customization of the old way to display a pager, see accessible_facetwp_pager_html function. + * + * https://facetwp.com/help-center/developers/hooks/output-hooks/facetwp_facet_pager_link/ * - * @param string $html - * @param array $args + * @param $output + * @param $params * * @return string + * + * @author Marie Comet + * */ - 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 ) ) { - $label = $args['facet']['label']; - if ( function_exists( 'facetwp_i18n' ) ) { - $label = facetwp_i18n( $label ); - } + public function facetwp_facet_pager_link( $html, $params ): string { + // Replace current link by a span + if ( str_contains( $html, 'active' ) ) { + $html = str_replace( '', '', $html ); + } - $html = sprintf( '%s', esc_attr( $args['facet']['name'] ), esc_html( $label ), $html ); + // Replace links by buttons and add class + $html = str_replace( [ '' ], $html ); - // Add id attribute to per_page select - if ( 'per_page' === $args['facet']['name'] ) { - $html = str_replace( '', $html ); - } + // Remove link tag for dots + if ( 'dots' === $params['extra_class'] ) { + $html = str_replace( '', '', $html ); } return $html;