Passed
Pull Request — master (#371)
by Brian
104:46
created

GetPaid_Meta_Box_Item_Details::save()   C

Complexity

Conditions 13
Paths 2

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 20
nc 2
nop 1
dl 0
loc 31
rs 6.6166
c 1
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
 * 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
        echo "<div class='bsui' style='max-width: 700px;'>";
30
31
        // Nonce field.
32
        wp_nonce_field( 'wpinv_item_meta_box_save', 'wpinv_vat_meta_box_nonce' );
33
34
        // Item price.
35
        do_action( 'wpinv_item_details_metabox_before_price', $item );
36
37
        $position       = wpinv_currency_position();
38
39
        echo '<div class="form-group row">';
40
        echo '<div class="col-sm-2 col-form-label">' . __( 'Item Price', 'invoicing' ) . '</div>';
41
        echo '<div class="col-sm-10">';
42
43
        // Price.
44
        echo '<div class="row"> <div class="col-sm-4">';
45
        echo aui()->input(
46
            array(
47
                'id'          => 'wpinv_item_price',
48
                'name'        => 'wpinv_item_price',
49
                'label'       => __( 'Item Price', 'invoicing' ),
50
                'placeholder' => wpinv_sanitize_amount( 0 ),
51
                'help_text'   => __( 'Enter the item price without the currency symbol.', 'invoicing' ),
52
                'value'       => $item->get_price( 'edit' ),
53
                'input_group_right' => 'left' == $position ? '' : wpinv_currency_symbol(),
54
                'input_group_left'  => 'left' == $position ? wpinv_currency_symbol() : '',
55
            )
56
        );
57
58
        // Recurring interval.
59
        echo '</div> <div class="col-sm-4 wpinv_show_if_recurring">';
60
        echo aui()->select(
61
            array(
62
                'id'          => 'wpinv_recurring_interval',
63
                'name'        => 'wpinv_recurring_interval',
64
                'label'       => __( 'Interval', 'invoicing' ),
65
                'placeholder' => __( 'Select Interval', 'invoicing' ),
66
                'value'       => $item->get_recurring_interval( 'edit' ),
67
                'options'     => array(
68
                    '1'  => __( 'every', 'invoicing' ),
69
                    '2'  => __( 'every 2nd', 'invoicing' ),
70
                    '3'  => __( 'every 3rd', 'invoicing' ),
71
                    '4'  => __( 'every 4th', 'invoicing' ),
72
                    '5'  => __( 'every 5th', 'invoicing' ),
73
                    '6'  => __( 'every 6th', 'invoicing' ),
74
                    '8'  => __( 'every 8th', 'invoicing' ),
75
                    '9'  => __( 'every 9th', 'invoicing' ),
76
                    '10' => __( 'every 10th', 'invoicing' ),
77
                    '11' => __( 'every 11th', 'invoicing' ),
78
                    '12' => __( 'every 12th', 'invoicing' ),
79
                    '13' => __( 'every 13th', 'invoicing' ),
80
                    '14' => __( 'every 14th', 'invoicing' ),
81
                )
82
            )
83
        );
84
85
        // Recurring Period.
86
        echo '</div> <div class="col-sm-4 wpinv_show_if_recurring">';
87
        echo aui()->select(
88
            array(
89
                'id'          => 'wpinv_recurring_period',
90
                'name'        => 'wpinv_recurring_period',
91
                'label'       => __( 'Period', 'invoicing' ),
92
                'placeholder' => __( 'Select Period', 'invoicing' ),
93
                '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

93
                'value'       => $item->get_recurring_period( /** @scrutinizer ignore-type */ 'edit' ),
Loading history...
94
                'options'     => array(
95
                    'D'  => __( 'day', 'invoicing' ),
96
                    'W'  => __( 'week', 'invoicing' ),
97
                    'M'  => __( 'month', 'invoicing' ),
98
                    'Y'  => __( 'year', 'invoicing' ),
99
                )
100
            )
101
        );
102
103
        echo '</div></div>';
104
105
        echo '</div></div>';
106
107
108
        do_action( 'wpinv_prices_metabox_price', $item );
109
110
        // Subscription toggle.
111
        echo '<div class="form-group row">';
112
        echo '<div class="col-sm-2 col-form-label">' . __( 'Is Recurring', 'invoicing' ) . '</div>';
113
        echo '<div class="col-sm-10">';
114
        echo aui()->input(
115
			array(
116
                'id'          => 'wpinv_is_recurring',
117
                'name'        => 'wpinv_is_recurring',
118
                'type'        => 'checkbox',
119
				'label'       => apply_filters( 'wpinv_is_recurring_toggle_text', __( 'Charge customers a recurring amount for this item', 'invoicing' ) ),
120
                'value'       => '1',
121
                'checked'     => $item->is_recurring(),
122
			)
123
        );
124
125
        do_action( 'wpinv_prices_metabox_is_recurring_field', $item );
126
        echo '</div></div>';
127
128
        // Dynamic pricing.
129
        if( $item->supports_dynamic_pricing() ) {
130
131
            do_action( 'wpinv_item_details_metabox_before_dynamic_pricing', $item );
132
133
            // NYP toggle.
134
            echo aui()->input(
135
                array(
136
                    'id'          => 'wpinv_name_your_price',
137
                    'name'        => 'wpinv_name_your_price',
138
                    'type'        => 'checkbox',
139
                    'label'       => apply_filters( 'wpinv_name_your_price_toggle_text', __( 'User can set a custom price', 'invoicing' ) ),
140
                    'value'       => '1',
141
                    'checked'     => $item->user_can_set_their_price(),
142
                    'label_type'  => 'horizontal',
143
                )
144
            );
145
146
            do_action( 'wpinv_prices_metabox_name_your_price_field', $item );
147
148
            $minimum_price_style = '';
149
            if( $item->user_can_set_their_price() ) {
150
                $minimum_price_style .= 'display: none;';
151
            }
152
            echo "<div class='wpinv-row-minimum-price' style='$minimum_price_style;'>";
153
154
            echo aui()->input(
155
                array(
156
                    'id'          => 'wpinv_minimum_price',
157
                    'name'        => 'wpinv_minimum_price',
158
                    'label'       => __( 'Minimum Price', 'invoicing' ),
159
                    'placeholder' => wpinv_sanitize_amount( 0 ),
160
                    'help_text'   => __( "What's the minimum price that a customer can set.", 'invoicing' ),
161
                    'value'       => $item->get_minimum_price( 'edit' ),
162
                    'label_type'  => 'horizontal',
163
                    'input_group_right' => 'left' == $position ? '' : wpinv_currency_symbol(),
164
                    'input_group_left'  => 'left' == $position ? wpinv_currency_symbol() : '',
165
                )
166
            );
167
168
            do_action( 'wpinv_prices_metabox_minimum_price_field', $item );
169
170
            echo "</div>";
171
172
            do_action( 'wpinv_item_details_metabox_dynamic_pricing', $item );
173
        }
174
175
        // Recurring details.
176
        do_action( 'wpinv_item_details_metabox_before_recurring_section', $item );
177
178
        echo "<div class='wpinv-row-recurring-fields'>";
179
180
        $class = $item->is_recurring() ? 'wpinv-recurring-y' : 'wpinv-recurring-n';
0 ignored issues
show
Unused Code introduced by
The assignment to $class is dead and can be removed.
Loading history...
181
182
        echo "</div>";
183
        do_action( 'wpinv_item_details_metabox_recurring_section', $item );
184
185
        echo "</div>";
186
         
187
         $item           = new WPInv_Item( $post->ID );
188
189
        $is_recurring         = $item->is_recurring();
0 ignored issues
show
Unused Code introduced by
The assignment to $is_recurring is dead and can be removed.
Loading history...
190
        $period               = $item->get_recurring_period();
0 ignored issues
show
Unused Code introduced by
The assignment to $period is dead and can be removed.
Loading history...
191
        $interval             = absint( $item->get_recurring_interval() );
192
        $times                = absint( $item->get_recurring_limit() );
0 ignored issues
show
Unused Code introduced by
The assignment to $times is dead and can be removed.
Loading history...
193
        $free_trial           = $item->has_free_trial();
0 ignored issues
show
Unused Code introduced by
The assignment to $free_trial is dead and can be removed.
Loading history...
194
        $trial_interval       = $item->get_trial_interval();
0 ignored issues
show
Unused Code introduced by
The assignment to $trial_interval is dead and can be removed.
Loading history...
195
        $trial_period         = $item->get_trial_period();
0 ignored issues
show
Unused Code introduced by
The assignment to $trial_period is dead and can be removed.
Loading history...
196
197
        $intervals            = array();
198
        for ( $i = 1; $i <= 90; $i++ ) {
199
            $intervals[$i] = $i;
200
        }
201
202
        $interval       = $interval > 0 ? $interval : 1;
0 ignored issues
show
Unused Code introduced by
The assignment to $interval is dead and can be removed.
Loading history...
203
/*
204
        <p class="wpinv-row-recurring-fields <?php echo $class;?>">
205
206
            <label class="wpinv-times" for="wpinv_recurring_limit"> <?php _e( 'for', 'invoicing' );?> <input class="small-text" type="number" value="<?php echo $times;?>" size="4" id="wpinv_recurring_limit" name="wpinv_recurring_limit" step="1" min="0"> <?php _e( 'time(s) <i>(select 0 for recurring forever until cancelled</i>)', 'invoicing' );?></label>
207
            <span class="clear wpi-trial-clr"></span>
208
            <label class="wpinv-free-trial" for="wpinv_free_trial">
209
                <input type="checkbox" name="wpinv_free_trial" id="wpinv_free_trial" value="1" <?php checked( true, (bool)$free_trial ); ?> /> 
210
                <?php echo __( 'Offer free trial for', 'invoicing' ); ?>
211
            </label>
212
            <label class="wpinv-trial-interval" for="wpinv_trial_interval">
213
                <input class="small-text" type="number" value="<?php echo $trial_interval;?>" size="4" id="wpinv_trial_interval" name="wpinv_trial_interval" step="1" min="1"> <select class="wpinv-select wpi_select2" id="wpinv_trial_period" name="wpinv_trial_period"><option value="D" <?php selected( 'D', $trial_period );?>><?php _e( 'day(s)', 'invoicing' ); ?></option><option value="W" <?php selected( 'W', $trial_period );?>><?php _e( 'week(s)', 'invoicing' ); ?></option><option value="M" <?php selected( 'M', $trial_period );?>><?php _e( 'month(s)', 'invoicing' ); ?></option><option value="Y" <?php selected( 'Y', $trial_period );?>><?php _e( 'year(s)', 'invoicing' ); ?></option></select>
214
            </label>
215
            <?php do_action( 'wpinv_prices_metabox_recurring_fields', $item ); ?>
216
        </p>
217
        <input type="hidden" id="_wpi_current_type" value="<?php echo wpinv_get_item_type( $post->ID ); ?>" />
218
        <?php do_action( 'wpinv_item_price_field', $post->ID ); ?>
219
        <?php*/
220
    }
