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

239 lines 6.2 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 mixed
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
98 $args['inbox_msg'] = $this->has_inbox_msg ? FrmEmailSummaryHelper::get_latest_inbox_message() : false;
99 $args['from_date'] = $this->from_date;
100 $args['to_date'] = $this->to_date;
101 $args['top_forms'] = FrmEmailSummaryHelper::get_top_forms( $this->from_date, $this->to_date );
102 $args['top_forms_label'] = $this->get_top_forms_label();
103 $args['dashboard_url'] = site_url() . '/wp-admin/admin.php?page=formidable';
104 $args['stats'] = array(
105 'entries' => array(
106 'label' => __( 'Entries created', 'formidable' ),
107 'count' => $entries_count,
108 'compare' => 0,
109 ),
110 );
111
112 if ( $this->has_out_of_date_plugins ) {
113 $args['out_of_date_plugins'] = FrmEmailSummaryHelper::get_out_of_date_plugins();
114 $args['plugins_url'] = site_url() . '/wp-admin/plugins.php';
115 }
116
117 $this->add_entries_comparison_data( $args['stats'] );
118 $this->add_payments_data( $args['stats'] );
119
120 return $args;
121 }
122
123 /**
124 * Adds entries comparison data.
125 *
126 * @param array $stats Statistics section data.
127 */
128 protected function add_entries_comparison_data( &$stats ) {
129 if ( ! $this->has_comparison ) {
130 return;
131 }
132
133 $prev_entries_count = FrmEmailSummaryHelper::get_entries_count( $this->prev_from_date, $this->prev_to_date );
134 $stats['entries']['compare'] = $this->get_compare_diff( $stats['entries']['count'], $prev_entries_count );
135 }
136
137 /**
138 * Adds payments count and total data.
139 *
140 * @param array $stats Statistics section data.
141 */
142 protected function add_payments_data( &$stats ) {
143 $payment_data = FrmEmailSummaryHelper::get_payments_data( $this->from_date, $this->to_date );
144 $stats['payments_count'] = array(
145 'label' => __( 'Payments collected', 'formidable' ),
146 'count' => $payment_data['count'],
147 'compare' => 0,
148 );
149
150 // Build total for each currency.
151 foreach ( $payment_data['total'] as $currency => $amount ) {
152 $stats[ 'payments_total_' . $currency ] = array(
153 // translators: currency name.
154 'label' => sprintf( __( 'Total %s', 'formidable' ), strtoupper( $currency ) ),
155 'count' => $amount,
156 'display' => $this->get_formatted_price( $amount, $currency ),
157 'compare' => 0,
158 );
159 }
160
161 if ( $this->has_comparison ) {
162 $prev_payment_data = FrmEmailSummaryHelper::get_payments_data( $this->prev_from_date, $this->prev_to_date );
163
164 if ( ! $payment_data['count'] && ! $prev_payment_data['count'] ) {
165 // Maybe this site doesn't collect payment, hide these sections.
166 unset( $stats['payments_count'] );
167 return;
168 }
169
170 $stats['payments_count']['compare'] = $this->get_compare_diff( $payment_data['count'], $prev_payment_data['count'] );
171
172 // Compare total for each currency.
173 foreach ( $payment_data['total'] as $currency => $amount ) {
174 if ( ! isset( $prev_payment_data['total'][ $currency ] ) ) {
175 $stats[ 'payments_total_' . $currency ]['compare'] = 1;
176 continue;
177 }
178
179 $stats[ 'payments_total_' . $currency ]['compare'] = $this->get_compare_diff( $amount, $prev_payment_data['total'][ $currency ] );
180 unset( $prev_payment_data['total'][ $currency ] );
181 }
182
183 // If prev month has more currencies.
184 foreach ( $prev_payment_data['total'] as $currency => $amount ) {
185 $stats[ 'payments_total_' . $currency ] = array(
186 // translators: currency name.
187 'label' => sprintf( __( 'Total %s', 'formidable' ), strtoupper( $currency ) ),
188 'count' => 0,
189 'display' => $this->get_formatted_price( 0, $currency ),
190 'compare' => -1,
191 );
192 }
193 }//end if
194 }
195
196 /**
197 * Gets formatted price.
198 *
199 * @param float $amount Amount.
200 * @param string|array $currency Currency string value or array.
201 * @return string
202 */
203 protected function get_formatted_price( $amount, $currency ) {
204 if ( ! is_array( $currency ) ) {
205 $currency = FrmCurrencyHelper::get_currency( $currency );
206 }
207 FrmTransLiteAppHelper::format_amount_for_currency( $currency, $amount );
208 return $amount;
209 }
210
211 /**
212 * Gets comparison diff between 2 values.
213 *
214 * @param float $current Current value.
215 * @param float $prev Previous value.
216 * @return float
217 */
218 public function get_compare_diff( $current, $prev ) {
219 if ( ! $current && ! $prev ) {
220 // No comparison if both are zero.
221 return 0;
222 }
223
224 if ( ! $prev ) {
225 // Increase 100%;
226 return 1;
227 }
228
229 return ( $current - $prev ) / $prev;
230 }
231
232 /**
233 * Gets label of Top forms section.
234 *
235 * @return string
236 */
237 abstract protected function get_top_forms_label();
238 }
239