class-donor-reports-table.php
6 years ago
class-earnings-report.php
5 years ago
class-form-reports-table.php
5 years ago
class-forms-report.php
7 years ago
class-gateways-report.php
7 years ago
class-gateways-reports-table.php
4 years ago
class-give-graph.php
4 years ago
graphing.php
4 years ago
reports.php
5 years ago
graphing.php
879 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Graphing Functions |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Admin/Reports |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 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 = []; |
| 56 | $sales_data = []; |
| 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[] = [ $start_date * 1000, $sales ]; |
| 74 | $earnings_data[] = [ $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[] = [ $start_date * 1000, $sales ]; |
| 96 | $earnings_data[] = [ $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[] = [ $start_date * 1000, $sales ]; |
| 147 | $earnings_data[] = [ $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[] = [ $start_date * 1000, $sales ]; |
| 165 | $earnings_data[] = [ $start_date * 1000, $earnings ]; |
| 166 | |
| 167 | } |
| 168 | |
| 169 | $i ++; |
| 170 | |
| 171 | endwhile; |
| 172 | |
| 173 | $y ++; |
| 174 | endwhile; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | $data = [ |
| 179 | __( 'Revenue', '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, [ 'dataType' => [ '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 revenue for period:', 'give' ); ?></strong></th> |
| 208 | <td><?php echo give_currency_filter( give_format_amount( $earnings_totals, [ '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 revenue:', 'give' ); ?></strong></th> |
| 217 | <td><?php echo give_currency_filter( give_format_amount( $estimated['earnings'], [ '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 donation form. |
| 249 | * |
| 250 | * @since 1.0 |
| 251 | * |
| 252 | * @param int $form_id |
| 253 | * |
| 254 | * @return void |
| 255 | */ |
| 256 | function give_reports_graph_of_form( $form_id = 0 ) { |
| 257 | // Retrieve the queried dates. |
| 258 | $dates = give_get_report_dates(); |
| 259 | |
| 260 | // Determine graph options. |
| 261 | switch ( $dates['range'] ) : |
| 262 | case 'today': |
| 263 | case 'yesterday': |
| 264 | $day_by_day = true; |
| 265 | break; |
| 266 | case 'last_year': |
| 267 | $day_by_day = false; |
| 268 | break; |
| 269 | case 'this_year': |
| 270 | $day_by_day = false; |
| 271 | break; |
| 272 | case 'last_quarter': |
| 273 | $day_by_day = false; |
| 274 | break; |
| 275 | case 'this_quarter': |
| 276 | $day_by_day = false; |
| 277 | break; |
| 278 | case 'other': |
| 279 | if ( $dates['m_end'] - $dates['m_start'] >= 2 || $dates['year_end'] > $dates['year'] ) { |
| 280 | $day_by_day = false; |
| 281 | } else { |
| 282 | $day_by_day = true; |
| 283 | } |
| 284 | break; |
| 285 | default: |
| 286 | $day_by_day = true; |
| 287 | break; |
| 288 | endswitch; |
| 289 | |
| 290 | $earnings_totals = (float) 0.00; // Total earnings for time period shown. |
| 291 | $sales_totals = 0; // Total sales for time period shown. |
| 292 | |
| 293 | $earnings_data = []; |
| 294 | $sales_data = []; |
| 295 | $stats = new Give_Payment_Stats(); |
| 296 | |
| 297 | if ( $dates['range'] == 'today' || $dates['range'] == 'yesterday' ) { |
| 298 | |
| 299 | // Hour by hour |
| 300 | $month = $dates['m_start']; |
| 301 | $hour = 0; |
| 302 | $minute = 0; |
| 303 | $second = 0; |
| 304 | while ( $hour <= 23 ) : |
| 305 | |
| 306 | if ( $hour == 23 ) { |
| 307 | $minute = $second = 59; |
| 308 | } |
| 309 | |
| 310 | $date = mktime( $hour, $minute, $second, $month, $dates['day'], $dates['year'] ); |
| 311 | $date_end = mktime( $hour + 1, $minute, $second, $month, $dates['day'], $dates['year'] ); |
| 312 | |
| 313 | $sales = $stats->get_sales( $form_id, $date, $date_end ); |
| 314 | $sales_totals += $sales; |
| 315 | |
| 316 | $earnings = $stats->get_earnings( $form_id, $date, $date_end ); |
| 317 | $earnings_totals += $earnings; |
| 318 | |
| 319 | $sales_data[] = [ $date * 1000, $sales ]; |
| 320 | $earnings_data[] = [ $date * 1000, $earnings ]; |
| 321 | |
| 322 | $hour ++; |
| 323 | endwhile; |
| 324 | |
| 325 | } elseif ( $dates['range'] == 'this_week' || $dates['range'] == 'last_week' ) { |
| 326 | |
| 327 | // Day by day. |
| 328 | $day = $dates['day']; |
| 329 | $day_end = $dates['day_end']; |
| 330 | $month = $dates['m_start']; |
| 331 | while ( $day <= $day_end ) : |
| 332 | |
| 333 | $date = mktime( 0, 0, 0, $month, $day, $dates['year'] ); |
| 334 | $date_end = mktime( 0, 0, 0, $month, $day + 1, $dates['year'] ); |
| 335 | $sales = $stats->get_sales( $form_id, $date, $date_end ); |
| 336 | $sales_totals += $sales; |
| 337 | |
| 338 | $earnings = $stats->get_earnings( $form_id, $date, $date_end ); |
| 339 | $earnings_totals += $earnings; |
| 340 | |
| 341 | $sales_data[] = [ $date * 1000, $sales ]; |
| 342 | $earnings_data[] = [ $date * 1000, $earnings ]; |
| 343 | |
| 344 | $day ++; |
| 345 | endwhile; |
| 346 | |
| 347 | } else { |
| 348 | |
| 349 | $y = $dates['year']; |
| 350 | |
| 351 | while ( $y <= $dates['year_end'] ) : |
| 352 | |
| 353 | $last_year = false; |
| 354 | |
| 355 | if ( $dates['year'] == $dates['year_end'] ) { |
| 356 | $month_start = $dates['m_start']; |
| 357 | $month_end = $dates['m_end']; |
| 358 | $last_year = true; |
| 359 | } elseif ( $y == $dates['year'] ) { |
| 360 | $month_start = $dates['m_start']; |
| 361 | $month_end = 12; |
| 362 | } else { |
| 363 | $month_start = 1; |
| 364 | $month_end = 12; |
| 365 | } |
| 366 | |
| 367 | $i = $month_start; |
| 368 | while ( $i <= $month_end ) : |
| 369 | |
| 370 | if ( $day_by_day ) { |
| 371 | |
| 372 | if ( $i == $month_end && $last_year ) { |
| 373 | |
| 374 | $num_of_days = $dates['day_end']; |
| 375 | |
| 376 | } else { |
| 377 | |
| 378 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
| 379 | |
| 380 | } |
| 381 | |
| 382 | $d = $dates['day']; |
| 383 | while ( $d <= $num_of_days ) : |
| 384 | |
| 385 | $date = mktime( 0, 0, 0, $i, $d, $y ); |
| 386 | $end_date = mktime( 23, 59, 59, $i, $d, $y ); |
| 387 | |
| 388 | $sales = $stats->get_sales( $form_id, $date, $end_date ); |
| 389 | $sales_totals += $sales; |
| 390 | |
| 391 | $earnings = $stats->get_earnings( $form_id, $date, $end_date ); |
| 392 | $earnings_totals += $earnings; |
| 393 | |
| 394 | $sales_data[] = [ $date * 1000, $sales ]; |
| 395 | $earnings_data[] = [ $date * 1000, $earnings ]; |
| 396 | $d ++; |
| 397 | |
| 398 | endwhile; |
| 399 | |
| 400 | } else { |
| 401 | |
| 402 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
| 403 | |
| 404 | $date = mktime( 0, 0, 0, $i, 1, $y ); |
| 405 | $end_date = mktime( 23, 59, 59, $i, $num_of_days, $y ); |
| 406 | |
| 407 | $sales = $stats->get_sales( $form_id, $date, $end_date ); |
| 408 | $sales_totals += $sales; |
| 409 | |
| 410 | $earnings = $stats->get_earnings( $form_id, $date, $end_date ); |
| 411 | $earnings_totals += $earnings; |
| 412 | |
| 413 | $sales_data[] = [ $date * 1000, $sales ]; |
| 414 | $earnings_data[] = [ $date * 1000, $earnings ]; |
| 415 | |
| 416 | } |
| 417 | |
| 418 | $i ++; |
| 419 | |
| 420 | endwhile; |
| 421 | |
| 422 | $y ++; |
| 423 | endwhile; |
| 424 | |
| 425 | } |
| 426 | |
| 427 | $data = [ |
| 428 | __( 'Revenue', 'give' ) => $earnings_data, |
| 429 | __( 'Donations', 'give' ) => $sales_data, |
| 430 | ]; |
| 431 | |
| 432 | ?> |
| 433 | <h3><span> |
| 434 | <?php |
| 435 | printf( |
| 436 | /* translators: %s: form title */ |
| 437 | esc_html__( 'Revenue Report for %s', 'give' ), |
| 438 | get_the_title( $form_id ) |
| 439 | ); |
| 440 | ?> |
| 441 | </span></h3> |
| 442 | <div id="give-dashboard-widgets-wrap"> |
| 443 | <div class="metabox-holder" style="padding-top: 0;"> |
| 444 | <div class="postbox"> |
| 445 | <div class="inside"> |
| 446 | <?php give_reports_graph_controls(); ?> |
| 447 | <?php |
| 448 | $graph = new Give_Graph( $data, [ 'dataType' => [ 'amount', 'count' ] ] ); |
| 449 | $graph->set( 'x_mode', 'time' ); |
| 450 | $graph->set( 'multiple_y_axes', true ); |
| 451 | $graph->display(); |
| 452 | ?> |
| 453 | </div> |
| 454 | </div> |
| 455 | <!--/.postbox --> |
| 456 | <table class="widefat reports-table alignleft" style="max-width:450px"> |
| 457 | <tbody> |
| 458 | <tr> |
| 459 | <th scope="row"><strong><?php _e( 'Total revenue for period:', 'give' ); ?></strong></th> |
| 460 | <td><?php echo give_currency_filter( give_format_amount( $earnings_totals, [ 'sanitize' => false ] ) ); ?></td> |
| 461 | </tr> |
| 462 | <tr class="alternate"> |
| 463 | <th scope="row"><strong><?php _e( 'Total donations for period:', 'give' ); ?></strong></th> |
| 464 | <td><?php echo $sales_totals; ?></td> |
| 465 | </tr> |
| 466 | <tr> |
| 467 | <th scope="row"><strong><?php _e( 'Average monthly revenue:', 'give' ); ?></strong></th> |
| 468 | <td><?php echo give_currency_filter( give_format_amount( give_get_average_monthly_form_earnings( $form_id ), [ 'sanitize' => false ] ) ); ?></td> |
| 469 | </tr> |
| 470 | <tr class="alternate"> |
| 471 | <th scope="row"><strong><?php _e( 'Average monthly donations:', 'give' ); ?></strong></th> |
| 472 | <td><?php echo number_format( give_get_average_monthly_form_sales( $form_id ), 0 ); ?></td> |
| 473 | </tr> |
| 474 | </tbody> |
| 475 | </table> |
| 476 | |
| 477 | <?php |
| 478 | /** |
| 479 | * Fires on report graphs widget. |
| 480 | * |
| 481 | * Allows you to add additional stats to the widget. |
| 482 | * |
| 483 | * @since 1.0 |
| 484 | */ |
| 485 | do_action( 'give_reports_graph_additional_stats' ); |
| 486 | ?> |
| 487 | |
| 488 | </div> |
| 489 | </div> |
| 490 | <?php |
| 491 | echo ob_get_clean(); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Show report graph date filters |
| 496 | * |
| 497 | * @since 1.0.0 |
| 498 | * @since 1.8.0 The hidden `view` field is replaced with `tab` field. |
| 499 | * |
| 500 | * @return void |
| 501 | */ |
| 502 | function give_reports_graph_controls() { |
| 503 | $date_options = apply_filters( |
| 504 | 'give_report_date_options', |
| 505 | [ |
| 506 | 'today' => __( 'Today', 'give' ), |
| 507 | 'yesterday' => __( 'Yesterday', 'give' ), |
| 508 | 'this_week' => __( 'This Week', 'give' ), |
| 509 | 'last_week' => __( 'Last Week', 'give' ), |
| 510 | 'this_month' => __( 'This Month', 'give' ), |
| 511 | 'last_month' => __( 'Last Month', 'give' ), |
| 512 | 'this_quarter' => __( 'This Quarter', 'give' ), |
| 513 | 'last_quarter' => __( 'Last Quarter', 'give' ), |
| 514 | 'this_year' => __( 'This Year', 'give' ), |
| 515 | 'last_year' => __( 'Last Year', 'give' ), |
| 516 | 'other' => __( 'Custom', 'give' ), |
| 517 | ] |
| 518 | ); |
| 519 | |
| 520 | $dates = give_get_report_dates(); |
| 521 | $display = $dates['range'] == 'other' ? '' : 'display: none;'; |
| 522 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings'; |
| 523 | |
| 524 | if ( empty( $dates['day_end'] ) ) { |
| 525 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, date( 'n' ), date( 'Y' ) ); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Fires before displaying report graph date filters. |
| 530 | * |
| 531 | * @since 1.0 |
| 532 | */ |
| 533 | do_action( 'give_report_graph_controls_before' ); |
| 534 | ?> |
| 535 | <form id="give-graphs-filter" method="get"> |
| 536 | <div class="tablenav top"> |
| 537 | <div class="actions"> |
| 538 | |
| 539 | <input type="hidden" name="post_type" value="give_forms" /> |
| 540 | <input type="hidden" name="page" value="give-reports" /> |
| 541 | <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" /> |
| 542 | |
| 543 | <?php if ( isset( $_GET['form-id'] ) ) : ?> |
| 544 | <input type="hidden" name="form-id" value="<?php echo absint( $_GET['form-id'] ); ?>" /> |
| 545 | <?php endif; ?> |
| 546 | |
| 547 | <div id="give-graphs-date-options-wrap"> |
| 548 | <select id="give-graphs-date-options" name="range"> |
| 549 | <?php foreach ( $date_options as $key => $option ) : ?> |
| 550 | <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $dates['range'] ); ?>><?php echo esc_html( $option ); ?></option> |
| 551 | <?php endforeach; ?> |
| 552 | </select> |
| 553 | |
| 554 | <div id="give-date-range-options" style="<?php echo esc_attr( $display ); ?>"> |
| 555 | <span class="screen-reader-text"><?php _e( 'From', 'give' ); ?> </span> |
| 556 | <select id="give-graphs-month-start" name="m_start" aria-label="Start Month"> |
| 557 | <?php for ( $i = 1; $i <= 12; $i ++ ) : ?> |
| 558 | <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> |
| 559 | <?php endfor; ?> |
| 560 | </select> |
| 561 | <select id="give-graphs-day-start" name="day" aria-label="Start Day"> |
| 562 | <?php for ( $i = 1; $i <= 31; $i ++ ) : ?> |
| 563 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 564 | <?php endfor; ?> |
| 565 | </select> |
| 566 | <select id="give-graphs-year-start" name="year" aria-label="Start Year"> |
| 567 | <?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?> |
| 568 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 569 | <?php endfor; ?> |
| 570 | </select> |
| 571 | <span class="screen-reader-text"><?php esc_html_e( 'To', 'give' ); ?> </span> |
| 572 | <span>–</span> |
| 573 | <select id="give-graphs-month-end" name="m_end" aria-label="End Month"> |
| 574 | <?php for ( $i = 1; $i <= 12; $i ++ ) : ?> |
| 575 | <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> |
| 576 | <?php endfor; ?> |
| 577 | </select> |
| 578 | <select id="give-graphs-day-end" name="day_end" aria-label="End Day"> |
| 579 | <?php for ( $i = 1; $i <= 31; $i ++ ) : ?> |
| 580 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day_end'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 581 | <?php endfor; ?> |
| 582 | </select> |
| 583 | <select id="give-graphs-year-end" name="year_end" aria-label="End Year"> |
| 584 | <?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?> |
| 585 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year_end'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
| 586 | <?php endfor; ?> |
| 587 | </select> |
| 588 | </div> |
| 589 | |
| 590 | <input type="submit" class="button-secondary" value="<?php _e( 'Filter', 'give' ); ?>" /> |
| 591 | </div> |
| 592 | |
| 593 | <input type="hidden" name="give_action" value="filter_reports" /> |
| 594 | </div> |
| 595 | </div> |
| 596 | </form> |
| 597 | <?php |
| 598 | /** |
| 599 | * Fires after displaying report graph date filters. |
| 600 | * |
| 601 | * @since 1.0 |
| 602 | */ |
| 603 | do_action( 'give_report_graph_controls_after' ); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Sets up the dates used to filter graph data |
| 608 | * |
| 609 | * Date sent via $_GET is read first and then modified (if needed) to match the |
| 610 | * selected date-range (if any) |
| 611 | * |
| 612 | * @since 1.0 |
| 613 | * |
| 614 | * @return array |
| 615 | */ |
| 616 | function give_get_report_dates() { |
| 617 | $dates = []; |
| 618 | |
| 619 | $current_time = current_time( 'timestamp' ); |
| 620 | |
| 621 | $dates['range'] = isset( $_GET['range'] ) ? $_GET['range'] : 'this_month'; |
| 622 | $dates['year'] = isset( $_GET['year'] ) ? $_GET['year'] : date( 'Y' ); |
| 623 | $dates['year_end'] = isset( $_GET['year_end'] ) ? $_GET['year_end'] : date( 'Y' ); |
| 624 | $dates['m_start'] = isset( $_GET['m_start'] ) ? $_GET['m_start'] : 1; |
| 625 | $dates['m_end'] = isset( $_GET['m_end'] ) ? $_GET['m_end'] : 12; |
| 626 | $dates['day'] = isset( $_GET['day'] ) ? $_GET['day'] : 1; |
| 627 | $dates['day_end'] = isset( $_GET['day_end'] ) ? $_GET['day_end'] : cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
| 628 | |
| 629 | // Modify dates based on predefined ranges. |
| 630 | switch ( $dates['range'] ) : |
| 631 | |
| 632 | case 'this_month': |
| 633 | $dates['m_start'] = date( 'n', $current_time ); |
| 634 | $dates['m_end'] = date( 'n', $current_time ); |
| 635 | $dates['day'] = 1; |
| 636 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
| 637 | $dates['year'] = date( 'Y' ); |
| 638 | $dates['year_end'] = date( 'Y' ); |
| 639 | break; |
| 640 | |
| 641 | case 'last_month': |
| 642 | if ( date( 'n' ) == 1 ) { |
| 643 | $dates['m_start'] = 12; |
| 644 | $dates['m_end'] = 12; |
| 645 | $dates['year'] = date( 'Y', $current_time ) - 1; |
| 646 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
| 647 | } else { |
| 648 | $dates['m_start'] = date( 'n' ) - 1; |
| 649 | $dates['m_end'] = date( 'n' ) - 1; |
| 650 | $dates['year_end'] = $dates['year']; |
| 651 | } |
| 652 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
| 653 | break; |
| 654 | |
| 655 | case 'today': |
| 656 | $dates['day'] = date( 'd', $current_time ); |
| 657 | $dates['day_end'] = date( 'd', $current_time ); |
| 658 | $dates['m_start'] = date( 'n', $current_time ); |
| 659 | $dates['m_end'] = date( 'n', $current_time ); |
| 660 | $dates['year'] = date( 'Y', $current_time ); |
| 661 | $dates['year_end'] = date( 'Y', $current_time ); |
| 662 | break; |
| 663 | |
| 664 | case 'yesterday': |
| 665 | $year = date( 'Y', $current_time ); |
| 666 | $month = date( 'n', $current_time ); |
| 667 | $day = date( 'd', $current_time ); |
| 668 | |
| 669 | if ( $month == 1 && $day == 1 ) { |
| 670 | |
| 671 | $year -= 1; |
| 672 | $month = 12; |
| 673 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 674 | |
| 675 | } elseif ( $month > 1 && $day == 1 ) { |
| 676 | |
| 677 | $month -= 1; |
| 678 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 679 | |
| 680 | } else { |
| 681 | |
| 682 | $day -= 1; |
| 683 | |
| 684 | } |
| 685 | |
| 686 | $dates['day'] = $day; |
| 687 | $dates['m_start'] = $month; |
| 688 | $dates['m_end'] = $month; |
| 689 | $dates['year'] = $year; |
| 690 | $dates['year_end'] = $year; |
| 691 | break; |
| 692 | |
| 693 | case 'this_week': |
| 694 | $dates['day'] = date( 'd', $current_time - ( date( 'w', $current_time ) - 1 ) * 60 * 60 * 24 ) - 1; |
| 695 | $dates['day'] += get_option( 'start_of_week' ); |
| 696 | $dates['day_end'] = $dates['day'] + 6; |
| 697 | $dates['m_start'] = date( 'n', $current_time ); |
| 698 | $dates['m_end'] = date( 'n', $current_time ); |
| 699 | $dates['year'] = date( 'Y', $current_time ); |
| 700 | break; |
| 701 | |
| 702 | case 'last_week': |
| 703 | $dates['day'] = date( 'd', $current_time - ( date( 'w' ) - 1 ) * 60 * 60 * 24 ) - 8; |
| 704 | $dates['day'] += get_option( 'start_of_week' ); |
| 705 | $dates['day_end'] = $dates['day'] + 6; |
| 706 | $dates['year'] = date( 'Y' ); |
| 707 | |
| 708 | if ( date( 'j', $current_time ) <= 7 ) { |
| 709 | $dates['m_start'] = date( 'n', $current_time ) - 1; |
| 710 | $dates['m_end'] = date( 'n', $current_time ) - 1; |
| 711 | if ( $dates['m_start'] <= 1 ) { |
| 712 | $dates['year'] = date( 'Y', $current_time ) - 1; |
| 713 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
| 714 | } |
| 715 | } else { |
| 716 | $dates['m_start'] = date( 'n', $current_time ); |
| 717 | $dates['m_end'] = date( 'n', $current_time ); |
| 718 | } |
| 719 | break; |
| 720 | |
| 721 | case 'this_quarter': |
| 722 | $month_now = date( 'n', $current_time ); |
| 723 | $dates['year'] = date( 'Y', $current_time ); |
| 724 | |
| 725 | if ( $month_now <= 3 ) { |
| 726 | |
| 727 | $dates['m_start'] = 1; |
| 728 | $dates['m_end'] = 4; |
| 729 | |
| 730 | } elseif ( $month_now <= 6 ) { |
| 731 | |
| 732 | $dates['m_start'] = 4; |
| 733 | $dates['m_end'] = 7; |
| 734 | |
| 735 | } elseif ( $month_now <= 9 ) { |
| 736 | |
| 737 | $dates['m_start'] = 7; |
| 738 | $dates['m_end'] = 10; |
| 739 | |
| 740 | } else { |
| 741 | |
| 742 | $dates['m_start'] = 10; |
| 743 | $dates['m_end'] = 1; |
| 744 | $dates['year_end'] = date( 'Y', $current_time ) + 1; |
| 745 | |
| 746 | } |
| 747 | break; |
| 748 | |
| 749 | case 'last_quarter': |
| 750 | $month_now = date( 'n', $current_time ); |
| 751 | $dates['year'] = date( 'Y', $current_time ); |
| 752 | $dates['year_end'] = date( 'Y', $current_time ); |
| 753 | |
| 754 | if ( $month_now <= 3 ) { |
| 755 | |
| 756 | $dates['m_start'] = 10; |
| 757 | $dates['m_end'] = 1; |
| 758 | $dates['year'] = date( 'Y', $current_time ) - 1; // Previous year. |
| 759 | |
| 760 | } elseif ( $month_now <= 6 ) { |
| 761 | |
| 762 | $dates['m_start'] = 1; |
| 763 | $dates['m_end'] = 4; |
| 764 | |
| 765 | } elseif ( $month_now <= 9 ) { |
| 766 | |
| 767 | $dates['m_start'] = 4; |
| 768 | $dates['m_end'] = 7; |
| 769 | |
| 770 | } else { |
| 771 | |
| 772 | $dates['m_start'] = 7; |
| 773 | $dates['m_end'] = 10; |
| 774 | |
| 775 | } |
| 776 | break; |
| 777 | |
| 778 | case 'this_year': |
| 779 | $dates['m_start'] = 1; |
| 780 | $dates['m_end'] = 12; |
| 781 | $dates['year'] = date( 'Y', $current_time ); |
| 782 | $dates['year_end'] = date( 'Y', $current_time ); |
| 783 | break; |
| 784 | |
| 785 | case 'last_year': |
| 786 | $dates['m_start'] = 1; |
| 787 | $dates['m_end'] = 12; |
| 788 | $dates['year'] = date( 'Y', $current_time ) - 1; |
| 789 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
| 790 | break; |
| 791 | |
| 792 | endswitch; |
| 793 | |
| 794 | return apply_filters( 'give_report_dates', $dates ); |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Grabs all of the selected date info and then redirects appropriately |
| 799 | * |
| 800 | * @since 1.0.0 |
| 801 | * @since 1.8.0 The `tab` query arg is added to the redirect. |
| 802 | * |
| 803 | * @param $data |
| 804 | */ |
| 805 | function give_parse_report_dates( $data ) { |
| 806 | $dates = give_get_report_dates(); |
| 807 | |
| 808 | $view = give_get_reporting_view(); |
| 809 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings'; |
| 810 | $id = isset( $_GET['form-id'] ) ? $_GET['form-id'] : null; |
| 811 | |
| 812 | wp_redirect( esc_url_raw(add_query_arg( $dates, admin_url( 'edit.php?post_type=give_forms&page=give-reports&legacy=true&tab=' . esc_attr( $tab ) . '&view=' . esc_attr( $view ) . '&form-id=' . absint( $id ) ) ) ) ); |
| 813 | give_die(); |
| 814 | } |
| 815 | |
| 816 | add_action( 'give_filter_reports', 'give_parse_report_dates' ); |
| 817 | |
| 818 | |
| 819 | /** |
| 820 | * Give Reports Refresh Button |
| 821 | * |
| 822 | * Outputs a "Refresh Reports" button for graphs |
| 823 | * |
| 824 | * @since 1.3 |
| 825 | */ |
| 826 | function give_reports_refresh_button() { |
| 827 | |
| 828 | $url = wp_nonce_url( |
| 829 | add_query_arg( |
| 830 | [ |
| 831 | 'give_action' => 'refresh_reports_transients', |
| 832 | 'give-messages[]' => 'refreshed-reports', |
| 833 | ] |
| 834 | ), |
| 835 | 'give-refresh-reports' |
| 836 | ); |
| 837 | |
| 838 | echo Give()->tooltips->render_link( |
| 839 | [ |
| 840 | 'label' => esc_attr__( 'Clicking this will clear the reports cache.', 'give' ), |
| 841 | 'tag_content' => '<span class="give-admin-button-icon give-admin-button-icon-update"></span>' . esc_html__( 'Refresh Report Data', 'give' ), |
| 842 | 'link' => esc_url( $url ), |
| 843 | 'position' => 'left', |
| 844 | 'attributes' => [ |
| 845 | 'class' => 'button alignright give-admin-button', |
| 846 | ], |
| 847 | ] |
| 848 | ); |
| 849 | } |
| 850 | |
| 851 | add_action( 'give_reports_graph_additional_stats', 'give_reports_refresh_button' ); |
| 852 | |
| 853 | /** |
| 854 | * Trigger the refresh of reports transients |
| 855 | * |
| 856 | * @param array $data Parameters sent from Settings page. |
| 857 | * |
| 858 | * @since 1.3 |
| 859 | * |
| 860 | * @return void |
| 861 | */ |
| 862 | function give_run_refresh_reports_transients( $data ) { |
| 863 | |
| 864 | if ( ! wp_verify_nonce( $data['_wpnonce'], 'give-refresh-reports' ) ) { |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | // Monthly stats. |
| 869 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
| 870 | |
| 871 | // Total earning. |
| 872 | delete_option( 'give_earnings_total' ); |
| 873 | |
| 874 | // @todo: Refresh only range related stat cache |
| 875 | give_delete_donation_stats(); |
| 876 | } |
| 877 | |
| 878 | add_action( 'give_refresh_reports_transients', 'give_run_refresh_reports_transients' ); |
| 879 |