Passed
Push — master ( eb862d...1f939a )
by Brian
06:09 queued 17s
created
includes/payments/class-getpaid-payment-form-submission-refresh-prices.php 1 patch
Indentation   +261 added lines, -261 removed lines patch added patch discarded remove patch
@@ -12,288 +12,288 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Payment_Form_Submission_Refresh_Prices {
14 14
 
15
-	/**
16
-	 * Contains the response for refreshing prices.
17
-	 * @var array
18
-	 */
19
-	public $response = array();
15
+    /**
16
+     * Contains the response for refreshing prices.
17
+     * @var array
18
+     */
19
+    public $response = array();
20 20
 
21 21
     /**
22
-	 * Class constructor
23
-	 *
24
-	 * @param GetPaid_Payment_Form_Submission $submission
25
-	 */
26
-	public function __construct( $submission ) {
27
-
28
-		$this->response = array(
29
-			'submission_id'                    => $submission->id,
22
+     * Class constructor
23
+     *
24
+     * @param GetPaid_Payment_Form_Submission $submission
25
+     */
26
+    public function __construct( $submission ) {
27
+
28
+        $this->response = array(
29
+            'submission_id'                    => $submission->id,
30 30
             'has_recurring'                    => $submission->has_recurring,
31
-			'has_subscription_group'           => $submission->has_subscription_group(),
32
-			'has_multiple_subscription_groups' => $submission->has_multiple_subscription_groups(),
31
+            'has_subscription_group'           => $submission->has_subscription_group(),
32
+            'has_multiple_subscription_groups' => $submission->has_multiple_subscription_groups(),
33 33
             'is_free'                          => ! $submission->should_collect_payment_details(),
34
-		);
35
-
36
-		$this->add_totals( $submission );
37
-		$this->add_texts( $submission );
38
-		$this->add_items( $submission );
39
-		$this->add_fees( $submission );
40
-		$this->add_discounts( $submission );
41
-		$this->add_taxes( $submission );
42
-		$this->add_gateways( $submission );
43
-		$this->add_data( $submission );
44
-
45
-	}
46
-
47
-	/**
48
-	 * Adds totals to a response for submission refresh prices.
49
-	 *
50
-	 * @param GetPaid_Payment_Form_Submission $submission
51
-	 */
52
-	public function add_totals( $submission ) {
53
-
54
-		$this->response = array_merge(
55
-			$this->response,
56
-			array(
57
-
58
-				'totals'        => array(
59
-					'subtotal'  => $submission->format_amount( $submission->get_subtotal() ),
60
-					'discount'  => $submission->format_amount( $submission->get_discount() ),
61
-					'fees'      => $submission->format_amount( $submission->get_fee() ),
62
-					'tax'       => $submission->format_amount( $submission->get_tax() ),
63
-					'total'     => $submission->format_amount( $submission->get_total() ),
64
-					'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ),
65
-				),
66
-
67
-				'recurring'     => array(
68
-					'subtotal'  => $submission->format_amount( $submission->get_recurring_subtotal() ),
69
-					'discount'  => $submission->format_amount( $submission->get_recurring_discount() ),
70
-					'fees'      => $submission->format_amount( $submission->get_recurring_fee() ),
71
-					'tax'       => $submission->format_amount( $submission->get_recurring_tax() ),
72
-					'total'     => $submission->format_amount( $submission->get_recurring_total() ),
73
-				),
74
-
75
-				'initial_amt'   => wpinv_round_amount( $submission->get_total(), null, true ),
76
-				'currency'      => $submission->get_currency(),
77
-
78
-			)
79
-		);
80
-
81
-	}
82
-
83
-	/**
84
-	 * Adds texts to a response for submission refresh prices.
85
-	 *
86
-	 * @param GetPaid_Payment_Form_Submission $submission
87
-	 */
88
-	public function add_texts( $submission ) {
89
-
90
-		$payable = $submission->format_amount( $submission->get_total() );
91
-		$groups  = getpaid_get_subscription_groups( $submission );
92
-
93
-		if ( $submission->has_recurring && 2 > count( $groups ) ) {
94
-
95
-			$recurring = new WPInv_Item( $submission->has_recurring );
96
-			$period    = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' );
97
-			$main_item = reset( $groups );
98
-
99
-			if ( $submission->get_total() == $submission->get_recurring_total() ) {
100
-				$payable = "$payable / $period";
101
-			} else if ( $main_item ) {
102
-
103
-				$main_item = reset( $main_item );
104
-
105
-				// Calculate the next renewal date.
106
-				$_period      = $main_item->get_recurring_period( true );
107
-				$_interval    = $main_item->get_recurring_interval();
108
-
109
-				// If the subscription item has a trial period...
110
-				if ( $main_item->has_free_trial() ) {
111
-					$_period   = $main_item->get_trial_period( true );
112
-					$_interval = $main_item->get_trial_interval();
113
-				}
114
-
115
-				$payable = sprintf(
116
-					__( '%1$s (renews at %2$s / %3$s)', 'invoicing' ),
117
-					$submission->format_amount( $submission->get_total() ),
118
-					$submission->format_amount( $submission->get_recurring_total() ),
119
-					$period
120
-				);
121
-
122
-				$payable .= sprintf(
123
-					'<small class="text-muted form-text">%s</small>',
124
-					sprintf(
125
-						__( 'First renewal on %s', 'invoicing' ),
126
-						getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) )
127
-					)
128
-				);
129
-
130
-			} else {
131
-				$payable = sprintf(
132
-					__( '%1$s (renews at %2$s / %3$s)', 'invoicing' ),
133
-					$submission->format_amount( $submission->get_total() ),
134
-					$submission->format_amount( $submission->get_recurring_total() ),
135
-					$period
136
-				);
137
-			}
138
-
139
-		}
140
-
141
-		$texts = array(
142
-			'.getpaid-checkout-total-payable' => $payable,
143
-		);
144
-
145
-		foreach ( $submission->get_items() as $item ) {
146
-			$item_id                                               = $item->get_id();
147
-			$initial_price                                         = $submission->format_amount( $item->get_sub_total() - $item->item_discount );
148
-			$recurring_price                                       = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount );
149
-			$texts[".item-$item_id .getpaid-form-item-price-desc"] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price );
150
-		}
151
-
152
-		$this->response = array_merge( $this->response, array( 'texts' => $texts ) );
153
-
154
-	}
155
-
156
-	/**
157
-	 * Adds items to a response for submission refresh prices.
158
-	 *
159
-	 * @param GetPaid_Payment_Form_Submission $submission
160
-	 */
161
-	public function add_items( $submission ) {
162
-
163
-		// Add items.
164
-		$items = array();
34
+        );
35
+
36
+        $this->add_totals( $submission );
37
+        $this->add_texts( $submission );
38
+        $this->add_items( $submission );
39
+        $this->add_fees( $submission );
40
+        $this->add_discounts( $submission );
41
+        $this->add_taxes( $submission );
42
+        $this->add_gateways( $submission );
43
+        $this->add_data( $submission );
44
+
45
+    }
46
+
47
+    /**
48
+     * Adds totals to a response for submission refresh prices.
49
+     *
50
+     * @param GetPaid_Payment_Form_Submission $submission
51
+     */
52
+    public function add_totals( $submission ) {
53
+
54
+        $this->response = array_merge(
55
+            $this->response,
56
+            array(
57
+
58
+                'totals'        => array(
59
+                    'subtotal'  => $submission->format_amount( $submission->get_subtotal() ),
60
+                    'discount'  => $submission->format_amount( $submission->get_discount() ),
61
+                    'fees'      => $submission->format_amount( $submission->get_fee() ),
62
+                    'tax'       => $submission->format_amount( $submission->get_tax() ),
63
+                    'total'     => $submission->format_amount( $submission->get_total() ),
64
+                    'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ),
65
+                ),
66
+
67
+                'recurring'     => array(
68
+                    'subtotal'  => $submission->format_amount( $submission->get_recurring_subtotal() ),
69
+                    'discount'  => $submission->format_amount( $submission->get_recurring_discount() ),
70
+                    'fees'      => $submission->format_amount( $submission->get_recurring_fee() ),
71
+                    'tax'       => $submission->format_amount( $submission->get_recurring_tax() ),
72
+                    'total'     => $submission->format_amount( $submission->get_recurring_total() ),
73
+                ),
74
+
75
+                'initial_amt'   => wpinv_round_amount( $submission->get_total(), null, true ),
76
+                'currency'      => $submission->get_currency(),
77
+
78
+            )
79
+        );
80
+
81
+    }
82
+
83
+    /**
84
+     * Adds texts to a response for submission refresh prices.
85
+     *
86
+     * @param GetPaid_Payment_Form_Submission $submission
87
+     */
88
+    public function add_texts( $submission ) {
89
+
90
+        $payable = $submission->format_amount( $submission->get_total() );
91
+        $groups  = getpaid_get_subscription_groups( $submission );
92
+
93
+        if ( $submission->has_recurring && 2 > count( $groups ) ) {
94
+
95
+            $recurring = new WPInv_Item( $submission->has_recurring );
96
+            $period    = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' );
97
+            $main_item = reset( $groups );
98
+
99
+            if ( $submission->get_total() == $submission->get_recurring_total() ) {
100
+                $payable = "$payable / $period";
101
+            } else if ( $main_item ) {
102
+
103
+                $main_item = reset( $main_item );
104
+
105
+                // Calculate the next renewal date.
106
+                $_period      = $main_item->get_recurring_period( true );
107
+                $_interval    = $main_item->get_recurring_interval();
108
+
109
+                // If the subscription item has a trial period...
110
+                if ( $main_item->has_free_trial() ) {
111
+                    $_period   = $main_item->get_trial_period( true );
112
+                    $_interval = $main_item->get_trial_interval();
113
+                }
114
+
115
+                $payable = sprintf(
116
+                    __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ),
117
+                    $submission->format_amount( $submission->get_total() ),
118
+                    $submission->format_amount( $submission->get_recurring_total() ),
119
+                    $period
120
+                );
121
+
122
+                $payable .= sprintf(
123
+                    '<small class="text-muted form-text">%s</small>',
124
+                    sprintf(
125
+                        __( 'First renewal on %s', 'invoicing' ),
126
+                        getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) )
127
+                    )
128
+                );
129
+
130
+            } else {
131
+                $payable = sprintf(
132
+                    __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ),
133
+                    $submission->format_amount( $submission->get_total() ),
134
+                    $submission->format_amount( $submission->get_recurring_total() ),
135
+                    $period
136
+                );
137
+            }
138
+
139
+        }
140
+
141
+        $texts = array(
142
+            '.getpaid-checkout-total-payable' => $payable,
143
+        );
165 144
 
