Passed
Pull Request — master (#518)
by
unknown
06:06
created

GetPaid_Meta_Box_Item_Details::output()   F

Complexity

Conditions 11
Paths 320

Size

Total Lines 295
Code Lines 199

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 199
c 1
b 0
f 0
nc 320
nop 1
dl 0
loc 295
rs 3.9866

How to fix   Long Method    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
 * Item 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_Item_Details Class.
16
 */
17
class GetPaid_Meta_Box_Item_Details {
18
19
    /**
20
	 * Output the metabox.
21
	 *
22
	 * @param WP_Post $post
23
	 */
24
    public static function output( $post ) {
25
26
        // Prepare the item.
27
        $item = new WPInv_Item( $post );
28
29
        // Nonce field.
30
        wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' );
31
32
        // Set the currency position.
33
        $position = wpinv_currency_position();
34
35
        if ( $position == 'left_space' ) {
36
            $position = 'left';
37
        }
38
39
        if ( $position == 'right_space' ) {
40
            $position = 'right';
41
        }
42
43
        ?>
44
        <input type="hidden" id="_wpi_current_type" value="<?php echo esc_attr( $item->get_type( 'edit' ) ); ?>" />
45
        <style>
46
            #poststuff .input-group-text,
47
            #poststuff .form-control {
48
                border-color: #7e8993;
49
            }
50
51
            .bsui label.col-sm-3.col-form-label {
52
                font-weight: 600;
53
            }
54
55
        </style>
56
        <div class='bsui' style='max-width: 600px;padding-top: 10px;'>
57
58
            <?php do_action( 'wpinv_item_details_metabox_before_price', $item ); ?>
59
            <div class="form-group row">
60
                <label class="col-sm-3 col-form-label" for="wpinv_item_price"><span><?php _e( 'Item Price', 'invoicing' )?></span></label>
61
                <div class="col-sm-8">
62
                    <div class="row">
63
                        <div class="col-sm-4 getpaid-price-input">
64
                            <div class="input-group input-group-sm">
65
                                <?php if( 'left' == $position ) : ?>
66
                                <div class="input-group-prepend">
67
                                    <span class="input-group-text" id="wpinv_item_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
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

67
                                    <span class="input-group-text" id="wpinv_item_price_symbol"><?php echo /** @scrutinizer ignore-type */ wpinv_currency_symbol(); ?></span>
Loading history...
68
                                </div>
69
                                <?php endif; ?>
70
                                <input type="text" name="wpinv_item_price" id="wpinv_item_price" value="<?php echo esc_attr( $item->get_price( 'edit' ) ); ?>" placeholder="<?php echo esc_attr( wpinv_sanitize_amount( 0 ) ); ?>" class="form-control">
71
72
                                <?php if( 'left' != $position ) : ?>
73
                                <div class="input-group-append">
74
                                    <span class="input-group-text" id="wpinv_item_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
75
                                </div>
76
                                <?php endif; ?>
77
                            </div>
78
79
                        </div>
80
                        <div class="col-sm-4 wpinv_show_if_recurring">
81
                            <?php
82
                                echo aui()->select(
83
                                    array(
84
                                        'id'               => 'wpinv_recurring_interval',
85
                                        'name'             => 'wpinv_recurring_interval',
86
                                        'label'            => __( 'Interval', 'invoicing' ),
87
                                        'placeholder'      => __( 'Select Interval', 'invoicing' ),
88
                                        'value'            => $item->get_recurring_interval( 'edit' ),
89
                                        'select2'          => true,
90
                                        'data-allow-clear' => 'false',
91
                                        'options'          => array(
92
                                            '1'  => __( 'every', 'invoicing' ),
93
                                            '2'  => __( 'every 2nd', 'invoicing' ),
94
                                            '3'  => __( 'every 3rd', 'invoicing' ),
95
                                            '4'  => __( 'every 4th', 'invoicing' ),
96
                                            '5'  => __( 'every 5th', 'invoicing' ),
97
                                            '6'  => __( 'every 6th', 'invoicing' ),
98
                                            '8'  => __( 'every 8th', 'invoicing' ),
99
                                            '9'  => __( 'every 9th', 'invoicing' ),
100
                                            '10' => __( 'every 10th', 'invoicing' ),
101
                                            '11' => __( 'every 11th', 'invoicing' ),
102
                                            '12' => __( 'every 12th', 'invoicing' ),
103
                                            '13' => __( 'every 13th', 'invoicing' ),
104
                                            '14' => __( 'every 14th', 'invoicing' ),
105
                                        )
106
                                    )
107
                                );
108
                            ?>
109
                        </div>
110
                        <div class="col-sm-4 wpinv_show_if_recurring">
111
                            <?php
112
                                echo aui()->select(
113
                                    array(
114
                                        'id'               => 'wpinv_recurring_period',
115
                                        'name'             => 'wpinv_recurring_period',
116
                                        'label'            => __( 'Period', 'invoicing' ),
117
                                        'placeholder'      => __( 'Select Period', 'invoicing' ),
118
                                        'value'            => $item->get_recurring_period( 'edit' ),
0 ignored issues
show
Bug introduced by
'edit' of type string is incompatible with the type boolean expected by parameter $full of WPInv_Item::get_recurring_period(). ( Ignorable by Annotation )

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

118
                                        'value'            => $item->get_recurring_period( /** @scrutinizer ignore-type */ 'edit' ),
Loading history...
119
                                        'select2'          => true,
120
                                        'data-allow-clear' => 'false',
121
                                        'options'     => array(
122
                                            'D'  => __( 'day', 'invoicing' ),
123
                                            'W'  => __( 'week', 'invoicing' ),
124
                                            'M'  => __( 'month', 'invoicing' ),
125
                                            'Y'  => __( 'year', 'invoicing' ),
126
                                        )
127
                                    )
128
                                );
129
                            ?>
130
                        </div>
131
                    </div>
132
                    <div class="row">
133
                        <div class="col-sm-12">
134
                            <?php
135
136
                                // Dynamic pricing.
137
                                if( $item->supports_dynamic_pricing() ) {
138
139
                                    do_action( 'wpinv_item_details_metabox_before_dynamic_pricing_checkbox', $item );
140
141
                                    // NYP toggle.
142
                                    echo aui()->input(
143
                                        array(
144
                                            'id'          => 'wpinv_name_your_price',
145
                                            'name'        => 'wpinv_name_your_price',
146
                                            'type'        => 'checkbox',
147
                                            'label'       => apply_filters( 'wpinv_name_your_price_toggle_text', __( 'Let customers name their price', 'invoicing' ) ),
148
                                            'value'       => '1',
149
                                            'checked'     => $item->user_can_set_their_price(),
150
                                            'no_wrap'     => true,
151
                                        )
152
                                    );
153
154
                                    do_action( 'wpinv_item_details_metabox_dynamic_pricing_checkbox', $item );
155
156
                                }
157
158
                                // Subscriptions.
159
                                do_action( 'wpinv_item_details_metabox_before_subscription_checkbox', $item );
160
                                echo aui()->input(
161
                                    array(
162
                                        'id'          => 'wpinv_is_recurring',
163
                                        'name'        => 'wpinv_is_recurring',
164
                                        'type'        => 'checkbox',
165
                                        'label'       => apply_filters( 'wpinv_is_recurring_toggle_text', __( 'Charge customers a recurring amount for this item', 'invoicing' ) ),
166
                                        'value'       => '1',
167
                                        'checked'     => $item->is_recurring(),
168
                                        'no_wrap'     => true,
169
                                    )
170
                                );
171
                                do_action( 'wpinv_item_details_metabox_subscription_checkbox', $item );
172
173
                            ?>
174
                            <div class="wpinv_show_if_recurring">
175
                                <em><?php echo wpinv_get_recurring_gateways_text(); ?></em>
176
                            </div>
177
                        </div>
178
                    </div>
179
                </div>
180
                <div class="col-sm-1 pt-2 pl-0">
181
                    <span class="wpi-help-tip dashicons dashicons-editor-help wpinv_show_if_recurring" title="<?php esc_attr_e( 'Set the subscription price, billing interval and period.', 'invoicing' ); ?>"></span>
182
                </div>
183
            </div>
184
            <?php do_action( 'wpinv_item_details_metabox_after_price', $item ); ?>
185
186
            <?php if( $item->supports_dynamic_pricing() ) : ?>
187
                <?php do_action( 'wpinv_item_details_metabox_before_minimum_price', $item ); ?>
188
                <div class="wpinv_show_if_dynamic wpinv_minimum_price">
189
190
                    <div class="form-group row">
191
                        <label for="wpinv_minimum_price" class="col-sm-3 col-form-label">
192
                            <?php _e( 'Minimum Price', 'invoicing' );?>
193
                        </label>
194
                        <div class="col-sm-8">
195
                            <div class="input-group input-group-sm">
196
                                <?php if( 'left' == $position ) : ?>
197
                                    <div class="input-group-prepend">
198
                                        <span class="input-group-text" id="wpinv_item_minimum_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
199
                                    </div>
200
                                <?php endif; ?>
201
202
                                <input type="text" name="wpinv_minimum_price" id="wpinv_minimum_price" value="<?php echo esc_attr( $item->get_minimum_price( 'edit' ) ); ?>" placeholder="<?php echo esc_attr( wpinv_sanitize_amount( 0 ) ); ?>" class="form-control">
203
204
                                <?php if( 'left' != $position ) : ?>
205
                                    <div class="input-group-append">
206
                                        <span class="input-group-text" id="wpinv_item_minimum_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
207
                                    </div>
208
                                <?php endif; ?>
209
                            </div>
210
                        </div>
211
212
                        <div class="col-sm-1 pt-2 pl-0">
213
                            <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Enter the minimum amount that users are allowed to set', 'invoicing' ); ?>"></span>
214
                        </div>
215
                    </div>
216
217
                </div>
218
                <?php do_action( 'wpinv_item_details_metabox_minimum_price', $item ); ?>
219
            <?php endif; ?>
220
221
            <?php
222
            // one time Subscriptions.
223
            do_action( 'wpinv_item_details_metabox_before_one_time_subscription_checkbox', $item );
224
            ?>
225
            <div class="wpinv_show_if_recurring wpinv_one_time_recurring">
226
                <div class="form-group row">
227
                    <label for="wpinv_is_one_time_recurring" class="col-sm-3 col-form-label">
228
                        <?php _e( 'One time only (non-recurring)?', 'invoicing' );?>
229
                    </label> 
230
                    
231
                    <div class="col-sm-8">
232
                        <div class="row">
233
                            <div class="col-sm-12">
234
                                <div class="custom-control custom-checkbox">
235
                                    <input type="checkbox" value="1" <?php echo ($item->is_one_time_recurring()) ? 'checked' : ''; ?> class="form-control  custom-control-input"  name="wpinv_is_one_time_recurring" id="wpinv_is_one_time_recurring" /><label for="wpinv_is_one_time_recurring" class=" custom-control-label">Change customers a recurring amount for one time only.</label>
236
                                </div>
237
                                <div class="wpinv_show_if_recurring">
238
                                    <em><span class="form-text text-muted"><?php echo apply_filters( 'wpinv_is_one_time_recurring_toggle_help_text', __( 'It will appear as a recurring payment, at the next renewal date it will complete the subscription without any deductions.', 'invoicing' ) ) ?></span></em>
239
                                </div>
240
                            </div>
241
                        </div>
242
                    </div>
243
                </div>
244
            </div>
245
            <?php
246
            do_action( 'wpinv_item_details_metabox_one_time_subscription_checkbox', $item );
247
            ?>
248
249
            <?php do_action( 'wpinv_item_details_metabox_before_maximum_renewals', $item ); ?>
250
            <div class="wpinv_show_if_recurring wpinv_maximum_renewals">
251
252
                <div class="form-group row">
253
                    <label for="wpinv_recurring_limit" class="col-sm-3 col-form-label">
254
                        <?php _e( 'Maximum Renewals', 'invoicing' );?>
255
                    </label>
256
                    <div class="col-sm-8">
257
                        <input type="number" value="<?php echo esc_attr( $item->get_recurring_limit( 'edit' ) ); ?>" placeholder="0" name="wpinv_recurring_limit" id="wpinv_recurring_limit" style="width: 100%;" />
258
                    </div>
259
                    <div class="col-sm-1 pt-2 pl-0">
260
                        <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'Leave empty if you want the subscription to renew until it is cancelled.', 'invoicing' ); ?>"></span>
261
                    </div>
262
                </div>
263
264
            </div>
265
            <?php do_action( 'wpinv_item_details_metabox_maximum_renewals', $item ); ?>
266
267
            <?php do_action( 'wpinv_item_details_metabox_before_free_trial', $item ); ?>
268
            <div class="wpinv_show_if_recurring wpinv_free_trial">
269
270
                <div class="form-group row">
271
                    <label class="col-sm-3 col-form-label" for="wpinv_trial_interval"><?php _e( 'Free Trial', 'invoicing' )?></label>
272
273
                    <div class="col-sm-8">
274
                        <div class="row">
275
                            <div class="col-sm-6">
276
                                <?php $value = $item->has_free_trial() ? $item->get_trial_interval( 'edit' ) : 0;?>
277
278
                                <div>
279
                                    <input type="number" name="wpinv_trial_interval" style="width: 100%;" placeholder="0" id="wpinv_trial_interval" value="<?php echo esc_attr( $value ); ?>" >
280
                                </div>
281
                            </div>
282
                            <div class="col-sm-6">
283
                                <?php
284
                                    echo aui()->select(
285
                                        array(
286
                                            'id'               => 'wpinv_trial_period',
287
                                            'name'             => 'wpinv_trial_period',
288
                                            'label'            => __( 'Trial Period', 'invoicing' ),
289
                                            'placeholder'      => __( 'Trial Period', 'invoicing' ),
290
                                            'value'            => $item->get_trial_period( 'edit' ),
0 ignored issues
show
Bug introduced by
'edit' of type string is incompatible with the type boolean expected by parameter $full of WPInv_Item::get_trial_period(). ( Ignorable by Annotation )

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

290
                                            'value'            => $item->get_trial_period( /** @scrutinizer ignore-type */ 'edit' ),
Loading history...
291
                                            'select2'          => true,
292
                                            'data-allow-clear' => 'false',
293
                                            'no_wrap'          => true,
294
                                            'options'          => array(
295
                                                'D'  => __( 'day(s)', 'invoicing' ),
296
                                                'W'  => __( 'week(s)', 'invoicing' ),
297
                                                'M'  => __( 'month(s)', 'invoicing' ),
298
                                                'Y'  => __( 'year(s)', 'invoicing' ),
299
                                            )
300
                                        )
301
                                    );
302
                                ?>
303
304
                            </div>
305
                        </div>
306
                    </div>
307
308
                    <div class="col-sm-1 pt-2 pl-0">
309
                        <span class="wpi-help-tip dashicons dashicons-editor-help" title="<?php esc_attr_e( 'An optional period of time to wait before charging the first recurring payment.', 'invoicing' ); ?>"></span>
310
                    </div>
311
312
                </div>
313
314
            </div>
315
            <?php do_action( 'wpinv_item_details_metabox__free_trial', $item ); ?>
316
317
            <?php do_action( 'wpinv_item_details_metabox_item_details', $item ); ?>
318
        </div>
319
        <?php
320
321
    }
