Passed
Push — master ( 389694...fc57ae )
by Brian
04:51
created

GetPaid_Meta_Box_Discount_Details::save()   C

Complexity

Conditions 12
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 18
nc 1
nop 1
dl 0
loc 26
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Discount Details
5
 *
6
 * Display the item data meta box.
7
 *
8
 */
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit; // Exit if accessed directly
12
}
13
14
/**
15
 * GetPaid_Meta_Box_Discount_Details Class.
16
 */
17
class GetPaid_Meta_Box_Discount_Details {
18
19
    /**
20
	 * Output the metabox.
21
	 *
22
	 * @param WP_Post $post
23
	 */
24
    public static function output( $post ) {
25
26
        // Prepare the discount.
27
        $discount = new WPInv_Discount( $post );
28
29
        // Nonce field.
30
        wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' );
31
32
        do_action( 'wpinv_discount_form_top', $discount );
33
34
        // Set the currency position.
35
        $position = wpinv_currency_position();
36
37
        if ( $position == 'left_space' ) {
38
            $position = 'left';
39
        }
40
41
        if ( $position == 'right_space' ) {
42
            $position = 'right';
43
        }
44
45
        ?>
46
47
        <style>
48
            #poststuff .input-group-text,
49
            #poststuff .form-control {
50
                border-color: #7e8993;
51
            }
52
        </style>
53
        <div class='bsui' style='max-width: 600px;padding-top: 10px;'>
54
55
            <?php do_action( 'wpinv_discount_form_first', $discount ); ?>
56
57
            <?php do_action( 'wpinv_discount_form_before_code', $discount ); ?>
58
            <div class="form-group row">
59
                <label for="wpinv_discount_code" class="col-sm-3 col-form-label">
60
                    <?php _e( 'Discount Code', 'invoicing' );?>
61
                </label>
62
                <div class="col-sm-8">
63
                    <div class="row">
64
                        <div class="col-sm-12 form-group">
65
                            <input type="text" value="<?php echo esc_attr( $discount->get_code( 'edit' ) ); ?>" placeholder="SUMMER_SALE" name="wpinv_discount_code" id="wpinv_discount_code" style="width: 100%;" />
66
                        </div>
67
                        <div class="col-sm-12">
68
                            <?php
69
                                do_action( 'wpinv_discount_form_before_single_use', $discount );
70
71
                                echo aui()->input(
72
                                    array(
73
                                        'id'          => 'wpinv_discount_single_use',
74
                                        'name'        => 'wpinv_discount_single_use',
75
                                        'type'        => 'checkbox',
76
                                        'label'       => __( 'Each customer can only use this discount once', 'invoicing' ),
77
                                        'value'       => '1',
78
                                        'checked'     => $discount->is_single_use(),
79
                                    )
80
                                );
81
82
                                do_action( 'wpinv_discount_form_single_use', $discount );
83
                            ?>
84
                        </div>
85
                        <div class="col-sm-12">
86
                            <?php
87
                                do_action( 'wpinv_discount_form_before_recurring', $discount );
88
89
                                echo aui()->input(
90
                                    array(
91
                                        'id'          => 'wpinv_discount_recurring',
92
                                        'name'        => 'wpinv_discount_recurring',
93
                                        'type'        => 'checkbox',
94
                                        'label'       => __( 'Apply this discount to all recurring payments for subscriptions', 'invoicing' ),
95
                                        'value'       => '1',
96
                                        'checked'     => $discount->is_recurring(),
97
                                    )
98
                                );
99
100
                                do_action( 'wpinv_discount_form_recurring', $discount );
101
                            ?>
102
                        </div>
103
                    </div>
104
                </div>
105
                <div class="col-sm-1 pt-2 pl-0">
106
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Enter a discount code such as 10OFF.', 'invoicing' ); ?>"></span>
107
                </div>
108
            </div>
109
            <?php do_action( 'wpinv_discount_form_code', $discount ); ?>
110
111
            <?php do_action( 'wpinv_discount_form_before_type', $discount ); ?>
112
            <div class="form-group row">
113
                <label for="wpinv_discount_type" class="col-sm-3 col-form-label">
114
                    <?php _e( 'Discount Type', 'invoicing' );?>
115
                </label>
116
                <div class="col-sm-8">
117
                    <?php
118
                        echo aui()->select(
119
                            array(
120
                                'id'               => 'wpinv_discount_type',
121
                                'name'             => 'wpinv_discount_type',
122
                                'label'            => __( 'Discount Type', 'invoicing' ),
123
                                'placeholder'      => __( 'Select Discount Type', 'invoicing' ),
124
                                'value'            => $discount->get_type( 'edit' ),
125
                                'select2'          => true,
126
                                'data-allow-clear' => 'false',
127
                                'options'          => wpinv_get_discount_types()
128
                            )
129
                        );
130
                    ?>
131
                </div>
132
                <div class="col-sm-1 pt-2 pl-0">
133
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Discount type.', 'invoicing' ); ?>"></span>
134
                </div>
135
            </div>
136
            <?php do_action( 'wpinv_discount_form_type', $discount ); ?>
137
138
            <?php do_action( 'wpinv_discount_form_before_amount', $discount ); ?>
139
            <div class="form-group row <?php echo esc_attr( $discount->get_type( 'edit' ) ); ?>" id="wpinv_discount_amount_wrap">
140
                <label for="wpinv_discount_amount" class="col-sm-3 col-form-label">
141
                    <?php _e( 'Discount Amount', 'invoicing' );?>
142
                </label>
143
                <div class="col-sm-8">
144
                    <div class="input-group input-group-sm">
145
                        <?php if( 'left' == $position ) : ?>
146
                            <div class="input-group-prepend left wpinv-if-flat">
147
                                <span class="input-group-text">
148
                                    <?php echo wpinv_currency_symbol(); ?>
0 ignored issues
show
Bug introduced by
Are you sure wpinv_currency_symbol() of type array|mixed|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

148
                                    <?php echo /** @scrutinizer ignore-type */ wpinv_currency_symbol(); ?>
Loading history...
149
                                </span>
150
                            </div>
151
                        <?php endif; ?>
152
153
                        <input type="text" name="wpinv_discount_amount" id="wpinv_discount_amount" value="<?php echo esc_attr( $discount->get_amount( 'edit' ) ); ?>" placeholder="0" class="form-control">
154
155
                        <?php if( 'right' == $position ) : ?>
156
                            <div class="input-group-prepend left wpinv-if-flat">
157
                                <span class="input-group-text">
158
                                    <?php echo wpinv_currency_symbol(); ?>
159
                                </span>
160
                            </div>
161
                        <?php endif; ?>
162
                        <div class="input-group-append right wpinv-if-percent">
163
                            <span class="input-group-text">%</span>
164
                        </div>
165
                    </div>
166
                </div>
167
                <div class="col-sm-1 pt-2 pl-0">
168
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Enter the discount value. Ex: 10', 'invoicing' ); ?>"></span>
169
                </div>
170
            </div>
171
            <?php do_action( 'wpinv_discount_form_amount', $discount ); ?>
172
173
            <?php do_action( 'wpinv_discount_form_before_items', $discount ); ?>
174
            <div class="form-group row">
175
                <label for="wpinv_discount_items" class="col-sm-3 col-form-label">
176
                    <?php _e( 'Items', 'invoicing' );?>
177
                </label>
178
                <div class="col-sm-8">
179
                    <?php
180
                        echo aui()->select(
181
                            array(
182
                                'id'               => 'wpinv_discount_items',
183
                                'name'             => 'wpinv_discount_items[]',
184
                                'label'            => __( 'Items', 'invoicing' ),
185
                                'placeholder'      => __( 'Select Items', 'invoicing' ),
186
                                'value'            => $discount->get_items( 'edit' ),
187
                                'select2'          => true,
188
                                'multiple'         => true,
189
                                'data-allow-clear' => 'false',
190
                                'options'          => wpinv_get_published_items_for_dropdown()
191
                            )
192
                        );
193
                    ?>
194
                </div>
195
                <div class="col-sm-1 pt-2 pl-0">
196
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Select the items that are allowed to use this discount or leave blank to use this discount all items.', 'invoicing' ); ?>"></span>
197
                </div>
198
            </div>
199
            <?php do_action( 'wpinv_discount_form_items', $discount ); ?>
200
201
            <?php do_action( 'wpinv_discount_form_before_excluded_items', $discount ); ?>
202
            <div class="form-group row">
203
                <label for="wpinv_discount_excluded_items" class="col-sm-3 col-form-label">
204
                    <?php _e( 'Excluded Items', 'invoicing' );?>
205
                </label>
206
                <div class="col-sm-8">
207
                    <?php
208
                        echo aui()->select(
209
                            array(
210
                                'id'               => 'wpinv_discount_excluded_items',
211
                                'name'             => 'wpinv_discount_excluded_items[]',
212
                                'label'            => __( 'Excluded Items', 'invoicing' ),
213
                                'placeholder'      => __( 'Select Items', 'invoicing' ),
214
                                'value'            => $discount->get_excluded_items( 'edit' ),
215
                                'select2'          => true,
216
                                'multiple'         => true,
217
                                'data-allow-clear' => 'false',
218
                                'options'          => wpinv_get_published_items_for_dropdown()
219
                            )
220
                        );
221
                    ?>
222
                </div>
223
                <div class="col-sm-1 pt-2 pl-0">
224
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Select all the items that are not allowed to use this discount.', 'invoicing' ); ?>"></span>
225
                </div>
226
            </div>
227
            <?php do_action( 'wpinv_discount_form_excluded_items', $discount ); ?>
228
229
            <?php do_action( 'wpinv_discount_form_before_required_items', $discount ); ?>
230
            <div class="form-group row">
231
                <label for="wpinv_discount_required_items" class="col-sm-3 col-form-label">
232
                    <?php _e( 'Required Items', 'invoicing' );?>
233
                </label>
234
                <div class="col-sm-8">
235
                    <?php
236
                        echo aui()->select(
237
                            array(
238
                                'id'               => 'wpinv_discount_required_items',
239
                                'name'             => 'wpinv_discount_required_items[]',
240
                                'label'            => __( 'Required Items', 'invoicing' ),
241
                                'placeholder'      => __( 'Select Items', 'invoicing' ),
242
                                'value'            => $discount->get_required_items( 'edit' ),
243
                                'select2'          => true,
244
                                'multiple'         => true,
245
                                'data-allow-clear' => 'false',
246
                                'options'          => wpinv_get_published_items_for_dropdown()
247
                            )
248
                        );
249
                    ?>
250
                </div>
251
                <div class="col-sm-1 pt-2 pl-0">
252
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Select all the items that are required to be in the cart before using this discount.', 'invoicing' ); ?>"></span>
253
                </div>
254
            </div>
255
            <?php do_action( 'wpinv_discount_form_required_items', $discount ); ?>
256
257
            <?php do_action( 'wpinv_discount_form_before_start', $discount ); ?>
258
            <div class="form-group row">
259
                <label for="wpinv_discount_start" class="col-sm-3 col-form-label">
260
                    <?php _e( 'Start Date', 'invoicing' );?>
261
                </label>
262
                <div class="col-sm-8">
263
                    <?php
264
                        echo aui()->input(
265
                            array(
266
                                'type'        => 'datepicker',
267
                                'id'          => 'wpinv_discount_start',
268
                                'name'        => 'wpinv_discount_start',
269
                                'label'       => __( 'Start Date', 'invoicing' ),
270
                                'placeholder' => 'YYYY-MM-DD 00:00',
271
                                'class'       => 'form-control-sm',
272
                                'value'       => $discount->get_start_date( 'edit' ),
273
                                'extra_attributes' => array(
274
                                    'data-enable-time' => 'true',
275
                                    'data-time_24hr'   => 'true',
276
                                    'data-allow-input' => 'true',
277
                                ),
278
                            )
279
                        );
280
                    ?>
281
                </div>
282
                <div class="col-sm-1 pt-2 pl-0">
283
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'For no start date, leave blank. If entered, the discount can only be used after or on this date.', 'invoicing' ); ?>"></span>
284
                </div>
