forked from telabotanica/wp-theme-telabotanica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
153 lines (130 loc) · 4.14 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
$search_query = sanitize_text_field( get_search_query() );
// Are we searching in a specific index?
$current_index = sanitize_key( get_query_var( 'in', false ) );
// Redirect some indices to the category or page, while preserving the query
switch ($current_index) {
case 'actualites':
$redirect_url = get_category_link( get_category_by_slug( 'actualites' ) );
break;
case 'evenements':
$redirect_url = get_category_link( get_category_by_slug( 'evenements' ) );
break;
case 'projets':
$redirect_url = get_permalink(get_page_by_path( 'projets' ));
break;
}
if ( isset($redirect_url) ) {
wp_redirect( $redirect_url . '?q=' . $search_query );
exit;
}
// Force a small header (without use cases navigation)
$header_small = true;
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
if ( telabotanica_algolia_check(true) ) :
// Get Algolia instance
$algolia_client = telabotanica_algolia_client();
$algolia_autocomplete_config = telabotanica_algolia_config()['autocomplete'];
if ( $current_index ) :
// Retrieve the label for the current index
$indices = $algolia_autocomplete_config['sources'];
foreach ( $indices as $index ) :
if ( $index['index_id'] === $current_index ) {
$current_index = [
'id' => $current_index,
'label' => $index['label'],
'name' => $index['index_name'],
'filters' => @$index['filters'] ?: []
];
break;
}
endforeach;
// Perform the search
$index = $algolia_client->initIndex($current_index['name']);
$results = $index->search($search_query, [
'hitsPerPage' => 20
]);
the_telabotanica_module('cover-search', [
'index' => $current_index['id'],
'total_results' => false,
'instantsearch' => true
]);
?>
<div class="layout-content-col">
<div class="layout-wrapper">
<aside class="layout-column">
<?php
the_telabotanica_module('search-filters', [
'filters' => $current_index['filters']
]);
the_telabotanica_module('button-top');
?>
</aside>
<div class="layout-content">
<?php
the_telabotanica_module('breadcrumbs', [
'items' => [
[
'href' => esc_url( home_url( '/' ) . '?s=' . $search_query ),
'text' => __('Recherche', 'telabotanica')
],
[
'text' => $current_index['label']
],
[ 'text' => '<span id="search-stats">' . sprintf( _n(
'%s résultat trouvé',
'%s résultats trouvés',
$results['nbHits'],
'telabotanica'
), number_format_i18n( $results['nbHits'] ) ) . '</span>' ]
]
]);
echo '<div id="search-hits">';
foreach ($results['hits'] as $hit) {
$hit['type'] = $current_index['id'];
the_telabotanica_module('search-hit', $hit);
}
echo '</div>';
// TODO: add pagination
?>
</div>
</div>
</div>
<?php
else :
// Perform several queries in a single API call
$queries = [];
foreach ( $algolia_autocomplete_config['sources'] as $index ) :
$queries[] = [
'indexName' => $index['index_name'],
'query' => $search_query,
'hitsPerPage' => $index['settings']['hitsPerPage'] * 2,
'facetFilters' => array_key_exists('facetFilters', $index['settings']) ? $index['settings']['facetFilters'] : null
];
endforeach;
$results = $algolia_client->multipleQueries($queries);
$total_results = array_sum(array_column($results['results'], 'nbHits'));
the_telabotanica_module('cover-search', [
'total_results' => $total_results
]);
?>
<div class="layout-central-col is-wide adjacent-top">
<div class="layout-wrapper">
<div class="layout-content">
<?php
the_telabotanica_module('search-results', $results);
?>
</div>
</div>
</div>
<?php
endif;
endif;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>