forked from daibach/edtechhub-database-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
233 lines (174 loc) · 6.39 KB
/
functions.php
File metadata and controls
233 lines (174 loc) · 6.39 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* Functions and definitions
*
* @package WordPress
* @subpackage ETHDB
* @since EdTech Hub Database 0.1
*/
add_action( 'init', 'ethdb_register_menus' );
add_action( 'wp_enqueue_scripts', 'ethdb_register_styles' );
add_action( 'wp_enqueue_scripts', 'ethdb_register_scripts' );
add_action( 'pre_get_posts', 'ethdb_change_post_order' );
add_theme_support( 'title-tag' );
function ethdb_register_styles() {
$theme_version = wp_get_theme()->get( 'Version' );
wp_enqueue_style( 'preconnect', 'https://fonts.gstatic.com', array(), null);
wp_enqueue_style( 'ethdb-font', 'https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap', array(), null);
wp_enqueue_style( 'tp-fontello-css', 'https://edtechhub.org/wp-content/plugins/essential-grid/public/assets/font/fontello/css/fontello.css?ver=3.0.7', array(), null);
wp_enqueue_style( 'ekoterra-icons-css', 'https://edtechhub.org/wp-content/themes/edtechhub/css/font-icons/css/fontello-embedded.css', array(), null);
wp_enqueue_style( 'ethdb-bootstrap', get_template_directory_uri().'/bootstrap.min.css', array(), null );
wp_enqueue_style( 'ethdb-style', get_stylesheet_uri(), array(), $theme_version );
}
function ethdb_register_scripts() {
$theme_version = wp_get_theme()->get( 'Version' );
wp_enqueue_script( 'ethdb-bootstrap-js', get_template_directory_uri().'/bootstrap.min.js', array(), null);
wp_enqueue_script( 'ethdb-script-js', get_template_directory_uri().'/script.js', array(), null);
}
function ethdb_register_menus() {
register_nav_menu('ethdb-header-menu',__( 'Header Menu' ));
}
function ethdb_change_post_order($query)
{
if ( ($query->is_tax() || $query->is_post_type_archive('tools')) && $query->is_main_query())
{
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC');
}
}
function ethdb_build_filter_list($query) {
$primary_taxonomy = $query->query_vars['taxonomy'];
$search_terms = $query->tax_query->queries;
$taxonomies = array();
foreach($search_terms as $term) {
if(! array_key_exists($term['taxonomy'],$taxonomies)) {
$this_taxonomy = get_taxonomy($term['taxonomy']);
$taxonomies[$term['taxonomy']] = array(
'taxonomy' => $term['taxonomy'],
'object' => $this_taxonomy,
'items' => array(),
);
}
foreach($term['terms'] as $t) {
$t_info = get_term_by( 'slug', $t, $term['taxonomy'] );
array_push($taxonomies[$term['taxonomy']]['items'],$t_info);
}
}
$filter_list = array(
'filters' => $taxonomies,
'primary_taxonomy' => $primary_taxonomy,
'advanced_filter' => false,
);
if(count($taxonomies) > 1) { $filter_list['advanced_filter'] = true; }
return $filter_list;
}
function ethdb_display_filters($query,$filter_list=NULL) {
if(is_null($filter_list)) {
$filter_list = ethdb_build_filter_list($query);
}
$output = "";
if($filter_list['advanced_filter']) {
$output .= "<ul>";
foreach($filter_list['filters'] as $filter) {
if($filter['taxonomy']!=$filter_list['primary_taxonomy']) {
$taxonomy_name = $filter['object']->labels->name;
$output .= "<li>";
$output .= $taxonomy_name.": ";
$terms_in_this_filter = array();
foreach($filter['items'] as $item) {
$term_name = $item->name;
$url = ethdb_build_filter_url($filter_list,array('tax'=>$filter['taxonomy'],'term'=>$item->slug));
$text = "<span>";
$text .= $term_name;
$text .= " <a href=\"$url\" title=\"Remove the filter $taxonomy_name: $term_name\">x</a>";
$text .= "</span> ";
array_push($terms_in_this_filter,$text);
}
$output .= implode(" & ",$terms_in_this_filter);
$output .= "</li>\n";
}
}
$output .= "</ul>";
}
return $output;
}
function ethdb_build_filter_url($filter_list,$remove=NULL) {
$url = "/";
//build primary taxonomy url
$primary = $filter_list['filters'][$filter_list['primary_taxonomy']];
//add primary taxonomy slug
$url .= $primary['object']->rewrite['slug'].'/';
//build item list and separate with "+"
$primary_items = array();
foreach($primary['items'] as $item) {
array_push($primary_items,$item->slug);
}
$url .= implode("+",$primary_items)."/";
if($filter_list['advanced_filter']) {
//build secondary filters
$secondary_filters = array();
foreach($filter_list['filters'] as $filter) {
if($filter['taxonomy'] != $filter_list['primary_taxonomy']) {
$filter_url = $filter['object']->query_var."=";
$filter_items = array();
foreach($filter['items'] as $item) {
if(!is_null($remove) && ($remove['tax']===$filter['taxonomy'] && $remove['term']===$item->slug)) {
//this item should be removed, so don't do anything
} else {
array_push($filter_items,$item->slug);
}
}
if(!empty($filter_items)) {
$filter_url .= implode("+",$filter_items);
array_push($secondary_filters,$filter_url);
}
}
}
if(!empty($secondary_filters)) {
$url .= "?".implode("&",$secondary_filters);
}
}
return $url;
}
function ethdb_get_filter_title($query,$page,$fiter_list=NULL) {
if(is_tax()) {
if(is_null($filter_list)) {
$filter_list = ethdb_build_filter_list($query);
}
$title = "";
$search_terms = $query->tax_query->queries;
//primary taxonomy defines how the title starts
switch($filter_list['primary_taxonomy']) {
case 'tool_region':
case 'tool_country':
case 'tool_language':
$title .= "Tools available in ";
break;
case 'tool_connectivity_requirement':
case 'tool_hardware_requirement':
$title .= "Tools which require ";
break;
case 'tool_terms_and_costs':
case 'tool_code_type':
$title .= "Tools which are ";
break;
default:
$title .= "Tools for ";
}
//get the primary search terms
$primary_terms = array();
foreach($search_terms as $term) {
if($term['taxonomy']==$filter_list['primary_taxonomy']) {
foreach($term['terms'] as $t) {
$t_name = get_term_by( 'slug', $t, $term['taxonomy'] );
array_push($primary_terms,$t_name->name);
}
}
}
$title.= implode(" & ",$primary_terms);
$title.=$page;
return $title;
} else {
return "";
}
}