PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmEmailStats.php

FrmEmailStats.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26, at classes/models/FrmEmailStats.php

292 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
97 $entries_count = FrmEmailSummaryHelper::get_entries_count( $this->from_date, $this->to_date );
98 $entries_stat = array(
99 'entries' => array(
100 'label' => __( 'Entries created', 'formidable' ),
101 'count' => $entries_count,
102 'compare' => 0,
103 ),
104 );
105
106 $args['inbox_msg'] = $this->has_inbox_msg ? FrmEmailSummaryHelper::get_latest_inbox_message() : false;
107 $args['from_date'] = $this->from_date;
108 $args['to_date'] = $this->to_date;
109 $args['top_forms'] = FrmEmailSummaryHelper::get_top_forms( $this->from_date, $this->to_date );
110 $args['top_forms_label'] = $this->get_top_forms_label();
111 $args['dashboard_url'] = site_url() . '/wp-admin/admin.php?page=formidable';
112 $args['stats'] = isset( $args['stats'] ) ? array_merge( $entries_stat, $args['stats'] ) : $entries_stat;
113
114 if ( $this->has_out_of_date_plugins ) {
115 $args['out_of_date_plugins'] = FrmEmailSummaryHelper::get_out_of_date_plugins();
116 $args['plugins_url'] = site_url() . '/wp-admin/plugins.php';
117 }
118
119 $this->add_entries_comparison_data( $args['stats'] );
120 $this->add_payments_data( $args['stats'] );
121
122 return $args;
123 }
124
125 /**
126 * Adds entries comparison data.
127 *
128 * @param array $stats Statistics section data.
129 *
130 * @return void
131 */
132 protected function add_entries_comparison_data( &$stats ) {
133 if ( ! $this->has_comparison ) {
134 return;
135 }
136
137 $prev_entries_count = FrmEmailSummaryHelper::get_entries_count( $this->prev_from_date, $this->prev_to_date );
138 $stats['entries']['compare'] = $this->get_compare_diff( $stats['entries']['count'], $prev_entries_count );
139 }
140
141 /**
142 * Adds payments count and total data.
143 *
144 * @param array $stats Statistics section data.
145 *
146 * @return void
147 */
148 protected function add_payments_data( &$stats ) {
149 $payment_data = FrmEmailSummaryHelper::get_payments_data( $this->from_date, $this->to_date );
150 $stats['payments_count'] = array(
151 'label' => __( 'Payments collected', 'formidable' ),
152 'count' => $payment_data['count'],
153 'compare' => 0,
154 );
155
156 // Build total for each currency.
157 foreach ( $payment_data['total'] as $currency => $amount ) {
158 $stats[ 'payments_total_' . $currency ] = array(
159 // translators: currency name.
160 'label' => sprintf( __( 'Total %s', 'formidable' ), strtoupper( $currency ) ),
161 'count' => $amount,
162 'display' => $this->get_formatted_price( $amount, $currency ),
163 'compare' => 0,
164 );
165 }
166
167 if ( $this->has_comparison ) {
168 $prev_payment_data = FrmEmailSummaryHelper::get_payments_data( $this->prev_from_date, $this->prev_to_date );
169
170 if ( ! $payment_data['count'] && ! $prev_payment_data['count'] ) {
171 // Maybe this site doesn't collect payment, hide these sections.
172 unset( $stats['payments_count'] );
173 return;
174 }
175
176 $stats['payments_count']['compare'] = $this->get_compare_diff( $payment_data['count'], $prev_payment_data['count'] );
177
178 // Compare total for each currency.
179 foreach ( $payment_data['total'] as $currency => $amount ) {
180 if ( ! isset( $prev_payment_data['total'][ $currency ] ) ) {
181 $stats[ 'payments_total_' . $currency ]['compare'] = 1;
182 continue;
183 }
184
185 $stats[ 'payments_total_' . $currency ]['compare'] = $this->get_compare_diff( $amount, $prev_payment_data['total'][ $currency ] );
186 unset( $prev_payment_data['total'][ $currency ] );
187 }
188
189 // If prev month has more currencies.
190 foreach ( $prev_payment_data['total'] as $currency => $amount ) {
191 $stats[ 'payments_total_' . $currency ] = array(
192 // translators: currency name.
193 'label' => sprintf( __( 'Total %s', 'formidable' ), strtoupper( $currency ) ),
194 'count' => 0,
195 'display' => $this->get_formatted_price( 0, $currency ),
196 'compare' => -1,
197 );
198 }
199 }//end if
200 }
201
202 /**
203 * Gets formatted price.
204 *
205 * @param float $amount Amount.
206 * @param array|string $currency Currency string value or array.
207 *
208 * @return string
209 */
210 protected function get_formatted_price( $amount, $currency ) {
211 if ( ! is_array( $currency ) ) {
212 $currency = FrmCurrencyHelper::get_currency( $currency );
213 }
214 FrmTransLiteAppHelper::format_amount_for_currency( $currency, $amount );
215 return $amount;
216 }
217
218 /**
219 * Gets comparison diff between 2 values.
220 *
221 * @param float $current Current value.
222 * @param float $prev Previous value.
223 *
224 * @return float
225 */
226 public function get_compare_diff( $current, $prev ) {
227 if ( ! $current && ! $prev ) {
228 // No comparison if both are zero.
229 return 0;
230 }
231
232 if ( ! $prev ) {
233 // Increase 100%;
234 return 1;
235 }
236
237 return ( $current - $prev ) / $prev;
238 }
239
240 /**
241 * Gets label of Top forms section.
242 *
243 * @return string
244 */
245 abstract protected function get_top_forms_label();
246
247 /**
248 * @since 6.11.1
249 *
250 * @return int
251 */
252 public function get_date_range() {
253 return $this->date_range;
254 }
255
256 /**
257 * @since 6.11.1
258 *
259 * @return string
260 */
261 public function get_from_date() {
262 return $this->from_date;
263 }
264
265 /**
266 * @since 6.11.1
267 *
268 * @return string
269 */
270 public function get_to_date() {
271 return $this->to_date;
272 }
273
274 /**
275 * @since 6.11.1
276 *
277 * @return string
278 */
279 public function get_prev_from_date() {
280 return $this->prev_from_date;
281 }
282
283 /**
284 * @since 6.11.1
285 *
286 * @return string
287 */
288 public function get_prev_to_date() {
289 return $this->prev_to_date;
290 }
291 }
292