| @@ -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(); |
| @@ -40,14 +44,25 @@ | ||
| 40 | 44 | } |
| 41 | 45 | } |
| 42 | 46 | |
| 43 | 47 | /** |
| 44 | - * Remove the cron when the plugin is deactivated. | |
| 48 | + * Remove the payment cron when all gateways are disconnected. | |
| 45 | 49 | * |
| 50 | + * @since 6.32.1 | |
| 51 | + * | |
| 52 | + * @param string $gateway 'stripe', 'square', or 'paypal'. | |
| 53 | + * @param string $mode 'test' or 'live'. | |
| 54 | + * | |
| 46 | 55 | * @return void |
| 47 | 56 | */ |
| 48 | - public static function remove_cron() { | |
| 49 | - 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 | + } | |
| 50 | 65 | } |
| 51 | 66 | |
| 52 | 67 | /** |
| 53 | 68 | * Process overdue subscriptions. |
| @@ -54,21 +69,27 @@ | ||
| 54 | 69 | * |
| 55 | 70 | * @return void |
| 56 | 71 | */ |
| 57 | 72 | public static function run_payment_cron() { |
| 58 | - $frm_sub = new FrmTransLiteSubscription(); | |
| 59 | - $frm_payment = new FrmTransLitePayment(); | |
| 73 | + $frm_sub = new FrmTransLiteSubscription(); | |
| 74 | + $frm_payment = new FrmTransLitePayment(); | |
| 75 | + $overdue_subscriptions = $frm_sub->get_overdue_subscriptions(); | |
| 60 | 76 | |
| 61 | - $overdue_subscriptions = $frm_sub->get_overdue_subscriptions(); | |
| 62 | - FrmTransLiteLog::log_message( 'Stripe Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.', false ); | |
| 77 | + if ( ! $overdue_subscriptions && ! $frm_sub->get_active_subscriptions() ) { | |
| 78 | + return; | |
| 79 | + } | |
| 63 | 80 | |
| 81 | + FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.', false ); | |
| 82 | + | |
| 64 | 83 | foreach ( $overdue_subscriptions as $sub ) { |
| 65 | 84 | $last_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' ); |
| 85 | + | |
| 66 | 86 | if ( ! $last_payment ) { |
| 67 | 87 | continue; |
| 68 | 88 | } |
| 69 | 89 | |
| 70 | 90 | $log_message = 'Subscription #' . $sub->id . ': '; |
| 91 | + | |
| 71 | 92 | if ( $sub->status === 'future_cancel' ) { |
| 72 | 93 | FrmTransLiteSubscriptionsController::change_subscription_status( |
| 73 | 94 | array( |
| 74 | 95 | 'status' => 'canceled', |
| @@ -80,9 +101,9 @@ | ||
| 80 | 101 | $log_message .= 'Failed triggers run on canceled subscription. '; |
| 81 | 102 | } else { |
| 82 | 103 | // Get the most recent payment after the gateway has a chance to create one. |
| 83 | 104 | $check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' ); |
| 84 | - $new_payment = $check_payment->id != $last_payment->id; | |
| 105 | + $new_payment = (int) $check_payment->id !== (int) $last_payment->id; | |
| 85 | 106 | $last_payment = $check_payment; |
| 86 | 107 | $status = 'no'; |
| 87 | 108 | |
| 88 | 109 | if ( ! $last_payment ) { |
| @@ -100,14 +121,15 @@ | ||
| 100 | 121 | $last_payment = false; |
| 101 | 122 | } |
| 102 | 123 | |
| 103 | 124 | $log_message .= $status . ' triggers run '; |
| 125 | + | |
| 104 | 126 | if ( $last_payment ) { |
| 105 | 127 | $log_message .= 'on payment #' . $last_payment->id; |
| 106 | 128 | } |
| 107 | 129 | }//end if |
| 108 | 130 | |
| 109 | - FrmTransLiteLog::log_message( 'Stripe Cron Message', $log_message ); | |
| 131 | + FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', $log_message ); | |
| 110 | 132 | |
| 111 | 133 | self::maybe_trigger_changes( |
| 112 | 134 | array( |
| 113 | 135 | 'status' => $status, |
| @@ -126,8 +148,9 @@ | ||
| 126 | 148 | * @return void |
| 127 | 149 | */ |
| 128 | 150 | private static function update_sub_for_new_payment( $sub, $last_payment ) { |
| 129 | 151 | $frm_sub = new FrmTransLiteSubscription(); |
| 152 | + | |
| 130 | 153 | if ( $last_payment->status === 'complete' ) { |
| 131 | 154 | $frm_sub->update( |
| 132 | 155 | $sub->id, |
| 133 | 156 | array( |
| @@ -144,8 +167,9 @@ | ||
| 144 | 167 | * Add to the fail count. |
| 145 | 168 | * If the subscription has failed > 3 times, set it to canceled. |
| 146 | 169 | * |
| 147 | 170 | * @param object $sub |
| 171 | + * | |
| 148 | 172 | * @return void |
| 149 | 173 | */ |
| 150 | 174 | private static function add_one_fail( $sub ) { |
| 151 | 175 | $frm_sub = new FrmTransLiteSubscription(); |
| @@ -164,8 +188,9 @@ | ||
| 164 | 188 | } |
| 165 | 189 | |
| 166 | 190 | /** |
| 167 | 191 | * @param array $atts |
| 192 | + * | |
| 168 | 193 | * @return void |
| 169 | 194 | */ |
| 170 | 195 | private static function maybe_trigger_changes( $atts ) { |
| 171 | 196 | if ( $atts['payment'] ) { |
| @@ -179,20 +204,54 @@ | ||
| 179 | 204 | * |
| 180 | 205 | * @since 6.22 |
| 181 | 206 | * |
| 182 | 207 | * @param array $args |
| 208 | + * | |
| 183 | 209 | * @return void |
| 184 | 210 | */ |
| 185 | 211 | public static function add_repeat_cadence_value( $args ) { |
| 186 | 212 | $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 ' />'; | |
| 213 | + | |
| 214 | + if ( empty( $action->post_content['repeat_cadence'] ) ) { | |
| 215 | + return; | |
| 196 | 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' ); | |
| 197 | 256 | } |
| 198 | 257 | } |