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

173 lines 4.3 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
67 $log_message = 'Subscription #' . $sub->id . ': ';
68 if ( $sub->status === 'future_cancel' ) {
69 FrmTransLiteSubscriptionsController::change_subscription_status(
70 array(
71 'status' => 'canceled',
72 'sub' => $sub,
73 )
74 );
75
76 $status = 'failed';
77 $log_message .= 'Failed triggers run on canceled subscription. ';
78 } else {
79 // Get the most recent payment after the gateway has a chance to create one.
80 $check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
81 $new_payment = $check_payment->id != $last_payment->id;
82 $last_payment = $check_payment;
83 $status = 'no';
84
85 if ( ! $last_payment ) {
86 $log_message .= 'No payments found for subscription #' . $sub->id . '. ';
87 self::add_one_fail( $sub );
88 } elseif ( $new_payment ) {
89 $status = $last_payment->status;
90 self::update_sub_for_new_payment( $sub, $last_payment );
91 } elseif ( $last_payment->expire_date < gmdate( 'Y-m-d' ) ) {
92 // The payment has expired, and no new payment was made.
93 $status = 'failed';
94 self::add_one_fail( $sub );
95 } else {
96 // Don't run any triggers.
97 $last_payment = false;
98 }
99
100 $log_message .= $status . ' triggers run ';
101 if ( $last_payment ) {
102 $log_message .= 'on payment #' . $last_payment->id;
103 }
104 }//end if
105
106 FrmTransLiteLog::log_message( 'Stripe Cron Message', $log_message );
107
108 self::maybe_trigger_changes(
109 array(
110 'status' => $status,
111 'payment' => $last_payment,
112 )
113 );
114
115 unset( $sub );
116 }//end foreach
117 }
118
119 /**
120 * @param object $sub
121 * @param object $last_payment
122 *
123 * @return void
124 */
125 private static function update_sub_for_new_payment( $sub, $last_payment ) {
126 $frm_sub = new FrmTransLiteSubscription();
127 if ( $last_payment->status === 'complete' ) {
128 $frm_sub->update(
129 $sub->id,
130 array(
131 'fail_count' => 0,
132 'next_bill_date' => $last_payment->expire_date,
133 )
134 );
135 } elseif ( $last_payment->status === 'failed' ) {
136 self::add_one_fail( $sub );
137 }
138 }
139
140 /**
141 * Add to the fail count.
142 * If the subscription has failed > 3 times, set it to canceled.
143 *
144 * @param object $sub
145 * @return void
146 */
147 private static function add_one_fail( $sub ) {
148 $frm_sub = new FrmTransLiteSubscription();
149 $fail_count = $sub->fail_count + 1;
150 $new_values = compact( 'fail_count' );
151 $frm_sub->update( $sub->id, $new_values );
152
153 if ( $fail_count > 3 ) {
154 FrmTransLiteSubscriptionsController::change_subscription_status(
155 array(
156 'status' => 'canceled',
157 'sub' => $sub,
158 )
159 );
160 }
161 }
162
163 /**
164 * @param array $atts
165 * @return void
166 */
167 private static function maybe_trigger_changes( $atts ) {
168 if ( $atts['payment'] ) {
169 FrmTransLiteActionsController::trigger_payment_status_change( $atts );
170 }
171 }
172 }
173