PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.2
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / shortcodes / class-charitable-stat-shortcode.php

class-charitable-stat-shortcode.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.6.2, at includes/shortcodes/class-charitable-stat-shortcode.php

150 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for parsing and displaying the output of the [charitable_stat] shortcode.
4 *
5 * @package Charitable_Stat/Classes/Charitable_Stat_Shortcode
6 * @author Eric Daams
7 * @copyright Copyright (c) 2018, Studio 164a
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.6.0
10 * @version 1.6.0
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Stat_Shortcode' ) ) :
19
20 /**
21 * Charitable_Stat_Shortcode
22 *
23 * @since 1.6.0
24 */
25 class Charitable_Stat_Shortcode {
26
27 /**
28 * The type of query.
29 *
30 * @since 1.6.0
31 *
32 * @var string
33 */
34 private $type;
35
36 /**
37 * Mixed set of arguments for the query.
38 *
39 * @since 1.6.0
40 *
41 * @var array
42 */
43 private $args;
44
45 /**
46 * Create class object.
47 *
48 * @since 1.6.0
49 *
50 * @param array $atts User-defined attributes.
51 */
52 private function __construct( $atts ) {
53 $this->args = $this->parse_args( $atts );
54 $this->type = $this->args['display'];
55 $this->report = $this->get_report();
56 }
57
58 /**
59 * Create class object.
60 *
61 * @since 1.6.0
62 *
63 * @param array $atts User-defined attributes.
64 * @return string
65 */
66 public static function display( $atts ) {
67 $object = new Charitable_Stat_Shortcode( $atts );
68
69 return $object->get_query_result();
70 }
71
72 /**
73 * Return the query result.
74 *
75 * @since 1.6.0
76 *
77 * @return string
78 */
79 public function get_query_result() {
80 switch ( $this->type ) {
81 case 'progress':
82 $total = $this->report->get_report( 'amount' );
83
84 if ( ! $this->args['goal'] ) {
85 return charitable_format_money( $total );
86 }
87
88 $percent = ( $total / $this->args['goal'] ) * 100;
89
90 return '<div class="campaign-progress-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="' . $percent . '"><span class="bar" style="width:' . $percent . '%;"></span></div>';
91
92 case 'total':
93 return charitable_format_money( $this->report->get_report( 'amount' ) );
94
95 case 'donors':
96 case 'donations':
97 return (string) $this->report->get_report( $this->type );
98 }
99 }
100
101 /**
102 * Parse shortcode attributes.
103 *
104 * @since 1.6.0
105 *
106 * @param array $atts User-defined attributes.
107 * @return array
108 */
109 private function parse_args( $atts ) {
110 $defaults = array(
111 'display' => 'total',
112 'campaigns' => '',
113 'goal' => false,
114 );
115
116 $args = shortcode_atts( $defaults, $atts, 'charitable_stat' );
117 $args['campaigns'] = strlen( $args['campaigns'] ) ? explode( ',', $args['campaigns'] ) : array();
118
119 return $args;
120 }
121
122 /**
123 * Run the report for the shortcode.
124 *
125 * @since 1.6.0
126 *
127 * @return Charitable_Donation_Report
128 */
129 private function get_report() {
130 return new Charitable_Donation_Report( $this->get_report_args() );
131 }
132
133 /**
134 * Return the arguments used for generating the report.
135 *
136 * @since 1.6.0
137 *
138 * @return array
139 */
140 private function get_report_args() {
141 $args = array();
142 $args['report_type'] = in_array( $this->type, array( 'progress', 'total' ) ) ? 'amount' : $this->type;
143 $args['campaigns'] = $this->args['campaigns'];
144
145 return $args;
146 }
147 }
148
149 endif;
150