PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.7
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.7
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.7, at includes/shortcodes/class-charitable-stat-shortcode.php

151 lines 3.3 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 $total = charitable_sanitize_amount( $total, true );
89 $percent = ( $total / $this->args['goal'] ) * 100;
90
91 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>';
92
93 case 'total':
94 return charitable_format_money( $this->report->get_report( 'amount' ) );
95
96 case 'donors':
97 case 'donations':
98 return (string) $this->report->get_report( $this->type );
99 }
100 }
101
102 /**
103 * Parse shortcode attributes.
104 *
105 * @since 1.6.0
106 *
107 * @param array $atts User-defined attributes.
108 * @return array
109 */
110 private function parse_args( $atts ) {
111 $defaults = array(
112 'display' => 'total',
113 'campaigns' => '',
114 'goal' => false,
115 );
116
117 $args = shortcode_atts( $defaults, $atts, 'charitable_stat' );
118 $args['campaigns'] = strlen( $args['campaigns'] ) ? explode( ',', $args['campaigns'] ) : array();
119
120 return $args;
121 }
122
123 /**
124 * Run the report for the shortcode.
125 *
126 * @since 1.6.0
127 *
128 * @return Charitable_Donation_Report
129 */
130 private function get_report() {
131 return new Charitable_Donation_Report( $this->get_report_args() );
132 }
133
134 /**
135 * Return the arguments used for generating the report.
136 *
137 * @since 1.6.0
138 *
139 * @return array
140 */
141 private function get_report_args() {
142 $args = array();
143 $args['report_type'] = in_array( $this->type, array( 'progress', 'total' ) ) ? 'amount' : $this->type;
144 $args['campaigns'] = $this->args['campaigns'];
145
146 return $args;
147 }
148 }
149
150 endif;
151