-
Notifications
You must be signed in to change notification settings - Fork 0
/
pew-research-charts.php
508 lines (425 loc) · 14.6 KB
/
pew-research-charts.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/*
Plugin Name: Pew Research Charts
Description: Interactive charts within WordPress are as easy as a custom post type
Version: 0.5
Author: Adam Nekola and Russell Heimlich
Author URI: http://www.pewresearch.org
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Clear rewrite rules on activation
*/
function pew_charts_plugin_activate() {
pew_charts_init();
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'pew_charts_plugin_activate' );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
/**
* Compatability: array_replace_recursive() is PHP 5.3 and greater
* For more: http://php.net/manual/en/function.array-replace-recursive.php
*/
if (!function_exists('array_replace_recursive')){
function array_replace_recursive($array, $array1){
if (!function_exists('recurse')){
function recurse($array, $array1){
foreach ($array1 as $key => $value)
{
if (!isset($array[$key]) || (isset($array[$key]) && !is_array($array[$key])))
{
$array[$key] = array();
}
if (is_array($value))
{
$value = recurse($array[$key], $value);
}
$array[$key] = $value;
}
return $array;
}
}
$args = func_get_args();
$array = $args[0];
if (!is_array($array))
{
return $array;
}
for ($i = 1; $i < count($args); $i++)
{
if (is_array($args[$i]))
{
$array = recurse($array, $args[$i]);
}
}
return $array;
}
}
global $pew_chart_options;
$pew_chart_options = array();
/* ------------------------- */
/* Create a custom post type */
/* ------------------------- */
function pew_charts_init() {
$labels = array(
'name' => 'Charts',
'singular_name' => 'Chart',
'menu_name' => 'Charts',
'parent_item_colon' => 'Parent Chart:',
'all_items' => 'All Charts',
'view_item' => 'View Chart',
'add_new_item' => 'Add New Chart',
'add_new' => 'Add New',
'edit_item' => 'Edit Chart',
'update_item' => 'Update Chart',
'search_items' => 'Search Chart',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
$args = array(
'label' => 'charts',
'description' => 'charts',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 60,
'menu_icon' => 'dashicons-chart-area',
'can_export' => true,
'has_archive' => 'charts',
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array(
'pages' => false
)
);
register_post_type( 'chart', $args );
}
add_action( 'init', 'pew_charts_init', 0 );
/**
* Making charts accessible with iframes
*/
function pew_charts_rewrite( $rules ) {
$new = array(
'chart/([^/]+)/iframe/?$' => 'index.php?chart=$matches[1]&iframe=1'
);
return array_merge($new, $rules);
}
add_filter( 'rewrite_rules_array', 'pew_charts_rewrite' );
/**
* New query vars for iframe rewrite rule Making these accessible with iframes
*/
function pew_charts_query_vars($query_vars) {
if (!in_array('iframe', $query_vars)) $query_vars[] = 'iframe';
return $query_vars;
}
add_filter( 'query_vars', 'pew_charts_query_vars' );
/* ----------------------------- */
/* Template heirarchy for iframe */
/* ----------------------------- */
function pew_charts_template( $template_path ) {
if ( get_post_type() == 'chart' ) {
if ( get_query_var('iframe') ) {
if ( $theme_file = locate_template( array ( 'single-chart-iframe.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . 'templates/single-chart-iframe.php';
}
}
}
return $template_path;
}
add_filter( 'template_include', 'pew_charts_template', 1 );
/**
* Header for iframe
*/
function get_pew_charts_header( $name = null ) {
do_action( 'get_header', $name);
if ( $theme_file = locate_template( array ( 'header-iframe.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . 'templates/header-iframe.php';
}
load_template( $template_path );
}
/* -------------------------------------- */
/* Register script, enqueue on chart post */
/* -------------------------------------- */
function pew_charts_scripts(){
if ( !wp_script_is( 'highcharts', 'registered' ) ){
$site_options = get_option( 'pew_charts' );
if ( $site_options['highcharts'] != '' ) {
$highcharts_url = $site_options['highcharts'];
} else {
$highcharts_url = 'http://code.highcharts.com/4.1.8/highcharts.js';
}
wp_register_script('highcharts', $highcharts_url, array('jquery'), false, true);
}
if ( !wp_script_is( 'tinysort', 'registered' ) )
wp_register_script('tinysort', plugin_dir_url( __FILE__ ) . 'js/tinysort.min.js', array('jquery'), false, true);
if ( !wp_script_is( 'waypoints', 'registered' ) )
wp_register_script('waypoints', plugin_dir_url( __FILE__ ) . 'js/waypoints.min.js', array('jquery'), false, true);
if ( !wp_script_is( 'highcharts-regression', 'registered' ) )
wp_register_script('highcharts-regression', plugin_dir_url( __FILE__ ) . 'js/highcharts-regression.min.js', array('highcharts'), false, true);
wp_register_script('pew-research-charts', plugin_dir_url( __FILE__ ) . 'js/pew-research-charts.js', array('highcharts','tinysort'), false, true);
wp_register_style('pew-research-charts', plugin_dir_url( __FILE__ ) . 'css/pew-research-charts.css');
if ( get_post_type() == 'chart' ) {
wp_enqueue_script('pew-research-charts');
wp_enqueue_style('pew-research-charts');
}
}
add_action('wp_enqueue_scripts', 'pew_charts_scripts');
/* ----------------------------------- */
/* Shortcode for charts on other posts */
/* ----------------------------------- */
function pew_chart_shortcode( $atts ) {
global $add_chart_script;
$a = shortcode_atts( array(
'id' => 0,
'slug' => false,
'title' => false,
'classes' => false,
'width' => 'none',
'float' => 'none'
), $atts );
$id = $a['id'];
$slug = $a['slug'];
$classes = $a['classes'];
if ( !$id && !$slug ) {
//No ID or slug. We can't do anything with that so bail...
return '';
}
$args = array(
'post_type' => 'chart',
'post_status' => 'publish',
'showposts' => 1
);
if( $id ) {
$args['p'] = $id;
}
if( $slug ) {
$args['name'] = $slug;
}
if( is_preview() || is_user_logged_in() ) {
$args['post_status'] = array('publish', 'pending', 'draft', 'future', 'private');
}
$charts = get_posts( $args );
if( is_wp_error($charts) || !is_array($charts) || empty($charts) ) {
return '';
}
$chart = $charts[0];
pew_chart_prep_chart_options( $chart );
// This is make the script echo in the footer
$add_chart_script[] = $chart->ID;
// Ability to customize
$classes = explode(',', $classes);
$classes = array_merge($classes, array('embedded_chart', 'chart' . $chart->ID));
$chart_addl_classes = apply_filters('chart_addl_classes', $classes, $atts);
$chart_shortcode_title = apply_filters('chart_shortcode_title', ($a['title'] ? $a['title'] : get_the_title($chart->ID)));
$margin = ($a['float'] == 'left' ? '0 30px 0 0' : ($a['float'] == 'right' ? '0 0 0 30px' : '0'));
$chart_contain_style = apply_filters('chart_contain_style', 'max-width:'.$a['width'].'; float:'.$a['float'].'; margin:'.$margin);
// Now we print the title and the content
$html = '<div class="' . implode(' ', $chart_addl_classes) . '" style="'.$chart_contain_style.'">';
if ( current_user_can('edit_post', $chart->ID) ) {
$chart_shortcode_title .= ' | <a href="' . get_edit_post_link( $chart->ID ) . '" target="_blank">Edit</a>';
}
$html .= '<h3>' . $chart_shortcode_title . '</h3>';
$html .= wpautop(get_post_field('post_content', $chart->ID)); //Potential for infinite loop if the chart body has a [chart] shortcode in it.
$html .= '</div>';
$site_options = get_option( 'pew_charts' );
if( isset( $site_options['waypoints'] ) && !empty( $site_options['waypoints'] ) ) {
wp_enqueue_script('waypoints');
}
wp_enqueue_script('pew-research-charts');
wp_enqueue_style('pew-research-charts');
return $html;
}
add_shortcode( 'chart', 'pew_chart_shortcode' );
/*
* If it's a single chart page we need to be sure to prep the chart options properly and add them to the bottom of the page for the JavaScript
*/
function pew_chart_prep_single_chart_page() {
if( get_post_type() == 'chart' && is_single() ) {
pew_chart_prep_chart_options();
}
}
add_action( 'wp_head', 'pew_chart_prep_single_chart_page' );
function get_pew_chart_meta( $chart_id = false ) {
if( !$chart_id ) {
$post = get_post();
$chart_id = $post->ID;
}
// Use WordPress' caching functionality so if the same $chart_id is requested we can return the previous work we did...
$cache_key = 'pew_chart_options_' . $chart_id;
$data = wp_cache_get( $cache_key );
if( !$data ) {
$data = get_post_meta( $chart_id, 'chart_meta', true );
if ( !is_array($chart_meta) ) {
$chart_meta = array();
}
$whitelisted = array('credits', 'credits_link', 'chartheight', 'charttype', 'chartsubtitle', 'xaxistype', 'xaxislabel', 'yaxismax', 'yaxislabel', 'zoomtype', 'inverted', 'iframe', 'hidemarkers', 'args');
foreach( $whitelisted as $key ) {
if( !isset( $data[ $key ] ) ) {
$data[ $key ] = '';
}
}
wp_cache_set( $cache_key, $data );
}
return $data;
}
function pew_chart_prep_chart_options( $chart = FALSE ) {
global $pew_chart_options;
//Get site wide defaults
$site_options = get_option( 'pew_charts' );
if( isset( $site_options['defaults'] ) && !empty( $site_options['defaults'] ) ) {
$default_options = json_decode( $site_options['defaults'] , true);
}
if( !$default_options ) {
$default_options = array();
}
//$chart is a $post object
if( !$chart ) {
$site_options['waypoints'] = false;
$chart = get_post();
}
//Get chart options
$options = get_pew_chart_meta( $chart->ID );
if( isset( $options['args'] ) && !empty( $options['args'] ) ) {
if(version_compare(phpversion(), '5.3.0', '>=')) {
$custom_chart_options = json_decode($options['args'], true, 10);
}
else {
$custom_chart_options = json_decode($options['args'], true);
}
}
if( !$custom_chart_options ) {
$custom_chart_options = array();
}
$chart_options = array(
'chart' => array(),
'subtitle' => array(),
'xAxis' => array(
'title' => array()
),
'yAxis' => array(
'title' => array()
),
'plotOptions' => array(
'line' => array(
'marker' => array(
'enabled' => true
)
),
'area' => array(
'marker' => array(
'enabled' => true
)
),
'bar' => array(
'stacking' => null ),
'column' => array(
'stacking' => null ),
'pie' => array(
'visible' => true,
'dataLabels' => array(
'enabled' => true
)
)
)
);
if( $options['charttype'] ) {
$chart_options['chart']['type'] = $options['charttype'];
}
if( $options['zoomtype'] && $options['zoomtype'] != 'none' ) {
$chart_options['chart']['zoomType'] = $options['zoomtype'];
}
if( $options['chartsubtitle'] && $options['chartsubtitle'] != 'none' ) {
$chart_options['subtitle']['text'] = $options['chartsubtitle'];
}
if( $options['seriesstacking'] == true ) {
$chart_options['plotOptions']['area']['stacking'] = 'normal';
$chart_options['plotOptions']['bar']['stacking'] = 'normal';
$chart_options['plotOptions']['column']['stacking'] = 'normal';
$chart_options['plotOptions']['line']['stacking'] = 'normal';
}
if( $options['inverted'] && $options['inverted'] != 'none' ) {
$chart_options['chart']['inverted'] = true;
}
if( $options['xaxislabel'] && $options['xaxislabel'] != 'none' ) {
$chart_options['xAxis']['title']['text'] = $options['xaxislabel'];
}
if( $options['yaxislabel'] && $options['yaxislabel'] != 'none' ) {
$chart_options['yAxis']['title']['text'] = $options['yaxislabel'];
}
if( $options['yaxismax'] && $options['yaxismax'] != 'none' ) {
$chart_options['yAxis']['max'] = $options['yaxismax'];
}
if( $options['xaxistype'] && $options['xaxistype'] != 'none' ) {
$chart_options['xAxis']['type'] = $options['xaxistype'];
}
if( $options['hidemarkers'] ) {
$chart_options['plotOptions']['line']['marker']['enabled'] = false;
$chart_options['plotOptions']['area']['marker']['enabled'] = false;
}
$chart_options['html'] = array(
'waypoints' => $site_options['waypoints'],
'data_tab' => _('Data'),
'chart_tab' => _('Chart'),
'height' => ( $options['chartheight'] && preg_match('/[0-9]+([px]{2}|)$/i', $options['chartheight']) ? str_replace(array('px','PX'),'',$options['chartheight']).'px' : '400px'),
'iframe' => $options['iframe'],
'iframe_tab' => _('Embed'),
'iframe_text' => _('Copy and paste the below iframe code into your own website to embed this chart.'),
'URL' => get_permalink( $chart->ID ),
'id' => $chart->ID,
'domain' => get_site_url(),
'credits' => ($options['credits'] ? $options['credits'] : $site_options['credits']),
'creditsURL' => ($options['credits_link'] ? $options['credits_link'] : $site_options['credits_link']),
'creditText' => _('Source: ')
);
$js_options = array_replace_recursive( (array) $default_options, $chart_options, (array) $custom_chart_options );
$js_options = apply_filters( 'pew_chart_options', $js_options, $chart );
$pew_chart_options[] = json_encode( $js_options );
}
function print_pew_chart_options() {
global $pew_chart_options;
$post = get_post();
if( is_admin() ||
is_home() ||
!is_single() &&
get_post_type() != 'charts' &&
!has_shortcode( $post->post_content, 'chart' )
) {
return;
}
?>
<script>
jQuery(document).ready(function($) {
var pewChartOptions = [<?php echo implode( $pew_chart_options, ', ' ); ?>];
$('table.pew-chart').each(function(index, table) {
//Having the debugbar plugin enabled can pickup extra tables and wreak havoc...
if( index >= pewChartOptions.length ) {
return true;
}
$table = $(table);
if( pewChartOptions[index].chart.type == 'none' ) {
return true; //Skip this iteration and go on to the next one.
}
new Highcharts.visualize( $table, pewChartOptions[index] );
});
});
</script>
<?php }
add_action( 'wp_footer', 'print_pew_chart_options', 99 );
/*
* Include the admin code.
*/
if( is_admin() ) {
include plugin_dir_path( __FILE__ ) . '/pew-research-charts-admin.php';
include plugin_dir_path( __FILE__ ) . '/pew-research-charts-csv-import.php';
}