@@ -57,6 +57,64 @@ protected function extra_tablenav($which) {
5757 return ;
5858 }
5959
60+ $ available_post_types = [];
61+ $ post_types = function_exists ('get_post_types ' ) ? get_post_types (['public ' => true ]) : [];
62+ if (is_array ($ post_types )) {
63+ foreach ($ post_types as $ post_type ) {
64+ if (is_string ($ post_type ) && $ post_type !== '' ) {
65+ $ available_post_types [] = $ post_type ;
66+ }
67+ }
68+ }
69+
70+ $ selected_post_type = '' ;
71+ if (isset ($ _GET ['post_type ' ])) {
72+ $ candidate = sanitize_key (wp_unslash ($ _GET ['post_type ' ]));
73+ if ($ candidate !== '' ) {
74+ $ selected_post_type = $ candidate ;
75+ }
76+ }
77+
78+ if (!empty ($ available_post_types )) {
79+ echo '<div class="alignleft actions"> ' ;
80+ echo '<label class="screen-reader-text" for="blc-post-type-filter"> ' . esc_html__ ('Filtrer par type de contenu ' , 'liens-morts-detector-jlg ' ) . '</label> ' ;
81+
82+ $ select = '<select name="post_type" id="blc-post-type-filter" aria-label=" ' . esc_attr__ ('Filtrer par type de contenu ' , 'liens-morts-detector-jlg ' ) . '"> ' ;
83+ $ select .= '<option value=""> ' . esc_html__ ('Tous les types de contenu ' , 'liens-morts-detector-jlg ' ) . '</option> ' ;
84+
85+ foreach ($ available_post_types as $ post_type ) {
86+ $ label = $ post_type ;
87+ if (function_exists ('get_post_type_object ' )) {
88+ $ post_type_object = get_post_type_object ($ post_type );
89+ if (is_object ($ post_type_object )) {
90+ if (isset ($ post_type_object ->labels ) && is_object ($ post_type_object ->labels ) && !empty ($ post_type_object ->labels ->singular_name )) {
91+ $ label = (string ) $ post_type_object ->labels ->singular_name ;
92+ } elseif (!empty ($ post_type_object ->label )) {
93+ $ label = (string ) $ post_type_object ->label ;
94+ }
95+ }
96+ }
97+
98+ $ is_selected = ($ selected_post_type === $ post_type ) ? ' selected="selected" ' : '' ;
99+ $ select .= sprintf (
100+ '<option value="%1$s"%3$s>%2$s</option> ' ,
101+ esc_attr ($ post_type ),
102+ esc_html ($ label ),
103+ $ is_selected
104+ );
105+ }
106+
107+ $ select .= '</select> ' ;
108+ echo $ select ; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
109+
110+ printf (
111+ '<input type="submit" class="button" value="%s" /> ' ,
112+ esc_attr__ ('Filtrer ' , 'liens-morts-detector-jlg ' )
113+ );
114+
115+ echo '</div> ' ;
116+ }
117+
60118 echo $ this ->get_search_box (); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
61119 }
62120
@@ -349,6 +407,24 @@ public function prepare_items($data = null, $total_items_override = null) {
349407 $ current_page = max (1 , (int ) $ this ->get_pagenum ());
350408 $ search_term = $ this ->get_search_term ();
351409
410+ $ available_post_types = [];
411+ $ post_types = function_exists ('get_post_types ' ) ? get_post_types (['public ' => true ]) : [];
412+ if (is_array ($ post_types )) {
413+ foreach ($ post_types as $ post_type ) {
414+ if (is_string ($ post_type ) && $ post_type !== '' ) {
415+ $ available_post_types [] = $ post_type ;
416+ }
417+ }
418+ }
419+
420+ $ selected_post_type = '' ;
421+ if (isset ($ _GET ['post_type ' ])) {
422+ $ candidate = sanitize_key (wp_unslash ($ _GET ['post_type ' ]));
423+ if ($ candidate !== '' ) {
424+ $ selected_post_type = $ candidate ;
425+ }
426+ }
427+
352428 if (is_array ($ data )) {
353429 $ total_items = ($ total_items_override !== null ) ? (int ) $ total_items_override : count ($ data );
354430 $ this ->set_pagination_args (['total_items ' => $ total_items , 'per_page ' => $ per_page ]);
@@ -373,6 +449,11 @@ public function prepare_items($data = null, $total_items_override = null) {
373449 $ params = array_merge ($ params , [$ like , $ like , $ like ]);
374450 }
375451
452+ if ($ selected_post_type !== '' && in_array ($ selected_post_type , $ available_post_types , true )) {
453+ $ where [] = 'post_type = %s ' ;
454+ $ params [] = $ selected_post_type ;
455+ }
456+
376457 if ($ current_view === 'internal ' ) {
377458 $ where [] = '(is_internal = 1 OR (is_internal IS NULL AND ' . $ internal_sql . ')) ' ;
378459 $ params = array_merge ($ params , $ internal_params );
0 commit comments