285
            </div>
286
            <?php do_action( 'wpinv_discount_form_start', $discount ); ?>
287
288
            <?php do_action( 'wpinv_discount_form_before_expiration', $discount ); ?>
289
            <div class="form-group row">
290
                <label for="wpinv_discount_expiration" class="col-sm-3 col-form-label">
291
                    <?php _e( 'Expiration Date', 'invoicing' );?>
292
                </label>
293
                <div class="col-sm-8">
294
                    <?php
295
                        echo aui()->input(
296
                            array(
297
                                'type'        => 'datepicker',
298
                                'id'          => 'wpinv_discount_expiration',
299
                                'name'        => 'wpinv_discount_expiration',
300
                                'label'       => __( 'Expiration Date', 'invoicing' ),
301
                                'placeholder' => 'YYYY-MM-DD 00:00',
302
                                'class'       => 'form-control-sm',
303
                                'value'       => $discount->get_end_date( 'edit' ),
304
                                'extra_attributes' => array(
305
                                    'data-enable-time' => 'true',
306
                                    'data-time_24hr'   => 'true',
307
                                    'data-min-date'    => 'today',
308
                                    'data-allow-input' => 'true',
309
                                    'data-input'       => 'true',
310
                                ),
311
                            )
312
                        );
313
                    ?>
