Passed
Push — master ( 3b5782...9f8a09 )
by Brian
186:14 queued 100:17
created

GetPaid_Meta_Box_Item_Details::save()   C

Complexity

Conditions 14
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 20
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 31
rs 6.2666

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
 * 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( 'wpinv_item_meta_box_save', 'wpinv_vat_meta_box_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
        <input type="hidden" id="_wpi_is_editable" value="<?php echo (int) $item->is_editable(); ?>" />
46
        <style>
47
            #poststuff .input-group-text,
48
            #poststuff .form-control {
49
                border-color: #7e8993;
50
            }
51
        </style>
52
        <div class='bsui' style='max-width: 600px;padding-top: 10px;'>
53
54
            <?php do_action( 'wpinv_item_details_metabox_before_price', $item ); ?>
55
            <div class="form-group row">
56
                <label class="col-sm-3 col-form-label" for="wpinv_item_price"><span><?php _e( 'Item Price', 'invoicing' )?></span></label>
57
                <div class="col-sm-8">
58
                    <div class="row">
59
                        <div class="col-sm-4 getpaid-price-input">
60
                            <div class="input-group input-group-sm">
61
                                <?php if( 'left' == $position ) : ?>
62
                                <div class="input-group-prepend">
63
                                    <span class="input-group-text" id="wpinv_item_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
64
                                </div>
65
                                <?php endif; ?>
66
                                <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">
67
68
                                <?php if( 'left' != $position ) : ?>
69
                                <div class="input-group-append">
70
                                    <span class="input-group-text" id="wpinv_item_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
71
                                </div>
72
                                <?php endif; ?>
73
                            </div>
74
75
                        </div>
76
                        <div class="col-sm-4 wpinv_show_if_recurring">
77
                            <?php
78
                                echo aui()->select(
79
                                    array(
80
                                        'id'               => 'wpinv_recurring_interval',
81
                                        'name'             => 'wpinv_recurring_interval',
82
                                        'label'            => __( 'Interval', 'invoicing' ),
83
                                        'placeholder'      => __( 'Select Interval', 'invoicing' ),
84
                                        'value'            => $item->get_recurring_interval( 'edit' ),
85
                                        'select2'          => true,
86
                                        'data-allow-clear' => 'false',
87
                                        'options'          => array(
88
                                            '1'  => __( 'every', 'invoicing' ),
89
                                            '2'  => __( 'every 2nd', 'invoicing' ),
90
                                            '3'  => __( 'every 3rd', 'invoicing' ),
91
                                            '4'  => __( 'every 4th', 'invoicing' ),
92
                                            '5'  => __( 'every 5th', 'invoicing' ),
93
                                            '6'  => __( 'every 6th', 'invoicing' ),
94
                                            '8'  => __( 'every 8th', 'invoicing' ),
95
                                            '9'  => __( 'every 9th', 'invoicing' ),
96
                                            '10' => __( 'every 10th', 'invoicing' ),
97
                                            '11' => __( 'every 11th', 'invoicing' ),
98
                                            '12' => __( 'every 12th', 'invoicing' ),
99
                                            '13' => __( 'every 13th', 'invoicing' ),
100
                                            '14' => __( 'every 14th', 'invoicing' ),
101
                                        )
102
                                    )
103
                                );
104
                            ?>
105
                        </div>
106
                        <div class="col-sm-4 wpinv_show_if_recurring">
107
                            <?php
108
                                echo aui()->select(
109
                                    array(
110
                                        'id'               => 'wpinv_recurring_period',
111
                                        'name'             => 'wpinv_recurring_period',
112
                                        'label'            => __( 'Period', 'invoicing' ),
113
                                        'placeholder'      => __( 'Select Period', 'invoicing' ),
114
                                        '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

114
                                        'value'            => $item->get_recurring_period( /** @scrutinizer ignore-type */ 'edit' ),
Loading history...
115
                                        'select2'          => true,
116
                                        'data-allow-clear' => 'false',
117
                                        'options'     => array(
118
                                            'D'  => __( 'day', 'invoicing' ),
119
                                            'W'  => __( 'week', 'invoicing' ),
120
                                            'M'  => __( 'month', 'invoicing' ),
121
                                            'Y'  => __( 'year', 'invoicing' ),
122
                                        )
123
                                    )
124
                                );
125
                            ?>
126
                        </div>
127
                    </div>
128
                    <div class="row">
129
                        <div class="col-sm-12">
130
                            <?php
131
132
                                // Dynamic pricing.
