class-donor-reports-table.php
8 years ago
class-earnings-report.php
8 years ago
class-form-reports-table.php
8 years ago
class-forms-report.php
8 years ago
class-gateways-report.php
8 years ago
class-gateways-reports-table.php
8 years ago
class-give-graph.php
8 years ago
graphing.php
8 years ago
reports.php
8 years ago
graphing.php
853 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Graphing Functions |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Reports |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Show report graphs |
| 19 | * |
| 20 | * @since 1.0 |
| 21 | * @return void |
| 22 | */ |
| 23 | function give_reports_graph() { |
| 24 | // Retrieve the queried dates. |
| 25 | $donation_stats = new Give_Payment_Stats(); |
| 26 | $dates = give_get_report_dates(); |
| 27 | |
| 28 | // Determine graph options. |
| 29 | switch ( $dates['range'] ) : |
| 30 | case 'today' : |
| 31 | case 'yesterday' : |
| 32 | $day_by_day = true; |
| 33 | break; |
| 34 | case 'last_year' : |
| 35 | case 'this_year' : |
| 36 | case 'last_quarter' : |
| 37 | case 'this_quarter' : |
| 38 | $day_by_day = false; |
| 39 | break; |
| 40 | case 'other' : |
| 41 | if ( $dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] && ( $dates['m_start'] != '12' && $dates['m_end'] != '1' ) ) { |
| 42 | $day_by_day = false; |
| 43 | } else { |
| 44 | $day_by_day = true; |
| 45 | } |
| 46 | break; |
| 47 | default: |
| 48 | $day_by_day = true; |
| 49 | break; |
| 50 | endswitch; |
| 51 | |
| 52 | $earnings_totals = 0.00; // Total earnings for time period shown. |
| 53 | $sales_totals = 0; // Total sales for time period shown. |
| 54 | |
| 55 | $earnings_data = array(); |
| 56 | $sales_data = array(); |
| 57 | |
| 58 | if ( 'today' === $dates['range'] || 'yesterday' === $dates['range'] ) { |
| 59 | |
| 60 | // Hour by hour. |
| 61 | $hour = 0; |
| 62 | $month = date( 'n', current_time( 'timestamp' ) ); |
| 63 | while ( $hour <= 23 ) : |
| 64 | |
| 65 | $start_date = mktime( $hour, 0, 0, $month, $dates['day'], $dates['year'] ); |
| 66 | $end_date = mktime( $hour, 59, 59, $month, $dates['day'], $dates['year'] ); |
| 67 | $sales = $donation_stats->get_sales( 0, $start_date, $end_date ); |
| 68 | $earnings = $donation_stats->get_earnings( 0, $start_date, $end_date ); |
| 69 | |
| 70 | $sales_totals += $sales; |
| 71 | $earnings_totals += $earnings; |
| 72 | |
| 73 | $sales_data[] = array( $start_date * 1000, $sales ); |
| 74 | $earnings_data[] = array( $start_date * 1000, $earnings ); |
| 75 | |
| 76 | $hour ++; |
| 77 | endwhile; |
| 78 | |
| 79 | } elseif ( 'this_week' === $dates['range'] || 'last_week' === $dates['range'] ) { |
| 80 | |
| 81 | // Day by day. |
| 82 | $day = $dates['day']; |
| 83 | $day_end = $dates['day_end']; |
| 84 | $month = $dates['m_start']; |
| 85 | while ( $day <= $day_end ) : |
| 86 | |
| 87 | $start_date = mktime( 0, 0, 0, $month, $day, $dates['year'] ); |
| 88 | $end_date = mktime( 23, 59, 59, $month, $day, $dates['year'] ); |
| 89 | $sales = $donation_stats->get_sales( 0, $start_date, $end_date ); |
| 90 | $earnings = $donation_stats->get_earnings( 0, $start_date, $end_date ); |
| 91 | |
| 92 | $sales_totals += $sales; |
| 93 | $earnings_totals += $earnings; |
| 94 | |
| 95 | $sales_data[] = array( $start_date * 1000, $sales ); |
| 96 | $earnings_data[] = array( $start_date * 1000, $earnings ); |
| 97 | $day ++; |
| 98 | endwhile; |
| 99 | |
| 100 | } else { |
| 101 | |
| 102 | $y = $dates['year']; |
| 103 | while ( $y <= $dates['year_end'] ) : |
| 104 | |
| 105 | if ( $dates['year'] === $dates['year_end'] ) { |
| 106 | $month_start = $dates['m_start']; |
| 107 | $month_end = $dates['m_end']; |
| 108 | } elseif ( $y === $dates['year'] ) { |
| 109 | $month_start = $dates['m_start']; |
| 110 | $month_end = 12; |
| 111 | } elseif ( $y === $dates['year_end'] ) { |
| 112 | $month_start = 1; |
| 113 | $month_end = $dates['m_end']; |
| 114 | } else { |
| 115 | $month_start = 1; |
| 116 | $month_end = 12; |
| 117 | } |
| 118 | |
| 119 | $i = $month_start; |
| 120 | while ( $i <= $month_end ) : |
| 121 | |
| 122 | if ( $day_by_day ) { |
| 123 | |
| 124 | if ( $i === $month_end ) { |
| 125 | |
| 126 | $num_of_days = $dates['day_end']; |
| 127 | |
| 128 | } else { |
| 129 | |
| 130 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
| 131 | |
| 132 | } |
| 133 | |
| 134 | $d = $dates['day']; |
| 135 | |
| 136 | while ( $d <= $num_of_days ) : |
| 137 | |
| 138 | $start_date = mktime( 0, 0, 0, $i, $d, $y ); |
| 139 | $end_date = mktime( 23, 59, 59, $i, $d, $y ); |
| 140 | $sales = $donation_stats->get_sales( 0, $start_date, $end_date ); |
| 141 | $earnings = $donation_stats->get_earnings( 0, $start_date, $end_date ); |
| 142 | |
| 143 | $sales_totals += $sales; |
| 144 | $earnings_totals += $earnings; |
| 145 | |
| 146 | $sales_data[] = array( $start_date * 1000, $sales ); |
| 147 | $earnings_data[] = array( $start_date * 1000, $earnings ); |
| 148 | |
| 149 | $d ++; |
| 150 | |
| 151 | endwhile; |
| 152 | |
| 153 | } else { |
| 154 | |
| 155 | // This Quarter, Last Quarter, This Year, Last Year. |
| 156 | $start_date = mktime( 0, 0, 0, $i, 1, $y ); |
| 157 | $end_date = mktime( 23, 59, 59, $i + 1, 0, $y ); |
| 158 | $sales = $donation_stats->get_sales( 0, $start_date, $end_date ); |
| 159 | $earnings = $donation_stats->get_earnings( 0, $start_date, $end_date ); |
| 160 | |
| 161 | $sales_totals += $sales; |
| 162 | $earnings_totals += $earnings; |
| 163 | |
| 164 | $sales_data[] = array( $start_date * 1000, $sales ); |
| 165 | $earnings_data[] = array( $start_date * 1000, $earnings ); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | $i ++; |
| 170 | |
| 171 | endwhile; |
| 172 | |
| 173 | $y ++; |
| 174 | endwhile; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | $data = array( |
| 179 | __( 'Income', 'give' ) => $earnings_data, |
| 180 | __( 'Donations', 'give' ) => $sales_data |
| 181 | ); |
| 182 | |
| 183 | // start our own output buffer. |
| 184 | ob_start(); |
| 185 | ?> |
| 186 | |
| 187 | <div id="give-dashboard-widgets-wrap"> |
| 188 | <div class="metabox-holder" style="padding-top: 0;"> |
| 189 | <div class="postbox"> |
| 190 | <div class="inside"> |
| 191 | <?php give_reports_graph_controls(); ?> |
| 192 | <?php |
| 193 | $graph = new Give_Graph( $data, array( 'dataType' => array( 'amount', 'count' ) ) ); |
| 194 | $graph->set( 'x_mode', 'time' ); |
| 195 | $graph->set( 'multiple_y_axes', true ); |
| 196 | $graph->display(); |
| 197 | |
| 198 | if ( 'this_month' === $dates['range'] ) { |
| 199 | $estimated = give_estimated_monthly_stats(); |
| 200 | } |
| 201 | ?> |
| 202 | </div> |
| 203 | </div> |
| 204 | <table class="widefat reports-table alignleft" style="max-width:450px"> |
| 205 | <tbody> |
| 206 | <tr> |
| 207 | <th scope="row"><strong><?php _e( 'Total income for period:', 'give' ); ?></strong></th> |
| 208 | <td><?php echo give_currency_filter( give_format_amount( $earnings_totals, array( 'sanitize' => false ) ) ); ?></td> |
| 209 | </tr> |
| 210 | <tr class="alternate"> |
| 211 | <th scope="row"><strong><?php _e( 'Total donations for period:', 'give' ); ?><strong></th> |
| 212 | <td><?php echo $sales_totals; ?></td> |
| 213 | </tr> |
| 214 | <?php if ( 'this_month' === $dates['range'] ) : ?> |
| 215 | <tr> |
| 216 | <th scope="row"><strong><?php _e( 'Estimated monthly income:', 'give' ); ?></strong></th> |
| 217 | <td><?php echo give_currency_filter( give_format_amount( $estimated['earnings'], array( 'sanitize' => false ) ) ); ?></td> |
| 218 | </tr> |
| 219 | <tr class="alternate"> |
| 220 | <th scope="row"><strong><?php _e( 'Estimated monthly donations:', 'give' ); ?></strong></th> |
| 221 | <td><?php echo floor( $estimated['sales'] ); ?></td> |
| 222 | </tr> |
| 223 | <?php endif; ?> |
| 224 | </table> |
| 225 | |
| 226 | <?php |
| 227 | /** |
| 228 | * Fires on report graphs widget. |
| 229 | * |
| 230 | * Allows you to add additional stats to the widget. |
| 231 | * |
| 232 | * @since 1.0 |
| 233 | */ |
| 234 | do_action( 'give_reports_graph_additional_stats' ); |
| 235 | ?> |
| 236 | |
| 237 | </div> |
| 238 | </div> |
| 239 | <?php |
| 240 | // get output buffer contents and end our own buffer. |
| 241 | $output = ob_get_contents(); |
| 242 | ob_end_clean(); |
| 243 | |
| 244 | echo $output; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Show report graphs of a specific product |
| 249 | * |
| 250 | * @since 1.0 |
| 251 | * |
| 252 | * @return void |
| 253 | */ |
| 254 | function give_reports_graph_of_form( $form_id = 0 ) { |
| 255 | // Retrieve the queried dates. |
| 256 | $dates = give_get_report_dates(); |
| 257 | |
| 258 | // Determine graph options. |
| 259 | switch ( $dates['range'] ) : |
| 260 | case 'today' : |
| 261 | case 'yesterday' : |
| 262 | $day_by_day = true; |
| 263 | break; |
| 264 | case 'last_year' : |
| 265 | $day_by_day = false; |
| 266 | break; |
| 267 | case 'this_year' : |
| 268 | $day_by_day = false; |
| 269 | break; |
| 270 | case 'last_quarter' : |
| 271 | $day_by_day = false; |
| 272 | break; |
| 273 | case 'this_quarter' : |
| 274 | $day_by_day = false; |
| 275 | break; |
| 276 | case 'other' : |
| 277 | if ( $dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] ) { |
| 278 | $day_by_day = false; |
| 279 | } else { |
| 280 | $day_by_day = true; |
| 281 | } |
| 282 | break; |
| 283 | default: |
| 284 | $day_by_day = true; |
| 285 | break; |
| 286 | endswitch; |
| 287 | |
| 288 | $earnings_totals = (float) 0.00; // Total earnings for time period shown. |
| 289 | $sales_totals = 0; // Total sales for time period shown. |
| 290 | |
| 291 | $earnings_data = array(); |
| 292 | $sales_data = array(); |
| 293 | $stats = new Give_Payment_Stats; |
| 294 | |
| 295 | if ( $dates['range'] == 'today' || $dates['range'] == 'yesterday' ) { |
| 296 | |
| 297 | // Hour by hour |
| 298 | $month = $dates['m_start']; |
| 299 | $hour = 0; |
| 300 | $minute = 0; |
| 301 | $second = 0; |
| 302 | while ( $hour <= 23 ) : |
| 303 | |
| 304 | if ( $hour == 23 ) { |
| 305 | $minute = $second = 59; |
| 306 | } |
| 307 | |
| 308 | $date = mktime( $hour, $minute, $second, $month, $dates['day'], $dates['year'] ); |
| 309 | $date_end = mktime( $hour + 1, $minute, $second, $month, $dates['day'], $dates['year'] ); |
| 310 | |
| 311 | $sales = $stats->get_sales( $form_id, $date, $date_end ); |
| 312 | $sales_totals += $sales; |
| 313 | |
| 314 | $earnings = $stats->get_earnings( $form_id, $date, $date_end ); |
| 315 | $earnings_totals += $earnings; |
| 316 | |
| 317 | $sales_data[] = array( $date * 1000, $sales ); |
| 318 | $earnings_data[] = array( $date * 1000, $earnings ); |
| 319 | |
| 320 | $hour ++; |
| 321 | endwhile; |
| 322 | |
| 323 | } elseif ( $dates['range'] == 'this_week' || $dates['range'] == 'last_week' ) { |
| 324 | |
| 325 | //Day by day. |
| 326 | $day = $dates['day']; |
| 327 | $day_end = $dates['day_end']; |
| 328 | $month = $dates['m_start']; |
| 329 | while ( $day <= $day_end ) : |
| 330 | |
| 331 | $date = mktime( 0, 0, 0, $month, $day, $dates['year'] ); |
| 332 | $date_end = mktime( 0, 0, 0, $month, $day + 1, $dates['year'] ); |
| 333 | $sales = $stats->get_sales( $form_id, $date, $date_end ); |
| 334 | $sales_totals += $sales; |
| 335 | |
| 336 | $earnings = $stats->get_earnings( $form_id, $date, $date_end ); |
| 337 | $earnings_totals += $earnings; |
| 338 | |
| 339 | $sales_data[] = array( $date * 1000, $sales ); |
| 340 | $earnings_data[] = array( $date * 1000, $earnings ); |
| 341 | |
| 342 | $day ++; |
| 343 | endwhile; |
| 344 | |
| 345 | } else { |
| 346 | |
| 347 | $y = $dates['year']; |
| 348 | |
| 349 | while ( $y <= $dates['year_end'] ) : |
| 350 | |
| 351 | $last_year = false; |
| 352 | |
| 353 | if ( $dates['year'] == $dates['year_end'] ) { |
| 354 | $month_start = $dates['m_start']; |
| 355 | $month_end = $dates['m_end']; |
| 356 | $last_year = true; |
| 357 | } elseif ( $y == $dates['year'] ) { |
| 358 | $month_start = $dates['m_start']; |
| 359 | $month_end = 12; |
| 360 | } else { |
| 361 | $month_start = 1; |
| 362 | $month_end = 12; |
| 363 | } |
| 364 | |
| 365 | $i = $month_start; |
| 366 | while ( $i <= $month_end ) : |
| 367 | |
| 368 | if ( $day_by_day ) { |
| 369 | |
| 370 | if ( $i == $month_end && $last_year ) { |
| 371 | |
| 372 | $num_of_days = $dates['day_end']; |
| 373 | |
| 374 | } else { |
| 375 | |
| 376 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
| 377 | |
| 378 | } |
| 379 | |
| 380 | $d = $dates['day']; |
| 381 | while ( $d <= $num_of_days ) : |
| 382 | |
| 383 | $date = mktime( 0, 0, 0, $i, $d, $y ); |
| 384 | $end_date = mktime( 23, 59, 59, $i, $d, $y ); |
| 385 | |
| 386 | $sales = $stats->get_sales( $form_id, $date, $end_date ); |
| 387 | $sales_totals += $sales; |
| 388 | |
| 389 | $earnings = $stats->get_earnings( $form_id, $date, $end_date ); |
| 390 | $earnings_totals += $earnings; |
| 391 | |
| 392 | $sales_data[] = array( $date * 1000, $sales ); |
| 393 | $earnings_data[] = array( $date * 1000, $earnings ); |
| 394 | $d ++; |
| 395 | |
| 396 | endwhile; |
| 397 | |
| 398 | } else { |
| 399 | |
| 400 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
| 401 | |
| 402 | $date = mktime( 0, 0, 0, $i, 1, $y ); |
| 403 | $end_date = mktime( 23, 59, 59, $i, $num_of_days, $y ); |
| 404 | |
| 405 | $sales = $stats->get_sales( $form_id, $date, $end_date ); |
| 406 | $sales_totals += $sales; |
| 407 | |
| 408 | $earnings = $stats->get_earnings( $form_id, $date, $end_date ); |
| 409 | $earnings_totals += $earnings; |
| 410 | |
| 411 | $sales_data[] = array( $date * 1000, $sales ); |
| 412 | $earnings_data[] = array( $date * 1000, $earnings ); |
| 413 | |
| 414 | } |
| 415 | |
| 416 | $i ++; |
| 417 | |
| 418 | endwhile; |
| 419 | |
| 420 | $y ++; |
| 421 | endwhile; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | $data = array( |
| 426 | __( 'Income', 'give' ) => $earnings_data, |
| 427 | __( 'Donations', 'give' ) => $sales_data |
| 428 | ); |
| 429 | |
| 430 | ?> |
| 431 | <h3><span><?php |
| 432 | printf( |
| 433 | /* translators: %s: form title */ |
| 434 | esc_html__( 'Income Report for %s', 'give' ), |
| 435 | get_the_title( $form_id ) |
| 436 | ); |
| 437 | ?></span></h3> |
| 438 | <div id="give-dashboard-widgets-wrap"> |
| 439 | <div class="metabox-holder" style="padding-top: 0;"> |
| 440 | <div class="postbox"> |
| 441 | <div class="inside"> |
| 442 | <?php give_reports_graph_controls(); ?> |
| 443 | <?php |
| 444 | $graph = new Give_Graph( $data, array( 'dataType' => array( 'amount', 'count' ) ) ); |
| 445 | $graph->set( 'x_mode', 'time' ); |
| 446 | $graph->set( 'multiple_y_axes', true ); |
| 447 | $graph->display(); |
| 448 | ?> |
| 449 | </div> |
| 450 | </div> |
| 451 | <!--/.postbox --> |
| 452 | <table class="widefat reports-table alignleft" style="max-width:450px"> |
| 453 | <tbody> |
| 454 | <tr> |
| 455 | <th scope="row"><strong><?php _e( 'Total income for period:', 'give' ); ?></strong></th> |
| 456 | <td><?php echo give_currency_filter( give_format_amount( $earnings_totals, array( 'sanitize' => false ) ) ); ?></td> |
| 457 | </tr> |
| 458 | <tr class="alternate"> |
| 459 | <th scope="row"><strong><?php _e( 'Total donations for period:', 'give' ); ?></strong></th> |
| 460 | <td><?php echo $sales_totals; ?></td> |
| 461 | </tr> |
| 462 | <tr> |
| 463 | <th scope="row"><strong><?php _e( 'Average monthly income:', 'give' ); ?></strong></th> |
| 464 | <td><?php echo give_currency_filter( give_format_amount( give_get_average_monthly_form_earnings( $form_id ), array( 'sanitize' => false ) ) ); ?></td> |
| 465 | </tr> |
| 466 | <tr class="alternate"> |
| 467 | <th scope="row"><strong><?php _e( 'Average monthly donations:', 'give' ); ?></strong></th> |
| 468 | <td><?php echo number_format( give_get_average_monthly_form_sales( $form_id ), 0 ); ?></td> |
| 469 | </tr> |
| 470 | </tbody> |
| 471 | </table> |
| 472 | </div> |
| 473 | </div> |
| 474 | <?php |
| 475 | echo ob_get_clean(); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Show report graph date filters |
| 480 | * |
| 481 | * @since 1.0.0 |
| 482 | * @since 1.8.0 The hidden `view` field is replaced with `tab` field. |
| 483 | * |
| 484 | * @return void |
| 485 | */ |
| 486 | function give_reports_graph_controls() { |
| 487 | $date_options = apply_filters( 'give_report_date_options', array( |
| 488 | 'today' => __( 'Today', 'give' ), |
| 489 | 'yesterday' => __( 'Yesterday', 'give' ), |
| 490 | 'this_week' => __( 'This Week', 'give' ), |
| 491 | 'last_week' => __( 'Last Week', 'give' ), |
| 492 | 'this_month' => __( 'This Month', 'give' ), |
| 493 | 'last_month' => __( 'Last Month', 'give' ), |
| 494 | 'this_quarter' => __( 'This Quarter', 'give' ), |
| 495 | 'last_quarter' => __( 'Last Quarter', 'give' ), |
| 496 | 'this_year' => __( 'This Year', 'give' ), |
| 497 | 'last_year' => __( 'Last Year', 'give' ), |
| 498 | 'other' => __( 'Custom', 'give' ) |
| 499 | ) ); |
| 500 | |
| 501 | $dates = give_get_report_dates(); |
| 502 | $display = $dates['range'] == 'other' ? '' : 'display: none;'; |
| 503 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings'; |
| 504 | |
| 505 | if ( empty( $dates['day_end'] ) ) { |
| 506 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, date( 'n' ), date( 'Y' ) ); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Fires before displaying report graph date filters. |
| 511 | * |
| 512 | * @since 1.0 |
| 513 | */ |
| 514 | do_action( 'give_report_graph_controls_before' ); |
| 515 | ?> |
| 516 | <form id="give-graphs-filter" method="get"> |
| 517 | <div class="tablenav top"> |
| 518 | <div class="actions"> |
| 519 | |
| 520 | <input type="hidden" name="post_type" value="give_forms" /> |
| 521 | <input type="hidden" name="page" value="give-reports" /> |
| 522 | <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" /> |
| 523 | |
| 524 | <?php if ( isset( $_GET['form-id'] ) ) : ?> |
| 525 | <input type="hidden" name="form-id" value="<?php echo absint( $_GET['form-id'] ); ?>" /> |
| 526 | <?php endif; ?> |
| 527 | |
| 528 | <div id="give-graphs-date-options-wrap"> |
| 529 | <select id="give-graphs-date-options" name="range"> |
| 530 | <?php foreach ( $date_options as $key => $option ) : ?> |
| 531 | <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $dates['range'] ); ?>><?php echo esc_html( $option ); ?></option> |
| 532 | <?php endforeach; ?> |
| 533 | </select> |
| 534 | |
| 535 | <div id="give-date-range-options" style="<?php echo esc_attr( $display ); ?>"> |
| 536 | <span class="screen-reader-text"><?php _e( 'From', 'give' ); ?> </span> |
| 537 | <select id="give-graphs-month-start" name="m_start" aria-label="Start Month"> |
| 538 | <?php for ( $i = 1; $i <= 12; $i ++ ) : ?> |
| 539 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['m_start'] ) ); ?>><?php echo esc_html( give_month_num_to_name( $i ) ); ?></option> |
| 540 | <?php endfor; ?> |
| 541 | </select> |
| 542 | <select id="give-graphs-day-start" name="day" aria-label="Start Day"> |
| 543 | <?php for ( $i = 1; $i <= 31; $i ++ ) : ?> |
| 544 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 545 | <?php endfor; ?> |
| 546 | </select> |
| 547 | <select id="give-graphs-year-start" name="year" aria-label="Start Year"> |
| 548 | <?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?> |
| 549 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 550 | <?php endfor; ?> |
| 551 | </select> |
| 552 | <span class="screen-reader-text"><?php esc_html_e( 'To', 'give' ); ?> </span> |
| 553 | <span>–</span> |
| 554 | <select id="give-graphs-month-end" name="m_end" aria-label="End Month"> |
| 555 | <?php for ( $i = 1; $i <= 12; $i ++ ) : ?> |
| 556 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['m_end'] ) ); ?>><?php echo esc_html( give_month_num_to_name( $i ) ); ?></option> |
| 557 | <?php endfor; ?> |
| 558 | </select> |
| 559 | <select id="give-graphs-day-end" name="day_end" aria-label="End Day"> |
| 560 | <?php for ( $i = 1; $i <= 31; $i ++ ) : ?> |
| 561 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day_end'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 562 | <?php endfor; ?> |
| 563 | </select> |
| 564 | <select id="give-graphs-year-end" name="year_end" aria-label="End Year"> |
| 565 | <?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?> |
| 566 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year_end'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 567 | <?php endfor; ?> |
| 568 | </select> |
| 569 | </div> |
| 570 | |
| 571 | <input type="submit" class="button-secondary" value="<?php _e( 'Filter', 'give' ); ?>" /> |
| 572 | </div> |
| 573 | |
| 574 | <input type="hidden" name="give_action" value="filter_reports" /> |
| 575 | </div> |
| 576 | </div> |
| 577 | </form> |
| 578 | <?php |
| 579 | /** |
| 580 | * Fires after displaying report graph date filters. |
| 581 | * |
| 582 | * @since 1.0 |
| 583 | */ |
| 584 | do_action( 'give_report_graph_controls_after' ); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Sets up the dates used to filter graph data |
| 589 | * |
| 590 | * Date sent via $_GET is read first and then modified (if needed) to match the |
| 591 | * selected date-range (if any) |
| 592 | * |
| 593 | * @since 1.0 |
| 594 | * |
| 595 | * @return array |
| 596 | */ |
| 597 | function give_get_report_dates() { |
| 598 | $dates = array(); |
| 599 | |
| 600 | $current_time = current_time( 'timestamp' ); |
| 601 | |
| 602 | $dates['range'] = isset( $_GET['range'] ) ? $_GET['range'] : 'this_month'; |
| 603 | $dates['year'] = isset( $_GET['year'] ) ? $_GET['year'] : date( 'Y' ); |
| 604 | $dates['year_end'] = isset( $_GET['year_end'] ) ? $_GET['year_end'] : date( 'Y' ); |
| 605 | $dates['m_start'] = isset( $_GET['m_start'] ) ? $_GET['m_start'] : 1; |
| 606 | $dates['m_end'] = isset( $_GET['m_end'] ) ? $_GET['m_end'] : 12; |
| 607 | $dates['day'] = isset( $_GET['day'] ) ? $_GET['day'] : 1; |
| 608 | $dates['day_end'] = isset( $_GET['day_end'] ) ? $_GET['day_end'] : cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
| 609 | |
| 610 | // Modify dates based on predefined ranges. |
| 611 | switch ( $dates['range'] ) : |
| 612 | |
| 613 | case 'this_month' : |
| 614 | $dates['m_start'] = date( 'n', $current_time ); |
| 615 | $dates['m_end'] = date( 'n', $current_time ); |
| 616 | $dates['day'] = 1; |
| 617 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
| 618 | $dates['year'] = date( 'Y' ); |
| 619 | $dates['year_end'] = date( 'Y' ); |
| 620 | break; |
| 621 | |
| 622 | case 'last_month' : |
| 623 | if ( date( 'n' ) == 1 ) { |
| 624 | $dates['m_start'] = 12; |
| 625 | $dates['m_end'] = 12; |
| 626 | $dates['year'] = date( 'Y', $current_time ) - 1; |
| 627 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
| 628 | } else { |
| 629 | $dates['m_start'] = date( 'n' ) - 1; |
| 630 | $dates['m_end'] = date( 'n' ) - 1; |
| 631 | $dates['year_end'] = $dates['year']; |
| 632 | } |
| 633 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
| 634 | break; |
| 635 | |
| 636 | case 'today' : |
| 637 | $dates['day'] = date( 'd', $current_time ); |
| 638 | $dates['day_end'] = date( 'd', $current_time ); |
| 639 | $dates['m_start'] = date( 'n', $current_time ); |
| 640 | $dates['m_end'] = date( 'n', $current_time ); |
| 641 | $dates['year'] = date( 'Y', $current_time ); |
| 642 | $dates['year_end'] = date( 'Y', $current_time ); |
| 643 | break; |
| 644 | |
| 645 | case 'yesterday' : |
| 646 | |
| 647 | $year = date( 'Y', $current_time ); |
| 648 | $month = date( 'n', $current_time ); |
| 649 | $day = date( 'd', $current_time ); |
| 650 | |
| 651 | if ( $month == 1 && $day == 1 ) { |
| 652 | |
| 653 | $year -= 1; |
| 654 | $month = 12; |
| 655 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 656 | |
| 657 | } elseif ( $month > 1 && $day == 1 ) { |
| 658 | |
| 659 | $month -= 1; |
| 660 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 661 | |
| 662 | } else { |
| 663 | |
| 664 | $day -= 1; |
| 665 | |
| 666 | } |
| 667 | |
| 668 | $dates['day'] = $day; |
| 669 | $dates['m_start'] = $month; |
| 670 | $dates['m_end'] = $month; |
| 671 | $dates['year'] = $year; |
| 672 | $dates['year_end'] = $year; |
| 673 | break; |
| 674 | |
| 675 | case 'this_week' : |
| 676 | $dates['day'] = date( 'd', $current_time - ( date( 'w', $current_time ) - 1 ) * 60 * 60 * 24 ) - 1; |
| 677 | $dates['day'] += get_option( 'start_of_week' ); |
| 678 | $dates['day_end'] = $dates['day'] + 6; |
| 679 | $dates['m_start'] = date( 'n', $current_time ); |
| 680 | $dates['m_end'] = date( 'n', $current_time ); |
| 681 | $dates['year'] = date( 'Y', $current_time ); |
| 682 | break; |
| 683 | |
| 684 | case 'last_week' : |
| 685 | $dates['day'] = date( 'd', $current_time - ( date( 'w' ) - 1 ) * 60 * 60 * 24 ) - 8; |
| 686 | $dates['day'] += get_option( 'start_of_week' ); |
| 687 | $dates['day_end'] = $dates['day'] + 6; |
| 688 | $dates['year'] = date( 'Y' ); |
| 689 | |
| 690 | if ( date( 'j', $current_time ) <= 7 ) { |
| 691 | $dates['m_start'] = date( 'n', $current_time ) - 1; |
| 692 | $dates['m_end'] = date( 'n', $current_time ) - 1; |
| 693 | if ( $dates['m_start'] <= 1 ) { |
| 694 | $dates['year'] = date( 'Y', $current_time ) - 1; |
| 695 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
| 696 | } |
| 697 | } else { |
| 698 | $dates['m_start'] = date( 'n', $current_time ); |
| 699 | $dates['m_end'] = date( 'n', $current_time ); |
| 700 | } |
| 701 | break; |
| 702 | |
| 703 | case 'this_quarter' : |
| 704 | $month_now = date( 'n', $current_time ); |
| 705 | $dates['year'] = date( 'Y', $current_time ); |
| 706 | |
| 707 | if ( $month_now <= 3 ) { |
| 708 | |
| 709 | $dates['m_start'] = 1; |
| 710 | $dates['m_end'] = 4; |
| 711 | |
| 712 | } else if ( $month_now <= 6 ) { |
| 713 | |
| 714 | $dates['m_start'] = 4; |
| 715 | $dates['m_end'] = 7; |
| 716 | |
| 717 | } else if ( $month_now <= 9 ) { |
| 718 | |
| 719 | $dates['m_start'] = 7; |
| 720 | $dates['m_end'] = 10; |
| 721 | |
| 722 | } else { |
| 723 | |
| 724 | $dates['m_start'] = 10; |
| 725 | $dates['m_end'] = 1; |
| 726 | $dates['year_end'] = date( 'Y', $current_time ) + 1; |
| 727 | |
| 728 | } |
| 729 | break; |
| 730 | |
| 731 | case 'last_quarter' : |
| 732 | $month_now = date( 'n', $current_time ); |
| 733 | $dates['year'] = date( 'Y', $current_time ); |
| 734 | $dates['year_end'] = date( 'Y', $current_time ); |
| 735 | |
| 736 | if ( $month_now <= 3 ) { |
| 737 | |
| 738 | $dates['m_start'] = 10; |
| 739 | $dates['m_end'] = 1; |
| 740 | $dates['year'] = date( 'Y', $current_time ) - 1; // Previous year. |
| 741 | |
| 742 | } else if ( $month_now <= 6 ) { |
| 743 | |
| 744 | $dates['m_start'] = 1; |
| 745 | $dates['m_end'] = 4; |
| 746 | |
| 747 | } else if ( $month_now <= 9 ) { |
| 748 | |
| 749 | $dates['m_start'] = 4; |
| 750 | $dates['m_end'] = 7; |
| 751 | |
| 752 | } else { |
| 753 | |
| 754 | $dates['m_start'] = 7; |
| 755 | $dates['m_end'] = 10; |
| 756 | |
| 757 | } |
| 758 | break; |
| 759 | |
| 760 | case 'this_year' : |
| 761 | $dates['m_start'] = 1; |
| 762 | $dates['m_end'] = 12; |
| 763 | $dates['year'] = date( 'Y', $current_time ); |
| 764 | $dates['year_end'] = date( 'Y', $current_time ); |
| 765 | break; |
| 766 | |
| 767 | case 'last_year' : |
| 768 | $dates['m_start'] = 1; |
| 769 | $dates['m_end'] = 12; |
| 770 | $dates['year'] = date( 'Y', $current_time ) - 1; |
| 771 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
| 772 | break; |
| 773 | |
| 774 | endswitch; |
| 775 | |
| 776 | return apply_filters( 'give_report_dates', $dates ); |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Grabs all of the selected date info and then redirects appropriately |
| 781 | * |
| 782 | * @since 1.0.0 |
| 783 | * @since 1.8.0 The `tab` query arg is added to the redirect. |
| 784 | * |
| 785 | * @param $data |
| 786 | */ |
| 787 | function give_parse_report_dates( $data ) { |
| 788 | $dates = give_get_report_dates(); |
| 789 | |
| 790 | $view = give_get_reporting_view(); |
| 791 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings'; |
| 792 | $id = isset( $_GET['form-id'] ) ? $_GET['form-id'] : null; |
| 793 | |
| 794 | wp_redirect( add_query_arg( $dates, admin_url( 'edit.php?post_type=give_forms&page=give-reports&tab=' . esc_attr( $tab ) . '&view=' . esc_attr( $view ) . '&form-id=' . absint( $id ) ) ) ); |
| 795 | give_die(); |
| 796 | } |
| 797 | |
| 798 | add_action( 'give_filter_reports', 'give_parse_report_dates' ); |
| 799 | |
| 800 | |
| 801 | /** |
| 802 | * Give Reports Refresh Button |
| 803 | * |
| 804 | * Outputs a "Refresh Reports" button for graphs |
| 805 | * |
| 806 | * @since 1.3 |
| 807 | */ |
| 808 | function give_reports_refresh_button() { |
| 809 | |
| 810 | $url = wp_nonce_url( add_query_arg( array( |
| 811 | 'give_action' => 'refresh_reports_transients', |
| 812 | 'give-messages[]' => 'refreshed-reports' |
| 813 | ) ), 'give-refresh-reports' ); |
| 814 | |
| 815 | echo Give()->tooltips->render_link( array( |
| 816 | 'label' => esc_attr__( 'Clicking this will clear the reports cache.', 'give' ), |
| 817 | 'tag_content' => '<span class="give-admin-button-icon give-admin-button-icon-update"></span>' . esc_html__( 'Refresh Report Data', 'give' ), |
| 818 | 'link' => $url, |
| 819 | 'position' => 'left', |
| 820 | 'attributes' => array( |
| 821 | 'class' => 'button alignright give-admin-button' |
| 822 | ) |
| 823 | ) ); |
| 824 | } |
| 825 | |
| 826 | add_action( 'give_reports_graph_additional_stats', 'give_reports_refresh_button' ); |
| 827 | |
| 828 | /** |
| 829 | * Trigger the refresh of reports transients |
| 830 | * |
| 831 | * @param array $data Parameters sent from Settings page. |
| 832 | * |
| 833 | * @since 1.3 |
| 834 | * |
| 835 | * @return void |
| 836 | */ |
| 837 | function give_run_refresh_reports_transients( $data ) { |
| 838 | |
| 839 | if ( ! wp_verify_nonce( $data['_wpnonce'], 'give-refresh-reports' ) ) { |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | // Monthly stats. |
| 844 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
| 845 | |
| 846 | // Total earning. |
| 847 | delete_option( 'give_earnings_total' ); |
| 848 | |
| 849 | // @todo: Refresh only range related stat cache |
| 850 | give_delete_donation_stats(); |
| 851 | } |
| 852 | |
| 853 | add_action( 'give_refresh_reports_transients', 'give_run_refresh_reports_transients' ); |