314
                </div>
315
                <div class="col-sm-1 pt-2 pl-0">
316
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Optionally set the date after which the discount will expire.', 'invoicing' ); ?>"></span>
317
                </div>
318
            </div>
319
            <?php do_action( 'wpinv_discount_form_expiration', $discount ); ?>
320
321
            <?php do_action( 'wpinv_discount_form_before_min_total', $discount ); ?>
322
            <div class="form-group row">
323
                <label for="wpinv_discount_min_total" class="col-sm-3 col-form-label">
324
                    <?php _e( 'Minimum Amount', 'invoicing' );?>
325
                </label>
326
                <div class="col-sm-8">
327
                    <div class="input-group input-group-sm">
328
                        <?php if( 'left' == $position ) : ?>
329
                            <div class="input-group-prepend">
330
                                <span class="input-group-text"><?php echo wpinv_currency_symbol(); ?></span>
331
                            </div>
332
                        <?php endif; ?>
333
334
                        <input type="text" name="wpinv_discount_min_total" id="wpinv_discount_min_total" value="<?php echo esc_attr( $discount->get_minimum_total( 'edit' ) ); ?>" placeholder="<?php esc_attr_e( 'No minimum', 'invoicing' ); ?>" class="form-control">
335
336
                        <?php if( 'left' != $position ) : ?>