322
323
    /**
324
	 * Save meta box data.
325
	 *
326
	 * @param int $post_id
327
	 */
328
	public static function save( $post_id ) {
329
330
        // Prepare the item.
331
        $item = new WPInv_Item( $post_id );
332
        
333
        // Load new data.
334
        $item->set_props(
335
			array(
336
				'price'                => isset( $_POST['wpinv_item_price'] ) ? (float) $_POST['wpinv_item_price'] : null,
337
				'vat_rule'             => isset( $_POST['wpinv_vat_rules'] ) ? wpinv_clean( $_POST['wpinv_vat_rules'] ) : null,
338
				'vat_class'            => isset( $_POST['wpinv_vat_class'] ) ? wpinv_clean( $_POST['wpinv_vat_class'] ) : null,
339
				'type'                 => isset( $_POST['wpinv_item_type'] ) ? wpinv_clean( $_POST['wpinv_item_type'] ) : null,
340
				'is_dynamic_pricing'   => isset( $_POST['wpinv_name_your_price'] ),
341
                'minimum_price'        => isset( $_POST['wpinv_minimum_price'] ) ? (float) $_POST['wpinv_minimum_price'] : null,
342
				'is_recurring'         => isset( $_POST['wpinv_is_recurring'] ),
343
                'is_one_time_recurring' => isset( $_POST['wpinv_is_one_time_recurring'] ) ? (int) $_POST['wpinv_is_one_time_recurring']  : 0,
344
				'recurring_period'     => isset( $_POST['wpinv_recurring_period'] ) ? wpinv_clean( $_POST['wpinv_recurring_period'] ) : null,
345
				'recurring_interval'   => isset( $_POST['wpinv_recurring_interval'] ) ? (int) $_POST['wpinv_recurring_interval'] : null,
346
347
				'recurring_limit'      => isset( $_POST['wpinv_recurring_limit'] ) ? ((isset( $_POST['wpinv_is_one_time_recurring'] )) ? 1 : (int) $_POST['wpinv_recurring_limit'] ) : null,
348
349
				'is_free_trial'        => isset( $_POST['wpinv_trial_interval'] ) ? ((isset( $_POST['wpinv_is_one_time_recurring'] )) ? 0 : ( 0 != (int) $_POST['wpinv_trial_interval'] ) )  : null,
350
351
				'trial_period'         => isset( $_POST['wpinv_trial_period'] ) ? wpinv_clean( $_POST['wpinv_trial_period'] ) : null,
352
				'trial_interval'       => isset( $_POST['wpinv_trial_interval'] )  ? ((isset( $_POST['wpinv_is_one_time_recurring'] )) ? 0 : (int) $_POST['wpinv_trial_interval'] ) : null,
353
			)
354
        );
355
356
		$item->save();
357
		do_action( 'getpaid_item_metabox_save', $post_id, $item );
358
	}
359
}
360