PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.21
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.21
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.21, at classes/models/FrmEmailStats.php

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