133
                                if( $item->supports_dynamic_pricing() ) {
134
135
                                    do_action( 'wpinv_item_details_metabox_before_dynamic_pricing_checkbox', $item );
136
137
                                    // NYP toggle.
138
                                    echo aui()->input(
139
                                        array(
140
                                            'id'          => 'wpinv_name_your_price',
141
                                            'name'        => 'wpinv_name_your_price',
142
                                            'type'        => 'checkbox',
143
                                            'label'       => apply_filters( 'wpinv_name_your_price_toggle_text', __( 'Let customers name their price', 'invoicing' ) ),
144
                                            'value'       => '1',
145
                                            'checked'     => $item->user_can_set_their_price(),
146
                                            'no_wrap'     => true,
147
                                        )
148
                                    );
149
150
                                    do_action( 'wpinv_item_details_metabox_dynamic_pricing_checkbox', $item );
151
152
                                }
153
154
                                // Subscriptions.
155
                                do_action( 'wpinv_item_details_metabox_before_subscription_checkbox', $item );
156
                                echo aui()->input(
157
                                    array(
158
                                        'id'          => 'wpinv_is_recurring',
159
                                        'name'        => 'wpinv_is_recurring',
160
                                        'type'        => 'checkbox',
161
                                        'label'       => apply_filters( 'wpinv_is_recurring_toggle_text', __( 'Charge customers a recurring amount for this item', 'invoicing' ) ),
162
                                        'value'       => '1',
163
                                        'checked'     => $item->is_recurring(),
164
                                        'no_wrap'     => true,
165
                                    )
166
                                );
167
                                do_action( 'wpinv_item_details_metabox_subscription_checkbox', $item );
168
                            ?>
169
                        </div>
170
                    </div>
171
                </div>
172
                <div class="col-sm-1 pt-2 pl-0">
173
                    <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>
174
                </div>
175
            </div>
176
            <?php do_action( 'wpinv_item_details_metabox_after_price', $item ); ?>
177
178
            <?php if( $item->supports_dynamic_pricing() ) : ?>
179
                <?php do_action( 'wpinv_item_details_metabox_before_minimum_price', $item ); ?>
180
                <div class="wpinv_show_if_dynamic wpinv_minimum_price">
181
182
                    <div class="form-group row">
183
                        <label for="wpinv_minimum_price" class="col-sm-3 col-form-label">
184
                            <?php _e( 'Minimum Price', 'invoicing' );?>
185
                        </label>
186
                        <div class="col-sm-8">
187
                            <div class="input-group input-group-sm">
188
                                <?php if( 'left' == $position ) : ?>
189
                                    <div class="input-group-prepend">
190
                                        <span class="input-group-text" id="wpinv_item_minimum_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
191
                                    </div>
192
                                <?php endif; ?>
193
194
                                <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">
195
196
                                <?php if( 'left' != $position ) : ?>
197
                                    <div class="input-group-append">
198
                                        <span class="input-group-text" id="wpinv_item_minimum_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
199
                                    </div>
200
                                <?php endif; ?>
201
                            </div>
202
                        </div>
203
204
                        <div class="col-sm-1 pt-2 pl-0">
205
                            <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>
206
                        </div>
207
                    </div>
208
209
                </div>
210
                <?php do_action( 'wpinv_item_details_metabox_minimum_price', $item ); ?>
211
            <?php endif; ?>
212
213
            <?php do_action( 'wpinv_item_details_metabox_before_maximum_renewals', $item ); ?>
214
            <div class="wpinv_show_if_recurring wpinv_maximum_renewals">
215
216
                <div class="form-group row">
217
                    <label for="wpinv_recurring_limit" class="col-sm-3 col-form-label">
218
                        <?php _e( 'Maximum Renewals', 'invoicing' );?>
219
                    </label>
220
                    <div class="col-sm-8">
221
                        <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%;" />
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( 'Leave empty if you want the subscription to renew until it is cancelled.', 'invoicing' ); ?>"></span>
225
                    </div>
226
                </div>
227
228
            </div>
229
            <?php do_action( 'wpinv_item_details_metabox_maximum_renewals', $item ); ?>
230
231
            <?php do_action( 'wpinv_item_details_metabox_before_free_trial', $item ); ?>
232
            <div class="wpinv_show_if_recurring wpinv_free_trial">
233
234
                <div class="form-group row">
235
                    <label class="col-sm-3 col-form-label" for="wpinv_trial_interval"><?php _e( 'Free Trial', 'invoicing' )?></label>
236
237
                    <div class="col-sm-8">