166 145
         foreach ( $submission->get_items() as $item ) {
167
-			$item_id           = $item->get_id();
168
-			$items["$item_id"] = $submission->format_amount( $item->get_sub_total() );
169
-		}
146
+            $item_id                                               = $item->get_id();
147
+            $initial_price                                         = $submission->format_amount( $item->get_sub_total() - $item->item_discount );
148
+            $recurring_price                                       = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount );
149
+            $texts[".item-$item_id .getpaid-form-item-price-desc"] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price );
150
+        }
170 151
 
171
-		$this->response = array_merge(
172
-			$this->response,
173
-			array( 'items' => $items )
174
-		);
152
+        $this->response = array_merge( $this->response, array( 'texts' => $texts ) );
175 153
 
176
-	}
154
+    }
177 155
 
178
-	/**
179
-	 * Adds fees to a response for submission refresh prices.
180
-	 *
181
-	 * @param GetPaid_Payment_Form_Submission $submission
182
-	 */
183
-	public function add_fees( $submission ) {
156
+    /**
157
+     * Adds items to a response for submission refresh prices.
158
+     *
159
+     * @param GetPaid_Payment_Form_Submission $submission
160
+     */
161
+    public function add_items( $submission ) {
184 162
 
185
-		$fees = array();
163
+        // Add items.
164
+        $items = array();
165
+
166
+        foreach ( $submission->get_items() as $item ) {
167
+            $item_id           = $item->get_id();
168
+            $items["$item_id"] = $submission->format_amount( $item->get_sub_total() );
169
+        }
170
+
171
+        $this->response = array_merge(
172
+            $this->response,
173
+            array( 'items' => $items )
174
+        );
175
+
176
+    }
177
+
178
+    /**
179
+     * Adds fees to a response for submission refresh prices.
180
+     *
181
+     * @param GetPaid_Payment_Form_Submission $submission
182
+     */
183
+    public function add_fees( $submission ) {
184
+
185
+        $fees = array();
186 186
 
187 187
         foreach ( $submission->get_fees() as $name => $data ) {
188
-			$fees[$name] = $submission->format_amount( $data['initial_fee'] );
189
-		}
188
+            $fees[$name] = $submission->format_amount( $data['initial_fee'] );
189
+        }
190 190
 
191
-		$this->response = array_merge(
192
-			$this->response,
193
-			array( 'fees' => $fees )
194
-		);
191
+        $this->response = array_merge(
192
+            $this->response,
193
+            array( 'fees' => $fees )
194
+        );
195 195
 
196
-	}
196
+    }
197 197
 
