| 1 |
<?php |
| 2 |
/** |
| 3 |
* Report: Trait |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2023, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.7.3 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\Report; |
| 13 |
|
| 14 |
/** |
| 15 |
* Report trait. |
| 16 |
* |
| 17 |
* @since 4.7.3 |
| 18 |
*/ |
| 19 |
trait ReportTrait { |
| 20 |
|
| 21 |
/** |
| 22 |
* Returns the delta between two numbers. |
| 23 |
* |
| 24 |
* @since 4.7.3 |
| 25 |
* |
| 26 |
* @param float $current Current number. |
| 27 |
* @param float $previous Previous number. |
| 28 |
* @return float |
| 29 |
*/ |
| 30 |
protected function get_delta( $current, $previous ) { |
| 31 |
if ( $current > 0 && $previous > 0 ) { |
| 32 |
return round( ( $current - $previous ) / $previous * 100 ); |
| 33 |
} |
| 34 |
|
| 35 |
return 0; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Determines a sane date range interval based on the date range type. |
| 40 |
* |
| 41 |
* E.g. if the date range type is '3months', the interval will be 'W' (week). |
| 42 |
* This prevents the chart from displaying too many datapoints. |
| 43 |
* |
| 44 |
* @since 4.7.3 |
| 45 |
* |
| 46 |
* @param \SimplePay\Core\Report\DateRange $range The date range to use for the report. |
| 47 |
* @return string The DatePeriod interval to use for the report. |
| 48 |
*/ |
| 49 |
protected function get_date_range_interval( $range ) { |
| 50 |
$range_diff = $range->start->diff( $range->end ); |
| 51 |
|
| 52 |
switch ( $range->type ) { |
| 53 |
case 'today': |
| 54 |
return 'H'; |
| 55 |
case '3months': |
| 56 |
return 'W'; |
| 57 |
case '12months': |
| 58 |
return 'M'; |
| 59 |
case 'yeartodate': |
| 60 |
if ( $range_diff->m > 1 ) { |
| 61 |
return 'M'; |
| 62 |
} elseif ( 1 === $range_diff->m ) { |
| 63 |
return 'W'; |
| 64 |
} else { |
| 65 |
return 'D'; |
| 66 |
} |
| 67 |
case 'custom': |
| 68 |
if ( $range_diff->days <= 2 ) { |
| 69 |
return 'H'; |
| 70 |
} elseif ( $range_diff->days <= 30 ) { |
| 71 |
return 'D'; |
| 72 |
} elseif ( $range_diff->days <= 90 ) { |
| 73 |
return 'W'; |
| 74 |
} else { |
| 75 |
return 'M'; |
| 76 |
} |
| 77 |
// last7. |
| 78 |
// 4weeks. |
| 79 |
default: |
| 80 |
return 'D'; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Determines the SQL date format to use for the given interval. |
| 86 |
* |
| 87 |
* @since 4.7.3 |
| 88 |
* |
| 89 |
* @param string $interval The DatePeriod interval to use for the report. |
| 90 |
* @return string The SQL date format to use for the report. |
| 91 |
*/ |
| 92 |
protected function get_sql_select_date_format( $interval ) { |
| 93 |
switch ( $interval ) { |
| 94 |
case 'H': |
| 95 |
return '%%Y-%%m-%%d %%H:00:00'; |
| 96 |
case 'D': |
| 97 |
return '%%Y-%%m-%%d'; |
| 98 |
case 'W': |
| 99 |
/** @var mixed $start_of_week */ |
| 100 |
$start_of_week = get_option( 'start_of_week', '0' ); |
| 101 |
$first_day_of_week = absint( is_scalar( $start_of_week ) ? (string) $start_of_week : '0' ); |
| 102 |
|
| 103 |
// Week begins on Monday, ISO 8601. |
| 104 |
if ( 1 === $first_day_of_week ) { |
| 105 |
return '%%x-%%v'; |
| 106 |
} |
| 107 |
|
| 108 |
// Week begins on day other than specified by ISO 8601. |
| 109 |
return '%%X-%%V'; |
| 110 |
case 'M': |
| 111 |
return '%%Y-%%m'; |
| 112 |
default: |
| 113 |
return '%%Y'; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Determines the PHP date format to use for the given interval. |
| 119 |
* |
| 120 |
* @since 4.7.3 |
| 121 |
* |
| 122 |
* @param string $interval The DatePeriod interval to use for the report. |
| 123 |
* @return string The PHP date format to use for the report. |
| 124 |
*/ |
| 125 |
protected function get_php_date_format( $interval ) { |
| 126 |
switch ( $interval ) { |
| 127 |
case 'H': |
| 128 |
return 'Y-m-d H:i:s'; |
| 129 |
case 'D': |
| 130 |
return 'Y-m-d'; |
| 131 |
case 'W': |
| 132 |
return 'Y-W'; |
| 133 |
case 'M': |
| 134 |
return 'Y-m'; |
| 135 |
default: |
| 136 |
return 'Y'; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Determines the SQL date SELECT string to use for the given interval. |
| 142 |
* |
| 143 |
* This ensures the query groups the results by the correct date interval. |
| 144 |
* |
| 145 |
* @since 4.7.3 |
| 146 |
* |
| 147 |
* @param string $interval The DatePeriod interval to use for the report. |
| 148 |
* @return string The SQL date SELECT string to use for the report. |
| 149 |
*/ |
| 150 |
protected function get_sql_select_date_as( $interval ) { |
| 151 |
$date_format = $this->get_sql_select_date_format( $interval ); |
| 152 |
$column = 'date_created'; |
| 153 |
|
| 154 |
switch ( $interval ) { |
| 155 |
case 'H': |
| 156 |
case 'D': |
| 157 |
return "DATE_FORMAT($column, '$date_format')"; |
| 158 |
case 'W': |
| 159 |
/** @var mixed $start_of_week */ |
| 160 |
$start_of_week = get_option( 'start_of_week', '0' ); |
| 161 |
$first_day_of_week = absint( is_scalar( $start_of_week ) ? (string) $start_of_week : '0' ); |
| 162 |
|
| 163 |
// Week begins on Monday, ISO 8601. |
| 164 |
if ( 1 === $first_day_of_week ) { |
| 165 |
return "DATE_FORMAT($column, '$date_format')"; |
| 166 |
} |
| 167 |
|
| 168 |
// Week begins on day other than specified by ISO 8601. |
| 169 |
return "CONCAT(YEAR($column), '-', LPAD( FLOOR( ( DAYOFYEAR($column) + ( ( DATE_FORMAT(MAKEDATE(YEAR($column),1), '%%w') - $first_day_of_week + 7 ) %% 7 ) - 1 ) / 7 ) + 1 , 2, '0'))"; |
| 170 |
case 'M': |
| 171 |
return "DATE_FORMAT($column, '$date_format')"; |
| 172 |
default: |
| 173 |
return "YEAR($column)"; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns the timezone offset for the current site. |
| 179 |
* |
| 180 |
* @since 4.7.3 |
| 181 |
* |
| 182 |
* @return string The timezone offset for the current site. |
| 183 |
*/ |
| 184 |
protected function get_timezone_offset() { |
| 185 |
/** @var string $offset */ |
| 186 |
$offset = get_option( 'gmt_offset', '0' ); |
| 187 |
/** @var float $offset */ |
| 188 |
$offset = (float) $offset; |
| 189 |
|
| 190 |
$hours = (int) $offset; |
| 191 |
$minutes = ( $offset - $hours ); |
| 192 |
|
| 193 |
$sign = ( $offset < 0 ) ? '-' : '+'; |
| 194 |
$abs_hour = abs( $hours ); |
| 195 |
$abs_mins = abs( $minutes * 60 ); |
| 196 |
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
| 197 |
|
| 198 |
return $tz_offset; |
| 199 |
} |
| 200 |
} |
| 201 |
|