admin
6 years ago
api
6 years ago
database
6 years ago
deprecated
6 years ago
donors
6 years ago
emails
6 years ago
forms
6 years ago
frontend
7 years ago
gateways
6 years ago
libraries
7 years ago
payments
6 years ago
actions.php
6 years ago
ajax-functions.php
6 years ago
class-give-async-process.php
7 years ago
class-give-background-updater.php
6 years ago
class-give-cache-setting.php
6 years ago
class-give-cache.php
6 years ago
class-give-cli-commands.php
6 years ago
class-give-comment.php
7 years ago
class-give-cron.php
6 years ago
class-give-donate-form.php
6 years ago
class-give-donor-wall-widget.php
6 years ago
class-give-donor.php
6 years ago
class-give-email-access.php
7 years ago
class-give-license-handler.php
6 years ago
class-give-logging.php
7 years ago
class-give-readme-parser.php
7 years ago
class-give-roles.php
6 years ago
class-give-scripts.php
6 years ago
class-give-session.php
6 years ago
class-give-stats.php
7 years ago
class-give-template-loader.php
8 years ago
class-give-tooltips.php
7 years ago
class-give-translation.php
8 years ago
class-notices.php
6 years ago
country-functions.php
6 years ago
currencies-list.php
6 years ago
currency-functions.php
7 years ago
error-tracking.php
7 years ago
filters.php
7 years ago
formatting.php
6 years ago
install.php
6 years ago
login-register.php
7 years ago
misc-functions.php
6 years ago
plugin-compatibility.php
6 years ago
post-types.php
6 years ago
price-functions.php
7 years ago
process-donation.php
6 years ago
setting-functions.php
6 years ago
shortcodes.php
6 years ago
template-functions.php
6 years ago
user-functions.php
6 years ago
class-give-stats.php
569 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Stats |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Give_Stats |
| 7 | * @copyright Copyright (c) 2016, Give |
| 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 | * Give_Stats Class |
| 19 | * |
| 20 | * Base class for other stats classes. Primarily for setting up dates and ranges. |
| 21 | * |
| 22 | * @since 1.0 |
| 23 | */ |
| 24 | class Give_Stats { |
| 25 | |
| 26 | /** |
| 27 | * The start date for the period we're getting stats for |
| 28 | * |
| 29 | * Can be a timestamp, formatted date, date string (such as August 3, 2013), |
| 30 | * or a predefined date string, such as last_week or this_month |
| 31 | * |
| 32 | * Predefined date options are: today, yesterday, this_week, last_week, this_month, last_month |
| 33 | * this_quarter, last_quarter, this_year, last_year |
| 34 | * |
| 35 | * @since 1.0 |
| 36 | * @access public |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $start_date; |
| 41 | |
| 42 | /** |
| 43 | * The end date for the period we're getting stats for |
| 44 | * |
| 45 | * Can be a timestamp, formatted date, date string (such as August 3, 2013), |
| 46 | * or a predefined date string, such as last_week or this_month |
| 47 | * |
| 48 | * Predefined date options are: today, yesterday, this_week, last_week, this_month, last_month |
| 49 | * this_quarter, last_quarter, this_year, last_year |
| 50 | * |
| 51 | * The end date is optional |
| 52 | * |
| 53 | * @since 1.0 |
| 54 | * @access public |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
| 58 | public $end_date; |
| 59 | |
| 60 | /** |
| 61 | * Flag to determine if current query is based on timestamps |
| 62 | * |
| 63 | * @since 1.0 |
| 64 | * @access public |
| 65 | * |
| 66 | * @var string |
| 67 | */ |
| 68 | public $timestamp; |
| 69 | |
| 70 | /** |
| 71 | * Class Constructor |
| 72 | * |
| 73 | * Set up the Give Stats Class. |
| 74 | * |
| 75 | * @since 1.0 |
| 76 | * @access public |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function __construct() { |
| 81 | /* nothing here. Call get_sales() and get_earnings() directly */ |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get Predefined Dates |
| 86 | * |
| 87 | * Retrieve the predefined date periods permitted. |
| 88 | * |
| 89 | * @since 1.0 |
| 90 | * @access public |
| 91 | * |
| 92 | * @return array Predefined dates. |
| 93 | */ |
| 94 | public function get_predefined_dates() { |
| 95 | $predefined = array( |
| 96 | 'today' => esc_html__( 'Today', 'give' ), |
| 97 | 'yesterday' => esc_html__( 'Yesterday', 'give' ), |
| 98 | 'this_week' => esc_html__( 'This Week', 'give' ), |
| 99 | 'last_week' => esc_html__( 'Last Week', 'give' ), |
| 100 | 'this_month' => esc_html__( 'This Month', 'give' ), |
| 101 | 'last_month' => esc_html__( 'Last Month', 'give' ), |
| 102 | 'this_quarter' => esc_html__( 'This Quarter', 'give' ), |
| 103 | 'last_quarter' => esc_html__( 'Last Quarter', 'give' ), |
| 104 | 'this_year' => esc_html__( 'This Year', 'give' ), |
| 105 | 'last_year' => esc_html__( 'Last Year', 'give' ), |
| 106 | ); |
| 107 | |
| 108 | return apply_filters( 'give_stats_predefined_dates', $predefined ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Setup the dates passed to our constructor. |
| 113 | * |
| 114 | * This calls the convert_date() member function to ensure the dates are formatted correctly. |
| 115 | * |
| 116 | * @since 1.0 |
| 117 | * @access public |
| 118 | * |
| 119 | * @param string $_start_date Start date. Default is 'this_month'. |
| 120 | * @param bool $_end_date End date. Default is false. |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | public function setup_dates( $_start_date = 'this_month', $_end_date = false ) { |
| 125 | |
| 126 | if ( empty( $_start_date ) ) { |
| 127 | $_start_date = 'this_month'; |
| 128 | } |
| 129 | |
| 130 | if ( empty( $_end_date ) ) { |
| 131 | $_end_date = $_start_date; |
| 132 | } |
| 133 | $this->start_date = $this->convert_date( $_start_date ); |
| 134 | $this->end_date = $this->convert_date( $_end_date, true ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Convert Date |
| 139 | * |
| 140 | * Converts a date to a timestamp. |
| 141 | * |
| 142 | * @since 1.0 |
| 143 | * @access public |
| 144 | * |
| 145 | * @param string $date Date. |
| 146 | * @param bool $end_date End date. Default is false. |
| 147 | * |
| 148 | * @return array|WP_Error If the date is invalid, a WP_Error object will be returned. |
| 149 | */ |
| 150 | public function convert_date( $date, $end_date = false ) { |
| 151 | |
| 152 | $this->timestamp = false; |
| 153 | $second = $end_date ? 59 : 0; |
| 154 | $minute = $end_date ? 59 : 0; |
| 155 | $hour = $end_date ? 23 : 0; |
| 156 | $day = 1; |
| 157 | $month = date( 'n', current_time( 'timestamp' ) ); |
| 158 | $year = date( 'Y', current_time( 'timestamp' ) ); |
| 159 | |
| 160 | if ( array_key_exists( (string) $date, $this->get_predefined_dates() ) ) { |
| 161 | |
| 162 | // This is a predefined date rate, such as last_week |
| 163 | switch ( $date ) { |
| 164 | |
| 165 | case 'this_month' : |
| 166 | |
| 167 | if ( $end_date ) { |
| 168 | |
| 169 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 170 | $hour = 23; |
| 171 | $minute = 59; |
| 172 | $second = 59; |
| 173 | } |
| 174 | |
| 175 | break; |
| 176 | |
| 177 | case 'last_month' : |
| 178 | |
| 179 | if ( $month == 1 ) { |
| 180 | |
| 181 | $month = 12; |
| 182 | $year --; |
| 183 | |
| 184 | } else { |
| 185 | |
| 186 | $month --; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | if ( $end_date ) { |
| 191 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 192 | } |
| 193 | |
| 194 | break; |
| 195 | |
| 196 | case 'today' : |
| 197 | |
| 198 | $day = date( 'd', current_time( 'timestamp' ) ); |
| 199 | |
| 200 | if ( $end_date ) { |
| 201 | $hour = 23; |
| 202 | $minute = 59; |
| 203 | $second = 59; |
| 204 | } |
| 205 | |
| 206 | break; |
| 207 | |
| 208 | case 'yesterday' : |
| 209 | |
| 210 | $day = date( 'd', current_time( 'timestamp' ) ) - 1; |
| 211 | |
| 212 | // Check if Today is the first day of the month (meaning subtracting one will get us 0) |
| 213 | if ( $day < 1 ) { |
| 214 | |
| 215 | // If current month is 1 |
| 216 | if ( 1 == $month ) { |
| 217 | |
| 218 | $year -= 1; // Today is January 1, so skip back to last day of December |
| 219 | $month = 12; |
| 220 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 221 | |
| 222 | } else { |
| 223 | |
| 224 | // Go back one month and get the last day of the month |
| 225 | $month -= 1; |
| 226 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 227 | |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | break; |
| 232 | |
| 233 | case 'this_week' : |
| 234 | |
| 235 | $days_to_week_start = ( date( 'w', current_time( 'timestamp' ) ) - 1 ) * 60 * 60 * 24; |
| 236 | $today = date( 'j', current_time( 'timestamp' ) ) * 60 * 60 * 24; |
| 237 | |
| 238 | if ( $today <= $days_to_week_start ) { |
| 239 | |
| 240 | if ( $month > 1 ) { |
| 241 | $month -= 1; |
| 242 | } else { |
| 243 | $month = 12; |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | if ( ! $end_date ) { |
| 249 | |
| 250 | // Getting the start day |
| 251 | |
| 252 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 1; |
| 253 | $day += get_option( 'start_of_week' ); |
| 254 | |
| 255 | } else { |
| 256 | |
| 257 | // Getting the end day |
| 258 | |
| 259 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 1; |
| 260 | $day += get_option( 'start_of_week' ) + 6; |
| 261 | |
| 262 | } |
| 263 | |
| 264 | break; |
| 265 | |
| 266 | case 'last_week' : |
| 267 | |
| 268 | $days_to_week_start = ( date( 'w', current_time( 'timestamp' ) ) - 1 ) * 60 * 60 * 24; |
| 269 | $today = date( 'j', current_time( 'timestamp' ) ) * 60 * 60 * 24; |
| 270 | |
| 271 | if ( $today <= $days_to_week_start ) { |
| 272 | |
| 273 | if ( $month > 1 ) { |
| 274 | $month -= 1; |
| 275 | } else { |
| 276 | $month = 12; |
| 277 | } |
| 278 | |
| 279 | } |
| 280 | |
| 281 | if ( ! $end_date ) { |
| 282 | |
| 283 | // Getting the start day |
| 284 | |
| 285 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 8; |
| 286 | $day += get_option( 'start_of_week' ); |
| 287 | |
| 288 | } else { |
| 289 | |
| 290 | // Getting the end day |
| 291 | |
| 292 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 8; |
| 293 | $day += get_option( 'start_of_week' ) + 6; |
| 294 | |
| 295 | } |
| 296 | |
| 297 | break; |
| 298 | |
| 299 | case 'this_quarter' : |
| 300 | |
| 301 | $month_now = date( 'n', current_time( 'timestamp' ) ); |
| 302 | |
| 303 | if ( $month_now <= 3 ) { |
| 304 | |
| 305 | if ( ! $end_date ) { |
| 306 | $month = 1; |
| 307 | } else { |
| 308 | $month = 3; |
| 309 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 310 | $hour = 23; |
| 311 | $minute = 59; |
| 312 | $second = 59; |
| 313 | } |
| 314 | |
| 315 | } else if ( $month_now <= 6 ) { |
| 316 | |
| 317 | if ( ! $end_date ) { |
| 318 | $month = 4; |
| 319 | } else { |
| 320 | $month = 6; |
| 321 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 322 | $hour = 23; |
| 323 | $minute = 59; |
| 324 | $second = 59; |
| 325 | } |
| 326 | |
| 327 | } else if ( $month_now <= 9 ) { |
| 328 | |
| 329 | if ( ! $end_date ) { |
| 330 | $month = 7; |
| 331 | } else { |
| 332 | $month = 9; |
| 333 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 334 | $hour = 23; |
| 335 | $minute = 59; |
| 336 | $second = 59; |
| 337 | } |
| 338 | |
| 339 | } else { |
| 340 | |
| 341 | if ( ! $end_date ) { |
| 342 | $month = 10; |
| 343 | } else { |
| 344 | $month = 12; |
| 345 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 346 | $hour = 23; |
| 347 | $minute = 59; |
| 348 | $second = 59; |
| 349 | } |
| 350 | |
| 351 | } |
| 352 | |
| 353 | break; |
| 354 | |
| 355 | case 'last_quarter' : |
| 356 | |
| 357 | $month_now = date( 'n', current_time( 'timestamp' ) ); |
| 358 | |
| 359 | if ( $month_now <= 3 ) { |
| 360 | |
| 361 | if ( ! $end_date ) { |
| 362 | $month = 10; |
| 363 | } else { |
| 364 | $year -= 1; |
| 365 | $month = 12; |
| 366 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 367 | $hour = 23; |
| 368 | $minute = 59; |
| 369 | $second = 59; |
| 370 | } |
| 371 | |
| 372 | } else if ( $month_now <= 6 ) { |
| 373 | |
| 374 | if ( ! $end_date ) { |
| 375 | $month = 1; |
| 376 | } else { |
| 377 | $month = 3; |
| 378 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 379 | $hour = 23; |
| 380 | $minute = 59; |
| 381 | $second = 59; |
| 382 | } |
| 383 | |
| 384 | } else if ( $month_now <= 9 ) { |
| 385 | |
| 386 | if ( ! $end_date ) { |
| 387 | $month = 4; |
| 388 | } else { |
| 389 | $month = 6; |
| 390 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 391 | $hour = 23; |
| 392 | $minute = 59; |
| 393 | $second = 59; |
| 394 | } |
| 395 | |
| 396 | } else { |
| 397 | |
| 398 | if ( ! $end_date ) { |
| 399 | $month = 7; |
| 400 | } else { |
| 401 | $month = 9; |
| 402 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 403 | $hour = 23; |
| 404 | $minute = 59; |
| 405 | $second = 59; |
| 406 | } |
| 407 | |
| 408 | } |
| 409 | |
| 410 | break; |
| 411 | |
| 412 | case 'this_year' : |
| 413 | |
| 414 | if ( ! $end_date ) { |
| 415 | $month = 1; |
| 416 | } else { |
| 417 | $month = 12; |
| 418 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 419 | $hour = 23; |
| 420 | $minute = 59; |
| 421 | $second = 59; |
| 422 | } |
| 423 | |
| 424 | break; |
| 425 | |
| 426 | case 'last_year' : |
| 427 | |
| 428 | $year -= 1; |
| 429 | if ( ! $end_date ) { |
| 430 | $month = 1; |
| 431 | } else { |
| 432 | $month = 12; |
| 433 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 434 | $hour = 23; |
| 435 | $minute = 59; |
| 436 | $second = 59; |
| 437 | } |
| 438 | |
| 439 | break; |
| 440 | |
| 441 | } |
| 442 | |
| 443 | |
| 444 | } else if ( is_numeric( $date ) ) { |
| 445 | |
| 446 | // return $date unchanged since it is a timestamp |
| 447 | $this->timestamp = true; |
| 448 | |
| 449 | } else if ( false !== strtotime( $date ) ) { |
| 450 | |
| 451 | $date = strtotime( $date, current_time( 'timestamp' ) ); |
| 452 | $year = date( 'Y', $date ); |
| 453 | $month = date( 'm', $date ); |
| 454 | $day = date( 'd', $date ); |
| 455 | |
| 456 | } else { |
| 457 | |
| 458 | return new WP_Error( 'invalid_date', esc_html__( 'Improper date provided.', 'give' ) ); |
| 459 | |
| 460 | } |
| 461 | |
| 462 | if ( false === $this->timestamp ) { |
| 463 | // Create an exact timestamp |
| 464 | $date = mktime( $hour, $minute, $second, $month, $day, $year ); |
| 465 | } |
| 466 | |
| 467 | return apply_filters( 'give_stats_date', $date, $end_date, $this ); |
| 468 | |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Count Where |
| 473 | * |
| 474 | * Modifies the WHERE flag for payment counts. |
| 475 | * |
| 476 | * @since 1.0 |
| 477 | * @access public |
| 478 | * |
| 479 | * @param string $where SQL WHERE statment. |
| 480 | * |
| 481 | * @return string |
| 482 | */ |
| 483 | public function count_where( $where = '' ) { |
| 484 | // Only get payments in our date range |
| 485 | |
| 486 | $start_where = ''; |
| 487 | $end_where = ''; |
| 488 | |
| 489 | if ( $this->start_date ) { |
| 490 | |
| 491 | if ( $this->timestamp ) { |
| 492 | $format = 'Y-m-d H:i:s'; |
| 493 | } else { |
| 494 | $format = 'Y-m-d 00:00:00'; |
| 495 | } |
| 496 | |
| 497 | $start_date = date( $format, $this->start_date ); |
| 498 | $start_where = " AND p.post_date >= '{$start_date}'"; |
| 499 | } |
| 500 | |
| 501 | if ( $this->end_date ) { |
| 502 | |
| 503 | if ( $this->timestamp ) { |
| 504 | $format = 'Y-m-d H:i:s'; |
| 505 | } else { |
| 506 | $format = 'Y-m-d 23:59:59'; |
| 507 | } |
| 508 | |
| 509 | $end_date = date( $format, $this->end_date ); |
| 510 | |
| 511 | $end_where = " AND p.post_date <= '{$end_date}'"; |
| 512 | } |
| 513 | |
| 514 | $where .= "{$start_where}{$end_where}"; |
| 515 | |
| 516 | return $where; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Payment Where |
| 521 | * |
| 522 | * Modifies the WHERE flag for payment queries. |
| 523 | * |
| 524 | * @since 1.0 |
| 525 | * @access public |
| 526 | * |
| 527 | * @param string $where SQL WHERE statment. |
| 528 | * |
| 529 | * @return string |
| 530 | */ |
| 531 | public function payments_where( $where = '' ) { |
| 532 | |
| 533 | global $wpdb; |
| 534 | |
| 535 | $start_where = ''; |
| 536 | $end_where = ''; |
| 537 | |
| 538 | if ( ! is_wp_error( $this->start_date ) ) { |
| 539 | |
| 540 | if ( $this->timestamp ) { |
| 541 | $format = 'Y-m-d H:i:s'; |
| 542 | } else { |
| 543 | $format = 'Y-m-d 00:00:00'; |
| 544 | } |
| 545 | |
| 546 | $start_date = date( $format, $this->start_date ); |
| 547 | $start_where = " AND $wpdb->posts.post_date >= '{$start_date}'"; |
| 548 | } |
| 549 | |
| 550 | if ( ! is_wp_error( $this->end_date ) ) { |
| 551 | |
| 552 | if ( $this->timestamp ) { |
| 553 | $format = 'Y-m-d H:i:s'; |
| 554 | } else { |
| 555 | $format = 'Y-m-d 23:59:59'; |
| 556 | } |
| 557 | |
| 558 | $end_date = date( $format, $this->end_date ); |
| 559 | |
| 560 | $end_where = " AND $wpdb->posts.post_date <= '{$end_date}'"; |
| 561 | } |
| 562 | |
| 563 | $where .= "{$start_where}{$end_where}"; |
| 564 | |
| 565 | return $where; |
| 566 | } |
| 567 | |
| 568 | } |
| 569 |