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