-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
304 lines (265 loc) · 11.9 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
<?php
/**
* KCSU website functions and definitions (can declare global variables here)
*
* @package KCSU
* @file functions.php
* @author Conor Burgess <[email protected]>
* @license BSD 3-Clause
*
*/
require_once('lib/string_format.php');
require_once('lib/PhpLib/set.php');
/**
* Enable auto-updates
*/
define( 'WP_AUTO_UPDATE_CORE', true );
/**
* Register and configure menus
*/
require_once('lib/wp_bootstrap_navwalker.php');
function register_menus() {
// The main navbar (top menu)
register_nav_menus(array(
'navbar' => __('Navigation Menu'),
'home-side-menu' => __('Front page about menu')
));
}
add_action('after_setup_theme', 'register_menus');
/**
* Custom post types and custom fields.
*/
require_once('custom_post_types.php');
// Note: Need to do this after setup to stop clashes with other installed ACF.
function register_kcsu_acf() {
require_once('custom_fields.php');
}
add_action('after_setup_theme', 'register_kcsu_acf');
/**
* Add scripts
*/
function kcsu_register_scripts()
{
wp_register_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.js', array( 'jquery' ), '2.3.2', true );
wp_register_script( 'timeago', get_template_directory_uri() . '/js/plugins/timeago.js', array( 'jquery' ), '1.4.1', true );
wp_enqueue_script( 'main', get_template_directory_uri() . '/js/main.js',
array( 'jquery', 'bootstrap', 'timeago'), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'kcsu_register_scripts' );
/**
* Returns an array of exec members
*/
function kcsu_get_exec()
{
$the_exec = array();
$args = array(
'post_type' => 'exec',
'posts_per_page' => -1,
);
$exec_query = new WP_Query( $args );
while ( $exec_query->have_posts() )
{
$exec_query->the_post();
$member = array(
'id' => get_the_ID(),
'title' => get_the_title(),
'description' => get_the_content(),
'email' => get_field('email_address'),
'link' => get_permalink(),
'excerpt' => get_the_excerpt(),
'user' => array()
);
// now fetch incumbent data
$incumbents = explode(',', get_field('incumbent'));
foreach($incumbents as $incumbent) {
$u = get_user_by('login', trim($incumbent));
if (empty($u->ID))
{
$u = get_user_by('login', 'Exec');
}
if(function_exists('get_wp_user_avatar')) {
$avatar_full = get_wp_user_avatar( $u->ID, $size = '100' );
} else {
$avatar_full = get_avatar( $u->ID, $size = '100' );
}
$avatar_image_src = "";
if(!empty($avatar_full)){
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $avatar_full, $matches, PREG_SET_ORDER);
$avatar_image_src = !empty($matches) ? $matches [0] [1] : "";
}
$avatar = '<img class="profile-pic" src="'.$avatar_image_src.'" alt="" />';
$member['user'][] = array(
'ID' => $u->ID,
'login' => $u->user_login,
'nicename' => $u->user_nicename,
'email' => $u->user_email,
'display_name' => $u->first_name . ' ' . $u->last_name,
'avatar' => $avatar
);
}
// Display the users nicely
$member['users_display'] = implode(', ', PhpLib\Set::extract($member, 'user.*.display_name'));
// and their avatars
$member['users_avatars'] = implode('', PhpLib\Set::extract($member, 'user.*.avatar'));
$the_exec[] = $member;
}
wp_reset_postdata(); // Reset global post data
return $the_exec;
}
/**
* Returns a single exec member by their postname (e.g. president)
*/
function kcsu_get_exec_member($name)
{
$exec_member;
$args = array(
'post_type' => 'exec',
'exec' => $name,
'posts_per_page' => 1
);
$exec_query = new WP_Query( $args );
while ( $exec_query->have_posts() )
{
$exec_query->the_post();
$exec_member = array(
'id' => get_the_ID(),
'title' => get_the_title(),
'description' => get_the_content(),
'email' => get_field('email_address'),
'user' => array(),
'link' => get_permalink(),
'excerpt' => get_the_excerpt()
);
// now fetch incumbent data
$incumbents = explode(',', get_field('incumbent'));
foreach($incumbents as $incumbent) {
$u = get_user_by('login', trim($incumbent));
if (empty($u->ID))
{
$u = get_user_by('login', 'Exec');
}
if(function_exists('get_wp_user_avatar')) {
$avatar_full = get_wp_user_avatar( $u->ID, $size = '100' );
} else {
$avatar_full = get_avatar( $u->ID, $size = '100' );
}
$avatar_image_src = "";
if(!empty($avatar_full)){
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $avatar_full, $matches, PREG_SET_ORDER);
$avatar_image_src = !empty($matches) ? $matches [0] [1] : "";
}
$avatar = '<img class="profile-pic" src="'.$avatar_image_src.'" alt="" />';
$exec_member['user'][] = array(
'ID' => $u->ID,
'login' => $u->user_login,
'nicename' => $u->user_nicename,
'email' => $u->user_email,
'display_name' => $u->first_name . ' ' . $u->last_name,
'avatar' => $avatar
);
}
// Display the users nicely
$exec_member['users_display'] = implode(', ', PhpLib\Set::extract($exec_member, 'user.*.display_name'));
// and their avatars
$exec_member['users_avatars'] = implode('', PhpLib\Set::extract($exec_member, 'user.*.avatar'));
}
wp_reset_postdata(); // Reset global post data
return $exec_member;
}
/**
* Returns all events happening before specified date, or in the next week if not supplied. Defaults to internal events, but can return external (type=1) or all events (type=22) too.
*/
function kcsu_get_upcoming_events($time_limit = '', $type = 0)
{
$events = array();
if ($time_limit == '')
$time_limit = date('Ymd', strtotime('+7 days'));
$today = date('Ymd', time());
switch ($type) {
case 0:
$post_type_query = array('event');
break;
case 1:
$post_type_query = array('external-event');
break;
case 2:
$post_type_query = array('event', 'external-event');
break;
default:
$post_type_query = array('event');
break;
}
$args = array(
'post_type' => $post_type_query,
'posts_per_page' => 50,
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date',
'value' => $today,
'compare' => '>=',
'type' => 'NUMERICAL',
),
array(
'key' => 'date',
'value' => $time_limit,
'compare' => '<=',
'type' => 'NUMERICAL',
),
),
);
$event_query = new WP_Query( $args );
while ( $event_query->have_posts() )
{
$event_query->the_post();
$date = DateTime::createFromFormat('Ymd', get_field('date'));
$events[] = array(
'id' => get_the_ID(),
'title' => get_the_title(),
'description' => get_the_content(),
'location' => get_field('location'),
'date' => $date->format('d/m/Y'),
'time' => get_field('time'),
'link' => get_permalink(),
'excerpt' => get_the_excerpt()
);
}
wp_reset_postdata(); // Reset global post data
return $events;
}
/**
* Sets the post excerpt length to 40 words
*/
function kcsu_excerpt_length( $length ) {
return 40;
}
add_filter( 'excerpt_length', 'kcsu_excerpt_length' );
// Add our widgets
/**
* Register our sidebars and widgetized areas.
*
*/
function kcsu_widgets_init() {
register_sidebar( array(
'name' => 'Home sidebar before menu',
'id' => 'home_right_1',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
register_sidebar( array(
'name' => 'Home sidebar after menu',
'id' => 'home_right_2',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'kcsu_widgets_init' );
require_once('custom_widgets.php');
?>