← All changes
|
square/controllers/FrmSquareLiteEventsController.php
+20
-24
6.33
→
6.22
View file →
| @@ -9,11 +9,8 @@ | ||
| 9 | 9 | * @var string |
| 10 | 10 | */ |
| 11 | 11 | public static $events_to_skip_option_name = 'frm_square_events_to_skip'; |
| 12 | 12 | |
| 13 | - /** | |
| 14 | - * @var object|null | |
| 15 | - */ | |
| 16 | 13 | private $event; |
| 17 | 14 | |
| 18 | 15 | /** |
| 19 | 16 | * Tell Square Connect API that the request came through by flushing early before processing. |
| @@ -54,9 +51,8 @@ | ||
| 54 | 51 | |
| 55 | 52 | if ( $unprocessed_event_ids ) { |
| 56 | 53 | $this->process_event_ids( $unprocessed_event_ids ); |
| 57 | 54 | } |
| 58 | - | |
| 59 | 55 | wp_send_json_success(); |
| 60 | 56 | } |
| 61 | 57 | |
| 62 | 58 | /** |
| @@ -62,9 +58,8 @@ | ||
| 62 | 58 | /** |
| 63 | 59 | * @since 6.22 |
| 64 | 60 | * |
| 65 | 61 | * @param array<string> $event_ids |
| 66 | - * | |
| 67 | 62 | * @return void |
| 68 | 63 | */ |
| 69 | 64 | private function process_event_ids( $event_ids ) { |
| 70 | 65 | foreach ( $event_ids as $event_id ) { |
| @@ -75,16 +70,15 @@ | ||
| 75 | 70 | set_transient( 'frm_square_last_process_' . $event_id, time(), 60 ); |
| 76 | 71 | |
| 77 | 72 | $this->event = FrmSquareLiteConnectHelper::get_event( $event_id ); |
| 78 | 73 | |
| 79 | - if ( ! is_object( $this->event ) ) { | |
| 74 | + if ( is_object( $this->event ) ) { | |
| 75 | + $this->handle_event(); | |
| 76 | + $this->track_handled_event( $event_id ); | |
| 77 | + FrmSquareLiteConnectHelper::process_event( $event_id ); | |
| 78 | + } else { | |
| 80 | 79 | $this->count_failed_event( $event_id ); |
| 81 | - continue; | |
| 82 | 80 | } |
| 83 | - | |
| 84 | - $this->handle_event(); | |
| 85 | - $this->track_handled_event( $event_id ); | |
| 86 | - FrmSquareLiteConnectHelper::process_event( $event_id ); | |
| 87 | 81 | } |
| 88 | 82 | } |
| 89 | 83 | |
| 90 | 84 | /** |
| @@ -90,9 +84,8 @@ | ||
| 90 | 84 | /** |
| 91 | 85 | * @since 6.22 |
| 92 | 86 | * |
| 93 | 87 | * @param string $event_id |
| 94 | - * | |
| 95 | 88 | * @return bool True if the event should be skipped. |
| 96 | 89 | */ |
| 97 | 90 | private function should_skip_event( $event_id ) { |
| 98 | 91 | if ( $this->last_attempt_to_process_event_is_too_recent( $event_id ) ) { |
| @@ -99,15 +92,17 @@ | ||
| 99 | 92 | return true; |
| 100 | 93 | } |
| 101 | 94 | |
| 102 | 95 | $option = get_option( self::$events_to_skip_option_name ); |
| 96 | + if ( ! is_array( $option ) ) { | |
| 97 | + return false; | |
| 98 | + } | |
| 103 | 99 | |
| 104 | - return is_array( $option ) && in_array( $event_id, $option, true ); | |
| 100 | + return in_array( $event_id, $option, true ); | |
| 105 | 101 | } |
| 106 | 102 | |
| 107 | 103 | /** |
| 108 | 104 | * @param string $event_id |
| 109 | - * | |
| 110 | 105 | * @return bool |
| 111 | 106 | */ |
| 112 | 107 | private function last_attempt_to_process_event_is_too_recent( $event_id ) { |
| 113 | 108 | $last_process_attempt = get_transient( 'frm_square_last_process_' . $event_id ); |
| @@ -117,17 +112,20 @@ | ||
| 117 | 112 | /** |
| 118 | 113 | * @since 6.22 |
| 119 | 114 | * |
| 120 | 115 | * @param string $event_id |
| 121 | - * | |
| 122 | 116 | * @return void |
| 123 | 117 | */ |
| 124 | 118 | private function count_failed_event( $event_id ) { |
| 125 | - $transient_name = 'frm_square_failed_event_' . $event_id; | |
| 126 | - $transient = get_transient( $transient_name ); | |
| 127 | - $failed_count = is_int( $transient ) ? $transient + 1 : 1; | |
| 119 | + $transient_name = 'frm_square_failed_event_' . $event_id; | |
| 120 | + $transient = get_transient( $transient_name ); | |
| 121 | + if ( is_int( $transient ) ) { | |
| 122 | + $failed_count = $transient + 1; | |
| 123 | + } else { | |
| 124 | + $failed_count = 1; | |
| 125 | + } | |
| 126 | + | |
| 128 | 127 | $maximum_retries = 3; |
| 129 | - | |
| 130 | 128 | if ( $failed_count >= $maximum_retries ) { |
| 131 | 129 | $this->track_handled_event( $event_id ); |
| 132 | 130 | } else { |
| 133 | 131 | set_transient( $transient_name, $failed_count, 4 * DAY_IN_SECONDS ); |
| @@ -140,9 +138,8 @@ | ||
| 140 | 138 | * |
| 141 | 139 | * @since 6.22 |
| 142 | 140 | * |
| 143 | 141 | * @param string $event_id |
| 144 | - * | |
| 145 | 142 | * @return void |
| 146 | 143 | */ |
| 147 | 144 | private function track_handled_event( $event_id ) { |
| 148 | 145 | $option = get_option( self::$events_to_skip_option_name ); |
| @@ -194,9 +191,8 @@ | ||
| 194 | 191 | 'payment' => $payment, |
| 195 | 192 | ) |
| 196 | 193 | ); |
| 197 | 194 | } |
| 198 | - | |
| 199 | 195 | return; |
| 200 | 196 | } |
| 201 | 197 | break; |
| 202 | 198 | case 'subscription.updated': |
| @@ -224,13 +220,13 @@ | ||
| 224 | 220 | /** |
| 225 | 221 | * Add a payment row for the payments table. |
| 226 | 222 | * |
| 227 | 223 | * @param string $subscription_id The Square ID for the current subscription. |
| 228 | - * | |
| 229 | 224 | * @return void |
| 230 | 225 | */ |
| 231 | 226 | private function add_subscription_payment( $subscription_id ) { |
| 232 | - $payment_id = $this->event->data->id; | |
| 227 | + $payment_id = $this->event->data->id; | |
| 228 | + | |
| 233 | 229 | $frm_payment = new FrmTransLitePayment(); |
| 234 | 230 | $payment = $frm_payment->get_one_by( $payment_id, 'receipt_id' ); |
| 235 | 231 | |
| 236 | 232 | if ( $payment ) { |
| @@ -239,9 +235,8 @@ | ||
| 239 | 235 | } |
| 240 | 236 | |
| 241 | 237 | $frm_sub = new FrmTransLiteSubscription(); |
| 242 | 238 | $sub = $frm_sub->get_one_by( $subscription_id, 'sub_id' ); |
| 243 | - | |
| 244 | 239 | if ( ! $sub ) { |
| 245 | 240 | return; |
| 246 | 241 | } |
| 247 | 242 | |
| @@ -271,8 +266,9 @@ | ||
| 271 | 266 | array( 'next_bill_date' => gmdate( 'Y-m-d', strtotime( $expire_date ) ) ) |
| 272 | 267 | ); |
| 273 | 268 | } |
| 274 | 269 | } |
| 270 | + | |
| 275 | 271 | |
| 276 | 272 | $frm_payment = new FrmTransLitePayment(); |
| 277 | 273 | $frm_payment->create( |
| 278 | 274 | array( |