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