| 1 |
<?php |
| 2 |
/** |
| 3 |
* Stats email class |
| 4 |
* |
| 5 |
* @since 6.7 |
| 6 |
* |
| 7 |
* @package Formidable |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
die( 'You are not allowed to call this page directly.' ); |
| 12 |
} |
| 13 |
|
| 14 |
abstract class FrmEmailStats extends FrmEmailSummary { |
| 15 |
|
| 16 |
/** |
| 17 |
* Should show inbox notice section? |
| 18 |
* |
| 19 |
* @var bool |
| 20 |
*/ |
| 21 |
protected $has_inbox_msg = false; |
| 22 |
|
| 23 |
/** |
| 24 |
* Should show comparison with the stats data of the previous date range? |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
protected $has_comparison = true; |
| 29 |
|
| 30 |
/** |
| 31 |
* Should show out of date plugin section? |
| 32 |
* |
| 33 |
* @var bool |
| 34 |
*/ |
| 35 |
protected $has_out_of_date_plugins = true; |
| 36 |
|
| 37 |
/** |
| 38 |
* From date. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $from_date; |
| 43 |
|
| 44 |
/** |
| 45 |
* To date. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
protected $to_date; |
| 50 |
|
| 51 |
/** |
| 52 |
* From date of the previous date range. |
| 53 |
* |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
protected $prev_from_date; |
| 57 |
|
| 58 |
/** |
| 59 |
* To date of the previous date range. |
| 60 |
* |
| 61 |
* @var string |
| 62 |
*/ |
| 63 |
protected $prev_to_date; |
| 64 |
|
| 65 |
/** |
| 66 |
* The number of days to get the stats data. |
| 67 |
* |
| 68 |
* @var int |
| 69 |
*/ |
| 70 |
protected $date_range; |
| 71 |
|
| 72 |
public function __construct() { |
| 73 |
parent::__construct(); |
| 74 |
|
| 75 |
$date_minus = $this->date_range - 1; |
| 76 |
|
| 77 |
$this->to_date = FrmEmailSummaryHelper::get_date_from_today(); |
| 78 |
$this->from_date = gmdate( 'Y-m-d', strtotime( $this->to_date . '-' . $date_minus . ' days' ) ); |
| 79 |
$this->prev_to_date = gmdate( 'Y-m-d', strtotime( $this->from_date . '-1 day' ) ); |
| 80 |
$this->prev_from_date = gmdate( 'Y-m-d', strtotime( $this->prev_to_date . '-' . $date_minus . ' days' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return false|string |
| 85 |
*/ |
| 86 |
protected function get_inner_content() { |
| 87 |
$args = $this->get_content_args(); |
| 88 |
|
| 89 |
ob_start(); |
| 90 |
include $this->get_include_file( 'stats' ); |
| 91 |
return ob_get_clean(); |
| 92 |
} |
| 93 |
|
| 94 |
protected function get_content_args() { |
| 95 |
$args = parent::get_content_args(); |
| 96 |
$entries_count = FrmEmailSummaryHelper::get_entries_count( $this->from_date, $this->to_date ); |
| 97 |
$entries_stat = array( |
| 98 |
'entries' => array( |
| 99 |
'label' => __( 'Entries created', 'formidable' ), |
| 100 |
'count' => $entries_count, |
| 101 |
'compare' => 0, |
| 102 |
), |
| 103 |
); |
| 104 |
|
| 105 |
$args['inbox_msg'] = $this->has_inbox_msg ? FrmEmailSummaryHelper::get_latest_inbox_message() : false; |
| 106 |
$args['from_date'] = $this->from_date; |
| 107 |
$args['to_date'] = $this->to_date; |
| 108 |
$args['top_forms'] = FrmEmailSummaryHelper::get_top_forms( $this->from_date, $this->to_date ); |
| 109 |
$args['top_forms_label'] = $this->get_top_forms_label(); |
| 110 |
$args['dashboard_url'] = site_url() . '/wp-admin/admin.php?page=formidable'; |
| 111 |
$args['stats'] = isset( $args['stats'] ) ? array_merge( $entries_stat, $args['stats'] ) : $entries_stat; |
| 112 |
|
| 113 |
if ( $this->has_out_of_date_plugins ) { |
| 114 |
$args['out_of_date_plugins'] = FrmEmailSummaryHelper::get_out_of_date_plugins(); |
| 115 |
$args['plugins_url'] = site_url() . '/wp-admin/plugins.php'; |
| 116 |
} |
| 117 |
|
| 118 |
$this->add_entries_comparison_data( $args['stats'] ); |
| 119 |
$this->add_payments_data( $args['stats'] ); |
| 120 |
|
| 121 |
return $args; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Adds entries comparison data. |
| 126 |
* |
| 127 |
* @param array $stats Statistics section data. |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
protected function add_entries_comparison_data( &$stats ) { |
| 132 |
if ( ! $this->has_comparison ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$prev_entries_count = FrmEmailSummaryHelper::get_entries_count( $this->prev_from_date, $this->prev_to_date ); |
| 137 |
$stats['entries']['compare'] = $this->get_compare_diff( $stats['entries']['count'], $prev_entries_count ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Adds payments count and total data. |
| 142 |
* |
| 143 |
* @param array $stats Statistics section data. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
protected function add_payments_data( &$stats ) { |
| 148 |
$payment_data = FrmEmailSummaryHelper::get_payments_data( $this->from_date, $this->to_date ); |
| 149 |
$stats['payments_count'] = array( |
| 150 |
'label' => __( 'Payments collected', 'formidable' ), |
| 151 |
'count' => $payment_data['count'], |
| 152 |
'compare' => 0, |
| 153 |
); |
| 154 |
|
| 155 |
// Build total for each currency. |
| 156 |
foreach ( $payment_data['total'] as $currency => $amount ) { |
| 157 |
$stats[ 'payments_total_' . $currency ] = array( |
| 158 |
// translators: currency name. |
| 159 |
'label' => sprintf( __( 'Total %s', 'formidable' ), strtoupper( $currency ) ), |
| 160 |
'count' => $amount, |
| 161 |
'display' => $this->get_formatted_price( $amount, $currency ), |
| 162 |
'compare' => 0, |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
if ( $this->has_comparison ) { |
| 167 |
$prev_payment_data = FrmEmailSummaryHelper::get_payments_data( $this->prev_from_date, $this->prev_to_date ); |
| 168 |
|
| 169 |
if ( ! $payment_data['count'] && ! $prev_payment_data['count'] ) { |
| 170 |
// Maybe this site doesn't collect payment, hide these sections. |
| 171 |
unset( $stats['payments_count'] ); |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
$stats['payments_count']['compare'] = $this->get_compare_diff( $payment_data['count'], $prev_payment_data['count'] ); |
| 176 |
|
| 177 |
// Compare total for each currency. |
| 178 |
foreach ( $payment_data['total'] as $currency => $amount ) { |
| 179 |
if ( ! isset( $prev_payment_data['total'][ $currency ] ) ) { |
| 180 |
$stats[ 'payments_total_' . $currency ]['compare'] = 1; |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
$stats[ 'payments_total_' . $currency ]['compare'] = $this->get_compare_diff( $amount, $prev_payment_data['total'][ $currency ] ); |
| 185 |
unset( $prev_payment_data['total'][ $currency ] ); |
| 186 |
} |
| 187 |
|
| 188 |
// If prev month has more currencies. |
| 189 |
foreach ( $prev_payment_data['total'] as $currency => $amount ) { |
| 190 |
$stats[ 'payments_total_' . $currency ] = array( |
| 191 |
// translators: currency name. |
| 192 |
'label' => sprintf( __( 'Total %s', 'formidable' ), strtoupper( $currency ) ), |
| 193 |
'count' => 0, |
| 194 |
'display' => $this->get_formatted_price( 0, $currency ), |
| 195 |
'compare' => -1, |
| 196 |
); |
| 197 |
} |
| 198 |
}//end if |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Gets formatted price. |
| 203 |
* |
| 204 |
* @param float $amount Amount. |
| 205 |
* @param array|string $currency Currency string value or array. |
| 206 |
* |
| 207 |
* @return string |
| 208 |
*/ |
| 209 |
protected function get_formatted_price( $amount, $currency ) { |
| 210 |
if ( ! is_array( $currency ) ) { |
| 211 |
$currency = FrmCurrencyHelper::get_currency( $currency ); |
| 212 |
} |
| 213 |
FrmTransLiteAppHelper::format_amount_for_currency( $currency, $amount ); |
| 214 |
return $amount; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Gets comparison diff between 2 values. |
| 219 |
* |
| 220 |
* @param float $current Current value. |
| 221 |
* @param float $prev Previous value. |
| 222 |
* |
| 223 |
* @return float |
| 224 |
*/ |
| 225 |
public function get_compare_diff( $current, $prev ) { |
| 226 |
if ( ! $current && ! $prev ) { |
| 227 |
// No comparison if both are zero. |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
if ( ! $prev ) { |
| 232 |
// Increase 100%; |
| 233 |
return 1; |
| 234 |
} |
| 235 |
|
| 236 |
return ( $current - $prev ) / $prev; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Gets label of Top forms section. |
| 241 |
* |
| 242 |
* @return string |
| 243 |
*/ |
| 244 |
abstract protected function get_top_forms_label(); |
| 245 |
|
| 246 |
/** |
| 247 |
* @since 6.11.1 |
| 248 |
* |
| 249 |
* @return int |
| 250 |
*/ |
| 251 |
public function get_date_range() { |
| 252 |
return $this->date_range; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* @since 6.11.1 |
| 257 |
* |
| 258 |
* @return string |
| 259 |
*/ |
| 260 |
public function get_from_date() { |
| 261 |
return $this->from_date; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* @since 6.11.1 |
| 266 |
* |
| 267 |
* @return string |
| 268 |
*/ |
| 269 |
public function get_to_date() { |
| 270 |
return $this->to_date; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* @since 6.11.1 |
| 275 |
* |
| 276 |
* @return string |
| 277 |
*/ |
| 278 |
public function get_prev_from_date() { |
| 279 |
return $this->prev_from_date; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* @since 6.11.1 |
| 284 |
* |
| 285 |
* @return string |
| 286 |
*/ |
| 287 |
public function get_prev_to_date() { |
| 288 |
return $this->prev_to_date; |
| 289 |
} |
| 290 |
} |
| 291 |
|