Passed
Pull Request — master (#376)
by Brian
106:18
created

GetPaid_Meta_Box_Discount_Details::output()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 344
Code Lines 279

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 279
c 1
b 0
f 0
dl 0
loc 344
rs 5.2177
cc 9
nc 256
nop 1

How to fix   Long Method   

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( 'wpinv_discount_metabox_nonce', 'wpinv_discount_metabox_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(); ?>
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_start', $discount ); ?>
230
            <div class="form-group row">
231
                <label for="wpinv_discount_start" class="col-sm-3 col-form-label">
232
                    <?php _e( 'Start Date', 'invoicing' );?>
233
                </label>
234
                <div class="col-sm-8">
235
                    <?php
236
                        echo aui()->input(
237
                            array(
238
                                'type'        => 'datepicker',
239
                                'id'          => 'wpinv_discount_start',
240
                                'name'        => 'wpinv_discount_start',
241
                                'label'       => __( 'Start Date', 'invoicing' ),
242
                                'placeholder' => 'YYYY-MM-DD 00:00',
243
                                'class'       => 'form-control-sm',
244
                                'value'       => $discount->get_start_date( 'edit' ),
245
                                'extra_attributes' => array(
246
                                    'data-enable-time' => 'true',
247
                                    'data-time_24hr'   => 'true',
248
                                    'data-allow-input' => 'true',
249
                                ),
250
                            )
251
                        );
252
                    ?>
253
                </div>
254
                <div class="col-sm-1 pt-2 pl-0">
255
                    <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>
256
                </div>
257
            </div>
258
            <?php do_action( 'wpinv_discount_form_start', $discount ); ?>
259
260
            <?php do_action( 'wpinv_discount_form_before_expiration', $discount ); ?>
261
            <div class="form-group row">
262
                <label for="wpinv_discount_expiration" class="col-sm-3 col-form-label">
263
                    <?php _e( 'Expiration Date', 'invoicing' );?>
264
                </label>
265
                <div class="col-sm-8">
266
                    <?php
267
                        echo aui()->input(
268
                            array(
269
                                'type'        => 'datepicker',
270
                                'id'          => 'wpinv_discount_expiration',
271
                                'name'        => 'wpinv_discount_expiration',
272
                                'label'       => __( 'Expiration Date', 'invoicing' ),
273
                                'placeholder' => 'YYYY-MM-DD 00:00',
274
                                'class'       => 'form-control-sm',
275
                                'value'       => $discount->get_end_date( 'edit' ),
276
                                'extra_attributes' => array(
277
                                    'data-enable-time' => 'true',
278
                                    'data-time_24hr'   => 'true',
279
                                    'data-min-date'    => 'today',
280
                                    'data-allow-input' => 'true',
281
                                    'data-input'       => 'true',
282
                                ),
283
                            )
284
                        );
285
                    ?>
286
                </div>
287
                <div class="col-sm-1 pt-2 pl-0">
288
                    <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>
289
                </div>
290
            </div>
291
            <?php do_action( 'wpinv_discount_form_expiration', $discount ); ?>
292
293
            <?php do_action( 'wpinv_discount_form_before_min_total', $discount ); ?>
294
            <div class="form-group row">
295
                <label for="wpinv_discount_min_total" class="col-sm-3 col-form-label">
296
                    <?php _e( 'Minimum Amount', 'invoicing' );?>
297
                </label>
298
                <div class="col-sm-8">
299
                    <div class="input-group input-group-sm">
300
                        <?php if( 'left' == $position ) : ?>
301
                            <div class="input-group-prepend">
302
                                <span class="input-group-text"><?php echo wpinv_currency_symbol(); ?></span>
303
                            </div>
304
                        <?php endif; ?>
305
306
                        <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">
307
308
                        <?php if( 'left' != $position ) : ?>
309
                            <div class="input-group-append">
310
                                <span class="input-group-text"><?php echo wpinv_currency_symbol(); ?></span>
311
                            </div>
312
                        <?php endif; ?>
313
                    </div>
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 minimum amount (including taxes) required to use this discount.', 'invoicing' ); ?>"></span>
317
                </div>
318
            </div>
319
            <?php do_action( 'wpinv_discount_form_min_total', $discount ); ?>
320
321
            <?php do_action( 'wpinv_discount_form_before_max_total', $discount ); ?>
322
            <div class="form-group row">
323
                <label for="wpinv_discount_max_total" class="col-sm-3 col-form-label">
324
                    <?php _e( 'Maximum 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_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">
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 maximum amount (including taxes) allowed when using this discount.', 'invoicing' ); ?>"></span>
345
                </div>
346
            </div>
347
            <?php do_action( 'wpinv_discount_form_before_max_total', $discount ); ?>
348
349
            <?php do_action( 'wpinv_discount_form_before_max_uses', $discount ); ?>
350
            <div class="form-group row">
351
                <label for="wpinv_discount_max_uses" class="col-sm-3 col-form-label">
352
                    <?php _e( 'Maximum Uses', 'invoicing' );?>
353
                </label>
354
                <div class="col-sm-8">
355
                    <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%;" />
356
                </div>
357
                <div class="col-sm-1 pt-2 pl-0">
358
                    <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>
359
                </div>
360
            </div>
361
            <?php do_action( 'wpinv_discount_form_max_uses', $discount ); ?>
362
363
            <?php do_action( 'wpinv_discount_form_last', $discount ); ?>
364
365
        </div>
366
        <?php
367
        do_action( 'wpinv_discount_form_bottom', $post );
368
    }
369
370
    /**
371
	 * Save meta box data.
372
	 *
373
	 * @param int $post_id
374
	 */
375
	public static function save( $post_id ) {
376
377
        // verify nonce
378
        if ( ! isset( $_POST['wpinv_discount_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['wpinv_discount_metabox_nonce'], 'wpinv_discount_metabox_nonce' ) ) {
379
            return;
380
        }
381
382
        // Prepare the discount.
383
        $discount = new WPInv_Discount( $post_id );
384
385
        // Load new data.
386
        $discount->set_props(
387
			array(
388
				'code'                 => isset( $_POST['wpinv_discount_code'] ) ? $_POST['wpinv_discount_code'] : null,
389
				'amount'               => isset( $_POST['wpinv_discount_amount'] ) ? $_POST['wpinv_discount_amount'] : null,
390
				'start'                => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null,
391
				'expiration'           => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null,
392
				'is_single_use'        => isset( $_POST['wpinv_discount_single_use'] ),
393
                'type'                 => isset( $_POST['wpinv_discount_type'] ) ? $_POST['wpinv_discount_type'] : null,
394
				'is_recurring'         => isset( $_POST['wpinv_discount_recurring'] ),
395
				'items'                => isset( $_POST['wpinv_discount_items'] ) ? $_POST['wpinv_discount_items'] : null,
396
				'excluded_items'       => isset( $_POST['wpinv_discount_excluded_items'] ) ? $_POST['wpinv_discount_excluded_items'] : null,
397
				'max_uses'             => isset( $_POST['wpinv_discount_max_uses'] ) ? $_POST['wpinv_discount_max_uses'] : null,
398
				'min_total'            => isset( $_POST['wpinv_discount_min_total'] ) ? $_POST['wpinv_discount_min_total'] : null,
399
				'max_total'            => isset( $_POST['wpinv_discount_max_total'] ) ? $_POST['wpinv_discount_max_total'] : null,
400
			)
401
        );
402
403
		$discount->save();
404
		do_action( 'getpaid_discount_metabox_save', $post_id, $discount );
405
	}
406
}
407