Model.php
285 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\MultiFormGoals\ProgressBar; |
| 4 | |
| 5 | |
| 6 | use Give\ValueObjects\Money; |
| 7 | |
| 8 | class Model { |
| 9 | |
| 10 | // Settings |
| 11 | protected $ids; |
| 12 | protected $tags; |
| 13 | protected $categories; |
| 14 | protected $goal; |
| 15 | protected $enddate; |
| 16 | protected $color; |
| 17 | |
| 18 | // Internal |
| 19 | protected $forms = []; |
| 20 | protected $donationRevenueResults; |
| 21 | |
| 22 | /** |
| 23 | * Constructs and sets up setting variables for a new Progress Bar model |
| 24 | * |
| 25 | * @param array $args Arguments for new Progress Bar, including 'ids' |
| 26 | * @since 2.9.0 |
| 27 | **/ |
| 28 | public function __construct( array $args ) { |
| 29 | isset( $args['ids'] ) ? $this->ids = $args['ids'] : $this->ids = []; |
| 30 | isset( $args['tags'] ) ? $this->tags = $args['tags'] : $this->tags = []; |
| 31 | isset( $args['categories'] ) ? $this->categories = $args['categories'] : $this->categories = []; |
| 32 | isset( $args['goal'] ) ? $this->goal = $args['goal'] : $this->goal = '1000'; |
| 33 | isset( $args['enddate'] ) ? $this->enddate = $args['enddate'] : $this->enddate = ''; |
| 34 | isset( $args['color'] ) ? $this->color = $args['color'] : $this->color = '#28c77b'; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get forms associated with Progress Bar |
| 39 | * |
| 40 | * @return array |
| 41 | * @since 2.9.0 |
| 42 | **/ |
| 43 | protected function getForms() { |
| 44 | |
| 45 | if ( ! empty( $this->forms ) ) { |
| 46 | return $this->forms; |
| 47 | } |
| 48 | |
| 49 | $query_args = [ |
| 50 | 'post_type' => 'give_forms', |
| 51 | 'post_status' => 'publish', |
| 52 | 'post__in' => $this->ids, |
| 53 | 'posts_per_page' => - 1, |
| 54 | 'fields' => 'ids', |
| 55 | 'tax_query' => [ |
| 56 | 'relation' => 'AND', |
| 57 | ], |
| 58 | ]; |
| 59 | |
| 60 | if ( ! empty( $this->tags ) ) { |
| 61 | $query_args['tax_query'][] = [ |
| 62 | 'taxonomy' => 'give_forms_tag', |
| 63 | 'terms' => $this->tags, |
| 64 | ]; |
| 65 | } |
| 66 | |
| 67 | if ( ! empty( $this->categories ) ) { |
| 68 | $query_args['tax_query'][] = [ |
| 69 | 'taxonomy' => 'give_forms_category', |
| 70 | 'terms' => $this->categories, |
| 71 | ]; |
| 72 | } |
| 73 | |
| 74 | $query = new \WP_Query( $query_args ); |
| 75 | |
| 76 | if ( $query->posts ) { |
| 77 | $this->forms = $query->posts; |
| 78 | return $query->posts; |
| 79 | } else { |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | protected function getDonations() { |
| 85 | $query_args = [ |
| 86 | 'post_status' => [ |
| 87 | 'publish', |
| 88 | 'give_subscription', |
| 89 | ], |
| 90 | 'number' => -1, |
| 91 | 'give_forms' => $this->getForms(), |
| 92 | ]; |
| 93 | $query = new \Give_Payments_Query( $query_args ); |
| 94 | return $query->get_payments(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get output markup for Progress Bar |
| 99 | * |
| 100 | * @return string |
| 101 | * @since 2.9.0 |
| 102 | **/ |
| 103 | public function getOutput() { |
| 104 | ob_start(); |
| 105 | $output = ''; |
| 106 | require $this->getTemplatePath(); |
| 107 | $output = ob_get_contents(); |
| 108 | ob_end_clean(); |
| 109 | return $output; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns query results for Donation Revenue. |
| 114 | * @since 2.9.0 |
| 115 | * @return stdClass seem MultiFormGoals/ProgressBar/Query.php |
| 116 | */ |
| 117 | protected function getDonationRevenueResults() { |
| 118 | if ( ! $this->donationRevenueResults ) { |
| 119 | $query = new Query( $this->getForms() ); |
| 120 | $this->donationRevenueResults = $query->getResults(); |
| 121 | } |
| 122 | return $this->donationRevenueResults; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get raw earnings value for Progress Bar |
| 127 | * |
| 128 | * @return string |
| 129 | * @since 2.9.0 |
| 130 | **/ |
| 131 | protected function getTotal() { |
| 132 | $query = new Query( $this->getForms() ); |
| 133 | $results = $query->getResults(); |
| 134 | return Money::ofMinor( $results->total, give_get_option( 'currency' ) )->getAmount(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get number of donations for Progress Bar |
| 139 | * |
| 140 | * @return int |
| 141 | * @since 2.9.0 |
| 142 | **/ |
| 143 | protected function getDonationCount() { |
| 144 | $results = $this->getDonationRevenueResults(); |
| 145 | return $results->count; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get formatted total remaining (ex: $75) |
| 150 | * |
| 151 | * @since 2.9.0 |
| 152 | */ |
| 153 | protected function getFormattedTotalRemaining() { |
| 154 | $total_remaining = ( $this->getGoal() - $this->getTotal() ) > 0 ? ( $this->getGoal() - $this->getTotal() ) : 0; |
| 155 | return give_currency_filter( |
| 156 | give_format_amount( |
| 157 | $total_remaining, |
| 158 | [ |
| 159 | 'sanitize' => false, |
| 160 | 'decimal' => false, |
| 161 | ] |
| 162 | ) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get goal for Progress Bar |
| 168 | * |
| 169 | * @return string |
| 170 | * @since 2.9.0 |
| 171 | **/ |
| 172 | protected function getGoal() { |
| 173 | return $this->goal; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get goal color for Progress Bar |
| 178 | * |
| 179 | * @return string |
| 180 | * @since 2.9.0 |
| 181 | **/ |
| 182 | protected function getColor() { |
| 183 | return $this->color; |
| 184 | } |
| 185 | /** |
| 186 | * Get template path for Progress Bar component template |
| 187 | * @since 2.9.0 |
| 188 | **/ |
| 189 | public function getTemplatePath() { |
| 190 | return GIVE_PLUGIN_DIR . '/src/MultiFormGoals/resources/views/progressbar.php'; |
| 191 | } |
| 192 | |
| 193 | protected function getFormattedTotal() { |
| 194 | return give_currency_filter( |
| 195 | give_format_amount( |
| 196 | $this->getTotal(), |
| 197 | [ |
| 198 | 'sanitize' => false, |
| 199 | 'decimal' => false, |
| 200 | ] |
| 201 | ) |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | protected function getFormattedGoal() { |
| 206 | return give_currency_filter( |
| 207 | give_format_amount( |
| 208 | $this->getGoal(), |
| 209 | [ |
| 210 | 'sanitize' => false, |
| 211 | 'decimal' => false, |
| 212 | ] |
| 213 | ) |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Get end date for Progress Bar |
| 219 | * |
| 220 | * @return string |
| 221 | * @since 2.9.0 |
| 222 | **/ |
| 223 | protected function getEndDate() { |
| 224 | return $this->enddate; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Get minutes remaining before Progress Bar end date |
| 229 | * |
| 230 | * @return string |
| 231 | * @since 2.9.0 |
| 232 | **/ |
| 233 | protected function getMinutesRemaining() { |
| 234 | $enddate = strtotime( $this->getEndDate() ); |
| 235 | if ( $enddate ) { |
| 236 | $now = current_time( 'timestamp', false ); |
| 237 | return $now < $enddate ? ( $enddate - $now ) / 60 : 0; |
| 238 | } else { |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Get time remaining before Progress Bar end date |
| 245 | * |
| 246 | * @return string |
| 247 | * @since 2.9.0 |
| 248 | **/ |
| 249 | protected function getTimeToGo() { |
| 250 | $minutes = $this->getMinutesRemaining(); |
| 251 | switch ( $minutes ) { |
| 252 | case $minutes > 1440: { |
| 253 | return round( $minutes / 1440 ); |
| 254 | } |
| 255 | case $minutes < 1440 && $minutes > 60: { |
| 256 | return round( $minutes / 60 ); |
| 257 | } |
| 258 | case $minutes < 60: { |
| 259 | return round( $minutes ); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Get time remaining before Progress Bar end date |
| 266 | * |
| 267 | * @return string |
| 268 | * @since 2.9.0 |
| 269 | **/ |
| 270 | protected function getTimeToGoLabel() { |
| 271 | $minutes = $this->getMinutesRemaining(); |
| 272 | switch ( $minutes ) { |
| 273 | case $minutes > 1440: { |
| 274 | return _n( 'day to go', 'days to go', $this->getTimeToGo(), 'give' ); |
| 275 | } |
| 276 | case $minutes < 1440 && $minutes > 60: { |
| 277 | return _n( 'hour to go', 'hours to go', $this->getTimeToGo(), 'give' ); |
| 278 | } |
| 279 | case $minutes < 60: { |
| 280 | return _n( 'minute to go', 'minutes to go', $this->getTimeToGo(), 'give' ); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 |