Passed
Pull Request — master (#284)
by Brian
06:37
created

WPInv_Meta_Box_Payment_Form::save()   C

Complexity

Conditions 14
Paths 9

Size

Total Lines 45
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 19
c 1
b 0
f 0
nc 9
nop 2
dl 0
loc 45
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
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
class WPInv_Meta_Box_Payment_Form {
8
9
    /**
10
     * Output fields.
11
     *
12
     * @param WP_Post $post
13
     */
14
    public static function output_shortcode( $post ) {
15
16
        if ( ! is_numeric( $post ) ) {
0 ignored issues
show
introduced by
The condition is_numeric($post) is always false.
Loading history...
17
            $post = $post->ID;
18
        }
19
20
        echo "<input type='text' style='min-width: 220px;' value='[wpinv_payment_form form=$post]' disabled>";
21
22
    }
23
    /**
24
     * Output fields.
25
     *
26
     * @param WP_Post $post
27
     */
28
    public static function output ( $post ) {
29
        $success_page        = get_post_meta( $post->ID, 'wpinv_success_page', true );
30
        $success_page        = empty( $success_page ) ? wpinv_get_success_page_uri() : $success_page
31
        ?>
32
33
        <div id="wpinv-form-builder" style="display: flex; flex-flow: wrap;">
34
35
            <div class="wpinv-form-builder-left bsui" style="flex: 0 0 320px;">
36
                <ul class="wpinv-form-builder-tabs  nav nav-tabs">
37
                    <li class='nav-item' v-if="active_tab!='new_item'">
38
                        <a @click.prevent="active_tab='new_item'" class="nav-link p-3" :class="{ 'active': active_tab=='new_item' }" href="#"><?php _e( 'Add new element', 'invoicing' ); ?></a>
39
                    </li>
40
                    <li class='nav-item' v-if='false'>
41
                        <a @click.prevent="active_tab='edit_item'" class="nav-link p-3" :class="{ 'active': active_tab=='edit_item' }" href="#"><?php _e( 'Edit element', 'invoicing' ); ?></a>
42
                    </li>
43
                    <li class='nav-item' v-if='false'>
44
                        <a @click.prevent="active_tab='settings'" class="nav-link p-3" :class="{ 'active': active_tab=='settings' }" href="#"><?php _e( 'Settings', 'invoicing' ); ?></a>
45
                    </li>
46
                </ul>
47
48
                <div class="wpinv-form-builder-tab-content bsui" style="margin-top: 16px;">
49
                    <div class="wpinv-form-builder-tab-pane" v-if="active_tab=='new_item'">
50
                        <div class="wpinv-form-builder-add-field-types">
51
                            <small class='form-text text-muted'><?php _e( 'Add an element by dragging it to the payment form.', 'invoicing' ); ?></small>
52
                            <draggable class="section mt-2" 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">
53
                                <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)" :class="{ 'd-none': element.defaults.premade }">
54
                                    <button class="button btn" style="width: 100%; cursor: move;">
55
                                        <span v-if="element.icon" class="dashicons dashicon-" :class="'dashicon-' + element.icon"></span>
56
                                        {{element.name}}
57
                                    </button>
58
                                </li>
59
                            </draggable>
60
61
                        </div>
62
                    </div>
63
64
                    <div class="wpinv-form-builder-tab-pane bsui" v-if="active_tab=='edit_item'" style="font-size: 16px;">
65
                        <div class="wpinv-form-builder-edit-field-wrapper">
66
                            <?php do_action( 'wpinv_payment_form_edit_element_template', 'active_form_element', $post ); ?>
67
                            <div>
68
                                <button type="button" class="button button-link button-link-delete" @click.prevent="removeField(active_form_element)" v-show="! active_form_element.premade"><?php _e( 'Delete Field', 'invoicing' ); ?></button>
69
                            </div>
70
                        </div>
71
                    </div>
72
73
                    <div class="wpinv-form-builder-tab-pane bsui" v-if="active_tab=='settings'">
74
                        <div class="wpinv-form-builder-settings-wrapper">
75
                        
76
                            <div class='form-group'>
77
                                <label for="wpi-success-page"><?php _e( 'Success Page', 'invoicing' ); ?></label>
78
                                <input  placeholder="https://" id='wpi-success-page' value="<?php echo esc_url( $success_page ); ?>" class='form-control' type='text'>
0 ignored issues
show
Bug introduced by
It seems like $success_page can also be of type false; however, parameter $url of esc_url() 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

78
                                <input  placeholder="https://" id='wpi-success-page' value="<?php echo esc_url( /** @scrutinizer ignore-type */ $success_page ); ?>" class='form-control' type='text'>
Loading history...
79
                                <small class='form-text text-muted'><?php _e( 'Where should we redirect users after successfuly completing their payment?', 'invoicing' ); ?></small>
80
                            </div>
81
82
                        </div>
83
                    </div>
84
85
                </div>
86
            </div>
87
88
            <div class="wpinv-form-builder-right" style="flex: 1; padding-top: 40px;border-left: 1px solid #ddd;padding-left: 20px;min-height: 520px;margin-left: 10px;">
89
90
                <small class='form-text text-muted' v-if='form_elements.length'><?php _e( 'Click on any element to edit or delete it.', 'invoicing' ); ?></small>
91
                <p class='form-text text-muted' v-if='! form_elements.length'><?php _e( 'This form is empty. Add new elements by dragging them from the right.', 'invoicing' ); ?></p>
92
93
                <draggable class="section bsui" v-model="form_elements" @add="highlightLastDroppedField" group="fields" tag="div" style="min-height: 100%; font-size: 16px;">
94
                    <div v-for="form_element in form_elements" class="wpinv-form-builder-element-preview" :class="{ active: active_form_element==form_element &&  active_tab=='edit_item' }" @click="active_tab = 'edit_item'; active_form_element = form_element">
95
                        <?php do_action( 'wpinv_payment_form_render_element_template', 'form_element', $post ); ?>
96
                    </div>
97
                </draggable>
98
            </div>
99
100
            <textarea style="display:none;" name="wpinv_form_elements" v-model="elementString"></textarea>
101
            <textarea style="display:none;" name="wpinv_form_items" v-model="itemString"></textarea>
102
        </div>
103
        
104
        <?php wp_nonce_field( 'wpinv_save_payment_form', 'wpinv_save_payment_form' ) ;?>
105
106
        <?php
107
    }
108
109
    /**
110
     * Saves our payment forms.
111
     */
112
    public static function save( $post_id, $post ) {
113
114
        remove_action( 'save_post', 'WPInv_Meta_Box_Payment_Form::save' );
115
116
        // $post_id and $post are required.
117
        if ( empty( $post_id ) || empty( $post ) ) {
118
            return;
119
        }
120
        
121
        // Ensure that this user can edit the post.
122
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
123
            return;
124
        }
125
126
        // Dont' save meta boxes for revisions or autosaves
127
        if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) {
128
            return;
129
        }
