ravinderk /
Give
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 = 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 | View Code Duplication | 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 | View Code Duplication | if ( $i === $month_end ) { |
|
|
0 ignored issues
–
show
|
|||
| 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 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 = array(); |
||
| 294 | $sales_data = array(); |
||
| 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[] = array( $date * 1000, $sales ); |
||
| 320 | $earnings_data[] = array( $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 | View Code Duplication | 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[] = array( $date * 1000, $sales ); |
||
| 342 | $earnings_data[] = array( $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 | View Code Duplication | if ( $i == $month_end && $last_year ) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 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[] = array( $date * 1000, $sales ); |
||
| 395 | $earnings_data[] = array( $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[] = array( $date * 1000, $sales ); |
||
| 414 | $earnings_data[] = array( $date * 1000, $earnings ); |
||
| 415 | |||
| 416 | } |
||
| 417 | |||
| 418 | $i ++; |
||
| 419 | |||
| 420 | endwhile; |
||
| 421 | |||
| 422 | $y ++; |
||
| 423 | endwhile; |
||
| 424 | |||
| 425 | } |
||
| 426 | |||
| 427 | $data = array( |
||
| 428 | __( 'Income', '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__( 'Income 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, array( 'dataType' => array( '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 income for period:', 'give' ); ?></strong></th> |
||
| 460 | <td><?php echo give_currency_filter( give_format_amount( $earnings_totals, array( '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 income:', 'give' ); ?></strong></th> |
||
| 468 | <td><?php echo give_currency_filter( give_format_amount( give_get_average_monthly_form_earnings( $form_id ), array( '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', array( |
||
| 505 | 'today' => __( 'Today', 'give' ), |
||
| 506 | 'yesterday' => __( 'Yesterday', 'give' ), |
||
| 507 | 'this_week' => __( 'This Week', 'give' ), |
||
| 508 | 'last_week' => __( 'Last Week', 'give' ), |
||
| 509 | 'this_month' => __( 'This Month', 'give' ), |
||
| 510 | 'last_month' => __( 'Last Month', 'give' ), |
||
| 511 | 'this_quarter' => __( 'This Quarter', 'give' ), |
||
| 512 | 'last_quarter' => __( 'Last Quarter', 'give' ), |
||
| 513 | 'this_year' => __( 'This Year', 'give' ), |
||
| 514 | 'last_year' => __( 'Last Year', 'give' ), |
||
| 515 | 'other' => __( 'Custom', 'give' ), |
||
| 516 | ) |
||
| 517 | ); |
||
| 518 | |||
| 519 | $dates = give_get_report_dates(); |
||
| 520 | $display = $dates['range'] == 'other' ? '' : 'display: none;'; |
||
| 521 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings'; |
||
| 522 | |||
| 523 | if ( empty( $dates['day_end'] ) ) { |
||
| 524 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, date( 'n' ), date( 'Y' ) ); |
||
| 525 | } |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Fires before displaying report graph date filters. |
||
| 529 | * |
||
| 530 | * @since 1.0 |
||
| 531 | */ |
||
| 532 | do_action( 'give_report_graph_controls_before' ); |
||
| 533 | ?> |
||
| 534 | <form id="give-graphs-filter" method="get"> |
||
| 535 | <div class="tablenav top"> |
||
| 536 | <div class="actions"> |
||
| 537 | |||
| 538 | <input type="hidden" name="post_type" value="give_forms" /> |
||
| 539 | <input type="hidden" name="page" value="give-reports" /> |
||
| 540 | <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" /> |
||
| 541 | |||
| 542 | <?php if ( isset( $_GET['form-id'] ) ) : ?> |
||
| 543 | <input type="hidden" name="form-id" value="<?php echo absint( $_GET['form-id'] ); ?>" /> |
||
| 544 | <?php endif; ?> |
||
| 545 | |||
| 546 | <div id="give-graphs-date-options-wrap"> |
||
| 547 | <select id="give-graphs-date-options" name="range"> |
||
| 548 | <?php foreach ( $date_options as $key => $option ) : ?> |
||
| 549 | <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $dates['range'] ); ?>><?php echo esc_html( $option ); ?></option> |
||
| 550 | <?php endforeach; ?> |
||
| 551 | </select> |
||
| 552 | |||
| 553 | <div id="give-date-range-options" style="<?php echo esc_attr( $display ); ?>"> |
||
| 554 | <span class="screen-reader-text"><?php _e( 'From', 'give' ); ?> </span> |
||
| 555 | <select id="give-graphs-month-start" name="m_start" aria-label="Start Month"> |
||
| 556 | View Code Duplication | <?php for ( $i = 1; $i <= 12; $i ++ ) : ?> |
|
| 557 | <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> |
||
| 558 | <?php endfor; ?> |
||
| 559 | </select> |
||
| 560 | <select id="give-graphs-day-start" name="day" aria-label="Start Day"> |
||
| 561 | <?php for ( $i = 1; $i <= 31; $i ++ ) : ?> |
||
| 562 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
||
| 563 | <?php endfor; ?> |
||
| 564 | </select> |
||
| 565 | <select id="give-graphs-year-start" name="year" aria-label="Start Year"> |
||
| 566 | View Code Duplication | <?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?> |
|
| 567 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
||
| 568 | <?php endfor; ?> |
||
| 569 | </select> |
||
| 570 | <span class="screen-reader-text"><?php esc_html_e( 'To', 'give' ); ?> </span> |
||
| 571 | <span>–</span> |
||
| 572 | <select id="give-graphs-month-end" name="m_end" aria-label="End Month"> |
||
| 573 | View Code Duplication | <?php for ( $i = 1; $i <= 12; $i ++ ) : ?> |
|
| 574 | <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> |
||
| 575 | <?php endfor; ?> |
||
| 576 | </select> |
||
| 577 | <select id="give-graphs-day-end" name="day_end" aria-label="End Day"> |
||
| 578 | <?php for ( $i = 1; $i <= 31; $i ++ ) : ?> |
||
| 579 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['day_end'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
||
| 580 | <?php endfor; ?> |
||
| 581 | </select> |
||
| 582 | <select id="give-graphs-year-end" name="year_end" aria-label="End Year"> |
||
| 583 | View Code Duplication | <?php for ( $i = 2007; $i <= date( 'Y' ); $i ++ ) : ?> |
|
| 584 | <option value="<?php echo absint( $i ); ?>" <?php echo esc_attr( selected( $i, $dates['year_end'] ) ); ?>><?php echo esc_html( $i ); ?></option> |
||
| 585 | <?php endfor; ?> |
||
| 586 | </select> |
||
| 587 | </div> |
||
| 588 | |||
| 589 | <input type="submit" class="button-secondary" value="<?php _e( 'Filter', 'give' ); ?>" /> |
||
| 590 | </div> |
||
| 591 | |||
| 592 | <input type="hidden" name="give_action" value="filter_reports" /> |
||
| 593 | </div> |
||
| 594 | </div> |
||
| 595 | </form> |
||
| 596 | <?php |
||
| 597 | /** |
||
| 598 | * Fires after displaying report graph date filters. |
||
| 599 | * |
||
| 600 | * @since 1.0 |
||
| 601 | */ |
||
| 602 | do_action( 'give_report_graph_controls_after' ); |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Sets up the dates used to filter graph data |
||
| 607 | * |
||
| 608 | * Date sent via $_GET is read first and then modified (if needed) to match the |
||
| 609 | * selected date-range (if any) |
||
| 610 | * |
||
| 611 | * @since 1.0 |
||
| 612 | * |
||
| 613 | * @return array |
||
| 614 | */ |
||
| 615 | function give_get_report_dates() { |
||
| 616 | $dates = array(); |
||
| 617 | |||
| 618 | $current_time = current_time( 'timestamp' ); |
||
| 619 | |||
| 620 | $dates['range'] = isset( $_GET['range'] ) ? $_GET['range'] : 'this_month'; |
||
| 621 | $dates['year'] = isset( $_GET['year'] ) ? $_GET['year'] : date( 'Y' ); |
||
| 622 | $dates['year_end'] = isset( $_GET['year_end'] ) ? $_GET['year_end'] : date( 'Y' ); |
||
| 623 | $dates['m_start'] = isset( $_GET['m_start'] ) ? $_GET['m_start'] : 1; |
||
| 624 | $dates['m_end'] = isset( $_GET['m_end'] ) ? $_GET['m_end'] : 12; |
||
| 625 | $dates['day'] = isset( $_GET['day'] ) ? $_GET['day'] : 1; |
||
| 626 | $dates['day_end'] = isset( $_GET['day_end'] ) ? $_GET['day_end'] : cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
||
| 627 | |||
| 628 | // Modify dates based on predefined ranges. |
||
| 629 | switch ( $dates['range'] ) : |
||
| 630 | |||
| 631 | case 'this_month': |
||
| 632 | $dates['m_start'] = date( 'n', $current_time ); |
||
| 633 | $dates['m_end'] = date( 'n', $current_time ); |
||
| 634 | $dates['day'] = 1; |
||
| 635 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
||
| 636 | $dates['year'] = date( 'Y' ); |
||
| 637 | $dates['year_end'] = date( 'Y' ); |
||
| 638 | break; |
||
| 639 | |||
| 640 | case 'last_month': |
||
| 641 | if ( date( 'n' ) == 1 ) { |
||
| 642 | $dates['m_start'] = 12; |
||
| 643 | $dates['m_end'] = 12; |
||
| 644 | $dates['year'] = date( 'Y', $current_time ) - 1; |
||
| 645 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
||
| 646 | } else { |
||
| 647 | $dates['m_start'] = date( 'n' ) - 1; |
||
| 648 | $dates['m_end'] = date( 'n' ) - 1; |
||
| 649 | $dates['year_end'] = $dates['year']; |
||
| 650 | } |
||
| 651 | $dates['day_end'] = cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ); |
||
| 652 | break; |
||
| 653 | |||
| 654 | case 'today': |
||
| 655 | $dates['day'] = date( 'd', $current_time ); |
||
| 656 | $dates['day_end'] = date( 'd', $current_time ); |
||
| 657 | $dates['m_start'] = date( 'n', $current_time ); |
||
| 658 | $dates['m_end'] = date( 'n', $current_time ); |
||
| 659 | $dates['year'] = date( 'Y', $current_time ); |
||
| 660 | $dates['year_end'] = date( 'Y', $current_time ); |
||
| 661 | break; |
||
| 662 | |||
| 663 | View Code Duplication | case 'yesterday': |
|
| 664 | $year = date( 'Y', $current_time ); |
||
| 665 | $month = date( 'n', $current_time ); |
||
| 666 | $day = date( 'd', $current_time ); |
||
| 667 | |||
| 668 | if ( $month == 1 && $day == 1 ) { |
||
| 669 | |||
| 670 | $year -= 1; |
||
| 671 | $month = 12; |
||
| 672 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
||
| 673 | |||
| 674 | } elseif ( $month > 1 && $day == 1 ) { |
||
| 675 | |||
| 676 | $month -= 1; |
||
| 677 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
||
| 678 | |||
| 679 | } else { |
||
| 680 | |||
| 681 | $day -= 1; |
||
| 682 | |||
| 683 | } |
||
| 684 | |||
| 685 | $dates['day'] = $day; |
||
| 686 | $dates['m_start'] = $month; |
||
| 687 | $dates['m_end'] = $month; |
||
| 688 | $dates['year'] = $year; |
||
| 689 | $dates['year_end'] = $year; |
||
| 690 | break; |
||
| 691 | |||
| 692 | case 'this_week': |
||
| 693 | $dates['day'] = date( 'd', $current_time - ( date( 'w', $current_time ) - 1 ) * 60 * 60 * 24 ) - 1; |
||
| 694 | $dates['day'] += get_option( 'start_of_week' ); |
||
| 695 | $dates['day_end'] = $dates['day'] + 6; |
||
| 696 | $dates['m_start'] = date( 'n', $current_time ); |
||
| 697 | $dates['m_end'] = date( 'n', $current_time ); |
||
| 698 | $dates['year'] = date( 'Y', $current_time ); |
||
| 699 | break; |
||
| 700 | |||
| 701 | case 'last_week': |
||
| 702 | $dates['day'] = date( 'd', $current_time - ( date( 'w' ) - 1 ) * 60 * 60 * 24 ) - 8; |
||
| 703 | $dates['day'] += get_option( 'start_of_week' ); |
||
| 704 | $dates['day_end'] = $dates['day'] + 6; |
||
| 705 | $dates['year'] = date( 'Y' ); |
||
| 706 | |||
| 707 | if ( date( 'j', $current_time ) <= 7 ) { |
||
| 708 | $dates['m_start'] = date( 'n', $current_time ) - 1; |
||
| 709 | $dates['m_end'] = date( 'n', $current_time ) - 1; |
||
| 710 | if ( $dates['m_start'] <= 1 ) { |
||
| 711 | $dates['year'] = date( 'Y', $current_time ) - 1; |
||
| 712 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
||
| 713 | } |
||
| 714 | } else { |
||
| 715 | $dates['m_start'] = date( 'n', $current_time ); |
||
| 716 | $dates['m_end'] = date( 'n', $current_time ); |
||
| 717 | } |
||
| 718 | break; |
||
| 719 | |||
| 720 | case 'this_quarter': |
||
| 721 | $month_now = date( 'n', $current_time ); |
||
| 722 | $dates['year'] = date( 'Y', $current_time ); |
||
| 723 | |||
| 724 | View Code Duplication | if ( $month_now <= 3 ) { |
|
| 725 | |||
| 726 | $dates['m_start'] = 1; |
||
| 727 | $dates['m_end'] = 4; |
||
| 728 | |||
| 729 | } elseif ( $month_now <= 6 ) { |
||
| 730 | |||
| 731 | $dates['m_start'] = 4; |
||
| 732 | $dates['m_end'] = 7; |
||
| 733 | |||
| 734 | } elseif ( $month_now <= 9 ) { |
||
| 735 | |||
| 736 | $dates['m_start'] = 7; |
||
| 737 | $dates['m_end'] = 10; |
||
| 738 | |||
| 739 | } else { |
||
| 740 | |||
| 741 | $dates['m_start'] = 10; |
||
| 742 | $dates['m_end'] = 1; |
||
| 743 | $dates['year_end'] = date( 'Y', $current_time ) + 1; |
||
| 744 | |||
| 745 | } |
||
| 746 | break; |
||
| 747 | |||
| 748 | case 'last_quarter': |
||
| 749 | $month_now = date( 'n', $current_time ); |
||
| 750 | $dates['year'] = date( 'Y', $current_time ); |
||
| 751 | $dates['year_end'] = date( 'Y', $current_time ); |
||
| 752 | |||
| 753 | View Code Duplication | if ( $month_now <= 3 ) { |
|
| 754 | |||
| 755 | $dates['m_start'] = 10; |
||
| 756 | $dates['m_end'] = 1; |
||
| 757 | $dates['year'] = date( 'Y', $current_time ) - 1; // Previous year. |
||
| 758 | |||
| 759 | } elseif ( $month_now <= 6 ) { |
||
| 760 | |||
| 761 | $dates['m_start'] = 1; |
||
| 762 | $dates['m_end'] = 4; |
||
| 763 | |||
| 764 | } elseif ( $month_now <= 9 ) { |
||
| 765 | |||
| 766 | $dates['m_start'] = 4; |
||
| 767 | $dates['m_end'] = 7; |
||
| 768 | |||
| 769 | } else { |
||
| 770 | |||
| 771 | $dates['m_start'] = 7; |
||
| 772 | $dates['m_end'] = 10; |
||
| 773 | |||
| 774 | } |
||
| 775 | break; |
||
| 776 | |||
| 777 | case 'this_year': |
||
| 778 | $dates['m_start'] = 1; |
||
| 779 | $dates['m_end'] = 12; |
||
| 780 | $dates['year'] = date( 'Y', $current_time ); |
||
| 781 | $dates['year_end'] = date( 'Y', $current_time ); |
||
| 782 | break; |
||
| 783 | |||
| 784 | case 'last_year': |
||
| 785 | $dates['m_start'] = 1; |
||
| 786 | $dates['m_end'] = 12; |
||
| 787 | $dates['year'] = date( 'Y', $current_time ) - 1; |
||
| 788 | $dates['year_end'] = date( 'Y', $current_time ) - 1; |
||
| 789 | break; |
||
| 790 | |||
| 791 | endswitch; |
||
| 792 | |||
| 793 | return apply_filters( 'give_report_dates', $dates ); |
||
| 794 | } |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Grabs all of the selected date info and then redirects appropriately |
||
| 798 | * |
||
| 799 | * @since 1.0.0 |
||
| 800 | * @since 1.8.0 The `tab` query arg is added to the redirect. |
||
| 801 | * |
||
| 802 | * @param $data |
||
| 803 | */ |
||
| 804 | function give_parse_report_dates( $data ) { |
||
| 805 | $dates = give_get_report_dates(); |
||
| 806 | |||
| 807 | $view = give_get_reporting_view(); |
||
| 808 | $tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'earnings'; |
||
| 809 | $id = isset( $_GET['form-id'] ) ? $_GET['form-id'] : null; |
||
| 810 | |||
| 811 | 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 ) ) ) ); |
||
| 812 | give_die(); |
||
| 813 | } |
||
| 814 | |||
| 815 | add_action( 'give_filter_reports', 'give_parse_report_dates' ); |
||
| 816 | |||
| 817 | |||
| 818 | /** |
||
| 819 | * Give Reports Refresh Button |
||
| 820 | * |
||
| 821 | * Outputs a "Refresh Reports" button for graphs |
||
| 822 | * |
||
| 823 | * @since 1.3 |
||
| 824 | */ |
||
| 825 | function give_reports_refresh_button() { |
||
| 826 | |||
| 827 | $url = wp_nonce_url( |
||
| 828 | add_query_arg( |
||
| 829 | array( |
||
| 830 | 'give_action' => 'refresh_reports_transients', |
||
| 831 | 'give-messages[]' => 'refreshed-reports', |
||
| 832 | ) |
||
| 833 | ), 'give-refresh-reports' |
||
| 834 | ); |
||
| 835 | |||
| 836 | echo Give()->tooltips->render_link( |
||
| 837 | array( |
||
| 838 | 'label' => esc_attr__( 'Clicking this will clear the reports cache.', 'give' ), |
||
| 839 | 'tag_content' => '<span class="give-admin-button-icon give-admin-button-icon-update"></span>' . esc_html__( 'Refresh Report Data', 'give' ), |
||
| 840 | 'link' => $url, |
||
| 841 | 'position' => 'left', |
||
| 842 | 'attributes' => array( |
||
| 843 | 'class' => 'button alignright give-admin-button', |
||
| 844 | ), |
||
| 845 | ) |
||
| 846 | ); |
||
| 847 | } |
||
| 848 | |||
| 849 | add_action( 'give_reports_graph_additional_stats', 'give_reports_refresh_button' ); |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Trigger the refresh of reports transients |
||
| 853 | * |
||
| 854 | * @param array $data Parameters sent from Settings page. |
||
| 855 | * |
||
| 856 | * @since 1.3 |
||
| 857 | * |
||
| 858 | * @return void |
||
| 859 | */ |
||
| 860 | function give_run_refresh_reports_transients( $data ) { |
||
| 861 | |||
| 862 | if ( ! wp_verify_nonce( $data['_wpnonce'], 'give-refresh-reports' ) ) { |
||
| 863 | return; |
||
| 864 | } |
||
| 865 | |||
| 866 | // Monthly stats. |
||
| 867 | Give_Cache::delete( Give_Cache::get_key( 'give_estimated_monthly_stats' ) ); |
||
| 868 | |||
| 869 | // Total earning. |
||
| 870 | delete_option( 'give_earnings_total' ); |
||
| 871 | |||
| 872 | // @todo: Refresh only range related stat cache |
||
| 873 | give_delete_donation_stats(); |
||
| 874 | } |
||
| 875 | |||
| 876 | add_action( 'give_refresh_reports_transients', 'give_run_refresh_reports_transients' ); |
||
| 877 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.