Passed
Pull Request — master (#284)
by Brian
07:10
created

WPInv_Meta_Box_Form_Items::output()   C

Complexity

Conditions 10
Paths 38

Size

Total Lines 100
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 61
c 1
b 0
f 0
nc 38
nop 1
dl 0
loc 100
rs 6.9842

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
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
class WPInv_Meta_Box_Form_Items {
8
9
    /**
10
     * Outputs the payment forms items select.
11
     *
12
     * @param WP_Post $post
13
     */
14
    public static function output( $post ) {
15
16
        $items = get_post_meta( $post->ID, 'wpinv_payment_form_items', true );
17
18
        if ( empty( $items ) || ! is_array( $items ) ) {
19
            $items = array();
20
        }
21
22
        ?>
23
24
        <div class="wpinv-items-wrap-pending" id="wpinv_items_wrap">
25
            <table id="wpinv_items" class="wpinv-items" cellspacing="0" cellpadding="0">
26
27
                <thead>
28
                    <tr>
29
                        <th class="id"><?php _e( 'ID', 'invoicing' );?></th>
30
                        <th class="title"><?php _e( 'Name', 'invoicing' );?></th>
31
                        <th class="desc"><?php _e( 'Description', 'invoicing' );?></th>
32
                        <th class="price"><?php _e( 'Price', 'invoicing' );?></th>
33
                        <th class="action"></th>
34
                    </tr>
35
                </thead>
36
37
                <tbody class="wpinv-line-items">
38
                    <?php
39
40
                        foreach ( $items as $item_data ) {
41
42
                            $id   = isset( $item['id'] ) ? (int) $item['id'] : 0;
43
                            $item = new WPInv_Item( $id );
44
45
                            if ( empty( $item ) || $item->post_type != 'wpi_item' ) {
0 ignored issues
show
Bug Best Practice introduced by
The property post_type does not exist on WPInv_Item. Since you implemented __get, consider adding a @property annotation.
Loading history...
46
                                continue;
47
                            }
48
                            
49
                            $name          = isset( $item_data['name'] ) ? sanitize_text_field( $item_data['name'] ) : $item->get_name();
50
                            $price         = isset( $item_data['price'] ) ? wpinv_format_amount( $item_data['price'] ) : $item->get_price();
51
                            $description   = isset( $item_data['description'] ) ? esc_textarea( $item_data['description'] ) : $item->get_summary();
52
53
                    ?>
54
55
                    <tr class="item" data-item-id="<?php echo $id; ?>">
56
                        <td class="id"><?php echo $id; ?></td>
57
                        <td class="title">
58
                            <a href="<?php echo esc_url( get_edit_post_link( $id ) ); ?>" target="_blank"><?php echo $name ; ?></a>
59
                            <?php echo wpinv_get_item_suffix( $id ); ?>
60
                        </td>
61
                        <td class="meta">
62
                            <?php echo $description ; ?>
63
                        </td>
64
                        <td class="price">
65
                            <?php echo $price ; ?>
66
                        </td>
67
                    </tr>
68
69
                    <?php } ?>
70
71
                    <tr class="item create-item" style="display:none;" id="wpinv-payment-form-quick-add">
72
                        <td class="id"></td>
73
74
                        <td class="title">
75
                            <input type="text" class="regular-text" placeholder="<?php _e( 'Item Name', 'invoicing' ); ?>" value="" id="wpinv_create_payment_form_item_name" />
76
77
                            <div class="wp-clearfix">
78
                                <label class="wpi-item-actions">
79
                                    <span class="input-text-wrap">
80
                                        <input type="button" value="<?php esc_attr_e( 'Add', 'invoicing' ); ?>" class="button button-primary" id="wpinv-payment-form-save-item">
81
                                        <input type="button" value="Cancel" class="button button-secondary" id="wpinv-payment-form-cancel-item">
82
                                    </span>
83
                                </label>
84
                            </div>
85
                        </td>
86
87
                        <td class="meta">
88
                            <textarea placeholder="<?php esc_attr_e( 'Item description', 'invoicing' ) ?>" id="wpinv_create_payment_form_item_description" class="large-text" rows="3"></textarea>
89
                        </td>
90
91
                        <td class="price">
92
                            <input type="text" placeholder="0.00" class="wpi-field-price wpi-price" id="wpinv_create_payment_form_item_price" />
93
                        </td>
94
95
                    </tr>
96
                </tbody>
97
            </table>
98
99
            <div class="wpinv-actions">
100
101
                <?php
102
103
                    echo wpinv_item_dropdown( array(
104
                        'name'             => 'wpinv_payment_form_item',
105
                        'id'               => 'wpinv_payment_form_item',
106
                        'show_recurring'   => true,
107
                        'class'            => 'wpi_select2',
108
                    ) );
109
110
                ?>
111
112
                <input type="button" value="<?php esc_attr_e( 'Add item to form', 'invoicing'); ?>" class="button button-primary" id="wpinv-payment-form-add-item" />
113
                <input type="button" value="<?php esc_attr_e( 'Create new item', 'invoicing' );?>" class="button button-primary" id="wpinv-payment-form-new-item" />
114
115
            </div>
116
        </div>
117
        <?php
118
    }
119
120
     /**
121
     * Outputs the payment options.
122
     *
123
     * @param WP_Post $post
124
     */
125
    public static function output_options( $post ) {
126
127
        $post_id             = ! empty( $post->ID ) ? $post->ID : 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $post_id is dead and can be removed.
Loading history...
128
        $success_page        = get_post_meta( $post->ID, 'wpinv_success_page', true );
129
        $button_text         = get_post_meta( $post->ID, 'wpinv_button_text', true );
130
        $processing_text     = get_post_meta( $post->ID, 'wpinv_processing_text', true );
131
        $supports_quantities = (int) get_post_meta( $post->ID, 'wpinv_form_supports_quantities', true );
132
        $supports_discounts  = (int) get_post_meta( $post->ID, 'wpinv_form_supports_discounts', true );
133
        $enable_taxes        = (int) get_post_meta( $post->ID, 'wpinv_form_supports_quantities', true );
134
135
        $values = array(
136
            'success_page'         => empty( $success_page ) ? wpinv_get_success_page_uri() : $success_page,
137
            'button_text'          => empty( $button_text ) ? __( 'Pay Now', 'invoicing' ) : $button_text,
138
            'processing_text'      => empty( $processing_text ) ? __( 'Processing', 'invoicing' ) : $processing_text,
139
            'supports_quantities'  => empty( $supports_quantities ) ? 0 : 1,
140
            'enable_taxes'         => empty( $enable_taxes ) ? 0 : 1,
141
            'supports_discounts'   => empty( $supports_discounts ) ? 0 : 1,
142
        );
143
144
        ?>
145
146
        <div class="wpinv-items-wrap-pending" id="wpinv_options_wrap">
147
            <table class="form-table">
148
                <tbody>
149
150
                    <tr class="form-field-success_page">
151
                        <th scope="row"><label for="field_success_page"><?php _e( 'Success Page', 'invoicing' ); ?></label></th>
152
                        <td>
153
                            <div>
154
                                <input type="text" class="regular-text" name="success_page" id="field_success_page" value="<?php echo esc_attr( $values['success_page'] ); ?>">
0 ignored issues
show
Bug introduced by
It seems like $values['success_page'] can also be of type false; however, parameter $text of esc_attr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

154
                                <input type="text" class="regular-text" name="success_page" id="field_success_page" value="<?php echo esc_attr( /** @scrutinizer ignore-type */ $values['success_page'] ); ?>">
Loading history...
155
                                <p class="description"><?php _e( 'Where should we redirect users after successfuly completing their payment?', 'invoicing' ); ?></p>
156
                            </div>
157
                        </td>
158
                    </tr>
159
160
                    <tr class="form-field-button_text">
161
                        <th scope="row"><label for="field_button_text"><?php _e( 'Button Text', 'invoicing' ); ?></label></th>
162
                        <td>
163
                            <div>
164
                                <input type="text" class="regular-text" name="button_text" id="field_button_text" value="<?php echo esc_attr( $values['button_text'] ); ?>">
165
                                <p class="description"><?php _e( 'Payment button text', 'invoicing' ); ?></p>
166
                            </div>
167
                        </td>
168
                    </tr>
169
170
                    <tr class="form-field-processing_text">
171
                        <th scope="row"><label for="field_processing_text"><?php _e( 'Processing Text', 'invoicing' ); ?></label></th>
172
                        <td>
173
                            <div>
174
                                <input type="text" class="regular-text" name="processing_text" id="field_processing_text" value="<?php echo esc_attr( $values['processing_text'] ); ?>">
175
                                <p class="description"><?php _e( 'Processing payment button text', 'invoicing' ); ?></p>
176
                            </div>
177
                        </td>
178
                    </tr>
179
180
                    <tr class="form-field-supports_quantities">
181
                        <th scope="row"></th>
182
                        <td>
183
                            <div>
184
                                <label>
185
                                    <input type="checkbox" name="supports_quantities" id="field_supports_quantities" value="1" <?php checked( $values['supports_quantities'], 1 ); ?>>
186
                                    <span><?php _e( 'Let users set custom item quantities', 'invoicing' ); ?></span>
187
                                </label>
188
                            </div>
189
                        </td>
190
                    </tr>
191
192
                    <tr class="form-field-enable_taxes">
193
                        <th scope="row"></th>
194
                        <td>
195
                            <div>
196
                                <label>
197
                                    <input type="checkbox" name="enable_taxes" id="field_enable_taxes" value="1" <?php checked( $values['enable_taxes'], 1 ); ?>>
198
                                    <span><?php _e( 'Enable tax calculations', 'invoicing' ); ?></span>
199
                                </label>
200
                            </div>
201
                        </td>
202
                    </tr>
203
204
                    <tr class="form-field-supports_discounts">
205
                        <th scope="row"></th>
206
                        <td>
207
                            <div>
208
                                <label>
209
                                    <input type="checkbox" name="supports_discounts" id="field_supports_discounts" value="1" <?php checked( $values['supports_discounts'], 1 ); ?>>
210
                                    <span><?php _e( 'Enable coupon codes', 'invoicing' ); ?></span>
211
                                </label>
212
                            </div>
213
                        </td>
214
                    </tr>
215
216
                </tbody>
217
            </table>
218
        </div>
219
        <?php
220
    }
221
222
    /**
223
     * Output fields.
224
     *
225
     * @param WP_Post $post
226
     */
227
    public static function form_design ( $post ) {
228
        ?>
229
230
        <div id="wpinv-form-builder" style="display: flex; flex-flow: wrap;">
231
232
            <div class="wpinv-form-builder-left" style="flex: 0 0 320px;">
233
                <div class="wpinv-form-builder-tabs nav-tab-wrapper">
234
                    <a @click.prevent="active_tab='new_item'" class="nav-tab" :class="{ 'nav-tab-active': active_tab=='new_item' }" href="#"><?php _e( 'Add new element', 'invoicing' ); ?></a>
235
                    <a @click.prevent="active_tab='edit_item'" class="nav-tab" :class="{ 'nav-tab-active': active_tab=='edit_item' }" href="#"><?php _e( 'Edit element', 'invoicing' ); ?></a>
236
                </div>
237
238
                <div class="wpinv-form-builder-tab-content bsui" style="margin-top: 16px;">
239
                    <div class="wpinv-form-builder-tab-pane" v-if="active_tab=='new_item'">
240
                        <div class="wpinv-form-builder-add-field-types">
241
242
                            <draggable class="section" style="display: flex; flex-flow: wrap; justify-content: space-between;" v-model="elements" :group="{ name: 'fields', pull: 'clone', put: false }" :sort="false" :clone="addDraggedField" tag="ul" filter=".wpinv-undraggable">
243
                                <li v-for="element in elements" style="width: 49%; background-color: #fafafa; margin-bottom: 9px; cursor: move; border: 1px solid #eeeeee;" @click.prevent="addField(element)">
244
                                    <button class="button btn" style="width: 100%; cursor: move;">
245
                                        <span v-if="element.icon" class="dashicons dashicon-" :class="'dashicon-' + element.icon"></span>
246
                                        {{element.name}}
247
                                    </button>
248
                                </li>
249
                            </draggable>
250
251
                        </div>
252
                    </div>
253
254
                <div class="wpinv-form-builder-tab-pane" v-if="active_tab=='edit_item'">
255
                    <div class="wpinv-form-builder-edit-field-wrapper"></div>
256
                </div>
257
258
            </div>
259
        </div>
260
261
        <div class="wpinv-form-builder-right" style="flex: 1; padding-top: 40px;border-left: 1px solid #ddd;padding-left: 20px;min-height: 400px;margin-left: 10px;">
262
263
            <draggable class="section bsui" v-model="form_elements" group="fields" tag="div" style="min-height: 100%; font-size: 16px;">
264
                <div v-for="form_element in form_elements" class="form-group">
265
                    <?php do_action( 'wpinv_payment_form_render_element_template', 'form_element', $post ); ?>
266
                </div>
267
            </draggable>
268
269
        </div>
270
271
        </div>
272
        
273
        <?php
274
    }
275
276
    public static function save( $post_id, $data, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

276
    public static function save( $post_id, /** @scrutinizer ignore-unused */ $data, $post ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $post is not used and could be removed. ( Ignorable by Annotation )

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

276
    public static function save( $post_id, $data, /** @scrutinizer ignore-unused */ $post ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $post_id is not used and could be removed. ( Ignorable by Annotation )

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

276
    public static function save( /** @scrutinizer ignore-unused */ $post_id, $data, $post ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
277
278
    }
279
}
280