221
222
    /**
223
	 * Save meta box data.
224
	 *
225
	 * @param int $post_id
226
	 */
227
	public static function save( $post_id ) {
228
229
        // verify nonce
230
        if ( ! isset( $_POST['wpinv_vat_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['wpinv_vat_meta_box_nonce'], 'wpinv_item_meta_box_save' ) ) {
231
            return;
232
        }
233
234
        // Prepare the item.
235
        $item = new WPInv_Item( $post_id );
236
237
        // Load new data.
238
        $item->set_props(
239
			array(
240
				'price'                => isset( $_POST['wpinv_item_price'] ) ? (float) $_POST['wpinv_item_price'] : null,
241
				'vat_rule'             => isset( $_POST['wpinv_vat_rules'] ) ? $_POST['wpinv_vat_rules'] : null,
242
				'vat_class'            => isset( $_POST['wpinv_vat_class'] ) ? $_POST['wpinv_vat_class'] : null,
243
				'type'                 => isset( $_POST['wpinv_item_type'] ) ? (int) $_POST['wpinv_item_type'] : null,
244
				'is_dynamic_pricing'   => isset( $_POST['wpinv_name_your_price'] ),
245
				'minimum_price'        => isset( $_POST['wpinv_minimum_price'] ) ? (float) $_POST['wpinv_minimum_price'] : null,
246
				'is_recurring'         => isset( $_POST['wpinv_is_recurring'] ),
247
				'recurring_period'     => isset( $_POST['wpinv_recurring_period'] ) ? wpinv_clean( $_POST['wpinv_recurring_period'] ) : null,
248
				'recurring_interval'   => isset( $_POST['wpinv_recurring_interval'] ) ? (int) $_POST['wpinv_recurring_interval'] : null,
249
				'recurring_limit'      => isset( $_POST['wpinv_recurring_limit'] ) ? (int) $_POST['wpinv_recurring_limit'] : null,
250
				'is_free_trial'        => isset( $_POST['wpinv_free_trial'] ) ,
251
				'trial_period'         => isset( $_POST['wpinv_trial_period'] ) ? wpinv_clean( $_POST['wpinv_trial_period'] ) : null,
252
				'trial_interval'       => isset( $_POST['wpinv_trial_interval'] ) ? (int) $_POST['wpinv_trial_interval'] : null,
253
			)
254
        );
255
256
		$item->save();
257
		do_action( 'getpaid_item_metabox_save', $post_id, $item );
258
	}
259
}
260