@@ -14,7 +14,7 @@ public function boot( Service_Container $container ): void {
14
14
add_filter ( 'facetwp_load_a11y ' , '__return_true ' );
15
15
add_filter ( 'facetwp_facets ' , [ $ this , 'register_facets ' ], 40 );
16
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 );
17
+ add_filter ( 'facetwp_facet_pager_link ' , [ $ this , 'facetwp_facet_pager_link ' ], 10 , 2 );
18
18
}
19
19
20
20
/**
@@ -60,6 +60,8 @@ public function register_facets( array $facets ): array {
60
60
61
61
/**
62
62
* 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.
63
65
*
64
66
* @param $output
65
67
* @param $params
@@ -90,33 +92,37 @@ public function accessible_facetwp_pager_html( $output, $params ): string {
90
92
$ output .= '<span class="facetwp-pager-label sr-only"> ' . "$ text_page $ page $ text_of $ total_pages</span> " ;
91
93
92
94
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
+ );
94
99
} else {
95
100
$ output .= '<span class="facetwp-page previouspostslink" aria-hidden="true" tabindex="-1" style="visibility: hidden"></span> ' ;
96
101
}
97
102
98
103
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
+ ) ;
103
108
}
109
+
104
110
if ( 1 < ( $ page - $ step ) ) {
105
111
$ output .= '<span class="facetwp-page-more" aria-hidden="true">...</span> ' ;
106
112
}
107
113
108
114
for ( $ i = 2 ; $ i > 0 ; $ i -- ) {
109
115
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 > ' ;
111
117
}
112
118
}
113
119
114
120
// 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 > ' ;
116
122
117
123
for ( $ i = 1 ; $ i <= 2 ; $ i ++ ) {
118
124
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 > ' ;
120
126
}
121
127
}
122
128
@@ -125,14 +131,15 @@ public function accessible_facetwp_pager_html( $output, $params ): string {
125
131
}
126
132
127
133
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
+ );
132
139
}
133
140
134
141
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 > ' ;
136
143
} else {
137
144
$ output .= '<span class="facetwp-page nextpostslink" aria-hidden="true" style="visibility: hidden;" tabindex="-1"></span> ' ;
138
145
}
@@ -142,32 +149,33 @@ public function accessible_facetwp_pager_html( $output, $params ): string {
142
149
}
143
150
144
151
/**
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/
147
157
*
148
- * @param string $html
149
- * @param array $args
158
+ * @param $output
159
+ * @param $params
150
160
*
151
161
* @return string
162
+ *
163
+ * @author Marie Comet
164
+ *
152
165
*/
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
+ }
164
172
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 );
166
175
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 );
171
179
}
172
180
173
181
return $ html ;
0 commit comments