PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-form.php

class-form.php in Sessions 2.3.1, at includes/system/class-form.php

322 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Forms handling
4 *
5 * Handles all forms operations and generation.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace POSessions\System;
13
14 use Feather;
15
16 /**
17 * Define the forms functionality.
18 *
19 * Handles all forms operations and generation.
20 *
21 * @package System
22 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
23 * @since 1.0.0
24 */
25 class Form {
26
27 /**
28 * Initializes the class and set its properties.
29 *
30 * @since 1.0.0
31 */
32 public function __construct() {
33 }
34
35 /**
36 * Get an input form field for number.
37 *
38 * @param string $id The id (and the name) of the control.
39 * @param integer $value The current value.
40 * @param integer $min Minimal number for input.
41 * @param integer $max Maximal number for input.
42 * @param integer $step Step value for the control.
43 * @param string $description Optional. A description to display.
44 * @param string $unit Optional. A unit to display just after the control.
45 * @param boolean $full_width Optional. Is the control full width?
46 * @param boolean $enabled Optional. Is the control enabled?
47 * @return string The HTML string ready to print.
48 * @since 1.0.0
49 */
50 public function field_input_integer( $id, $value, $min, $max, $step, $description = null, $full_width = true, $enabled = true, $unit = null ) {
51 if ( $full_width ) {
52 $width = ' style="width:100%;"';
53 } else {
54 $width = '';
55 }
56 $html = '<input' . ( $enabled ? '' : ' disabled' ) . ' name="' . $id . '" type="number" step="' . $step . '" min="' . $min . '" max="' . $max . '"id="' . $id . '" value="' . $value . '"' . $width . ' class="regular-text"/>';
57 if ( isset( $unit ) ) {
58 $html .= '&nbsp;<label for="' . $id . '">' . $unit . '</label>';
59 }
60 if ( isset( $description ) ) {
61 $html .= '<p class="description">' . $description . '</p>';
62 }
63 return $html;
64 }
65
66 /**
67 * Echoes an input form field for number.
68 *
69 * @param array $args The call arguments.
70 * @since 1.0.0
71 */
72 public function echo_field_input_integer( $args ) {
73 echo $this->field_input_integer( $args['id'], $args['value'], $args['min'], $args['max'], $args['step'], $args['description'], $args['full_width'], $args['enabled'] );
74 }
75
76 /**
77 * Get a text form field.
78 *
79 * @param string $id The id (and the name) of the control.
80 * @param string $value The string to put in the text field.
81 * @param string $description Optional. A description to display.
82 * @param boolean $full_width Optional. Is the control full width?
83 * @param boolean $enabled Optional. Is the control enabled?
84 * @param string $placeholder Optional. Placeholder of the input.
85 * @return string The HTML string ready to print.
86 * @since 1.0.0
87 */
88 public function field_input_text( $id, $value = '', $description = null, $full_width = true, $enabled = true, $placeholder = '' ) {
89 if ( $full_width ) {
90 $width = ' style="width:100%;"';
91 } else {
92 $width = '';
93 }
94 $html = '<input' . ( $enabled ? '' : ' disabled' ) . ' class="regular-text" name="' . $id . '" placeholder="' . $placeholder . '" type="text" id="' . $id . '" value="' . $value . '"' . $width . ' class="regular-text"/>';
95 if ( isset( $description ) ) {
96 $html .= '<p class="description">' . $description . '</p>';
97 }
98 return $html;
99 }
100
101 /**
102 * Echoes a text form field.
103 *
104 * @param array $args The call arguments.
105 * @since 1.0.0
106 */
107 public function echo_field_input_text( $args ) {
108 echo $this->field_input_text( $args['id'], $args['value'], $args['description'], $args['full_width'], $args['enabled'], $args['placeholder'] );
109 }
110
111 /**
112 * Get a text form field.
113 *
114 * @param string $id The id (and the name) of the control.
115 * @param string $value The string to put in the text field.
116 * @param string $description Optional. A description to display.
117 * @param integer $columns Optional. Number of columns.
118 * @param integer $lines Optional. Number of lines.
119 * @param boolean $enabled Optional. Is the control enabled?
120 * @return string The HTML string ready to print.
121 * @since 1.0.0
122 */
123 public function field_input_textarea( $id, $value = '', $description = null, $columns = 80, $lines = 3, $enabled = true ) {
124 $html = '<textarea' . ( $enabled ? '' : ' disabled' ) . ' name="' . $id . '" id="' . $id . '" cols="' . $columns . '" rows="' . $lines . '" class="regular-text code">' . $value . '</textarea>';
125 if ( isset( $description ) ) {
126 $html .= '<p class="description">' . $description . '</p>';
127 }
128 return $html;
129 }
130
131 /**
132 * Echoes a text form field.
133 *
134 * @param array $args The call arguments.
135 * @since 1.0.0
136 */
137 public function echo_field_input_textarea( $args ) {
138 echo $this->field_input_textarea( $args['id'], $args['value'], $args['description'], $args['columns'], $args['lines'], $args['enabled'] );
139 }
140
141 /**
142 * Get a password form field.
143 *
144 * @param string $id The id (and the name) of the control.
145 * @param string $value The string to put in the text field.
146 * @param string $description Optional. A description to display.
147 * @param boolean $full_width Optional. Is the control full width?
148 * @param boolean $enabled Optional. Is the control enabled?
149 * @return string The HTML string ready to print.
150 * @since 1.0.0
151 */
152 public function field_input_password( $id, $value = '', $description = null, $full_width = true, $enabled = true ) {
153 if ( $full_width ) {
154 $width = ' style="width:100%;"';
155 } else {
156 $width = '';
157 }
158 $html = '<input' . ( $enabled ? '' : ' disabled' ) . ' name="' . $id . '" type="text" id="' . $id . '" value="' . $value . '"' . $width . ' class="regular-text"/>';
159 if ( isset( $description ) ) {
160 $html .= '<p class="description">' . $description . '</p>';
161 }
162 return $html;
163 }
164
165 /**
166 * Echoes a password form field.
167 *
168 * @param array $args The call arguments.
169 * @since 1.0.0
170 */
171 public function echo_field_input_password( $args ) {
172 echo $this->field_input_password( $args['id'], $args['value'], $args['description'], $args['full_width'], $args['enabled'] );
173 }
174
175 /**
176 * Get a select form field.
177 *
178 * @param array $list The list of options.
179 * @param string $id The id (and the name) of the control.
180 * @param int|string $value The string to put in the text field.
181 * @param string $description Optional. A description to display.
182 * @param boolean $full_width Optional. Is the control full width?
183 * @param boolean $enabled Optional. Is the control enabled?
184 * @return string The HTML string ready to print.
185 * @since 1.0.0
186 */
187 public function field_select( $list, $id, $value, $description = null, $full_width = true, $enabled = true ) {
188 if ( $full_width ) {
189 $width = ' style="width:100%;"';
190 } else {
191 $width = '';
192 }
193 $html = '';
194 foreach ( $list as $val ) {
195 $html .= '<option value="' . $val[0] . '"' . ( $val[0] == $value ? ' selected="selected"' : '' ) . ( isset( $val[2] ) && ! $val[2] ? ' disabled' : '' ) . '>' . $val[1] . '</option>';
196 }
197 $html = '<select' . $width . ( $enabled ? '' : ' disabled' ) . ' name="' . $id . '" id="' . $id . '">' . $html . '</select>';
198 if ( isset( $description ) ) {
199 $html .= '<p class="description">' . $description . '</p>';
200 }
201 return $html;
202 }
203
204 /**
205 * Echoes a select form field.
206 *
207 * @param array $args The call arguments.
208 * @since 1.0.0
209 */
210 public function echo_field_select( $args ) {
211 echo $this->field_select( $args['list'], $args['id'], $args['value'], $args['description'], $args['full_width'], $args['enabled'] );
212 }
213
214 /**
215 * Get a radio form field.
216 *
217 * @param array $list The list of options.
218 * @param string $id The id (and the name) of the control.
219 * @param int|string $value The string to put in the text field.
220 * @param string $description Optional. A description to display.
221 * @param boolean $full_width Optional. Is the control full width?
222 * @param boolean $enabled Optional. Is the control enabled?
223 * @return string The HTML string ready to print.
224 * @since 1.0.0
225 */
226 public function field_radio( $list, $id, $value, $description = null, $full_width = true, $enabled = true ) {
227 if ( $full_width ) {
228 $width = ' style="width:100%;"';
229 } else {
230 $width = '';
231 }
232 $html = '';
233 foreach ( $list as $val ) {
234 $html .= '<label><input' . ( $enabled ? '' : ' disabled' ) . ' id="' . $id . '" name="' . $id . '" type="radio" value="' . $val[0] . '"' . ( $val[0] == $value ? ' checked="checked"' : '' ) . '/>' . $val[1] . '</label>';
235 if ( $val !== end( $list ) ) {
236 $html .= '<br/>';
237 }
238 }
239 $html = '<fieldset' . $width . '>' . $html . '</fieldset>';
240 if ( isset( $description ) ) {
241 $html .= '<p class="description">' . $description . '</p>';
242 }
243 return $html;
244 }
245
246 /**
247 * Echoes a radio form field.
248 *
249 * @param array $args The call arguments.
250 * @since 1.0.0
251 */
252 public function echo_field_radio( $args ) {
253 echo $this->field_radio( $args['list'], $args['id'], $args['value'], $args['description'], $args['full_width'], $args['enabled'] );
254 }
255
256 /**
257 * Get a checkbox form field.
258 *
259 * @param string $text The text of the checkbox.
260 * @param string $id The id (and the name) of the control.
261 * @param boolean $checked Optional. Is the checkbox on?
262 * @param string $description Optional. A description to display.
263 * @param string $more Optional. The content of a "more" box.
264 * @param boolean $full_width Optional. Is the control full width?
265 * @param boolean $enabled Optional. Is the control enabled?
266 * @return string The HTML string ready to print.
267 * @since 1.0.0
268 */
269 public function field_checkbox( $text, $id, $checked = false, $description = null, $more = null, $full_width = true, $enabled = true ) {
270 if ( $full_width ) {
271 $width = ' style="width:100%;"';
272 } else {
273 $width = '';
274 }
275 $html = '<fieldset' . $width . '><label><input' . ( $enabled ? '' : ' disabled' ) . ' name="' . $id . '" type="checkbox" value="1"' . ( $checked ? ' checked="checked"' : '' ) . '/>' . $text . '</label></fieldset>';
276 if ( isset( $description ) ) {
277 if ( isset( $more ) ) {
278 $description .= '<img id="button-' . $id . '" style="cursor:pointer;vertical-align:middle;width:16px;margin-left:6px;" src="' . Feather\Icons::get_base64( 'help-circle', 'none', '#9999BB' ) . '" />';
279 }
280 $html .= '<p class="description">' . $description . '</p>';
281 }
282 if ( isset( $more ) ) {
283 $html .= '<p id="more-' . $id . '" style="line-height:1.8em;word-break:break-word;font-size:smaller;padding:8px;border-radius:2px;background-color:#F9F9F9;display:none;">' . $more . '</p>';
284 $html .= '<script>jQuery(document).ready(function($){$("#button-' . $id . '").click(function(){$("#more-' . $id . '").slideToggle(200);});});</script>';
285 }
286 return $html;
287 }
288
289 /**
290 * Echoes a checkbox form field.
291 *
292 * @param array $args The call arguments.
293 * @since 1.0.0
294 */
295 public function echo_field_checkbox( $args ) {
296 echo $this->field_checkbox( $args['text'], $args['id'], $args['checked'], $args['description'], array_key_exists( 'more', $args ) ? $args['more'] : null, $args['full_width'], $args['enabled'] );
297 }
298
299 /**
300 * Get a simple text in form field.
301 *
302 * @param string $text The text.
303 * @return string The HTML string ready to print.
304 * @since 1.0.0
305 */
306 public function field_simple_text( $text ) {
307 $html = $text;
308 return $html;
309 }
310
311 /**
312 * Echoes a simple text in form field.
313 *
314 * @param array $args The call arguments.
315 * @since 1.0.0
316 */
317 public function echo_field_simple_text( $args ) {
318 echo $this->field_simple_text( $args['text'] );
319 }
320
321 }
322