-
Notifications
You must be signed in to change notification settings - Fork 0
/
donationgoalthermometer-functions.php
46 lines (34 loc) · 1.23 KB
/
donationgoalthermometer-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
<?php
/*
* Custom functions for the Donation Goal Thermometer plugin.
*/
function dgthermometer_shortcode( $atts = array() ) {
// Get atts and assign vars.
$donated_total = '';
if ( ! empty( $atts['donated'] ) ) {
$donated_total = $atts['donated'];
}
$goal_total = '';
if ( ! empty( $atts['goal'] ) ) {
$goal_total = $atts['goal'];
}
$data_pct = ( floatval( $donated_total ) / floatval( $goal_total ) ) * 100;
// Round pct to 2 decimal places.
$data_pct = number_format( $data_pct, 2, '.', '' );
$vertical = 'vertical' === $atts[0] ? 'vertical' : '';
$orientation = 'vertical' === $atts[0] ? 'data-orientation="vertical"' : '';
ob_start();
echo '<section class="donation-goal-thermometer ' . $vertical . '">';
echo '<div class="meter" data-percent="' . $data_pct . '" data-donated="' . $donated_total . '" data-goal="' . $goal_total . '" ' . $orientation . '></div>';
echo '</section>';
// Put contents into a var.
$output = ob_get_contents();
// Clear the memory.
ob_end_clean();
// Output everything.
return $output;
}
function dgthermometer_register_shortcodes() {
add_shortcode( 'donation-goal-thermometer', 'dgthermometer_shortcode' );
}
add_action( 'init', 'dgthermometer_register_shortcodes' );