forked from geomarts/chartsjs-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
90 lines (80 loc) · 2.91 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
<?php
function playground_scripts_and_styles() {
$theme_uri = get_template_directory_uri();
wp_enqueue_style( 'bootstrap-style', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css' );
wp_enqueue_style( 'playground-style', $theme_uri . '/style.css' );
wp_enqueue_script( 'chart', 'https://cdn.jsdelivr.net/npm/chart.js' );
wp_enqueue_script( 'bootstrap-script', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js' );
//single chart
if ( is_page_template( 'page-templates/single-chart.php' ) ) :
wp_enqueue_script( 'playground-script-single-chart', $theme_uri . '/assets/js/single-chart.js', array(), null, true );
wp_localize_script(
'playground-script-single-chart',
'global_obj',
array(
'chart' => get_single_chart(),
)
);
endif;
//tabs with multiple charts
if ( is_page_template( 'page-templates/tabs.php' ) ) :
wp_enqueue_script( 'playground-script-multiple-charts', $theme_uri . '/assets/js/multiple-charts.js', array(), null, true );
wp_localize_script(
'playground-script-multiple-charts',
'global_obj',
array(
'charts' => get_multiple_charts(),
)
);
endif;
}
add_action( 'wp_enqueue_scripts', 'playground_scripts_and_styles' );
function get_single_chart() {
$chart_single = (object) array(
'legends' => (object) array(),
'labels' => array(),
'bars' => (object) array(
'bar1' => array(),
'bar2' => array(),
),
'title' => '',
);
$chart = get_field( 'chart' );
$chart_legends = $chart['legends'];
$chart_single->title = esc_html( $chart['title'] );
$chart_single->legends->legend1 = esc_html( $chart_legends['legend1'] );
$chart_single->legends->legend2 = esc_html( $chart_legends['legend2'] );
foreach ( $chart['rows'] as $row ) :
array_push( $chart_single->labels, esc_html( $row['label'] ) );
array_push( $chart_single->bars->bar1, esc_html( $row['bar1'] ) );
array_push( $chart_single->bars->bar2, esc_html( $row['bar2'] ) );
endforeach;
return $chart_single;
}
function get_multiple_charts() {
$charts = array();
$tabs = get_field( 'tabs' );
foreach ( $tabs as $tab ) :
$chart_single = (object) array(
'legends' => (object) array(),
'labels' => array(),
'bars' => (object) array(
'bar1' => array(),
'bar2' => array(),
),
'title' => '',
);
$chart = $tab['chart'];
$chart_legends = $chart['legends'];
$chart_single->title = esc_html( $chart['title'] );
$chart_single->legends->legend1 = esc_html( $chart_legends['legend1'] );
$chart_single->legends->legend2 = esc_html( $chart_legends['legend2'] );
foreach ( $chart['rows'] as $row ) :
array_push( $chart_single->labels, esc_html( $row['label'] ) );
array_push( $chart_single->bars->bar1, esc_html( $row['bar1'] ) );
array_push( $chart_single->bars->bar2, esc_html( $row['bar2'] ) );
endforeach;
array_push( $charts, $chart_single );
endforeach;
return $charts;
}