| @@ -5,8 +5,12 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmTransLiteAppController { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | + * Install or upgrade database structures. | |
| 10 | + * | |
| 11 | + * @param mixed $old_db_version Previous database version, or false on fresh install. | |
| 12 | + * | |
| 9 | 13 | * @return void |
| 10 | 14 | */ |
| 11 | 15 | public static function install( $old_db_version = false ) { |
| 12 | 16 | self::maybe_schedule_cron(); |
| @@ -15,8 +19,22 @@ | ||
| 15 | 19 | $db->upgrade( $old_db_version ); |
| 16 | 20 | } |
| 17 | 21 | |
| 18 | 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 | + /** | |
| 19 | 37 | * Schedule the payment cron if it is not already scheduled. |
| 20 | 38 | * |
| 21 | 39 | * @return void |
| 22 | 40 | */ |
| @@ -26,14 +44,25 @@ | ||
| 26 | 44 | } |
| 27 | 45 | } |
| 28 | 46 | |
| 29 | 47 | /** |
| 30 | - * Remove the cron when the plugin is deactivated. | |
| 48 | + * Remove the payment cron when all gateways are disconnected. | |
| 31 | 49 | * |
| 50 | + * @since 6.32.1 | |
| 51 | + * | |
| 52 | + * @param string $gateway 'stripe', 'square', or 'paypal'. | |
| 53 | + * @param string $mode 'test' or 'live'. | |
| 54 | + * | |
| 32 | 55 | * @return void |
| 33 | 56 | */ |
| 34 | - public static function remove_cron() { | |
| 35 | - wp_clear_scheduled_hook( 'frm_payment_cron' ); | |
| 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 | + } | |
| 36 | 65 | } |
| 37 | 66 | |
| 38 | 67 | /** |
| 39 | 68 | * Process overdue subscriptions. |
| @@ -40,18 +69,27 @@ | ||
| 40 | 69 | * |
| 41 | 70 | * @return void |
| 42 | 71 | */ |
| 43 | 72 | public static function run_payment_cron() { |
| 44 | - $frm_sub = new FrmTransLiteSubscription(); | |
| 45 | - $frm_payment = new FrmTransLitePayment(); | |
| 73 | + $frm_sub = new FrmTransLiteSubscription(); | |
| 74 | + $frm_payment = new FrmTransLitePayment(); | |
| 75 | + $overdue_subscriptions = $frm_sub->get_overdue_subscriptions(); | |
| 46 | 76 | |
| 47 | - $overdue_subscriptions = $frm_sub->get_overdue_subscriptions(); | |
| 48 | - FrmTransLiteLog::log_message( 'Stripe Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.' ); | |
| 77 | + if ( ! $overdue_subscriptions && ! $frm_sub->get_active_subscriptions() ) { | |
| 78 | + return; | |
| 79 | + } | |
| 49 | 80 | |
| 81 | + FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.', false ); | |
| 82 | + | |
| 50 | 83 | foreach ( $overdue_subscriptions as $sub ) { |
| 51 | 84 | $last_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' ); |
| 52 | 85 | |
| 86 | + if ( ! $last_payment ) { | |
| 87 | + continue; | |
| 88 | + } | |
| 89 | + | |
| 53 | 90 | $log_message = 'Subscription #' . $sub->id . ': '; |
| 91 | + | |
| 54 | 92 | if ( $sub->status === 'future_cancel' ) { |
| 55 | 93 | FrmTransLiteSubscriptionsController::change_subscription_status( |
| 56 | 94 | array( |
| 57 | 95 | 'status' => 'canceled', |
| @@ -63,9 +101,9 @@ | ||
| 63 | 101 | $log_message .= 'Failed triggers run on canceled subscription. '; |
| 64 | 102 | } else { |
| 65 | 103 | // Get the most recent payment after the gateway has a chance to create one. |
| 66 | 104 | $check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' ); |
| 67 | - $new_payment = $check_payment->id != $last_payment->id; | |
| 105 | + $new_payment = (int) $check_payment->id !== (int) $last_payment->id; | |
| 68 | 106 | $last_payment = $check_payment; |
| 69 | 107 | $status = 'no'; |
| 70 | 108 | |
| 71 | 109 | if ( ! $last_payment ) { |
| @@ -74,23 +112,24 @@ | ||
| 74 | 112 | } elseif ( $new_payment ) { |
| 75 | 113 | $status = $last_payment->status; |
| 76 | 114 | self::update_sub_for_new_payment( $sub, $last_payment ); |
| 77 | 115 | } elseif ( $last_payment->expire_date < gmdate( 'Y-m-d' ) ) { |
| 78 | - // the payment has expired, and no new payment was made | |
| 116 | + // The payment has expired, and no new payment was made. | |
| 79 | 117 | $status = 'failed'; |
| 80 | 118 | self::add_one_fail( $sub ); |
| 81 | 119 | } else { |
| 82 | - // don't run any triggers | |
| 120 | + // Don't run any triggers. | |
| 83 | 121 | $last_payment = false; |
| 84 | 122 | } |
| 85 | 123 | |
| 86 | 124 | $log_message .= $status . ' triggers run '; |
| 125 | + | |
| 87 | 126 | if ( $last_payment ) { |
| 88 | 127 | $log_message .= 'on payment #' . $last_payment->id; |
| 89 | 128 | } |
| 90 | - } | |
| 129 | + }//end if | |
| 91 | 130 | |
| 92 | - FrmTransLiteLog::log_message( 'Stripe Cron Message', $log_message ); | |
| 131 | + FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', $log_message ); | |
| 93 | 132 | |
| 94 | 133 | self::maybe_trigger_changes( |
| 95 | 134 | array( |
| 96 | 135 | 'status' => $status, |
| @@ -98,9 +137,9 @@ | ||
| 98 | 137 | ) |
| 99 | 138 | ); |
| 100 | 139 | |
| 101 | 140 | unset( $sub ); |
| 102 | - } | |
| 141 | + }//end foreach | |
| 103 | 142 | } |
| 104 | 143 | |
| 105 | 144 | /** |
| 106 | 145 | * @param object $sub |
| @@ -109,13 +148,14 @@ | ||
| 109 | 148 | * @return void |
| 110 | 149 | */ |
| 111 | 150 | private static function update_sub_for_new_payment( $sub, $last_payment ) { |
| 112 | 151 | $frm_sub = new FrmTransLiteSubscription(); |
| 152 | + | |
| 113 | 153 | if ( $last_payment->status === 'complete' ) { |
| 114 | 154 | $frm_sub->update( |
| 115 | 155 | $sub->id, |
| 116 | 156 | array( |
| 117 | - 'fail_count' => 0, | |
| 157 | + 'fail_count' => 0, | |
| 118 | 158 | 'next_bill_date' => $last_payment->expire_date, |
| 119 | 159 | ) |
| 120 | 160 | ); |
| 121 | 161 | } elseif ( $last_payment->status === 'failed' ) { |
| @@ -127,8 +167,9 @@ | ||
| 127 | 167 | * Add to the fail count. |
| 128 | 168 | * If the subscription has failed > 3 times, set it to canceled. |
| 129 | 169 | * |
| 130 | 170 | * @param object $sub |
| 171 | + * | |
| 131 | 172 | * @return void |
| 132 | 173 | */ |
| 133 | 174 | private static function add_one_fail( $sub ) { |
| 134 | 175 | $frm_sub = new FrmTransLiteSubscription(); |
| @@ -147,8 +188,9 @@ | ||
| 147 | 188 | } |
| 148 | 189 | |
| 149 | 190 | /** |
| 150 | 191 | * @param array $atts |
| 192 | + * | |
| 151 | 193 | * @return void |
| 152 | 194 | */ |
| 153 | 195 | private static function maybe_trigger_changes( $atts ) { |
| 154 | 196 | if ( $atts['payment'] ) { |
| @@ -153,6 +195,63 @@ | ||
| 153 | 195 | private static function maybe_trigger_changes( $atts ) { |
| 154 | 196 | if ( $atts['payment'] ) { |
| 155 | 197 | FrmTransLiteActionsController::trigger_payment_status_change( $atts ); |
| 156 | 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' ); | |
| 157 | 256 | } |
| 158 | 257 | } |