Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrgMap.php
More file actions
Latest commit
336 lines (313 loc) · 10.9 KB
/
Copy pathOrgMap.php
File metadata and controls
336 lines (313 loc) · 10.9 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
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
<?php
/*Plugin Name: Organization Map
Plugin URI: https://github.com/level73/OrgMap
Description: Creates a CPT for WordPress along with two custom taxonomies and displays a map of organizations
Version: 1.0
Author: Alan Zard
Author URI: https://level73.it
Text Domain: orgmap
License: GPL 3.0
*/
require_once(plugin_dir_path(__FILE__) . 'OrgMap.class.php');
newOrgMapSetup();
define('ICONSTRIP', true);
define('TOOLTIPS' , false);
add_image_size( 'country-header', 1280, 250, true );
functionsanitize_details( $resources ) {
returnwp_kses_post( $resources );
}
/** Enqueue Scripts for the map **/
functionOrgMap_enqueue_script() {
/** stylesheet **/
wp_enqueue_style( 'orgmap_css', plugin_dir_url( __FILE__ ) . 'css/orgmap_plugin.css' );
/** javascript **/
wp_enqueue_script( 'raphael', plugin_dir_url( __FILE__ ) . 'js/raphael.min.js', null, '2.2.0' );
wp_enqueue_script( 'mapael', plugin_dir_url( __FILE__ ) . 'js/jquery.mapael.min.js', array('jquery', 'raphael'), '2.0.0');
wp_enqueue_script( 'orgmap_js', plugin_dir_url( __FILE__ ) . 'js/orgmap.js', array('mapael'), '1.0.0' );
}
add_action('wp_enqueue_scripts', 'OrgMap_enqueue_script');
/** Manage templates and bind them to ur our CPT/CTX **/
functionOrgMap_Organization_single($template){
global$post;
if($post->post_type == 'organization'){
$plugin_path = plugin_dir_path(__FILE__);
$template_name = 'single_organization.php';
return$plugin_path . '/templates/' . $template_name;
}
return$template;
}
add_filter('single_template', 'OrgMap_Organization_single', 1);
functionOrgMap_Country_archive($template){
remove_all_filters( current_filter(), PHP_INT_MAX );
$current = get_queried_object();
if($current->taxonomy == 'countries'){
$plugin_path = plugin_dir_path(__FILE__);
$template_name = 'archive_country.php';
return$plugin_path . '/templates/' . $template_name;
}
return$template;
}
add_filter('taxonomy_template', 'OrgMap_Country_archive', 1);
/** List of SDG Taxonomy terms with the correct colors **/
functionOrgMap_org_sdg_terms($icons = true){
global$post;
$terms = get_the_terms($post->id, 'sdgs');
if(!empty($terms) && !is_wp_error($terms)){
echo'<ul class="sdg-term-list">';
foreach($termsas$term){
$sdg_order = get_term_meta($term->term_id, 'sdg_order', true);
echo'<li class="sdg-term-' . $sdg_order . (!$icons ? '' : ' sdg-icon-list') . '"><a class="sdg-taxonomy-term ' . (!$icons ? '' : 'sdg-icon') . ' sdg-term-' . $sdg_order . '" href="/sdg/' . $term->slug . '" title="' . $term->name . '" alt="' . $term->name . '" >' . $term->name . '</a></li>';
}
echo'</ul>';
}
}
/** Print SDG Taxonomy terms as a class **/
functionOrgMap_org_sdg_classes(){
global$post;
$terms = get_the_terms($post->id, 'sdgs');
if(!empty($terms) && !is_wp_error($terms)){
$classes = array();
foreach($termsas$term){
$sdg_order = get_term_meta($term->term_id, 'sdg_order', true);
$classes[] = 'sdg-class-' . $sdg_order;
}
echoimplode('', $classes);
}
}
/** List of SDG Taxonomy terms with the correct colors **/
functionOrgMap_sdg_terms($icons = true){
$terms = get_terms(array(
'taxonomy' => 'sdgs',
'meta_key' => 'sdg_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'hide_empty' => false
));
if(!empty($terms) && !is_wp_error($terms)){
echo'<ul class="sdg-term-list">';
foreach($termsas$term){
$sdg_order = get_term_meta($term->term_id, 'sdg_order', true);
echo'<li class="sdg-term-' . $sdg_order . (!$icons ? '' : ' sdg-icon-list') . '"><a data-sdg="' . $sdg_order . '" class="sdg-taxonomy-term ' . (!$icons ? '' : 'sdg-icon') . ' sdg-term-' . $sdg_order . '" href="/sdg/' . $term->slug . '" title="' . $term->name . '" alt="' . $term->name . '" >' . $term->name . '</a></li>';
}
echo'</ul>';
}
}
/** Shortcode to have a Map on a page/post **/
add_shortcode('orgmap', 'OrgMap_shortcode');
functionOrgMap_shortcode(){
// get all the_terms
$sdgs = get_terms(array(
'taxonomy' => 'sdgs',
'meta_key' => 'sdg_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'hide_empty' => false
));
// Get All Countries
$countries = get_terms(array(
'taxonomy' => 'countries',
'orderby' => 'slug',
'order' => 'ASC',
'hide_empty' => false
));
// Count Organizations to set the legend slices dynamically
$orgCount = wp_count_posts('organization');
// Only published profiles
$orgCount = $orgCount->publish;
$levels = array(
'lower' => 3,
'higher' => 8
);
if($orgCount > 12) {
$levels['lower'] = floor( $orgCount / 4 );
$levels['higher'] = ceil( $levels['lower'] * 2.5);
}
// Include the ISO2 code here, do it once instead of a million times
$countryList = array();
foreach( $countriesas$country ){
$iso2 = strtoupper($country->slug);
$countryList[$iso2] = array(
'value' => 0,
'href' => '/country/' . $country->slug,
);
$all[$iso2] = array(
'value' => $country->count,
'href' => '/country/' . $country->slug,
);
if(TOOLTIPS){
$countryList[$iso2]['tooltip'] = array(
'cssClass' => 'orgmapTooltip',
'content' => '<h3>' . $country->name . '</h3><p><strong>0</strong> ' . __('Organizations or Initiatives') . '</p>'
);
$all[$iso2]['tooltip'] = array(
'cssClass' => 'orgmapTooltip',
'content' => '<h3>' . $country->name . '</h3><p><strong>' . $country->count . '</strong> ' . ($country->count !== 1 ? __('Organizations or Initiatives') : __('Organization or Initiative') ) . '</p>'
);
}
}
// I'm going to store my query objects in here once I sorted them out
$results = array();
// Prepare the Full List
$results['all'] = array( 'areas' => $all );
// Cycle over Term to get related Orgs
foreach( $sdgsas$sdg ){
$sdg_order = get_term_meta($sdg->term_id, 'sdg_order', true);
// Set SDG Slug as Array Index for current iteration, add the list of Areas
$results[$sdg_order] = array('areas' => $countryList);
// set arguments for WP_Query
$args = array(
'post_type' => 'organization',
'tax_query' => array(
array(
'taxonomy' => 'sdgs',
'field' => 'id',
'terms' => $sdg->term_id
)
)
);
$orgs = newWP_Query($args);
$howManyOrgs = 0;
// Let's loop this
while( $orgs->have_posts() ) : $orgs->the_post();
$terms = get_the_terms(get_the_ID(), 'countries');
foreach($termsas$country){
$iso2 = strtoupper($country->slug);
// add to specific SDG
$results[$sdg_order]['areas'][$iso2]['value']++;
$val = $results[$sdg_order]['areas'][$iso2]['value'];
if(TOOLTIPS){
$results[$sdg_order]['areas'][$iso2]['tooltip'] = array(
'cssClass' => 'orgmapTooltip',
'content' => '<h3>' . $country->name . '</h3><p><strong>' . $val . '</strong> ' . ($val !== 1 ? __('Organizations or Initiatives') : __('Organization or Initiative') ) . '</p>'
);
}
/*
// add to Full List
$results['all']['areas'][$iso2]['value']++;
$allVal = $results['all']['areas'][$iso2]['value'];
$results['all']['areas'][$iso2]['tooltip'] = array(
'cssClass' => 'orgmapTooltip',
'content' => '<h3>' . $country->name . '</h3><p><strong>' . $countryList[$iso2]->count . '</strong> ' . ($allVal !== 1 ? __('Organizations/Initiatives') : __('Organization/Initiative') ) . '</p>'
);
*/
}
$howManyOrgs++;
endwhile;
// clean up post data
wp_reset_postdata();
}
?>
<div class="orgmap">
<div class="map"></div>
<div class="areaLegend"></div>
</div>
<div class="orgmap-sdg-list">
<p><?phpecho__('Filter the Map by SDG:', 'orgmap'); ?> <span id="orgmap-filter">ALL</span></p>
<div class="areaLegend"></div>
<ul class="sdg-icon-list">
<?php
if(!empty($sdgs) && !is_wp_error($sdgs)){
foreach($sdgsas$sdg){
$sdg_order = get_term_meta($sdg->term_id, 'sdg_order', true);
?>
<li <?phpecho (ICONSTRIP ? 'class="icon-strip"' : ''); ?>>
<a href="#" class="orgmap-map-control sdg-taxonomy-term sdg-list-element sdg-icon sdg-term-<?phpecho$sdg_order; ?>" data-sdg="<?phpecho$sdg_order; ?>" data-sdg-name="<?phpecho$sdg->name; ?>"></a>
</li>
<?php
}
}
?>
<li <?phpecho (ICONSTRIP ? 'class="icon-strip"' : ''); ?>><a href="#" class="orgmap-map-control sdg-taxonomy-term sdg-list-element sdg-icon sdg-term-all" data-sdg="all" data-sdg-name="All SDGs">ALL</a>
</ul>
</div>
<script src="<?phpechoplugin_dir_url(__FILE__); ?>js/maps/world_countries_miller.js"></script>
<script>
jQuery(document).ready(function(){
/** Init Map **/
var theData = <?phpechojson_encode($results, JSON_FORCE_OBJECT); ?>;
jQuery('.orgmap-map-control').click(function(e){
e.preventDefault();
var sdg = jQuery(this).data('sdg');
var filterName = jQuery(this).data('sdg-name');
jQuery('.orgmap-map-control').fadeTo(200, 0.4);
jQuery(this).fadeTo(200, 1);;
jQuery('#orgmap-filter').removeClass(function() {
return jQuery(this).attr("class")
}).addClass('sdg-term-' + sdg).html(filterName);
jQuery('.orgmap').trigger('update', [{
mapOptions: theData[sdg],
//replaceOptions: true,
animDuration: 300
}]);
});
jQuery('.orgmap').mapael({
map : {
name : "world_countries_miller",
defaultArea: {
attrs: {
fill: "white",
stroke: "#232323",
"stroke-width": 0.3
},
attrsHover: {
fill: "#EAF9C0"
}
},
zoom: {
enabled: true,
step: 0.25,
maxLevel: 20
}
},
legend: {
area: {
display: false,
slices: [
{
min: 0,
max: 0,
attrs: {
fill: "white"
},
label: "No Organization"
},
{
min: 1,
max: 1,
attrs: {
fill: "#758F77"
},
label: "1 Organization"
},
{
min: 1,
max: <?phpecho$levels['lower']; ?>,
attrs: {
fill: "#537355"
},
label: "Between 1 and <?phpecho$levels['lower']; ?> Organizations"
},
{
min: <?phpecho$levels['lower']; ?>,
max: <?phpecho$levels['higher']; ?>,
attrs: {
fill: "#375339"
},
label: "Between <?phpecho$levels['lower']; ?> and <?phpecho$levels['higher']; ?> Organizations"
},
{
min: <?phpecho$levels['higher']; ?>,
attrs: {
fill: "#1E2E20"
},
label: "More than <?phpecho$levels['higher']; ?> Organizations"
}
]
},
},
areas: theData["all"]["areas"]
});
});
</script>
<?php
}