Passed
Push — master ( c00bf2...a0fba5 )
by Brian
04:10
created

GetPaid_Meta_Box_Item_Details::output()   F

Complexity

Conditions 11
Paths 640

Size

Total Lines 244
Code Lines 158

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 158
nc 640
nop 1
dl 0
loc 244
rs 2.92
c 0
b 0
f 0

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( getpaid_unstandardize_amount( $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
                                _e( 'every' );
83
                                echo "&nbsp;";
84
                            ?>
85
                            <input type="number" style="max-width: 60px;" value="<?php echo esc_attr( $item->get_recurring_interval( 'edit' ) ); ?>" placeholder="1" name="wpinv_recurring_interval" id="wpinv_recurring_interval" />
86
                        </div>
87
                        <div class="col-sm-4 wpinv_show_if_recurring">
88
                            <?php
89
                                echo aui()->select(
90
                                    array(
91
                                        'id'               => 'wpinv_recurring_period',
92
                                        'name'             => 'wpinv_recurring_period',
93
                                        'label'            => __( 'Period', 'invoicing' ),
94
                                        'placeholder'      => __( 'Select Period', 'invoicing' ),
95
                                        '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

95
                                        'value'            => $item->get_recurring_period( /** @scrutinizer ignore-type */ 'edit' ),
Loading history...
96
                                        'select2'          => true,
97
                                        'data-allow-clear' => 'false',
98
                                        'options'     => array(
99
                                            'D'  => __( 'day(s)', 'invoicing' ),
100
                                            'W'  => __( 'week(s)', 'invoicing' ),
101
                                            'M'  => __( 'month(s)', 'invoicing' ),
102
                                            'Y'  => __( 'year(s)', 'invoicing' ),
103
                                        )
104
                                    )
105
                                );
106
                            ?>
107
                        </div>
108
                    </div>
109
                    <div class="row">
110
                        <div class="col-sm-12">
111
                            <?php
112
113
                                // Dynamic pricing.
114
                                if( $item->supports_dynamic_pricing() ) {
115
116
                                    do_action( 'wpinv_item_details_metabox_before_dynamic_pricing_checkbox', $item );
117
118
                                    // NYP toggle.
119
                                    echo aui()->input(
120
                                        array(
121
                                            'id'          => 'wpinv_name_your_price',
122
                                            'name'        => 'wpinv_name_your_price',
123
                                            'type'        => 'checkbox',
124
                                            'label'       => apply_filters( 'wpinv_name_your_price_toggle_text', __( 'Let customers name their price', 'invoicing' ) ),
125
                                            'value'       => '1',
126
                                            'checked'     => $item->user_can_set_their_price(),
127
                                            'no_wrap'     => true,
128
                                        )
129
                                    );
130
131
                                    do_action( 'wpinv_item_details_metabox_dynamic_pricing_checkbox', $item );
132
133
                                }
134
135
                                // Subscriptions.
136
                                do_action( 'wpinv_item_details_metabox_before_subscription_checkbox', $item );
137
                                echo aui()->input(
138
                                    array(
139
                                        'id'          => 'wpinv_is_recurring',
140
                                        'name'        => 'wpinv_is_recurring',
141
                                        'type'        => 'checkbox',
142
                                        'label'       => apply_filters( 'wpinv_is_recurring_toggle_text', __( 'Charge customers a recurring amount for this item', 'invoicing' ) ),
143
                                        'value'       => '1',
144
                                        'checked'     => $item->is_recurring(),
145
                                        'no_wrap'     => true,
146
                                    )
147
                                );
148
                                do_action( 'wpinv_item_details_metabox_subscription_checkbox', $item );
149
150
                            ?>
151
                            <div class="wpinv_show_if_recurring">
152
                                <em><?php echo wpinv_get_recurring_gateways_text(); ?></em>
153
                            </div>
154
                        </div>
155
                    </div>
156
                </div>
157
                <div class="col-sm-1 pt-2 pl-0">
158
                    <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>
159
                </div>
160
            </div>
161
            <?php do_action( 'wpinv_item_details_metabox_after_price', $item ); ?>
162
163
            <?php if( $item->supports_dynamic_pricing() ) : ?>
164
                <?php do_action( 'wpinv_item_details_metabox_before_minimum_price', $item ); ?>
165
                <div class="wpinv_show_if_dynamic wpinv_minimum_price">
166
167
                    <div class="form-group row">
168
                        <label for="wpinv_minimum_price" class="col-sm-3 col-form-label">
169
                            <?php _e( 'Minimum Price', 'invoicing' );?>
170
                        </label>
171
                        <div class="col-sm-8">
172
                            <div class="input-group input-group-sm">
173
                                <?php if( 'left' == $position ) : ?>
174
                                    <div class="input-group-prepend">
175
                                        <span class="input-group-text" id="wpinv_item_minimum_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
176
                                    </div>
177
                                <?php endif; ?>
178
179
                                <input type="text" name="wpinv_minimum_price" id="wpinv_minimum_price" value="<?php echo esc_attr( getpaid_unstandardize_amount( $item->get_minimum_price( 'edit' ) ) ); ?>" placeholder="<?php echo esc_attr( wpinv_sanitize_amount( 0 ) ); ?>" class="form-control">
180
181
                                <?php if( 'left' != $position ) : ?>
182
                                    <div class="input-group-append">
183
                                        <span class="input-group-text" id="wpinv_item_minimum_price_symbol"><?php echo wpinv_currency_symbol(); ?></span>
184
                                    </div>
185
                                <?php endif; ?>
186
                            </div>
187
                        </div>
188
189
                        <div class="col-sm-1 pt-2 pl-0">
190
                            <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>
191
                        </div>
192
                    </div>
193
194
                </div>
195
                <?php do_action( 'wpinv_item_details_metabox_minimum_price', $item ); ?>
196
            <?php endif; ?>
197
198
            <?php do_action( 'wpinv_item_details_metabox_before_maximum_renewals', $item ); ?>
199
            <div class="wpinv_show_if_recurring wpinv_maximum_renewals">
200
201
                <div class="form-group row">
202
                    <label for="wpinv_recurring_limit" class="col-sm-3 col-form-label">
203
                        <?php _e( 'Maximum Renewals', 'invoicing' );?>
204
                    </label>
205
                    <div class="col-sm-8">
206
                        <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%;" />
207
                    </div>
208
                    <div class="col-sm-1 pt-2 pl-0">
209
                        <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>
210
                    </div>
211
                </div>
212
213
            </div>
214
            <?php do_action( 'wpinv_item_details_metabox_maximum_renewals', $item ); ?>
215
216
            <?php do_action( 'wpinv_item_details_metabox_before_free_trial', $item ); ?>
217
            <div class="wpinv_show_if_recurring wpinv_free_trial">
218
219
                <div class="form-group row">
220
                    <label class="col-sm-3 col-form-label" for="wpinv_trial_interval"><?php defined( 'GETPAID_PAID_TRIALS_VERSION' ) ? _e( 'Free/Paid Trial', 'invoicing' ) : _e( 'Free Trial', 'invoicing' )?></label>
221
222
                    <div class="col-sm-8">
223
                        <div class="row">
224
                            <div class="col-sm-6">
225
                                <?php $value = $item->has_free_trial() ? $item->get_trial_interval( 'edit' ) : 0;?>
226
227
                                <div>
228
                                    <input type="number" name="wpinv_trial_interval" style="width: 100%;" placeholder="0" id="wpinv_trial_interval" value="<?php echo esc_attr( $value ); ?>" >
229
                                </div>
230
                            </div>
231
                            <div class="col-sm-6">
232
                                <?php
233
                                    echo aui()->select(
234
                                        array(
235
                                            'id'               => 'wpinv_trial_period',
236
                                            'name'             => 'wpinv_trial_period',
237
                                            'label'            => __( 'Trial Period', 'invoicing' ),
238
                                            'placeholder'      => __( 'Trial Period', 'invoicing' ),
239
                                            '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

239
                                            'value'            => $item->get_trial_period( /** @scrutinizer ignore-type */ 'edit' ),
Loading history...
240
                                            'select2'          => true,
241
                                            'data-allow-clear' => 'false',
242
                                            'no_wrap'          => true,
243
                                            'options'          => array(
244
                                                'D'  => __( 'day(s)', 'invoicing' ),
245
                                                'W'  => __( 'week(s)', 'invoicing' ),
246
                                                'M'  => __( 'month(s)', 'invoicing' ),
247
                                                'Y'  => __( 'year(s)', 'invoicing' ),
248
                                            )
249
                                        )
250
                                    );
251
                                ?>
252
253
                            </div>
254
                        </div>
255
                    </div>
256
257
                    <div class="col-sm-1 pt-2 pl-0">
258
                        <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>
259
                    </div>
260
261
                </div>
262
263
            </div>
264
            <?php do_action( 'wpinv_item_details_metabox__free_trial', $item ); ?>
265
266
            <?php do_action( 'wpinv_item_details_metabox_item_details', $item ); ?>
267
        </div>
268
        <?php
269
270
    }
271
272
    /**
273
	 * Save meta box data.
274
	 *
275
	 * @param int $post_id
276
	 */
277
	public static function save( $post_id ) {
278
279
        // Prepare the item.
280
        $item = new WPInv_Item( $post_id );
281
282
        // Load new data.
283
        $item->set_props(
284
			array(
285
				'price'                => isset( $_POST['wpinv_item_price'] ) ? getpaid_standardize_amount( $_POST['wpinv_item_price'] ) : null,
286
				'vat_rule'             => isset( $_POST['wpinv_vat_rules'] ) ? wpinv_clean( $_POST['wpinv_vat_rules'] ) : null,
287
				'vat_class'            => isset( $_POST['wpinv_vat_class'] ) ? wpinv_clean( $_POST['wpinv_vat_class'] ) : null,
288
				'type'                 => isset( $_POST['wpinv_item_type'] ) ? wpinv_clean( $_POST['wpinv_item_type'] ) : null,
289
				'is_dynamic_pricing'   => isset( $_POST['wpinv_name_your_price'] ),
290
                'minimum_price'        => isset( $_POST['wpinv_minimum_price'] ) ? getpaid_standardize_amount( $_POST['wpinv_minimum_price'] ) : null,
291
				'is_recurring'         => isset( $_POST['wpinv_is_recurring'] ),
292
				'recurring_period'     => isset( $_POST['wpinv_recurring_period'] ) ? wpinv_clean( $_POST['wpinv_recurring_period'] ) : null,
293
				'recurring_interval'   => isset( $_POST['wpinv_recurring_interval'] ) ? (int) $_POST['wpinv_recurring_interval'] : 1,
294
				'recurring_limit'      => isset( $_POST['wpinv_recurring_limit'] ) ? (int) $_POST['wpinv_recurring_limit'] : null,
295
				'is_free_trial'        => isset( $_POST['wpinv_trial_interval'] ) ? ( 0 != (int) $_POST['wpinv_trial_interval'] ) : null,
296
				'trial_period'         => isset( $_POST['wpinv_trial_period'] ) ? wpinv_clean( $_POST['wpinv_trial_period'] ) : null,
297
				'trial_interval'       => isset( $_POST['wpinv_trial_interval'] ) ? (int) $_POST['wpinv_trial_interval'] : null,
298
			)
299
        );
300
301
		$item->save();
302
		do_action( 'getpaid_item_metabox_save', $post_id, $item );
303
	}
304
}
305