Skip to content

Commit 8ac1dc1

Browse files
committed
FacetWP Service : add customization to facet pager links // remove custom labels
1 parent 448ce7a commit 8ac1dc1

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

Diff for: inc/Services/Facet_WP.php

+42-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function boot( Service_Container $container ): void {
1414
add_filter( 'facetwp_load_a11y', '__return_true' );
1515
add_filter( 'facetwp_facets', [ $this, 'register_facets' ], 40 );
1616
add_filter( 'facetwp_pager_html', [ $this, 'accessible_facetwp_pager_html' ], 10, 2 );
17-
add_filter( 'facetwp_facet_html', [ $this, 'accessible_facetwp_labels' ], 10, 2 );
17+
add_filter( 'facetwp_facet_pager_link', [ $this, 'facetwp_facet_pager_link' ], 10, 2 );
1818
}
1919

2020
/**
@@ -60,6 +60,8 @@ public function register_facets( array $facets ): array {
6060

6161
/**
6262
* Add custom and accessible FacetWP pagination.
63+
* This function apply only with the old way to display a pager : facetwp_display( 'pager' );
64+
* For customization of the pagination facet, see facetwp_facet_pager_link function.
6365
*
6466
* @param $output
6567
* @param $params
@@ -90,33 +92,37 @@ public function accessible_facetwp_pager_html( $output, $params ): string {
9092
$output .= '<span class="facetwp-pager-label sr-only">' . "$text_page $page $text_of $total_pages</span>";
9193

9294
if ( $page > 1 ) {
93-
$output .= '<a href="#" class="facetwp-page previouspostslink" data-page="' . ( $page - 1 ) . '">' . __( 'Previous', 'framework-textdomain' ) . '</a>';
95+
$output .= sprintf(
96+
'<button class="btn facetwp-page previouspostslink" data-page="%s">' . __( 'Previous', 'framework-textdomain' ) . '</button>',
97+
( $page - 1 )
98+
);
9499
} else {
95100
$output .= '<span class="facetwp-page previouspostslink" aria-hidden="true" tabindex="-1" style="visibility: hidden"></span>';
96101
}
97102

98103
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>';
104+
$output .= sprintf(
105+
'<button class="btn facetwp-page first-page" data-page="1"><span class="sr-only">%s</span><span aria-hidden="true">1</span></button>',
106+
__( 'First page', 'framework-textdomain' )
107+
);
103108
}
109+
104110
if ( 1 < ( $page - $step ) ) {
105111
$output .= '<span class="facetwp-page-more" aria-hidden="true">...</span>';
106112
}
107113

108114
for ( $i = 2; $i > 0; $i -- ) {
109115
if ( 0 < ( $page - $i ) ) {
110-
$output .= '<a href="#" class="facetwp-page" data-page="' . ( $page - $i ) . '"><span class="sr-only">' . __( 'Page', 'framework-textdomain' ) . '</span> ' . ( $page - $i ) . '</a>';
116+
$output .= '<button class="btn facetwp-page" data-page="' . ( $page - $i ) . '"><span class="sr-only">' . __( 'Page', 'framework-textdomain' ) . '</span> ' . ( $page - $i ) . '</button>';
111117
}
112118
}
113119

114120
// Current page
115-
$output .= '<a href="#" class="facetwp-page active" aria-current="true" data-page="' . $page . '"><span class="sr-only">' . __( 'Current page', 'framework-textdomain' ) . '</span> ' . $page . '</a>';
121+
$output .= '<span class="facetwp-page active" aria-current="true" data-page="' . $page . '"><span class="sr-only">' . __( 'Current page', 'framework-textdomain' ) . '</span> ' . $page . '</span>';
116122

117123
for ( $i = 1; $i <= 2; $i ++ ) {
118124
if ( $total_pages >= ( $page + $i ) ) {
119-
$output .= '<a href="#" class="facetwp-page" data-page="' . ( $page + $i ) . '"><span class="sr-only">' . __( 'Page', 'framework-textdomain' ) . '</span> ' . ( $page + $i ) . '</a>';
125+
$output .= '<button class="btn facetwp-page" data-page="' . ( $page + $i ) . '"><span class="sr-only">' . __( 'Page', 'framework-textdomain' ) . '</span> ' . ( $page + $i ) . '</button>';
120126
}
121127
}
122128

@@ -125,14 +131,15 @@ public function accessible_facetwp_pager_html( $output, $params ): string {
125131
}
126132

127133
if ( $total_pages > ( $page + 2 ) ) {
128-
$output .= '<a href="#" class="facetwp-page last-page" data-page="' . $total_pages . '">
129-
<span class="sr-only">' . __( 'Last page', 'framework-textdomain' ) . '</span>
130-
<span aria-hidden="true">' . $total_pages . '</span>
131-
</a>';
134+
$output .= sprintf(
135+
'<button class="btn facetwp-page last-page" data-page="%1$s"><span class="sr-only">%2$s</span><span aria-hidden="true">%1$s</span></button>',
136+
$total_pages,
137+
__( 'Last page', 'framework-textdomain' )
138+
);
132139
}
133140

134141
if ( $page < $total_pages && $total_pages > 1 ) {
135-
$output .= '<a href="#" class="facetwp-page nextpostslink" data-page="' . ( $page + 1 ) . '">' . __( 'Next', 'framework-textdomain' ) . '</a>';
142+
$output .= '<button class="btn facetwp-page nextpostslink" data-page="' . ( $page + 1 ) . '">' . __( 'Next', 'framework-textdomain' ) . '</button>';
136143
} else {
137144
$output .= '<span class="facetwp-page nextpostslink" aria-hidden="true" style="visibility: hidden;" tabindex="-1"></span>';
138145
}
@@ -142,32 +149,33 @@ public function accessible_facetwp_pager_html( $output, $params ): string {
142149
}
143150

144151
/**
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.
152+
* Customize pagination facet output
153+
* This function apply only on pagination facets : facetwp_display( 'facet', 'pagination' );
154+
* For customization of the old way to display a pager, see accessible_facetwp_pager_html function.
155+
*
156+
* https://facetwp.com/help-center/developers/hooks/output-hooks/facetwp_facet_pager_link/
147157
*
148-
* @param string $html
149-
* @param array $args
158+
* @param $output
159+
* @param $params
150160
*
151161
* @return string
162+
*
163+
* @author Marie Comet
164+
*
152165
*/
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-
$label = $args['facet']['label'];
161-
if ( function_exists( 'facetwp_i18n' ) ) {
162-
$label = facetwp_i18n( $label );
163-
}
166+
public function facetwp_facet_pager_link( $html, $params ): string {
167+
// Replace current link by a span
168+
if ( str_contains( $html, 'active' ) ) {
169+
$html = str_replace( '<a', '<span', $html );
170+
$html = str_replace( '</a>', '</span>', $html );
171+
}
164172

165-
$html = sprintf( '<label class="facetwp-label" for="%s">%s</label>%s', esc_attr( $args['facet']['name'] ), esc_html( $label ), $html );
173+
// Replace links by buttons and add class
174+
$html = str_replace( [ '<a class="', '/a>' ], [ '<button class="btn ', '/button>' ], $html );
166175

167-
// Add id attribute to per_page select
168-
if ( 'per_page' === $args['facet']['name'] ) {
169-
$html = str_replace( '<select class="facetwp-per-page-select">', '<select class="facetwp-per-page-select" id="per_page">', $html );
170-
}
176+
// Remove link tag for dots
177+
if ( 'dots' === $params['extra_class'] ) {
178+
$html = str_replace( '<a class="facetwp-page dots">…</a>', '<span class="facetwp-page dots">…</span>', $html );
171179
}
172180

173181
return $html;

0 commit comments

Comments
 (0)