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

render_time_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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
            if ( method_exists( $this, "render_{$element}_template" ) ) {
19
                add_action( 'wpinv_payment_form_render_element_template', array( $this, "render_{$element}_template" ), 10, 2 );
20
            }
21
        }
22
        
23
    }
24
25
    /**
26
     * Returns all the elements that can be added to a form.
27
     */
28
    public function get_elements() {
29
30
        if ( ! empty( $this->elements ) ) {
31
            return $this->elements;
32
        }
33
34
        $this->elements = array(
35
36
            array(
37
                'type' => 'heading',
38
                'name' => __( 'Heading', 'invoicing' ),
39
            ),
40
41
            array(
42
                'type' => 'paragraph',
43
                'name' => __( 'Paragraph', 'invoicing' ),
44
            ),
45
46
            array(
47
                'type' => 'text',
48
                'name' => __( 'Text Input', 'invoicing' ),
49
            ),
50
51
            array(
52
                'type' => 'textarea',
53
                'name' => __( 'Textarea', 'invoicing' ),
54
            ),
55
56
            array(
57
                'type' => 'select',
58
                'name' => __( 'Dropdown', 'invoicing' ),
59
            ),
60
61
            array(
62
                'type' => 'checkbox',
63
                'name' => __( 'Checkboxes', 'invoicing' ),
64
            ),
65
66
            array( 
67
                'type' => 'radio',
68
                'name' => __( 'Multiple Choice', 'invoicing' ),
69
            ),
70
71
            array( 
72
                'type' => 'date',
73
                'name' => __( 'Date', 'invoicing' ),
74
            ),
75
76
            array( 
77
                'type' => 'time',
78
                'name' => __( 'Time', 'invoicing' ),
79
            ),
80
81
            array( 
82
                'type' => 'number',
83
                'name' => __( 'Number', 'invoicing' ),
84
            ),
85
86
            array( 
87
                'type' => 'website',
88
                'name' => __( 'Website', 'invoicing' ),
89
            ),
90
91
            array( 
92
                'type' => 'email',
93
                'name' => __( 'Email', 'invoicing' ),
94
            ),
95
        );
96
97
        $this->elements = apply_filters( 'wpinv_filter_core_payment_form_elements', $this->elements );
98
        return $this->elements;
99
    }
100
101
    /**
102
     * Returns the restrict markup.
103
     */
104
    public function get_restrict_markup( $field, $field_type ) {
105
        $restrict = "$field.type=='$field_type'";
106
        return "v-if=\"$restrict\"";
107
    }
108
109
    /**
110
     * Renders the title element template.
111
     */
112
    public function render_heading_template( $field ) {
113
        $restrict = $this->get_restrict_markup( $field, 'heading' );
114
        $label    = "$field.name";
115
        echo "<h1 $restrict v-html='$label'></h1>";
116
    }
117
118
    /**
119
     * Renders a paragraph element template.
120
     */
121
    public function render_paragraph_template( $field ) {
122
        $restrict = $this->get_restrict_markup( $field, 'paragraph' );
123
        $label    = "$field.name";
124
        echo "<p $restrict v-html='$label'></p>";
125
    }
126
127
    /**
128
     * Renders the text element template.
129
     */
130
    public function render_text_template( $field ) {
131
        $restrict = $this->get_restrict_markup( $field, 'text' );
132
        $label    = "$field.name";
133
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
134
        echo "<input class='form-control' type='text'></div>";
135
    }
136
137
    /**
138
     * Renders the textarea element template.
139
     */
140
    public function render_textarea_template( $field ) {
141
        $restrict = $this->get_restrict_markup( $field, 'textarea' );
142
        $label    = "$field.name";
143
        echo "<div $restrict><label>{{" . $label . "}}</label>";
144
        echo "<textarea class='form-control' rows='3'></textarea></div>";
145
    }
146
147
    /**
148
     * Renders the select element template.
149
     */
150
    public function render_select_template( $field ) {
151
        $restrict = $this->get_restrict_markup( $field, 'select' );
152
        $label    = "$field.name";
153
        echo "<div $restrict><label>{{" . $label . "}}</label>";
154
        echo "<select class='form-control custom-select'></select></div>";
155
    }
156
157
    /**
158
     * Renders the checkbox element template.
159
     */
160
    public function render_checkbox_template( $field ) {
161
        $restrict = $this->get_restrict_markup( $field, 'checkbox' );
162
        $label    = "$field.name";
163
        echo "<div class='form-check' $restrict>";
164
        echo "<input class='form-check-input' type='checkbox' />";
165
        echo "<label class='form-check-label'>{{" . $label . "}}</label>";
166
        echo '</div>';
167
    }
168
169
    /**
170
     * Renders radio select fields.
171
     */
172
    public function render_radio_template( $field ) {
173
        $restrict = $this->get_restrict_markup( $field, 'radio' );
174
        $label    = "$field.name";
175
        echo "<div class='form-check' $restrict>";
176
        echo "<input class='form-check-input' type='radio' />";
177
        echo "<label class='form-check-label'>{{" . $label . "}}</label>";
178
        echo '</div>';
179
    }
180
181
    /**
182
     * Renders the email element template.
183
     */
184
    public function render_email_template( $field ) {
185
        $restrict = $this->get_restrict_markup( $field, 'email' );
186
        $label    = "$field.name";
187
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
188
        echo "<input class='form-control' type='email'></div>";
189
    }
190
191
    /**
192
     * Renders the website element template.
193
     */
194
    public function render_website_template( $field ) {
195
        $restrict = $this->get_restrict_markup( $field, 'website' );
196
        $label    = "$field.name";
197
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
198
        echo "<input class='form-control' type='url'></div>";
199
    }
200
201
    /**
202
     * Renders the date element template.
203
     */
204
    public function render_date_template( $field ) {
205
        $restrict = $this->get_restrict_markup( $field, 'date' );
206
        $label    = "$field.name";
207
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
208
        echo "<input class='form-control' type='date'></div>";
209
    }
210
211
    /**
212
     * Renders the time element template.
213
     */
214
    public function render_time_template( $field ) {
215
        $restrict = $this->get_restrict_markup( $field, 'time' );
216
        $label    = "$field.name";
217
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
218
        echo "<input class='form-control' type='time'></div>";
219
    }
220
221
    /**
222
     * Renders the number element template.
223
     */
224
    public function render_number_template( $field ) {
225
        $restrict = $this->get_restrict_markup( $field, 'number' );
226
        $label    = "$field.name";
227
        echo "<div $restrict><label $restrict>{{" . $label . "}}</label>";
228
        echo "<input class='form-control' type='number'></div>";
229
    }
230
  
231
}
232