238
                        <div class="row">
239
                            <div class="col-sm-6">
240
                                <?php $value = $item->has_free_trial() ? $item->get_trial_interval( 'edit' ) : 0;?>
241
242
                                <div>
243
                                    <input type="number" name="wpinv_trial_interval" style="width: 100%;" placeholder="0" id="wpinv_trial_interval" value="<?php echo esc_attr( $value ); ?>" >
244
                                </div>
245
                            </div>
246
                            <div class="col-sm-6">
247
                                <?php
248
                                    echo aui()->select(
249
                                        array(
250
                                            'id'               => 'wpinv_trial_period',
251
                                            'name'             => 'wpinv_trial_period',
252
                                            'label'            => __( 'Trial Period', 'invoicing' ),
253
                                            'placeholder'      => __( 'Trial Period', 'invoicing' ),
254
                                            'value'            => $item->get_recurring_period( 'edit' ),
255
                                            'select2'          => true,
256
                                            'data-allow-clear' => 'false',
257
                                            'no_wrap'          => true,
258
                                            'options'          => array(
259
                                                'D'  => __( 'day(s)', 'invoicing' ),
260
                                                'W'  => __( 'week(s)', 'invoicing' ),
261
                                                'M'  => __( 'month(s)', 'invoicing' ),
262
                                                'Y'  => __( 'year(s)', 'invoicing' ),
263
                                            )
264
                                        )
265
                                    );
266
                                ?>
267
268
                            </div>
269
                        </div>
270
                    </div>
271
272
                    <div class="col-sm-1 pt-2 pl-0">
273
                        <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>
274
                    </div>
275
276
                </div>
277
278
            </div>
279
            <?php do_action( 'wpinv_item_details_metabox__free_trial', $item ); ?>
280
281
            <?php do_action( 'wpinv_item_details_metabox_item_details', $item ); ?>
282
        </div>
283
        <?php
284
285
    }
286
287
    /**
288
	 * Save meta box data.
289
	 *
290
	 * @param int $post_id
291
	 */
292
	public static function save( $post_id ) {
293
294
        // verify nonce
295
        if ( ! isset( $_POST['wpinv_vat_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['wpinv_vat_meta_box_nonce'], 'wpinv_item_meta_box_save' ) ) {
296
            return;
297
        }
298
299
        // Prepare the item.
300
        $item = new WPInv_Item( $post_id );
301
302
        // Load new data.
303
        $item->set_props(
304
			array(
305
				'price'                => isset( $_POST['wpinv_item_price'] ) ? (float) $_POST['wpinv_item_price'] : null,
306
				'vat_rule'             => isset( $_POST['wpinv_vat_rules'] ) ? wpinv_clean( $_POST['wpinv_vat_rules'] ) : null,
307
				'vat_class'            => isset( $_POST['wpinv_vat_class'] ) ? wpinv_clean( $_POST['wpinv_vat_class'] ) : null,
308
				'type'                 => isset( $_POST['wpinv_item_type'] ) ? wpinv_clean( $_POST['wpinv_item_type'] ) : null,
309
				'is_dynamic_pricing'   => isset( $_POST['wpinv_name_your_price'] ),
310
                'minimum_price'        => isset( $_POST['wpinv_minimum_price'] ) ? (float) $_POST['wpinv_minimum_price'] : null,
311
				'is_recurring'         => isset( $_POST['wpinv_is_recurring'] ),
312
				'recurring_period'     => isset( $_POST['wpinv_recurring_period'] ) ? wpinv_clean( $_POST['wpinv_recurring_period'] ) : null,
313
				'recurring_interval'   => isset( $_POST['wpinv_recurring_interval'] ) ? (int) $_POST['wpinv_recurring_interval'] : null,
314
				'recurring_limit'      => isset( $_POST['wpinv_recurring_limit'] ) ? (int) $_POST['wpinv_recurring_limit'] : null,
315
				'is_free_trial'        => isset( $_POST['wpinv_trial_interval'] ) ? ( 0 != (int) $_POST['wpinv_trial_interval'] ) : null,
316
				'trial_period'         => isset( $_POST['wpinv_trial_period'] ) ? wpinv_clean( $_POST['wpinv_trial_period'] ) : null,
317
				'trial_interval'       => isset( $_POST['wpinv_trial_interval'] ) ? (int) $_POST['wpinv_trial_interval'] : null,
318
			)
319
        );
320
321
		$item->save();
322
		do_action( 'getpaid_item_metabox_save', $post_id, $item );
323
	}
324
}
325