Issues (4296)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

includes/class-give-stats.php (29 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 15
	 * @var    string
67 15
	 */
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 2
	 *
78
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
79 2
	 */
80 2
	public function __construct() {
81 2
		/* nothing here. Call get_sales() and get_earnings() directly */
82 2
	}
83 2
84 2
	/**
85 2
	 * Get Predefined Dates
86 2
	 *
87 2
	 * Retrieve the predefined date periods permitted.
88 2
	 *
89 2
	 * @since  1.0
90
	 * @access public
91 2
	 *
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 2
108
		return apply_filters( 'give_stats_predefined_dates', $predefined );
109 2
	}
110
111
	/**
112
	 * Setup the dates passed to our constructor.
113 2
	 *
114 2
	 * This calls the convert_date() member function to ensure the dates are formatted correctly.
115 2
	 *
116
	 * @since  1.0
117 2
	 * @access public
118 2
	 *
119 2
	 * @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 1
		}
129
130 1
		if ( empty( $_end_date ) ) {
131 1
			$_end_date = $_start_date;
132 1
		}
133 1
134 1
		$this->start_date = $this->convert_date( $_start_date );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->convert_date($_start_date) of type array or object<WP_Error> is incompatible with the declared type string of property $start_date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
135 1
		$this->end_date   = $this->convert_date( $_end_date, true );
0 ignored issues
show
It seems like $_end_date defined by parameter $_end_date on line 124 can also be of type boolean; however, Give_Stats::convert_date() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation Bug introduced by
It seems like $this->convert_date($_end_date, true) of type array or object<WP_Error> is incompatible with the declared type string of property $end_date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
136 1
	}
137
138 1
	/**
139
	 * Convert Date
140
	 *
141
	 * Converts a date to a timestamp.
142
	 *
143 1
	 * @since  1.0
144
	 * @access public
145 1
	 *
146
	 * @param  string $date     Date.
147 1
	 * @param  bool   $end_date End date. Default is false.
148 1
	 *
149 1
	 * @return array|WP_Error   If the date is invalid, a WP_Error object will be returned.
150 1
	 */
151 1
	public function convert_date( $date, $end_date = false ) {
152
153 1
		$this->timestamp = false;
0 ignored issues
show
Documentation Bug introduced by
The property $timestamp was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
154
		$second          = $end_date ? 59 : 0;
155
		$minute          = $end_date ? 59 : 0;
156
		$hour            = $end_date ? 23 : 0;
157
		$day             = 1;
158
		$month           = date( 'n', current_time( 'timestamp' ) );
159
		$year            = date( 'Y', current_time( 'timestamp' ) );
160
161
		if ( array_key_exists( (string) $date, $this->get_predefined_dates() ) ) {
162
163
			// This is a predefined date rate, such as last_week
164
			switch ( $date ) {
165
166
				case 'this_month' :
167
168
					if ( $end_date ) {
169
170
						$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
171
						$hour   = 23;
172
						$minute = 59;
173
						$second = 59;
174
					}
175
176
					break;
177
178
				case 'last_month' :
179
180
					if ( $month == 1 ) {
0 ignored issues
show
Found "== 1". Use Yoda Condition checks, you must
Loading history...
181
182
						$month = 12;
183
						$year --;
184
185
					} else {
186
187
						$month --;
188
189
					}
190
191
					if ( $end_date ) {
192
						$day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
193
					}
194
195
					break;
196
197
				case 'today' :
198
199
					$day = date( 'd', current_time( 'timestamp' ) );
200
201
					if ( $end_date ) {
202
						$hour   = 23;
203
						$minute = 59;
204
						$second = 59;
205
					}
206
207
					break;
208
209
				case 'yesterday' :
210
211
					$day = date( 'd', current_time( 'timestamp' ) ) - 1;
212
213
					// Check if Today is the first day of the month (meaning subtracting one will get us 0)
214
					if ( $day < 1 ) {
215
216
						// If current month is 1
217
						if ( 1 == $month ) {
218
219
							$year -= 1; // Today is January 1, so skip back to last day of December
220
							$month = 12;
221
							$day   = cal_days_in_month( CAL_GREGORIAN, $month, $year );
222
223
						} else {
224
225
							// Go back one month and get the last day of the month
226
							$month -= 1;
227
							$day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
228
229
						}
230
					}
231
232
					break;
233
234 View Code Duplication
				case 'this_week' :
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...
235
236
					$days_to_week_start = ( date( 'w', current_time( 'timestamp' ) ) - 1 ) * 60 * 60 * 24;
237
					$today              = date( 'j', current_time( 'timestamp' ) ) * 60 * 60 * 24;
238
239
					if ( $today <= $days_to_week_start ) {
240
241
						if ( $month > 1 ) {
242
							$month -= 1;
243
						} else {
244
							$month = 12;
245
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
246
247
					}
248
249
					if ( ! $end_date ) {
250
251
						// Getting the start day
252
253
						$day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 1;
254
						$day += get_option( 'start_of_week' );
255
256
					} else {
257
258
						// Getting the end day
259
260
						$day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 1;
261
						$day += get_option( 'start_of_week' ) + 6;
262
263
					}
264
265
					break;
266
267 View Code Duplication
				case 'last_week' :
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...
268
269
					$days_to_week_start = ( date( 'w', current_time( 'timestamp' ) ) - 1 ) * 60 * 60 * 24;
270
					$today              = date( 'j', current_time( 'timestamp' ) ) * 60 * 60 * 24;
271
272
					if ( $today <= $days_to_week_start ) {
273
274
						if ( $month > 1 ) {
275
							$month -= 1;
276
						} else {
277
							$month = 12;
278
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
279
280
					}
281
282
					if ( ! $end_date ) {
283
284
						// Getting the start day
285
286
						$day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 8;
287
						$day += get_option( 'start_of_week' );
288
289
					} else {
290
291
						// Getting the end day
292
293
						$day = date( 'd', current_time( 'timestamp' ) - $days_to_week_start ) - 8;
294
						$day += get_option( 'start_of_week' ) + 6;
295
296
					}
297
298
					break;
299
300 View Code Duplication
				case 'this_quarter' :
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...
301
302
					$month_now = date( 'n', current_time( 'timestamp' ) );
303
304
					if ( $month_now <= 3 ) {
305
306
						if ( ! $end_date ) {
307
							$month = 1;
308
						} else {
309
							$month  = 3;
310
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
311
							$hour   = 23;
312
							$minute = 59;
313
							$second = 59;
314
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
315
316
					} else if ( $month_now <= 6 ) {
317
318
						if ( ! $end_date ) {
319
							$month = 4;
320
						} else {
321
							$month  = 6;
322
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
323
							$hour   = 23;
324
							$minute = 59;
325
							$second = 59;
326
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
327
328
					} else if ( $month_now <= 9 ) {
329
330
						if ( ! $end_date ) {
331
							$month = 7;
332
						} else {
333
							$month  = 9;
334
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
335
							$hour   = 23;
336
							$minute = 59;
337
							$second = 59;
338
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
339
340
					} else {
341
342
						if ( ! $end_date ) {
343
							$month = 10;
344
						} else {
345
							$month  = 12;
346
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
347
							$hour   = 23;
348
							$minute = 59;
349
							$second = 59;
350
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
351
352
					}
353
354
					break;
355
356 View Code Duplication
				case 'last_quarter' :
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...
357
358
					$month_now = date( 'n', current_time( 'timestamp' ) );
359
360
					if ( $month_now <= 3 ) {
361
362
						if ( ! $end_date ) {
363
							$month = 10;
364
						} else {
365
							$year -= 1;
366
							$month  = 12;
367
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
368
							$hour   = 23;
369
							$minute = 59;
370
							$second = 59;
371
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
372
373
					} else if ( $month_now <= 6 ) {
374
375
						if ( ! $end_date ) {
376
							$month = 1;
377
						} else {
378
							$month  = 3;
379
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
380
							$hour   = 23;
381
							$minute = 59;
382
							$second = 59;
383
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
384
385
					} else if ( $month_now <= 9 ) {
386
387
						if ( ! $end_date ) {
388
							$month = 4;
389
						} else {
390
							$month  = 6;
391
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
392
							$hour   = 23;
393
							$minute = 59;
394
							$second = 59;
395
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
396
397
					} else {
398
399
						if ( ! $end_date ) {
400
							$month = 7;
401
						} else {
402
							$month  = 9;
403
							$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
404
							$hour   = 23;
405
							$minute = 59;
406
							$second = 59;
407
						}
0 ignored issues
show
Blank line found after control structure
Loading history...
408
409
					}
410
411
					break;
412
413 View Code Duplication
				case 'this_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...
414
415
					if ( ! $end_date ) {
416
						$month = 1;
417
					} else {
418
						$month  = 12;
419
						$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
420
						$hour   = 23;
421
						$minute = 59;
422 1
						$second = 59;
423
					}
424
425
					break;
426
427 View Code Duplication
				case '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...
428
429
					$year -= 1;
430
					if ( ! $end_date ) {
431
						$month = 1;
432
					} else {
433
						$month  = 12;
434
						$day    = cal_days_in_month( CAL_GREGORIAN, $month, $year );
435
						$hour   = 23;
436
						$minute = 59;
437
						$second = 59;
438
					}
439
440 1
					break;
441
442 1
			}
0 ignored issues
show
Blank line found after control structure
Loading history...
443 1
0 ignored issues
show
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
444
445 1
		} else if ( is_numeric( $date ) ) {
446
447
			// return $date unchanged since it is a timestamp
448
			$this->timestamp = true;
0 ignored issues
show
Documentation Bug introduced by
The property $timestamp was declared of type string, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
449
450
		} else if ( false !== strtotime( $date ) ) {
451
452
			$date  = strtotime( $date, current_time( 'timestamp' ) );
453
			$year  = date( 'Y', $date );
454
			$month = date( 'm', $date );
455
			$day   = date( 'd', $date );
456 1
457
		} else {
458
459 1
			return new WP_Error( 'invalid_date', esc_html__( 'Improper date provided.', 'give' ) );
460 1
461
		}
462 1
463
		if ( false === $this->timestamp ) {
464 1
			// Create an exact timestamp
465
			$date = mktime( $hour, $minute, $second, $month, $day, $year );
466
		}
467 1
468
		return apply_filters( 'give_stats_date', $date, $end_date, $this );
469
470 1
	}
471 1
472 1
	/**
473
	 * Count Where
474 1
	 *
475
	 * Modifies the WHERE flag for payment counts.
476 1
	 *
477
	 * @since  1.0
478
	 * @access public
479 1
	 *
480
	 * @param  string $where SQL WHERE statment.
481
	 * 
482 1
	 * @return string
483
	 */
484 1
	public function count_where( $where = '' ) {
485 1
		// Only get payments in our date range
486
487 1
		$start_where = '';
488
		$end_where   = '';
489 1
490 View Code Duplication
		if ( $this->start_date ) {
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...
491
492
			if ( $this->timestamp ) {
493
				$format = 'Y-m-d H:i:s';
494
			} else {
495
				$format = 'Y-m-d 00:00:00';
496
			}
497
498
			$start_date  = date( $format, $this->start_date );
499
			$start_where = " AND p.post_date >= '{$start_date}'";
500
		}
501
502 View Code Duplication
		if ( $this->end_date ) {
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...
503
504
			if ( $this->timestamp ) {
505
				$format = 'Y-m-d H:i:s';
506
			} else {
507
				$format = 'Y-m-d 23:59:59';
508
			}
509
510
			$end_date = date( $format, $this->end_date );
511
512
			$end_where = " AND p.post_date <= '{$end_date}'";
513
		}
514
515
		$where .= "{$start_where}{$end_where}";
516
517
		return $where;
518
	}
519
520
	/**
521
	 * Payment Where
522
	 *
523
	 * Modifies the WHERE flag for payment queries.
524
	 *
525
	 * @since  1.0
526
	 * @access public
527
	 *
528
	 * @param  string $where SQL WHERE statment.
529
	 *
530
	 * @return string
531
	 */
532
	public function payments_where( $where = '' ) {
533
534
		global $wpdb;
535
536
		$start_where = '';
537
		$end_where   = '';
538
539 View Code Duplication
		if ( ! is_wp_error( $this->start_date ) ) {
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...
540
541
			if ( $this->timestamp ) {
542
				$format = 'Y-m-d H:i:s';
543
			} else {
544
				$format = 'Y-m-d 00:00:00';
545
			}
546
547
			$start_date  = date( $format, $this->start_date );
548
			$start_where = " AND $wpdb->posts.post_date >= '{$start_date}'";
549
		}
550
551 View Code Duplication
		if ( ! is_wp_error( $this->end_date ) ) {
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...
552
553
			if ( $this->timestamp ) {
554
				$format = 'Y-m-d H:i:s';
555
			} else {
556
				$format = 'Y-m-d 23:59:59';
557
			}
558
559
			$end_date = date( $format, $this->end_date );
560
561
			$end_where = " AND $wpdb->posts.post_date <= '{$end_date}'";
562
		}
563
564
		$where .= "{$start_where}{$end_where}";
565
566
		return $where;
567
	}
568
569
}
570