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

WPInv_Payment_Form_Elements   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 417
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 204
dl 0
loc 417
rs 10
c 2
b 0
f 0
wmc 23

19 Methods

Rating   Name   Duplication   Size   Complexity  
B get_elements() 0 124 2
A render_time_template() 0 5 1
A render_date_template() 0 5 1
A edit_text_template() 0 28 1
A render_website_template() 0 5 1
A render_text_template() 0 8 1
A edit_heading_template() 0 18 1
A render_select_template() 0 5 1
A render_textarea_template() 0 8 1
A get_restrict_markup() 0 3 1
A edit_paragraph_template() 0 9 1
A edit_textarea_template() 0 28 1
A render_paragraph_template() 0 4 1
A render_heading_template() 0 3 1
A __construct() 0 11 4
A render_email_template() 0 5 1
A render_checkbox_template() 0 7 1
A render_radio_template() 0 7 1
A render_number_template() 0 5 1
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
    exit; // Exit if accessed directly
5
}
6
7
class WPInv_Payment_Form_Elements {
8
9
    /**
10
     * @param array payment form elements
11
     */
12
    protected $elements;
13
14
    public function __construct() {
15
16
        foreach( $this->get_elements() as $element ) {
17
            $element = $element['type'];
18
19
            if ( method_exists( $this, "render_{$element}_template" ) ) {
20
                add_action( 'wpinv_payment_form_render_element_template', array( $this, "render_{$element}_template" ), 10, 2 );
21
            }
22
23
            if ( method_exists( $this, "edit_{$element}_template" ) ) {
24
                add_action( 'wpinv_payment_form_edit_element_template', array( $this, "edit_{$element}_template" ), 10, 2 );
25
            }
26
27
        }
28
        
29
    }
30
31
    /**
32
     * Returns all the elements that can be added to a form.
33
     */
34
    public function get_elements() {
35
36
        if ( ! empty( $this->elements ) ) {
37
            return $this->elements;
38
        }
39
40
        $this->elements = array(
41
42
            array(
43
                'type'     => 'heading',
44
                'name'     => __( 'Heading', 'invoicing' ),
45
                'defaults' => array(
46
                    'level' => 'h1',
47
                    'text'  => __( 'Heading', 'invoicing' ),
48
                )
49
            ),
50
51
            array(
52
                'type' => 'paragraph',
53
                'name' => __( 'Paragraph', 'invoicing' ),
54
                'defaults'  => array(
55
                    'text'  => __( 'Paragraph text', 'invoicing' ),
56
                )
57
            ),
58
59
            array(
60
                'type' => 'text',
61
                'name' => __( 'Text Input', 'invoicing' ),
62
                'defaults'  => array(
63
                    'placeholder'  => __( 'Enter some text', 'invoicing' ),
64
                    'value'        => '',
65
                    'label'        => __( 'Field Label', 'invoicing' ),
66
                    'description'  => '',
67
                    'required'     => false,
68
                )
69
            ),
70
71
            array(
72
                'type' => 'textarea',
73
                'name' => __( 'Textarea', 'invoicing' ),
74
                'defaults'         => array(
75
                    'placeholder'  => __( 'Enter your text hear', 'invoicing' ),
76
                    'value'        => '',
77
                    'label'        => __( 'Textarea Label', 'invoicing' ),
78
                    'description'  => '',
79
                    'required'     => false,
80
                )
81
            ),
82
83
            array(
84
                'type' => 'select',
85
                'name' => __( 'Dropdown', 'invoicing' ),
86
                'defaults'         => array(
87
                    'placeholder'  => __( 'Select a value', 'invoicing' ),
88
                    'value'        => '',
89
                    'label'        => __( 'Dropdown Label', 'invoicing' ),
90
                )
91
            ),
92
93
            array(
94
                'type' => 'checkbox',
95
                'name' => __( 'Checkboxes', 'invoicing' ),
96
                'defaults'         => array(
97
                    'value'        => '1',
98
                    'label'        => __( 'Checkbox Label', 'invoicing' ),
99
                )
100
            ),
101
102
            array( 
103
                'type' => 'radio',
104
                'name' => __( 'Multiple Choice', 'invoicing' ),
105
                'defaults'     => array(
106
                    'label'    => __( 'Select one choice', 'invoicing' ),
107
                    'choices'  => array(
108
                        __( 'Choice One', 'invoicing' ),
109
                        __( 'Choice Two', 'invoicing' ),
110
                        __( 'Choice Three', 'invoicing' )
111
                    ),
112
                )
113
            ),
114
115
            array( 
116
                'type' => 'date',
117
                'name' => __( 'Date', 'invoicing' ),
118
                'defaults' => array(
119
                    'label'    => __( 'Date', 'invoicing' ),
120
                )
121
            ),
122
123
            array( 
124
                'type' => 'time',
125
                'name' => __( 'Time', 'invoicing' ),
126
                'defaults' => array(
127
                    'label'    => __( 'Time', 'invoicing' ),
128
                )
129
            ),
130
131
            array( 
132
                'type' => 'number',
133
                'name' => __( 'Number', 'invoicing' ),
134
                'defaults' => array(
135
                    'label'    => __( 'Number', 'invoicing' ),
136
                )
137
            ),
138
139
            array( 
140
                'type' => 'website',
141
                'name' => __( 'Website', 'invoicing' ),
142
                'defaults' => array(
143
                    'label'    => __( 'Website', 'invoicing' ),
144
                )
145
            ),
146
147
            array( 
148
                'type' => 'email',
149
                'name' => __( 'Email', 'invoicing' ),
150
                'defaults'  => array(
151
                    'label' => __( 'Email Address', 'invoicing' ),
152
                )
153
            )
154
        );
155
156
        $this->elements = apply_filters( 'wpinv_filter_core_payment_form_elements', $this->elements );
157
        return $this->elements;
158
    }
159
160
    /**
161
     * Returns the restrict markup.
162
     */
163
    public function get_restrict_markup( $field, $field_type ) {
164
        $restrict = "$field.type=='$field_type'";
165
        return "v-if=\"$restrict\"";
166
    }
167
168
    /**
169
     * Renders the title element template.
170
     */
171
    public function render_heading_template( $field ) {
172
        $restrict = $this->get_restrict_markup( $field, 'heading' );
173
        echo "<component :is='$field.level' $restrict v-html='$field.text'></component>";
174
    }
175
176
    /**
177
     * Renders the edit title element template.
178
     */
179
    public function edit_heading_template( $field ) {
180
        $restrict = $this->get_restrict_markup( $field, 'heading' );
181
        $label    = __( 'Heading', 'invoicing' );
182
        $label2   = __( 'Select Heading Level', 'invoicing' );
183
        $id       = $field . '.id + "_edit"';
184
        $id2      = $field . '.id + "_edit2"';
185
186
        echo "
187
            <div $restrict>
188
                <div class='form-group'>
189
                    <label :for='$id'>$label</label>
190
                    <input class='form-control' :id='$id' v-model='$field.text' type='text' />
191
                </div>
192
193
                <div class='form-group'>
194
                    <label :for='$id2'>$label2</label>
195
196
                    <select class='form-control custom-select' :id='$id2' v-model='$field.level'>
197
                        <option value='h1'>H1</option>
198
                        <option value='h2'>H2</option>
199
                        <option value='h3'>H3</option>
200
                        <option value='h4'>H4</option>
201
                        <option value='h5'>H5</option>
202
                        <option value='h6'>H6</option>
203
                        <option value='h7'>H7</option>
204
                    </select>
205
                </div>
206
            </div>
207
        ";
208
209
    }
210
211
    /**
212
     * Renders a paragraph element template.
213
     */
214
    public function render_paragraph_template( $field ) {
215
        $restrict = $this->get_restrict_markup( $field, 'paragraph' );
216
        $label    = "$field.text";
217
        echo "<p $restrict v-html='$label' style='font-size: 16px;'></p>";
218
    }
219
220
    /**
221
     * Renders the edit paragraph element template.
222
     */
223
    public function edit_paragraph_template( $field ) {
224
        $restrict = $this->get_restrict_markup( $field, 'paragraph' );
225
        $label    = __( 'Enter your text', 'invoicing' );
226
        $id       = $field . '.id + "_edit"';
227
        echo "
228
            <div $restrict>
229
                <div class='form-group'>
230
                    <label :for='$id'>$label</label>
231
                    <textarea :id='$id' v-model='$field.text' class='form-control' rows='3'></textarea>
232
                </div>
233
            </div>
234
        ";
235
236
    }
237
238
    /**
239
     * Renders the text element template.
240
     */
241
    public function render_text_template( $field ) {
242
        $restrict = $this->get_restrict_markup( $field, 'text' );
243
        $label    = "$field.label";
244
        echo "
245
            <div $restrict>
246
                <label :for='$field.id'>{{" . $label . "}}</label>
247
                <input :required='$field.required' :placeholder='$field.placeholder' :id='$field.id' class='form-control' type='text'>
248
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
249
            </div>    
250
        ";
251
    }
252
253
    /**
254
     * Renders the edit text element template.
255
     */
256
    public function edit_text_template( $field ) {
257
        $restrict = $this->get_restrict_markup( $field, 'text' );
258
        $label    = __( 'Field Label', 'invoicing' );
259
        $id       = $field . '.id + "_edit"';
260
        $label2   = __( 'Placeholder text', 'invoicing' );
261
        $id2      = $field . '.id + "_edit2"';
262
        $label3   = __( 'Help text', 'invoicing' );
263
        $label4   = esc_attr__( 'Add some help text for this field', 'invoicing' );
264
        $id3      = $field . '.id + "_edit3"';
265
        $label5   = __( 'Is this field required?', 'invoicing' );
266
        $id4      = $field . '.id + "_edit4"';
267
        echo "
268
            <div $restrict>
269
                <div class='form-group'>
270
                    <label :for='$id'>$label</label>
271
                    <input :id='$id' v-model='$field.label' class='form-control' />
272
                </div>
273
                <div class='form-group'>
274
                    <label :for='$id2'>$label2</label>
275
                    <input :id='$id2' v-model='$field.placeholder' class='form-control' />
276
                </div>
277
                <div class='form-group'>
278
                    <label :for='$id3'>$label3</label>
279
                    <textarea placeholder='$label4' :id='$id3' v-model='$field.description' class='form-control' rows='3'></textarea>
280
                </div>
281
                <div class='form-group form-check'>
282
                    <input :id='$id4' v-model='$field.required' type='checkbox' class='form-check-input' />
283
                    <label class='form-check-label' :for='$id4'>$label5</label>
284
                </div>
285
            </div>
286
        ";
287
288
    }
289
290
    /**
291
     * Renders the textarea element template.
292
     */
293
    public function render_textarea_template( $field ) {
294
        $restrict = $this->get_restrict_markup( $field, 'textarea' );
295
        $label    = "$field.label";
296
        echo "
297
            <div $restrict>
298
                <label :for='$field.id'>{{" . $label . "}}</label>
299
                <textarea :required='$field.required' :placeholder='$field.placeholder' :id='$field.id' class='form-control' rows='3'></textarea>
300
                <small v-if='$field.description' class='form-text text-muted' v-html='$field.description'></small>
301
            </div>    
302
        ";
303
    }
304
305
    /**
306
     * Renders the edit textarea element template.
307
     */
308
    public function edit_textarea_template( $field ) {
309
        $restrict = $this->get_restrict_markup( $field, 'textarea' );
310
        $label    = __( 'Field Label', 'invoicing' );
311
        $id       = $field . '.id + "_edit"';
312
        $label2   = __( 'Placeholder text', 'invoicing' );
313
        $id2      = $field . '.id + "_edit2"';
314
        $label3   = __( 'Help text', 'invoicing' );
315
        $label4   = esc_attr__( 'Add some help text for this field', 'invoicing' );
316
        $id3      = $field . '.id + "_edit3"';
317
        $label5   = __( 'Is this field required?', 'invoicing' );
318
        $id4      = $field . '.id + "_edit4"';
319
        echo "
320
            <div $restrict>
321
                <div class='form-group'>
322
                    <label :for='$id'>$label</label>
323
                    <input :id='$id' v-model='$field.label' class='form-control' />
324
                </div>
325
                <div class='form-group'>
326
                    <label :for='$id2'>$label2</label>
327
                    <input :id='$id2' v-model='$field.placeholder' class='form-control' />
328
                </div>
329
                <div class='form-group'>
330
                    <label :for='$id3'>$label3</label>
331
                    <textarea placeholder='$label4' :id='$id3' v-model='$field.description' class='form-control' rows='3'></textarea>
332
                </div>
333
                <div class='form-group form-check'>
334
                    <input :id='$id4' v-model='$field.required' type='checkbox' class='form-check-input' />
335
                    <label class='form-check-label' :for='$id4'>$label5</label>
336
                </div>
337
            </div>
338
        ";
339
340
    }
341
342
    /**
343
     * Renders the select element template.
344
     */
345
    public function render_select_template( $field ) {
346
        $restrict = $this->get_restrict_markup( $field, 'select' );
347
        $label    = "$field.name";
348
        echo "<div $restrict><label>{{" . $label . "}}</label>";
349
        echo "<select class='form-control custom-select'></select></div>";
350
    }
351
352
    /**
353
     * Renders the checkbox element template.
354
     */
355
    public function render_checkbox_template( $field ) {
356
        $restrict = $this->get_restrict_markup( $field, 'checkbox' );
357
        $label    = "$field.name";
358
        echo "<div class='form-check' $restrict>";
359
        echo "<input class='form-check-input' type='checkbox' />";
360
        echo "<label class='form-check-label'>{{" . $label . "}}</label>";
361
        echo '</div>';
362
    }
363
364
    /**
365
     * Renders radio select fields.
366
     */
367
    public function render_radio_template( $field ) {
368
        $restrict = $this->get_restrict_markup( $field, 'radio' );
369
        $label    = "$field.name";
370
        echo "<div class='form-check' $restrict>";
371
        echo "<input class='form-check-input' type='radio' />";
372
        echo "<label class='form-check-label'>{{" . $label . "}}</label>";
373
        echo '</div>';
374
    }
375
376
    /**
377
     * Renders the email element template.
378
     */
379
    public function render_email_template( $field ) {
380
        $restrict = $this->get_restrict_markup( $field, 'email' );
381
        $label    = "$field.name";
382
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
383
        echo "<input class='form-control' type='email'></div>";
384
    }
385
386
    /**
387
     * Renders the website element template.
388
     */
389
    public function render_website_template( $field ) {
390
        $restrict = $this->get_restrict_markup( $field, 'website' );
391
        $label    = "$field.name";
392
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
393
        echo "<input class='form-control' type='url'></div>";
394
    }
395
396
    /**
397
     * Renders the date element template.
398
     */
399
    public function render_date_template( $field ) {
400
        $restrict = $this->get_restrict_markup( $field, 'date' );
401
        $label    = "$field.name";
402
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
403
        echo "<input class='form-control' type='date'></div>";
404
    }
405
406
    /**
407
     * Renders the time element template.
408
     */
409
    public function render_time_template( $field ) {
410
        $restrict = $this->get_restrict_markup( $field, 'time' );
411
        $label    = "$field.name";
412
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
413
        echo "<input class='form-control' type='time'></div>";
414
    }
415
416
    /**
417
     * Renders the number element template.
418
     */
419
    public function render_number_template( $field ) {
420
        $restrict = $this->get_restrict_markup( $field, 'number' );
421
        $label    = "$field.name";
422
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
423
        echo "<input class='form-control' type='number'></div>";
424
    }
425
  
426
}
427