198
-	/**
199
-	 * Adds discounts to a response for submission refresh prices.
200
-	 *
201
-	 * @param GetPaid_Payment_Form_Submission $submission
202
-	 */
203
-	public function add_discounts( $submission ) {
198
+    /**
199
+     * Adds discounts to a response for submission refresh prices.
200
+     *
201
+     * @param GetPaid_Payment_Form_Submission $submission
202
+     */
203
+    public function add_discounts( $submission ) {
204 204
 
205
-		$discounts = array();
205
+        $discounts = array();
206 206
 
207 207
         foreach ( $submission->get_discounts() as $name => $data ) {
208
-			$discounts[$name] = $submission->format_amount( $data['initial_discount'] );
209
-		}
210
-
211
-		$this->response = array_merge(
212
-			$this->response,
213
-			array( 'discounts' => $discounts )
214
-		);
208
+            $discounts[$name] = $submission->format_amount( $data['initial_discount'] );
209
+        }
215 210
 
216
-	}
211
+        $this->response = array_merge(
212
+            $this->response,
213
+            array( 'discounts' => $discounts )
214
+        );
217 215
 
218
-	/**
219
-	 * Adds taxes to a response for submission refresh prices.
220
-	 *
221
-	 * @param GetPaid_Payment_Form_Submission $submission
222
-	 */
223
-	public function add_taxes( $submission ) {
216
+    }
224 217
 
