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