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

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

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