Recherche : chercher le texte saisi, pas sa version échappée pour le HTML
get_search_query() renvoie un texte échappé : `l'Oulipo` devenait `l'Oulipo` en base. Les noms contenant une apostrophe, un guillemet ou une esperluette n'étaient pas trouvés (programmes, axes, membres), et une expression entre guillemets ne fonctionnait pas en première page d'annonces. La version échappée ne sert plus qu'à l'affichage, les liens de filtre encodent le texte. La recherche de termes et de membres essaie aussi les deux formes d'apostrophe et les deux formes enregistrées de l'esperluette (`&`, `&`).
This commit is contained in:
@@ -21,6 +21,26 @@ function thalim_archive_excluded_cat_ids( bool $exclude_seances = true ): array
|
|||||||
return array_values( array_filter( $ids ) );
|
return array_values( array_filter( $ids ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formes à chercher en base pour un texte saisi : la comparaison SQL ne confond
|
||||||
|
* ni l'apostrophe droite et la courbe, ni « & » et sa forme enregistrée « & »
|
||||||
|
* (l'admin enregistre « & », les anciens imports ont gardé « & »).
|
||||||
|
*
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
function thalim_search_text_variants( string $text ): array {
|
||||||
|
$variants = [ $text ];
|
||||||
|
if ( strpos( $text, "'" ) !== false || strpos( $text, '’' ) !== false ) {
|
||||||
|
$variants = [ str_replace( '’', "'", $text ), str_replace( "'", '’', $text ) ];
|
||||||
|
}
|
||||||
|
if ( strpos( $text, '&' ) !== false ) {
|
||||||
|
foreach ( $variants as $variant ) {
|
||||||
|
$variants[] = str_replace( '&', '&', $variant );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array_values( array_unique( $variants ) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lit et assainit les filtres communs passés en GET sur les archives.
|
* Lit et assainit les filtres communs passés en GET sur les archives.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+55
-43
@@ -4,7 +4,8 @@ $context = Timber::context();
|
|||||||
// Les séances de séminaire sont incluses : post-card-helpers réécrit leur
|
// Les séances de séminaire sont incluses : post-card-helpers réécrit leur
|
||||||
// lien vers le séminaire parent + ancre #seance-{ID}.
|
// lien vers le séminaire parent + ancre #seance-{ID}.
|
||||||
$excluded_cat_ids = thalim_archive_excluded_cat_ids( false ); // Non classé (+ Vie du labo non connectés)
|
$excluded_cat_ids = thalim_archive_excluded_cat_ids( false ); // Non classé (+ Vie du labo non connectés)
|
||||||
$search_query = get_search_query();
|
$search_query = get_search_query(); // échappé : affichage uniquement (autoescape Twig désactivé)
|
||||||
|
$search_raw = get_search_query( false ); // texte tapé : requêtes et URLs
|
||||||
|
|
||||||
// Read filter query params
|
// Read filter query params
|
||||||
$f = thalim_archive_read_filters();
|
$f = thalim_archive_read_filters();
|
||||||
@@ -30,11 +31,11 @@ $context['active_rubrique'] = $active_rubrique_id;
|
|||||||
$search_base = thalim_en_url( home_url('/') );
|
$search_base = thalim_en_url( home_url('/') );
|
||||||
|
|
||||||
// Override annonces_url: rubrique reset stays on search page (no filter_cat)
|
// Override annonces_url: rubrique reset stays on search page (no filter_cat)
|
||||||
$context['annonces_url'] = add_query_arg(['s' => $search_query], $search_base);
|
$context['annonces_url'] = add_query_arg(['s' => urlencode($search_raw)], $search_base);
|
||||||
|
|
||||||
// Base params preserved across filter links (preserves search term)
|
// Base params preserved across filter links (preserves search term)
|
||||||
$base_filter_params = array_filter([
|
$base_filter_params = array_filter([
|
||||||
's' => $search_query,
|
's' => urlencode($search_raw),
|
||||||
'axe' => $active_axe ?: null,
|
'axe' => $active_axe ?: null,
|
||||||
'date_from' => $active_date_from ?: null,
|
'date_from' => $active_date_from ?: null,
|
||||||
'date_to' => $active_date_to ?: null,
|
'date_to' => $active_date_to ?: null,
|
||||||
@@ -61,7 +62,7 @@ if ($active_cat_id) {
|
|||||||
|
|
||||||
$query_args = [
|
$query_args = [
|
||||||
'post_type' => 'post',
|
'post_type' => 'post',
|
||||||
's' => $search_query,
|
's' => $search_raw,
|
||||||
'relevanssi' => true,
|
'relevanssi' => true,
|
||||||
'posts_per_page' => 12,
|
'posts_per_page' => 12,
|
||||||
'orderby' => 'relevance',
|
'orderby' => 'relevance',
|
||||||
@@ -116,7 +117,6 @@ $author_cards = [];
|
|||||||
if ( $search_query ) {
|
if ( $search_query ) {
|
||||||
$excluded_role_ids = thalim_excluded_role_ids(); // « À ranger », « Archive » (résolus par slug)
|
$excluded_role_ids = thalim_excluded_role_ids(); // « À ranger », « Archive » (résolus par slug)
|
||||||
$user_query_args = [
|
$user_query_args = [
|
||||||
'search' => '*' . $search_query . '*',
|
|
||||||
'search_columns' => ['display_name'],
|
'search_columns' => ['display_name'],
|
||||||
'number' => 6,
|
'number' => 6,
|
||||||
'orderby' => 'display_name',
|
'orderby' => 'display_name',
|
||||||
@@ -131,7 +131,13 @@ if ( $search_query ) {
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$user_query = new WP_User_Query( $user_query_args );
|
$found_users = [];
|
||||||
|
foreach ( thalim_search_text_variants( $search_raw ) as $variant ) {
|
||||||
|
$user_query_args['search'] = '*' . $variant . '*';
|
||||||
|
foreach ( ( new WP_User_Query( $user_query_args ) )->get_results() as $found_user ) {
|
||||||
|
$found_users[ $found_user->ID ] = $found_user;
|
||||||
|
}
|
||||||
|
}
|
||||||
$lang = thalim_current_language();
|
$lang = thalim_current_language();
|
||||||
|
|
||||||
// Direction IDs (same source as membres page and author page)
|
// Direction IDs (same source as membres page and author page)
|
||||||
@@ -139,7 +145,7 @@ if ( $search_query ) {
|
|||||||
$labo_directeur_id = $labo_page ? intval( get_post_meta( $labo_page->ID, 'directeur', true ) ) : 0;
|
$labo_directeur_id = $labo_page ? intval( get_post_meta( $labo_page->ID, 'directeur', true ) ) : 0;
|
||||||
$labo_adjoint_id = $labo_page ? intval( get_post_meta( $labo_page->ID, 'directeur_adjoint', true ) ) : 0;
|
$labo_adjoint_id = $labo_page ? intval( get_post_meta( $labo_page->ID, 'directeur_adjoint', true ) ) : 0;
|
||||||
|
|
||||||
foreach ( $user_query->get_results() as $user ) {
|
foreach ( array_slice( $found_users, 0, 6 ) as $user ) {
|
||||||
$avatar_url = thalim_get_user_avatar_url( $user->ID );
|
$avatar_url = thalim_get_user_avatar_url( $user->ID );
|
||||||
|
|
||||||
$role_id = get_user_meta( $user->ID, 'role_1', true );
|
$role_id = get_user_meta( $user->ID, 'role_1', true );
|
||||||
@@ -182,44 +188,50 @@ $context['author_cards'] = $author_cards;
|
|||||||
// Search taxonomy terms (axes thématiques + programmes de recherche)
|
// Search taxonomy terms (axes thématiques + programmes de recherche)
|
||||||
$taxonomy_cards = [];
|
$taxonomy_cards = [];
|
||||||
if ( $search_query ) {
|
if ( $search_query ) {
|
||||||
$matching_terms = get_terms([
|
$matching_terms = [];
|
||||||
'taxonomy' => [ 'axe_thematique', 'programme_de_recherche' ],
|
foreach ( thalim_search_text_variants( $search_raw ) as $variant ) {
|
||||||
'hide_empty' => false,
|
$found_terms = get_terms([
|
||||||
'name__like' => $search_query,
|
'taxonomy' => [ 'axe_thematique', 'programme_de_recherche' ],
|
||||||
]);
|
'hide_empty' => false,
|
||||||
if ( ! is_wp_error( $matching_terms ) ) {
|
'name__like' => $variant,
|
||||||
foreach ( $matching_terms as $term ) {
|
]);
|
||||||
$tax_obj = get_taxonomy( $term->taxonomy );
|
if ( ! is_wp_error( $found_terms ) ) {
|
||||||
// Comptage réel, mêmes critères que taxonomy.php : $term->count n'est pas
|
foreach ( $found_terms as $found_term ) {
|
||||||
// fiable sur ces taxonomies Pods, sans object_type (WordPress le remet à 0).
|
$matching_terms[ $found_term->term_id ] = $found_term;
|
||||||
$count_query = new WP_Query([
|
}
|
||||||
'post_type' => 'post',
|
|
||||||
'post_status' => 'publish',
|
|
||||||
'fields' => 'ids',
|
|
||||||
'posts_per_page' => 1,
|
|
||||||
'tax_query' => [
|
|
||||||
'relation' => 'AND',
|
|
||||||
[
|
|
||||||
'taxonomy' => $term->taxonomy,
|
|
||||||
'field' => 'term_id',
|
|
||||||
'terms' => [ $term->term_id ],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'taxonomy' => 'category',
|
|
||||||
'field' => 'term_id',
|
|
||||||
'terms' => [ thalim_cat_id('seance') ],
|
|
||||||
'operator' => 'NOT IN',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
$taxonomy_cards[] = [
|
|
||||||
'name' => $term->name,
|
|
||||||
'url' => get_term_link( $term ),
|
|
||||||
'taxonomy_label' => $tax_obj ? $tax_obj->labels->singular_name : $term->taxonomy,
|
|
||||||
'count' => $count_query->found_posts,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
foreach ( $matching_terms as $term ) {
|
||||||
|
$tax_obj = get_taxonomy( $term->taxonomy );
|
||||||
|
// Comptage réel, mêmes critères que taxonomy.php : $term->count n'est pas
|
||||||
|
// fiable sur ces taxonomies Pods, sans object_type (WordPress le remet à 0).
|
||||||
|
$count_query = new WP_Query([
|
||||||
|
'post_type' => 'post',
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'fields' => 'ids',
|
||||||
|
'posts_per_page' => 1,
|
||||||
|
'tax_query' => [
|
||||||
|
'relation' => 'AND',
|
||||||
|
[
|
||||||
|
'taxonomy' => $term->taxonomy,
|
||||||
|
'field' => 'term_id',
|
||||||
|
'terms' => [ $term->term_id ],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'taxonomy' => 'category',
|
||||||
|
'field' => 'term_id',
|
||||||
|
'terms' => [ thalim_cat_id('seance') ],
|
||||||
|
'operator' => 'NOT IN',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
$taxonomy_cards[] = [
|
||||||
|
'name' => $term->name,
|
||||||
|
'url' => get_term_link( $term ),
|
||||||
|
'taxonomy_label' => $tax_obj ? $tax_obj->labels->singular_name : $term->taxonomy,
|
||||||
|
'count' => $count_query->found_posts,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$context['taxonomy_cards'] = $taxonomy_cards;
|
$context['taxonomy_cards'] = $taxonomy_cards;
|
||||||
|
|
||||||
|
|||||||
@@ -87,6 +87,14 @@ check('natif hors champ conservé', thalim_pod_tax_sync_diff([5], [5], [5, 9]),
|
|||||||
check('orphelin sans coche conservé', thalim_pod_tax_sync_diff([], [], [9]), ['add' => [], 'remove' => []]);
|
check('orphelin sans coche conservé', thalim_pod_tax_sync_diff([], [], [9]), ['add' => [], 'remove' => []]);
|
||||||
check('décoché déjà absent du natif', thalim_pod_tax_sync_diff([5], [], []), ['add' => [], 'remove' => []]);
|
check('décoché déjà absent du natif', thalim_pod_tax_sync_diff([5], [], []), ['add' => [], 'remove' => []]);
|
||||||
|
|
||||||
|
echo "== thalim_search_text_variants ==\n";
|
||||||
|
check('texte simple', thalim_search_text_variants('Oulipo'), ['Oulipo']);
|
||||||
|
check('apostrophe droite', thalim_search_text_variants("l'Oulipo"), ["l'Oulipo", 'l’Oulipo']);
|
||||||
|
check('apostrophe courbe', thalim_search_text_variants('l’Oulipo'), ["l'Oulipo", 'l’Oulipo']);
|
||||||
|
check('esperluette', thalim_search_text_variants('a & b'), ['a & b', 'a & b']);
|
||||||
|
check('guillemets inchangés', thalim_search_text_variants('"punk"'), ['"punk"']);
|
||||||
|
check('les deux', thalim_search_text_variants("l'a & b"), ["l'a & b", 'l’a & b', "l'a & b", 'l’a & b']);
|
||||||
|
|
||||||
echo "== thalim_get_seance_parent_id ==\n";
|
echo "== thalim_get_seance_parent_id ==\n";
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$seance_id = (int) $wpdb->get_var(
|
$seance_id = (int) $wpdb->get_var(
|
||||||
|
|||||||
Reference in New Issue
Block a user