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

212 lines 5.2 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 cron when the plugin is deactivated.
49 *
50 * @return void
51 */
52 public static function remove_cron() {
53 wp_clear_scheduled_hook( 'frm_payment_cron' );
54 }
55
56 /**
57 * Process overdue subscriptions.
58 *
59 * @return void
60 */
61 public static function run_payment_cron() {
62 $frm_sub = new FrmTransLiteSubscription();
63 $frm_payment = new FrmTransLitePayment();
64 $overdue_subscriptions = $frm_sub->get_overdue_subscriptions();
65 FrmTransLiteLog::log_message( 'Stripe Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.', false );
66
67 foreach ( $overdue_subscriptions as $sub ) {
68 $last_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
69
70 if ( ! $last_payment ) {
71 continue;
72 }
73
74 $log_message = 'Subscription #' . $sub->id . ': ';
75
76 if ( $sub->status === 'future_cancel' ) {
77 FrmTransLiteSubscriptionsController::change_subscription_status(
78 array(
79 'status' => 'canceled',
80 'sub' => $sub,
81 )
82 );
83
84 $status = 'failed';
85 $log_message .= 'Failed triggers run on canceled subscription. ';
86 } else {
87 // Get the most recent payment after the gateway has a chance to create one.
88 $check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
89 $new_payment = (int) $check_payment->id !== (int) $last_payment->id;
90 $last_payment = $check_payment;
91 $status = 'no';
92
93 if ( ! $last_payment ) {
94 $log_message .= 'No payments found for subscription #' . $sub->id . '. ';
95 self::add_one_fail( $sub );
96 } elseif ( $new_payment ) {
97 $status = $last_payment->status;
98 self::update_sub_for_new_payment( $sub, $last_payment );
99 } elseif ( $last_payment->expire_date < gmdate( 'Y-m-d' ) ) {
100 // The payment has expired, and no new payment was made.
101 $status = 'failed';
102 self::add_one_fail( $sub );
103 } else {
104 // Don't run any triggers.
105 $last_payment = false;
106 }
107
108 $log_message .= $status . ' triggers run ';
109
110 if ( $last_payment ) {
111 $log_message .= 'on payment #' . $last_payment->id;
112 }
113 }//end if
114
115 FrmTransLiteLog::log_message( 'Stripe Cron Message', $log_message );
116
117 self::maybe_trigger_changes(
118 array(
119 'status' => $status,
120 'payment' => $last_payment,
121 )
122 );
123
124 unset( $sub );
125 }//end foreach
126 }
127
128 /**
129 * @param object $sub
130 * @param object $last_payment
131 *
132 * @return void
133 */
134 private static function update_sub_for_new_payment( $sub, $last_payment ) {
135 $frm_sub = new FrmTransLiteSubscription();
136
137 if ( $last_payment->status === 'complete' ) {
138 $frm_sub->update(
139 $sub->id,
140 array(
141 'fail_count' => 0,
142 'next_bill_date' => $last_payment->expire_date,
143 )
144 );
145 } elseif ( $last_payment->status === 'failed' ) {
146 self::add_one_fail( $sub );
147 }
148 }
149
150 /**
151 * Add to the fail count.
152 * If the subscription has failed > 3 times, set it to canceled.
153 *
154 * @param object $sub
155 *
156 * @return void
157 */
158 private static function add_one_fail( $sub ) {
159 $frm_sub = new FrmTransLiteSubscription();
160 $fail_count = $sub->fail_count + 1;
161 $new_values = compact( 'fail_count' );
162 $frm_sub->update( $sub->id, $new_values );
163
164 if ( $fail_count > 3 ) {
165 FrmTransLiteSubscriptionsController::change_subscription_status(
166 array(
167 'status' => 'canceled',
168 'sub' => $sub,
169 )
170 );
171 }
172 }
173
174 /**
175 * @param array $atts
176 *
177 * @return void
178 */
179 private static function maybe_trigger_changes( $atts ) {
180 if ( $atts['payment'] ) {
181 FrmTransLiteActionsController::trigger_payment_status_change( $atts );
182 }
183 }
184
185 /**
186 * This is called when the Payments submodule is active.
187 * It ensures that the hidden repeater cadence input exists even when another add-on is handling the settings.
188 *
189 * @since 6.22
190 *
191 * @param array $args
192 *
193 * @return void
194 */
195 public static function add_repeat_cadence_value( $args ) {
196 $action = $args['form_action'];
197
198 if ( empty( $action->post_content['repeat_cadence'] ) ) {
199 return;
200 }
201
202 $params = array(
203 'type' => 'hidden',
204 'class' => 'frm-repeat-cadence-value',
205 'value' => $action->post_content['repeat_cadence'],
206 );
207 echo '<input ';
208 FrmAppHelper::array_to_html_params( $params, true );
209 echo ' />';
210 }
211 }
212