admin
5 years ago
api
6 years ago
database
6 years ago
deprecated
6 years ago
donors
5 years ago
emails
6 years ago
forms
6 years ago
frontend
6 years ago
gateways
6 years ago
libraries
6 years ago
payments
6 years ago
actions.php
6 years ago
ajax-functions.php
6 years ago
class-give-async-process.php
6 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
6 years ago
class-give-cron.php
6 years ago
class-give-donate-form.php
6 years ago
class-give-donor.php
6 years ago
class-give-email-access.php
6 years ago
class-give-license-handler.php
6 years ago
class-give-logging.php
6 years ago
class-give-readme-parser.php
6 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
6 years ago
class-give-template-loader.php
6 years ago
class-give-tooltips.php
6 years ago
class-give-translation.php
6 years ago
class-notices.php
6 years ago
country-functions.php
6 years ago
currencies-list.php
6 years ago
currency-functions.php
6 years ago
error-tracking.php
6 years ago
filters.php
6 years ago
formatting.php
6 years ago
install.php
6 years ago
login-register.php
6 years ago
misc-functions.php
5 years ago
plugin-compatibility.php
6 years ago
post-types.php
6 years ago
price-functions.php
6 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
547 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 | if ( $end_date ) { |
| 167 | |
| 168 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 169 | $hour = 23; |
| 170 | $minute = 59; |
| 171 | $second = 59; |
| 172 | } |
| 173 | |
| 174 | break; |
| 175 | |
| 176 | case 'last_month': |
| 177 | if ( $month == 1 ) { |
| 178 | |
| 179 | $month = 12; |
| 180 | $year --; |
| 181 | |
| 182 | } else { |
| 183 | |
| 184 | $month --; |
| 185 | |
| 186 | } |
| 187 | |
| 188 | if ( $end_date ) { |
| 189 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 190 | } |
| 191 | |
| 192 | break; |
| 193 | |
| 194 | case 'today': |
| 195 | $day = date( 'd', current_time( 'timestamp' ) ); |
| 196 | |
| 197 | if ( $end_date ) { |
| 198 | $hour = 23; |
| 199 | $minute = 59; |
| 200 | $second = 59; |
| 201 | } |
| 202 | |
| 203 | break; |
| 204 | |
| 205 | case 'yesterday': |
| 206 | $day = date( 'd', current_time( 'timestamp' ) ) - 1; |
| 207 | |
| 208 | // Check if Today is the first day of the month (meaning subtracting one will get us 0) |
| 209 | if ( $day < 1 ) { |
| 210 | |
| 211 | // If current month is 1 |
| 212 | if ( 1 == $month ) { |
| 213 | |
| 214 | $year -= 1; // Today is January 1, so skip back to last day of December |
| 215 | $month = 12; |
| 216 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 217 | |
| 218 | } else { |
| 219 | |
| 220 | // Go back one month and get the last day of the month |
| 221 | $month -= 1; |
| 222 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 223 | |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | break; |
| 228 | |
| 229 | case 'this_week': |
| 230 | $days_to_week_start = ( date( 'w', current_time( 'timestamp' ) ) - 1 ) * 60 * 60 * 24; |
| 231 | $today = date( 'j', current_time( 'timestamp' ) ) * 60 * 60 * 24; |
| 232 | |
| 233 | if ( $today <= $days_to_week_start ) { |
| 234 | |
| 235 | if ( $month > 1 ) { |
| 236 | $month -= 1; |
| 237 | } else { |
| 238 | $month = 12; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if ( ! $end_date ) { |
| 243 | |
| 244 | // Getting the start day |
| 245 | |
| 246 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 1; |
| 247 | $day += get_option( 'start_of_week' ); |
| 248 | |
| 249 | } else { |
| 250 | |
| 251 | // Getting the end day |
| 252 | |
| 253 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 1; |
| 254 | $day += get_option( 'start_of_week' ) + 6; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | break; |
| 259 | |
| 260 | case 'last_week': |
| 261 | $days_to_week_start = ( date( 'w', current_time( 'timestamp' ) ) - 1 ) * 60 * 60 * 24; |
| 262 | $today = date( 'j', current_time( 'timestamp' ) ) * 60 * 60 * 24; |
| 263 | |
| 264 | if ( $today <= $days_to_week_start ) { |
| 265 | |
| 266 | if ( $month > 1 ) { |
| 267 | $month -= 1; |
| 268 | } else { |
| 269 | $month = 12; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if ( ! $end_date ) { |
| 274 | |
| 275 | // Getting the start day |
| 276 | |
| 277 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 8; |
| 278 | $day += get_option( 'start_of_week' ); |
| 279 | |
| 280 | } else { |
| 281 | |
| 282 | // Getting the end day |
| 283 | |
| 284 | $day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 8; |
| 285 | $day += get_option( 'start_of_week' ) + 6; |
| 286 | |
| 287 | } |
| 288 | |
| 289 | break; |
| 290 | |
| 291 | case 'this_quarter': |
| 292 | $month_now = date( 'n', current_time( 'timestamp' ) ); |
| 293 | |
| 294 | if ( $month_now <= 3 ) { |
| 295 | |
| 296 | if ( ! $end_date ) { |
| 297 | $month = 1; |
| 298 | } else { |
| 299 | $month = 3; |
| 300 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 301 | $hour = 23; |
| 302 | $minute = 59; |
| 303 | $second = 59; |
| 304 | } |
| 305 | } elseif ( $month_now <= 6 ) { |
| 306 | |
| 307 | if ( ! $end_date ) { |
| 308 | $month = 4; |
| 309 | } else { |
| 310 | $month = 6; |
| 311 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 312 | $hour = 23; |
| 313 | $minute = 59; |
| 314 | $second = 59; |
| 315 | } |
| 316 | } elseif ( $month_now <= 9 ) { |
| 317 | |
| 318 | if ( ! $end_date ) { |
| 319 | $month = 7; |
| 320 | } else { |
| 321 | $month = 9; |
| 322 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 323 | $hour = 23; |
| 324 | $minute = 59; |
| 325 | $second = 59; |
| 326 | } |
| 327 | } else { |
| 328 | |
| 329 | if ( ! $end_date ) { |
| 330 | $month = 10; |
| 331 | } else { |
| 332 | $month = 12; |
| 333 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 334 | $hour = 23; |
| 335 | $minute = 59; |
| 336 | $second = 59; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | break; |
| 341 | |
| 342 | case 'last_quarter': |
| 343 | $month_now = date( 'n', current_time( 'timestamp' ) ); |
| 344 | |
| 345 | if ( $month_now <= 3 ) { |
| 346 | |
| 347 | if ( ! $end_date ) { |
| 348 | $month = 10; |
| 349 | } else { |
| 350 | $year -= 1; |
| 351 | $month = 12; |
| 352 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 353 | $hour = 23; |
| 354 | $minute = 59; |
| 355 | $second = 59; |
| 356 | } |
| 357 | } elseif ( $month_now <= 6 ) { |
| 358 | |
| 359 | if ( ! $end_date ) { |
| 360 | $month = 1; |
| 361 | } else { |
| 362 | $month = 3; |
| 363 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 364 | $hour = 23; |
| 365 | $minute = 59; |
| 366 | $second = 59; |
| 367 | } |
| 368 | } elseif ( $month_now <= 9 ) { |
| 369 | |
| 370 | if ( ! $end_date ) { |
| 371 | $month = 4; |
| 372 | } else { |
| 373 | $month = 6; |
| 374 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 375 | $hour = 23; |
| 376 | $minute = 59; |
| 377 | $second = 59; |
| 378 | } |
| 379 | } else { |
| 380 | |
| 381 | if ( ! $end_date ) { |
| 382 | $month = 7; |
| 383 | } else { |
| 384 | $month = 9; |
| 385 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 386 | $hour = 23; |
| 387 | $minute = 59; |
| 388 | $second = 59; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | break; |
| 393 | |
| 394 | case 'this_year': |
| 395 | if ( ! $end_date ) { |
| 396 | $month = 1; |
| 397 | } else { |
| 398 | $month = 12; |
| 399 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 400 | $hour = 23; |
| 401 | $minute = 59; |
| 402 | $second = 59; |
| 403 | } |
| 404 | |
| 405 | break; |
| 406 | |
| 407 | case 'last_year': |
| 408 | $year -= 1; |
| 409 | if ( ! $end_date ) { |
| 410 | $month = 1; |
| 411 | } else { |
| 412 | $month = 12; |
| 413 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
| 414 | $hour = 23; |
| 415 | $minute = 59; |
| 416 | $second = 59; |
| 417 | } |
| 418 | |
| 419 | break; |
| 420 | |
| 421 | } |
| 422 | } elseif ( is_numeric( $date ) ) { |
| 423 | |
| 424 | // return $date unchanged since it is a timestamp |
| 425 | $this->timestamp = true; |
| 426 | |
| 427 | } elseif ( false !== strtotime( $date ) ) { |
| 428 | |
| 429 | $date = strtotime( $date, current_time( 'timestamp' ) ); |
| 430 | $year = date( 'Y', $date ); |
| 431 | $month = date( 'm', $date ); |
| 432 | $day = date( 'd', $date ); |
| 433 | |
| 434 | } else { |
| 435 | |
| 436 | return new WP_Error( 'invalid_date', esc_html__( 'Improper date provided.', 'give' ) ); |
| 437 | |
| 438 | } |
| 439 | |
| 440 | if ( false === $this->timestamp ) { |
| 441 | // Create an exact timestamp |
| 442 | $date = mktime( $hour, $minute, $second, $month, $day, $year ); |
| 443 | } |
| 444 | |
| 445 | return apply_filters( 'give_stats_date', $date, $end_date, $this ); |
| 446 | |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Count Where |
| 451 | * |
| 452 | * Modifies the WHERE flag for payment counts. |
| 453 | * |
| 454 | * @since 1.0 |
| 455 | * @access public |
| 456 | * |
| 457 | * @param string $where SQL WHERE statment. |
| 458 | * |
| 459 | * @return string |
| 460 | */ |
| 461 | public function count_where( $where = '' ) { |
| 462 | // Only get payments in our date range |
| 463 | |
| 464 | $start_where = ''; |
| 465 | $end_where = ''; |
| 466 | |
| 467 | if ( $this->start_date ) { |
| 468 | |
| 469 | if ( $this->timestamp ) { |
| 470 | $format = 'Y-m-d H:i:s'; |
| 471 | } else { |
| 472 | $format = 'Y-m-d 00:00:00'; |
| 473 | } |
| 474 | |
| 475 | $start_date = date( $format, $this->start_date ); |
| 476 | $start_where = " AND p.post_date >= '{$start_date}'"; |
| 477 | } |
| 478 | |
| 479 | if ( $this->end_date ) { |
| 480 | |
| 481 | if ( $this->timestamp ) { |
| 482 | $format = 'Y-m-d H:i:s'; |
| 483 | } else { |
| 484 | $format = 'Y-m-d 23:59:59'; |
| 485 | } |
| 486 | |
| 487 | $end_date = date( $format, $this->end_date ); |
| 488 | |
| 489 | $end_where = " AND p.post_date <= '{$end_date}'"; |
| 490 | } |
| 491 | |
| 492 | $where .= "{$start_where}{$end_where}"; |
| 493 | |
| 494 | return $where; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Payment Where |
| 499 | * |
| 500 | * Modifies the WHERE flag for payment queries. |
| 501 | * |
| 502 | * @since 1.0 |
| 503 | * @access public |
| 504 | * |
| 505 | * @param string $where SQL WHERE statment. |
| 506 | * |
| 507 | * @return string |
| 508 | */ |
| 509 | public function payments_where( $where = '' ) { |
| 510 | |
| 511 | global $wpdb; |
| 512 | |
| 513 | $start_where = ''; |
| 514 | $end_where = ''; |
| 515 | |
| 516 | if ( ! is_wp_error( $this->start_date ) ) { |
| 517 | |
| 518 | if ( $this->timestamp ) { |
| 519 | $format = 'Y-m-d H:i:s'; |
| 520 | } else { |
| 521 | $format = 'Y-m-d 00:00:00'; |
| 522 | } |
| 523 | |
| 524 | $start_date = date( $format, $this->start_date ); |
| 525 | $start_where = " AND $wpdb->posts.post_date >= '{$start_date}'"; |
| 526 | } |
| 527 | |
| 528 | if ( ! is_wp_error( $this->end_date ) ) { |
| 529 | |
| 530 | if ( $this->timestamp ) { |
| 531 | $format = 'Y-m-d H:i:s'; |
| 532 | } else { |
| 533 | $format = 'Y-m-d 23:59:59'; |
| 534 | } |
| 535 | |
| 536 | $end_date = date( $format, $this->end_date ); |
| 537 | |
| 538 | $end_where = " AND $wpdb->posts.post_date <= '{$end_date}'"; |
| 539 | } |
| 540 | |
| 541 | $where .= "{$start_where}{$end_where}"; |
| 542 | |
| 543 | return $where; |
| 544 | } |
| 545 | |
| 546 | } |
| 547 |