337
                            <div class="input-group-append">
338
                                <span class="input-group-text"><?php echo wpinv_currency_symbol(); ?></span>
339
                            </div>
340
                        <?php endif; ?>
341
                    </div>
342
                </div>
343
                <div class="col-sm-1 pt-2 pl-0">
344
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Optionally set the minimum amount (including taxes) required to use this discount.', 'invoicing' ); ?>"></span>
345
                </div>
346
            </div>
347
            <?php do_action( 'wpinv_discount_form_min_total', $discount ); ?>
348
349
            <?php do_action( 'wpinv_discount_form_before_max_total', $discount ); ?>
350
            <div class="form-group row">
351
                <label for="wpinv_discount_max_total" class="col-sm-3 col-form-label">
352
                    <?php _e( 'Maximum Amount', 'invoicing' );?>
353
                </label>
354
                <div class="col-sm-8">
355
                    <div class="input-group input-group-sm">
356
                        <?php if( 'left' == $position ) : ?>
357
                            <div class="input-group-prepend">
358
                                <span class="input-group-text"><?php echo wpinv_currency_symbol(); ?></span>
359
                            </div>
360
                        <?php endif; ?>
361
362
                        <input type="text" name="wpinv_discount_max_total" id="wpinv_discount_max_total" value="<?php echo esc_attr( $discount->get_maximum_total( 'edit' ) ); ?>" placeholder="<?php esc_attr_e( 'No maximum', 'invoicing' ); ?>" class="form-control">
