-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
364 lines (304 loc) · 11.4 KB
/
functions.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
#let's make sure that the bootstrap font-awesome system is included
function include_font_awesome() {
wp_register_style('font-awesome.css', '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css');
wp_enqueue_style('font-awesome.css');
}
add_action('wp_enqueue_scripts','include_font_awesome');
# let's include the local javascript file
function include_indexjs() {
wp_register_script('indexjs',get_stylesheet_directory_uri().'/js/index.min.js', array('jquery'), false, true);
wp_enqueue_script('indexjs');
}
add_action('wp_enqueue_scripts','include_indexjs');
# let's make sure to include bootstrap itself.
function include_bootstrap() {
wp_register_script('bootstrapjs',get_stylesheet_directory_uri().'/js/bootstrap.min.js', array('jquery'),false,true);
wp_enqueue_script('bootstrapjs');
}
add_action('wp_enqueue_scripts','include_bootstrap');
#let's get the header image working
$args = array(
'width' => 300,
'height' => 200,
'default-image' => get_template_directory_uri() . '/images/header.jpg',
'uploads' => true,
);
add_theme_support('custom-header', $args);
#let's get the footer image working
function IFMA_customize_register( $wp_customize) {
#add a new setting
$wp_customize->add_setting('footer_image_setting',array('default' => get_template_directory_uri() . '/images/footer.png', 'type'=> 'option'));
#add a new section
$wp_customize->add_section('footer_image_section',array('title' => __('Footer Image','IFMA-Courses')));
#add a control so we can change stuff
$wp_customize->add_control(new WP_Customize_Image_Control( $wp_customize, 'footer_image_control',array(
'label' => __('Footer Image', 'IFMA-Courses'),
'section' => 'footer_image_section',
'settings' => 'footer_image_setting'
)));
#
}
add_action('customize_register','IFMA_customize_register');
# let's get the title tags working
add_filter('wp_title','IFMA_homepage_title');
function IFMA_homepage_title($title) {
if (empty ($title) && (is_home() || is_front_page())){
return __(get_bloginfo('name'), 'theme_domain') . ' | ' . get_bloginfo('description');
}
return $title;
}
# let's register all of the menus
function register_menus() {
register_nav_menus(array(
'utility-menu' => __('Utility'),
'navigation-menu' => __('Navigation') ) );
}
add_action('init', 'register_menus');
function micro_nav_widget_init() {
register_sidebar(array(
'name' => 'MicroNav',
'id' => 'micro_nav',
'before_widget' => '<ul>',
'after_widget' => '</ul>',
'before_title' => '<li>',
'after_title' => '</li>',
) );
}
add_action('widgets_init', 'micro_nav_widget_init');
# let's register widget areas
function footer_widget_init() {
register_sidebars( 2, array(
'name' => 'Footer %d',
'id' => 'footer',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action('widgets_init', 'footer_widget_init');
function sidebar_widget_init() {
register_sidebar( array(
'name' => 'Sidebar',
'id' => 'sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
));
}
add_action('widgets_init', 'sidebar_widget_init');
# let's modify the admin menu
add_action('admin_menu','remove_default_post_type');
function remove_default_post_type() {
if (current_user_can('manage_options') === false) {
remove_menu_page('edit.php');
remove_menu_page('edit.php?post_type=page');
remove_menu_page('edit-comments.php');
remove_menu_page('themes.php');
remove_menu_page('plugins.php');
remove_menu_page('tools.php');
remove_menu_page('options-general.php');
remove_menu_page('users.php');
}
}
add_action('wp_before_admin_bar_render', 'modify_admin_bar');
function modify_admin_bar(){
if (current_user_can('manage_options') === false) {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('new-post');
$wp_admin_bar->remove_menu('new-page');
}
}
# a helper function
function walk_delivery_method($value,$key) {
if ($key == 0) {
echo $value;
} else {
echo ", ".$value;
}
}
// this function comes from http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe#PHP
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$earth_radius = 6371;
$dLat = deg2rad($latitude2 - $latitude1);
$dLon = deg2rad($longitude2 - $longitude1);
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);
$c = 2 * asin(sqrt($a));
$d = $earth_radius * $c;
return $d;
}
#Let's add some search functionality
#
# first make sure that the query vars for the filters are available to relevanssi
add_filter('query_vars', 'relevanssi_qvs');
function relevanssi_qvs($qv) {
array_push($qv, 'start_date');
array_push($qv, 'ifma_credential');
array_push($qv, 'online');
array_push($qv, 'on-site');
array_push($qv, 'online-scheduled');
array_push($qv, 'self-study');
array_push($qv, 'accredited');
array_push($qv, 'classroom');
array_push($qv, 'provided_by');
array_push($qv, 'college_accredited');
array_push($qv, 'ceu');
array_push($qv, 'meta_key');
return $qv;
}
#second, make sure that relevanssi fires if a filter is present but the search term is empty
#
#currently only tests if ifma_credential or start_date is present
add_filter('relevanssi_search_ok', 'search_trigger');
function search_trigger($search_ok){
global $wp_query;
if (!empty($wp_query->query_vars['ifma_credential']) || !empty($wp_query->query_vars['start_date'])){
$search_ok = true;
}
return $search_ok;
}
#now handle the relevanssi search, filtering if necessary
#
add_filter('relevanssi_hits_filter', 'filter_courses');
function filter_courses($hits) {
global $wp_query;
// just make sure that we have some results to work with:
if ($hits[0] == null) {
// no search hits, so return all
$args = array( 'post_type' => 'course', 'posts_per_page' => -1);
if (isset($_GET['orderby']) && $_GET['orderby'] == "title"){
$args["orderby"] = "title";
$args['order'] = $_GET['order'];
}
$hits[0] = get_posts($args);
}
// we have to sort the custom fields here.
if (isset($wp_query->query_vars['meta_key']) && $wp_query->query_vars['meta_key'] == 'start_date'){
$dateArray = array();
$returnArray = array();
foreach ($hits[0] as $hit) {
$dateArray[$hit->ID] = get_field('start_date',$hit->ID);
}
if ($_GET['order'] == "ASC") {
arsort($dateArray);
} else {
asort($dateArray);
}
foreach($dateArray as $ID => $date) {
foreach($hits[0] as $hit) {
if ($hit->ID == $ID){
$returnArray[] = $hit;
}
}
}
$hits[0] = $returnArray;
}
if (isset($wp_query->query_vars['meta_key']) && $wp_query->query_vars['meta_key'] == 'map_location'){
//this one requires that we get the location information of the user, calculate the distance, and then sort by distance
$_SERVER['REMOTE_ADDR'] = "12.215.42.19";
$user_location = json_decode(shell_exec('curl "http://api.hostip.info/get_json.php?position=true&ip='.strval($_SERVER['REMOTE_ADDR']).'"'));
$user_lng = $user_location->lng;
$user_lat = $user_location->lat;
//now that we have the user location, let's calculate distance
$mapArray = array();
$returnArray = array();
foreach ($hits[0] as $hit) {
//get the latitude and longitude for each hit
$location = get_field('map_location',$hit->ID);
$address = get_field('map_location',$location[0]->ID);
$distance = getDistance($user_lat,$user_lng,$address['lat'],$address['lng']);
$mapArray[$hit->ID] = $distance;
}
if ($_GET['order'] == "ASC") {
arsort($mapArray);
} else {
asort($mapArray);
}
foreach($mapArray as $ID => $distance) {
foreach($hits[0] as $hit) {
if ($hit->ID == $ID){
$returnArray[] = $hit;
}
}
}
$hits[0] = $returnArray;
}
// now filter the results
$results = array();
foreach ($hits[0] as $hit) {
//start filtering by start date and if no date is specified use current date.
if (isset($wp_query->query_vars['start_date']) && get_field('start_date',$hit->ID) != null) {
$start_date = implode(" ",$wp_query->query_vars['start_date']);
if (date(Ymd,strtotime($start_date)) > date(Ymd,strtotime(strval(get_field('end_date',$hit->ID))))) {
continue;
}
} elseif (isset($wp_query->query_vars['start_date']) != true && get_field('start_date',$hit->ID) != null){
if (date(Ymd,strtoTime('now')) > date(Ymd,strtotime(strval(get_field('end_date',$hit->ID))))) {
continue;
}
}
//var_dump(get_fields($hit));
//check that the result matches the correct credential
if (isset($wp_query->query_vars['ifma_credential'])) {
if ($wp_query->query_vars['ifma_credential']==='fmp' && !in_category('fmp',$hit)) {
continue;
}
elseif ($wp_query->query_vars['ifma_credential']==='sfp'&& !in_category('sfp',$hit)) {
continue;
}
elseif ($wp_query->query_vars['ifma_credential']==='cfm' && !in_category('cfm',$hit)) {
continue;
}
}
// check IFMA accreditation
if (isset($wp_query->query_vars['accredited']) && !get_field('accredited',$hit->ID)) {
continue;
}
// check College accreditation
if (isset($wp_query->query_vars['college_accredited']) && !get_field('college_accredited',$hit->ID)) {
continue;
}
// check CEU
if (isset($wp_query->query_vars['ceu']) && !get_field('ceu',$hit->ID)) {
continue;
}
// remove those without the specified delivery method
if (is_array(get_field('delivery_method',$hit->ID))) {
$suspect = false;
if (isset($wp_query->query_vars['online']) && !in_array('online', get_field('delivery_method',$hit->ID))){
$suspect = true;
} if (isset($wp_query->query_vars['on-site']) && !in_array('on-site', get_field('delivery_method',$hit->ID))) {
$suspect = true;
} if (isset($wp_query->query_vars['online-scheduled']) && !in_array('online-scheduled',get_field('delivery_method',$hit->ID))) {
$suspect = true;
} if (isset($wp_query->query_vars['self-study']) && !in_array('self-study',get_field('delivery_method',$hit->ID))) {
$suspect = true;
} if (isset($wp_query->query_vars['classroom']) && !in_array('classroom',get_field('delivery_method',$hit->ID))) {
$suspect = true;
}
if (isset($wp_query->query_vars['online']) && in_array('online', get_field('delivery_method',$hit->ID))){
$suspect = false;
} if (isset($wp_query->query_vars['on-site']) && in_array('on-site', get_field('delivery_method',$hit->ID))) {
$suspect = false;
} if (isset($wp_query->query_vars['online-scheduled']) && in_array('online-scheduled',get_field('delivery_method',$hit->ID))) {
$suspect = false;
} if (isset($wp_query->query_vars['self-study']) && in_array('self-study',get_field('delivery_method',$hit->ID))) {
$suspect = false;
} if (isset($wp_query->query_vars['classroom']) && in_array('classroom',get_field('delivery_method',$hit->ID))) {
$suspect = false;
}
// since hit fell through everything, we can assume it's a good variable
if ($suspect == false){
$results[]= $hit;
}
} else{
//doesn't have a specified delivery method, go ahead and return it.
$results[]= $hit;
}
}
$hits[0] = $results;
return $hits;
}
?>