PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.33
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.33
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 / stripe / controllers / FrmTransLiteAppController.php

FrmTransLiteAppController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.33, at stripe/controllers/FrmTransLiteAppController.php

258 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmTransLiteAppController {
7
8 /**
9 * Install or upgrade database structures.
10 *
11 * @param mixed $old_db_version Previous database version, or false on fresh install.
12 *
13 * @return void
14 */
15 public static function install( $old_db_version = false ) {
16 self::maybe_schedule_cron();
17
18 $db = new FrmTransLiteDb();
19 $db->upgrade( $old_db_version );
20 }
21
22 /**
23 * This is called on the frm_after_install hook that is called when Lite migrations have run.
24 *
25 * @return void
26 */
27 public static function on_after_install() {
28 if ( ! FrmTransLiteAppHelper::payments_table_exists() ) {
29 return;
30 }
31
32 $db = new FrmTransLiteDb();
33 $db->upgrade();
34 }
35
36 /**
37 * Schedule the payment cron if it is not already scheduled.
38 *
39 * @return void
40 */
41 public static function maybe_schedule_cron() {
42 if ( ! wp_next_scheduled( 'frm_payment_cron' ) ) {
43 wp_schedule_event( time(), 'daily', 'frm_payment_cron' );
44 }
45 }
46
47 /**
48 * Remove the payment cron when all gateways are disconnected.
49 *
50 * @since 6.32.1
51 *
52 * @param string $gateway 'stripe', 'square', or 'paypal'.
53 * @param string $mode 'test' or 'live'.
54 *
55 * @return void
56 */
57 public static function maybe_remove_payment_cron( $gateway, $mode ) {
58 $stripe_connected = FrmStrpLiteConnectHelper::at_least_one_mode_is_setup();
59 $square_connected = FrmSquareLiteConnectHelper::at_least_one_mode_is_setup();
60 $paypal_connected = FrmPayPalLiteConnectHelper::at_least_one_mode_is_setup();
61
62 if ( ! $stripe_connected && ! $square_connected && ! $paypal_connected ) {
63 wp_clear_scheduled_hook( 'frm_payment_cron' );
64 }
65 }
66
67 /**
68 * Process overdue subscriptions.
69 *
70 * @return void
71 */
72 public static function run_payment_cron() {
73 $frm_sub = new FrmTransLiteSubscription();
74 $frm_payment = new FrmTransLitePayment();
75 $overdue_subscriptions = $frm_sub->get_overdue_subscriptions();
76
77 if ( ! $overdue_subscriptions && ! $frm_sub->get_active_subscriptions() ) {
78 return;
79 }
80
81 FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.', false );
82
83 foreach ( $overdue_subscriptions as $sub ) {
84 $last_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
85
86 if ( ! $last_payment ) {
87 continue;
88 }
89
90 $log_message = 'Subscription #' . $sub->id . ': ';
91
92 if ( $sub->status === 'future_cancel' ) {
93 FrmTransLiteSubscriptionsController::change_subscription_status(
94 array(
95 'status' => 'canceled',
96 'sub' => $sub,
97 )
98 );
99
100 $status = 'failed';
101 $log_message .= 'Failed triggers run on canceled subscription. ';
102 } else {
103 // Get the most recent payment after the gateway has a chance to create one.
104 $check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
105 $new_payment = (int) $check_payment->id !== (int) $last_payment->id;
106 $last_payment = $check_payment;
107 $status = 'no';
108
109 if ( ! $last_payment ) {
110 $log_message .= 'No payments found for subscription #' . $sub->id . '. ';
111 self::add_one_fail( $sub );
112 } elseif ( $new_payment ) {
113 $status = $last_payment->status;
114 self::update_sub_for_new_payment( $sub, $last_payment );
115 } elseif ( $last_payment->expire_date < gmdate( 'Y-m-d' ) ) {
116 // The payment has expired, and no new payment was made.
117 $status = 'failed';
118 self::add_one_fail( $sub );
119 } else {
120 // Don't run any triggers.
121 $last_payment = false;
122 }
123
124 $log_message .= $status . ' triggers run ';
125
126 if ( $last_payment ) {
127 $log_message .= 'on payment #' . $last_payment->id;
128 }
129 }//end if
130
131 FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', $log_message );
132
133 self::maybe_trigger_changes(
134 array(
135 'status' => $status,
136 'payment' => $last_payment,
137 )
138 );
139
140 unset( $sub );
141 }//end foreach
142 }
143
144 /**
145 * @param object $sub
146 * @param object $last_payment
147 *
148 * @return void
149 */
150 private static function update_sub_for_new_payment( $sub, $last_payment ) {
151 $frm_sub = new FrmTransLiteSubscription();
152
153 if ( $last_payment->status === 'complete' ) {
154 $frm_sub->update(
155 $sub->id,
156 array(
157 'fail_count' => 0,
158 'next_bill_date' => $last_payment->expire_date,
159 )
160 );
161 } elseif ( $last_payment->status === 'failed' ) {
162 self::add_one_fail( $sub );
163 }
164 }
165
166 /**
167 * Add to the fail count.
168 * If the subscription has failed > 3 times, set it to canceled.
169 *
170 * @param object $sub
171 *
172 * @return void
173 */
174 private static function add_one_fail( $sub ) {
175 $frm_sub = new FrmTransLiteSubscription();
176 $fail_count = $sub->fail_count + 1;
177 $new_values = compact( 'fail_count' );
178 $frm_sub->update( $sub->id, $new_values );
179
180 if ( $fail_count > 3 ) {
181 FrmTransLiteSubscriptionsController::change_subscription_status(
182 array(
183 'status' => 'canceled',
184 'sub' => $sub,
185 )
186 );
187 }
188 }
189
190 /**
191 * @param array $atts
192 *
193 * @return void
194 */
195 private static function maybe_trigger_changes( $atts ) {
196 if ( $atts['payment'] ) {
197 FrmTransLiteActionsController::trigger_payment_status_change( $atts );
198 }
199 }
200
201 /**
202 * This is called when the Payments submodule is active.
203 * It ensures that the hidden repeater cadence input exists even when another add-on is handling the settings.
204 *
205 * @since 6.22
206 *
207 * @param array $args
208 *
209 * @return void
210 */
211 public static function add_repeat_cadence_value( $args ) {
212 $action = $args['form_action'];
213
214 if ( empty( $action->post_content['repeat_cadence'] ) ) {
215 return;
216 }
217
218 $params = array(
219 'type' => 'hidden',
220 'class' => 'frm-repeat-cadence-value',
221 'value' => $action->post_content['repeat_cadence'],
222 );
223 echo '<input ';
224 FrmAppHelper::array_to_html_params( $params, true );
225 echo ' />';
226 }
227
228 /**
229 * Gateway fields are included for add-on compatibility but we do not want it to be visible.
230 * They do however need to be visible when the payments submodule is active.
231 *
232 * @since 6.30
233 *
234 * @return void
235 */
236 public static function hide_gateway_fields_in_builder() {
237 wp_add_inline_style(
238 'formidable-admin',
239 '
240 #frm_builder_page li[data-ftype="gateway"] { display: none; }
241 .frm_field_box:has(li[data-ftype="gateway"]:only-child) { display: none; }
242 '
243 );
244 }
245
246 /**
247 * Remove the cron when the plugin is deactivated.
248 *
249 * @deprecated 6.32.1
250 *
251 * @return void
252 */
253 public static function remove_cron() {
254 _deprecated_function( __METHOD__, '6.32.1' );
255 wp_clear_scheduled_hook( 'frm_payment_cron' );
256 }
257 }
258