-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
349 lines (293 loc) · 9.1 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
<?php
/**
* functions.php
*
* This is a special file that WordPress looks for in the theme folder. It
* contains all of the functions that power the theme.
*
* Learn More
*
* https://developer.wordpress.org/themes/basics/theme-functions/
*
**/
// Check for Waymark
if (!class_exists('Waymark_Helper')) {
// Admin
if (is_admin() && current_user_can('manage_options')) {
// Display admin notice
add_action('admin_notices', function () {
echo '<div class="error"><p>';
// Plugin exists, but is not active
if (file_exists(WP_PLUGIN_DIR . '/waymark/Waymark.php')) {
$activate_url = wp_nonce_url(admin_url('plugins.php?action=activate&plugin=waymark/Waymark.php'), 'activate-plugin_waymark/Waymark.php');
echo '<b>Map First</b> requires <a href="https://www.waymark.dev/">Waymark</a> | <a href="' . $activate_url . '">Activate »</a>.';
// Plugin not installed
} else {
$install_url = wp_nonce_url(admin_url('update.php?action=install-plugin&plugin=waymark'), 'install-plugin_waymark');
echo '<b>Map First</b> requires <a href="https://www.waymark.dev/">Waymark</a> | <a href="' . $install_url . '">Install »</a>.';
}
echo '</p></div>';
});
// Front
} else {
die('<h1>Map First requires <a href="https://www.waymark.dev/">Waymark</a>!</h1>');
}
}
// Theme assets
function map_first_enqueue_assets() {
wp_enqueue_style('map-first', get_template_directory_uri() . '/dist/map-first.css');
wp_enqueue_script('map-first', get_template_directory_uri() . '/dist/map-first.js', [], false, true);
}
add_action('wp_enqueue_scripts', 'map_first_enqueue_assets');
function map_first_pre_get_posts($query) {
if (is_archive() && is_tax('waymark_collection')) {
$query->set('order', 'ASC');
}
}
add_action('pre_get_posts', 'map_first_pre_get_posts');
function map_first_customize_register($wp_customize) {
$wp_customize->add_section('map_first_section', array(
'title' => 'Map First',
));
$wp_customize->add_setting('map_first_header_logo', array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'esc_url_raw',
));
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'map_first_header_logo', array(
'label' => 'Header Logo Image',
'section' => 'map_first_section',
)));
$wp_customize->add_setting('map_first_body_background', array(
'default' => '#eeeeee',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control('map_first_body_background', array(
'label' => 'Page Background Colour',
'section' => 'map_first_section',
'type' => 'color',
));
$wp_customize->add_setting('map_first_body_color', array(
'default' => '#111111',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control('map_first_body_color', array(
'label' => 'Page Text Colour',
'section' => 'map_first_section',
'type' => 'color',
));
$wp_customize->add_setting('map_first_header_background', array(
'default' => '#212529',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control('map_first_header_background', array(
'label' => 'Header Background Colour',
'section' => 'map_first_section',
'type' => 'color',
));
$wp_customize->add_setting('map_first_header_color', array(
'default' => '#fff',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control('map_first_header_color', array(
'label' => 'Header Text Colour',
'section' => 'map_first_section',
'type' => 'color',
));
$wp_customize->add_setting('map_first_link_color', array(
'default' => '#0056b3',
'transport' => 'refresh',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control('map_first_link_color', array(
'label' => 'Content Link Colour',
'section' => 'map_first_section',
'type' => 'color',
));
}
add_action('customize_register', 'map_first_customize_register');
function map_first_widgets() {
register_sidebar(array(
'name' => 'Map First Header Content',
'id' => 'map-first-header-content',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h4 class="text-white">',
'after_title' => '</h4>',
));
register_sidebar(array(
'name' => 'Map First Footer Content',
'id' => 'map-first-footer-content',
'before_widget' => '<div>',
'after_widget' => '</div>',
));
}
add_action('widgets_init', 'map_first_widgets');
function map_first_menu() {
register_nav_menu('map-first-header-nav', 'Map First Header Nav');
}
add_action('init', 'map_first_menu');
function map_first_breadcrumb() {
global $post;
if ($post) {
$crumbs = array();
switch (true) {
// ========= Home =========
case is_front_page():
break;
// ========= Taxonomy =========
case is_archive():
$tax = get_queried_object();
if (isset($tax->name)) {
$crumbs[] = array(
'prepend' => ' Maps / ',
'text' => $tax->name,
);
}
break;
// ========= Map =========
case is_singular('waymark_map'):
//Collections
$collections = wp_get_post_terms($post->ID, 'waymark_collection');
//If we have Collections
if (sizeof($collections)) {
//Sort by count ASC
usort($collections, function ($a, $b) {
return $a->count < $b->count;
});
$crumbs[] = array(
// 'prepend' => ' Map / ',
'text' => $collections[0]->name,
'link' => get_term_link($collections[0]),
);
}
//Map Title
$crumbs[] = array(
'text' => get_the_title(),
);
break;
// ========= Posts / Pages =========
case is_page():
case is_single():
//Ancestors?
if ($post->post_parent) {
$ancestors = get_post_ancestors($post);
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor) {
$crumbs[] = array(
'text' => get_the_title($ancestor),
'link' => get_permalink($ancestor),
);
}
}
//Map Title
$crumbs[] = array(
'text' => get_the_title(),
);
break;
}
//Do we have something to display?
if (sizeof($crumbs)) {
echo '<span vocab="https://schema.org/" typeof="BreadcrumbList">' . "\n";
$position = 1;
foreach ($crumbs as $crumb) {
$prepend = isset($crumb['prepend']) ? $crumb['prepend'] : '';
$append = isset($crumb['append']) ? $crumb['append'] : '';
$text = isset($crumb['text']) ? $crumb['text'] : '';
echo ' /' . "\n";
echo ' <span property="itemListElement" typeof="ListItem">' . "\n";
echo $prepend;
if (isset($crumb['link'])) {
echo ' <a property="item" typeof="WebPage" href="' . $crumb['link'] . '">' . "\n";
}
echo ' <span property="name">' . $text . $append . '</span>' . "\n";
if (isset($crumb['link'])) {
echo ' </a>' . "\n";
}
echo $append;
echo ' <meta property="position" content="' . $position . '" />' . "\n";
echo ' </span>' . "\n";
}
echo ' </span>' . "\n";
}
//Children?
if (is_front_page()) {
$post_children = get_posts(array(
'post_type' => 'page',
'post_parent' => '0',
// Exclude home itself
'exclude' => get_option('page_on_front'),
));
} else {
$post_children = get_posts(array(
'post_type' => $post->post_type,
'post_parent' => $post->ID,
));
}
if ($post_children) {
echo ' /' . "\n";
echo '<select id="breadcrumb-nav">' . "\n";
echo ' <option selected="selected">...</option>' . "\n";
foreach ($post_children as $child) {
//No home
if ($child->ID != get_option('page_on_front')) {
echo ' <option value="' . get_permalink($child->ID) . '">' . $child->post_title . '</option>' . "\n";
}
}
echo '</select>' . "\n";
}
}
}
//Thanks! https://njengah.com/wordpress-custom-pagination/
function map_first_archive_pagination() {
global $wp_query;
$big = 9999999;
$pagination_list = paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages,
'type' => 'array',
'prev_text' => '<',
'next_text' => '>',
));
//If we have a list
if (isset($pagination_list) && sizeof($pagination_list)) {
//Format for Bootstrap
echo '<ul class="pagination justify-content-center mt-5">' . "\n";
$count = 0;
foreach ($pagination_list as $link) {
$class = 'page-item';
if (strpos($link, '<span') !== false) {
$class .= ' active';
}
echo '<li class="' . $class . '">' . "\n";
$bad = array(
'page-numbers',
);
$good = array(
'page-link',
);
echo str_replace($bad, $good, $link);
echo '</li>' . "\n";
}
echo '</ul>' . "\n";
}
}
function map_first_single_pagination() {
//Format for Bootstrap
echo '<ul class="pagination justify-content-center mt-5">' . "\n";
echo ' <li class="page-item">' . "\n";
$link = get_previous_post_link('%link', '< %title', true, '', 'waymark_collection');
echo str_replace('<a', '<a class="page-link"', $link);
echo ' </li>' . "\n";
echo ' <li class="page-item">' . "\n";
$link = get_next_post_link('%link', '%title >', true, '', 'waymark_collection');
echo str_replace('<a', '<a class="page-link"', $link);
echo ' </li>' . "\n";
echo '</ul>' . "\n";
}