130
131
        // Do not save for ajax requests.
132
        if ( ( defined( 'DOING_AJAX') && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
133
            return;
134
        }
135
136
        // Confirm the security nonce.
137
        if ( empty( $_POST['wpinv_save_payment_form'] ) || ! wp_verify_nonce( $_POST['wpinv_save_payment_form'], 'wpinv_save_payment_form' ) ) {
138
            return;
139
        }
140
141
        // Save form items.
142
        $form_items = json_decode( wp_unslash( $_POST['wpinv_form_items'] ), true );
0 ignored issues
show
Bug introduced by
It seems like wp_unslash($_POST['wpinv_form_items']) can also be of type array; however, parameter $json of json_decode() 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

142
        $form_items = json_decode( /** @scrutinizer ignore-type */ wp_unslash( $_POST['wpinv_form_items'] ), true );
Loading history...
143
144
        if ( empty( $form_items ) ) {
145
            $form_items = array();
146
        }
147
148
        update_post_meta( $post_id, 'wpinv_form_items', self::maybe_save_items( $form_items ) );
149
150
        // Save form elements.
151
        $wpinv_form_elements = json_decode( wp_unslash( $_POST['wpinv_form_elements'] ), true );
152
        if ( empty( $wpinv_form_elements ) ) {
153
            $wpinv_form_elements = array();
154
        }
155
156
        update_post_meta( $post_id, 'wpinv_form_elements', $wpinv_form_elements );
157
    }
158
159
    /**
160
     * Saves unsaved form items.
161
     */
162
    public static function maybe_save_items( $items ) {
163
164
        $saved = array();
165
166
        foreach( $items as $item ) {
167
168
            if ( is_numeric( $item['id'] ) ) {
169
                $saved[] = $item;
170
                continue;
171
            }
172
173
            $data = array(
174
                'post_title'   => sanitize_text_field( $item['title'] ),
175
                'post_excerpt' => wp_kses_post( $item['description'] ),
176
                'post_status'  => 'publish',
177
                'meta'         => array(
178
                    'type'      => 'custom',
179
                    'price'     => wpinv_sanitize_amount( $item['price'] ),
180
                    'vat_rule'  => 'digital',
181
                    'vat_class' => '_standard',
182
                )
183
            );
184
            
185
            $new_item  = new WPInv_Item();
186
            $new_item->create( $data );
187
    
188
            if ( ! empty( $new_item ) ) {
189
                $item['id'] = $new_item->ID;
190
                $saved[] = $item;
191
            }
192
193
        }
194
195
        return $saved;
196
197
    }
198
199
}
200
201
add_action( 'save_post_wpi_payment_form', 'WPInv_Meta_Box_Payment_Form::save', 10, 2 );
202
203