363
364
                        <?php if( 'left' != $position ) : ?>
365
                            <div class="input-group-append">
366
                                <span class="input-group-text"><?php echo wpinv_currency_symbol(); ?></span>
367
                            </div>
368
                        <?php endif; ?>
369
                    </div>
370
                </div>
371
                <div class="col-sm-1 pt-2 pl-0">
372
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Optionally set the maximum amount (including taxes) allowed when using this discount.', 'invoicing' ); ?>"></span>
373
                </div>
374
            </div>
375
            <?php do_action( 'wpinv_discount_form_before_max_total', $discount ); ?>
376
377
            <?php do_action( 'wpinv_discount_form_before_max_uses', $discount ); ?>
378
            <div class="form-group row">
379
                <label for="wpinv_discount_max_uses" class="col-sm-3 col-form-label">
380
                    <?php _e( 'Maximum Uses', 'invoicing' );?>
381
                </label>
382
                <div class="col-sm-8">
383
                    <input type="text" value="<?php echo esc_attr( $discount->get_max_uses( 'edit' ) ); ?>" placeholder="<?php esc_attr_e( 'Unlimited', 'invoicing' ); ?>" name="wpinv_discount_max_uses" id="wpinv_discount_max_uses" style="width: 100%;" />
384
                </div>
385
                <div class="col-sm-1 pt-2 pl-0">
386
                    <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Optionally set the maximum number of times that this discount code can be used.', 'invoicing' ); ?>"></span>
387
                </div>
388
            </div>
389
            <?php do_action( 'wpinv_discount_form_max_uses', $discount ); ?>
390
391
            <?php do_action( 'wpinv_discount_form_last', $discount ); ?>
392
393
        </div>
394
        <?php
395
        do_action( 'wpinv_discount_form_bottom', $post );
396
    }
397
398
    /**
399
	 * Save meta box data.
400
	 *
401
	 * @param int $post_id
402
	 */
403
	public static function save( $post_id ) {
404
405
        // Prepare the discount.
406
        $discount = new WPInv_Discount( $post_id );
407
408
        // Load new data.
409
        $discount->set_props(
410
			array(
411
				'code'                 => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null,
412
				'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null,
413
				'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
414
				'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
415
				'is_single_use'        => isset( $_POST['wpinv_discount_single_use'] ),
416
                'type'                 => isset( $_POST['wpinv_discount_type'] ) ? wpinv_clean( $_POST['wpinv_discount_type'] ) : null,
417
				'is_recurring'         => isset( $_POST['wpinv_discount_recurring'] ),
418
				'items'                => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(),
419
				'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(),
420
                'required_items'       => isset( $_POST['wpinv_discount_required_items'] ) ? wpinv_clean( $_POST['wpinv_discount_required_items'] ) : array(),
421
				'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null,
422
				'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null,
423
				'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null,
424
			)
425
        );
426
427
		$discount->save();
428
		do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
429
	}
430
}
431