225
-		$taxes  = array();
226
-		$markup = '';
218
+    /**
219
+     * Adds taxes to a response for submission refresh prices.
220
+     *
221
+     * @param GetPaid_Payment_Form_Submission $submission
222
+     */
223
+    public function add_taxes( $submission ) {
224
+
225
+        $taxes  = array();
226
+        $markup = '';
227 227
         foreach ( $submission->get_taxes() as $name => $data ) {
228
-			$name          = sanitize_text_field( $name );
229
-			$amount        = $submission->format_amount( $data['initial_tax'] );
230
-			$taxes[$name]  = $amount;
231
-			$markup       .= "<small class='form-text'>$name : $amount</small>";
232
-		}
233
-
234
-		if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) {
235
-			$this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup;
236
-		}
237
-
238
-		$this->response = array_merge(
239
-			$this->response,
240
-			array( 'taxes' => $taxes )
241
-		);
242
-
243
-	}
244
-
245
-	/**
246
-	 * Adds gateways to a response for submission refresh prices.
247
-	 *
248
-	 * @param GetPaid_Payment_Form_Submission $submission
249
-	 */
250
-	public function add_gateways( $submission ) {
251
-
252
-		$gateways = array_keys( wpinv_get_enabled_payment_gateways() );
253
-
254
-		if ( $this->response['has_recurring'] ) {
255
-
256
-			foreach ( $gateways as $i => $gateway ) {
257
-
258
-				if (
259
-					! getpaid_payment_gateway_supports( $gateway, 'subscription' )
260
-					|| ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) )
261
-					|| ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) {
262
-					unset( $gateways[ $i ] );
263
-				}
264
-
265
-			}
266
-
267
-		}
268
-
269
-		$gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission );
270
-		$this->response = array_merge(
271
-			$this->response,
272
-			array( 'gateways' => $gateways )
273
-		);
274
-
275
-	}
276
-
277
-	/**
278
-	 * Adds data to a response for submission refresh prices.
279
-	 *
280
-	 * @param GetPaid_Payment_Form_Submission $submission
281
-	 */
282
-	public function add_data( $submission ) {
283
-
284
-		$this->response = array_merge(
285
-			$this->response,
286
-			array(
287
-				'js_data' => apply_filters(
288
-					'getpaid_submission_js_data',
289
-					array(
290
-						'is_recurring' => $this->response['has_recurring'],
291
-					),
292
-					$submission
293
-				)
294
-			)
295
-		);
296
-
297
-	}
228
+            $name          = sanitize_text_field( $name );
229
+            $amount        = $submission->format_amount( $data['initial_tax'] );
230
+            $taxes[$name]  = $amount;
231
+            $markup       .= "<small class='form-text'>$name : $amount</small>";
232
+        }
233
+
234
+        if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) {
235
+            $this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup;
236
+        }
237
+
238
+        $this->response = array_merge(
239
+            $this->response,
240
+            array( 'taxes' => $taxes )
241
+        );
242
+
243
+    }
244
+
245
+    /**
246
+     * Adds gateways to a response for submission refresh prices.
247
+     *
248
+     * @param GetPaid_Payment_Form_Submission $submission
249
+     */
250
+    public function add_gateways( $submission ) {
251
+
252
+        $gateways = array_keys( wpinv_get_enabled_payment_gateways() );
253
+
254
+        if ( $this->response['has_recurring'] ) {
255
+
256
+            foreach ( $gateways as $i => $gateway ) {
257
+
258
+                if (
259
+                    ! getpaid_payment_gateway_supports( $gateway, 'subscription' )
260
+                    || ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) )
261
+                    || ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) {
262
+                    unset( $gateways[ $i ] );
263
+                }
264
+
265
+            }
266
+
267
+        }
268
+
269
+        $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission );
270
+        $this->response = array_merge(
271
+            $this->response,
272
+            array( 'gateways' => $gateways )
273
+        );
274
+
275
+    }
276
+
277
+    /**
278
+     * Adds data to a response for submission refresh prices.
279
+     *
280
+     * @param GetPaid_Payment_Form_Submission $submission
281
+     */
282
+    public function add_data( $submission ) {
283
+
284
+        $this->response = array_merge(
285
+            $this->response,
286
+            array(
287
+                'js_data' => apply_filters(
288
+                    'getpaid_submission_js_data',
289
+                    array(
290
+                        'is_recurring' => $this->response['has_recurring'],
291
+                    ),
292
+                    $submission
293
+                )
294
+            )
295
+        );
296
+
297
+    }
298 298
 
299 299
 }
Please login to merge, or discard this patch.