css
3 years ago
font
3 years ago
images
3 years ago
js
3 years ago
admin.php
3 years ago
edit.php
3 years ago
form.php
3 years ago
insert.php
3 years ago
manage.php
3 years ago
settings.php
3 years ago
tools.php
3 years ago
form.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; |
| 4 | |
| 5 | class SC_Admin_Form{ |
| 6 | |
| 7 | public static function table( $rows = array(), $print = false, $class = '' ){ |
| 8 | |
| 9 | $html = '<table class="form-table ' . esc_attr( $class ) . '">'; |
| 10 | |
| 11 | foreach( $rows as $row ){ |
| 12 | $html .= '<tr ' . ( isset( $row[2] ) ? $row[2] : '' ) . '>'; |
| 13 | $html .= '<th>' . ( isset( $row[0] ) ? $row[0] : '' ) . '</th>'; |
| 14 | $html .= '<td>' . ( isset( $row[1] ) ? $row[1] : '' ) . '</td>'; |
| 15 | $html .= '</tr>'; |
| 16 | } |
| 17 | |
| 18 | $html .= '</table>'; |
| 19 | |
| 20 | if( $print ){ |
| 21 | echo $html; |
| 22 | }else{ |
| 23 | return $html; |
| 24 | } |
| 25 | |
| 26 | } |
| 27 | |
| 28 | public static function field( $field_type, $params = array() ){ |
| 29 | |
| 30 | $fields = array( 'text', 'select', 'checkbox', 'textarea' ); |
| 31 | |
| 32 | $default_props = array( |
| 33 | 'id' => '', |
| 34 | 'name' => '', |
| 35 | 'class' => '', |
| 36 | 'value' => '', |
| 37 | 'list' => array(), |
| 38 | 'type' => 'text', |
| 39 | 'required' => '', |
| 40 | 'placeholder' => '', |
| 41 | 'rows' => '', |
| 42 | 'cols' => '', |
| 43 | 'helper' => '', |
| 44 | 'tooltip' => '', |
| 45 | 'before_text' => '', |
| 46 | 'after_text' => '', |
| 47 | 'custom' => '' |
| 48 | ); |
| 49 | |
| 50 | if( !in_array( $field_type, $fields ) ){ |
| 51 | return ''; |
| 52 | } |
| 53 | |
| 54 | $params = Shortcoder::set_defaults( $params, $default_props ); |
| 55 | $params = self::clean_attr( $params ); |
| 56 | $field_html = ''; |
| 57 | |
| 58 | extract( $params, EXTR_SKIP ); |
| 59 | |
| 60 | $id_attr = empty( $id ) ? '' : 'id="' . $id . '"'; |
| 61 | |
| 62 | switch( $field_type ){ |
| 63 | case 'text': |
| 64 | $field_html = "<input type='$type' class='$class' $id_attr name='$name' value='$value' placeholder='$placeholder' " . ( $required ? "required='$required'" : "" ) . " $custom />"; |
| 65 | break; |
| 66 | |
| 67 | case 'select': |
| 68 | $field_html .= "<select name='$name' class='$class' $id_attr $custom>"; |
| 69 | foreach( $list as $k => $v ){ |
| 70 | $field_html .= "<option value='$k'" . selected( $value, $k, false ) . ">$v</option>"; |
| 71 | } |
| 72 | $field_html .= "</select>"; |
| 73 | break; |
| 74 | |
| 75 | case 'textarea': |
| 76 | $field_html .= "<textarea $id_attr name='$name' class='$class' placeholder='$placeholder' rows='$rows' cols='$cols' $custom>$value</textarea>"; |
| 77 | break; |
| 78 | |
| 79 | case 'checkbox': |
| 80 | $field_html .= '<div class="radios_wrap">'; |
| 81 | foreach( $list as $k => $v ){ |
| 82 | $checked = in_array( $k, $value ) ? ' checked="checked"' : ''; |
| 83 | $field_html .= "<label class='lbl_margin' $custom><input type='checkbox' name='{$name}[]' class='$class' value='$k' $id_attr $checked /> $v </label>"; |
| 84 | } |
| 85 | $field_html .= '</div>'; |
| 86 | break; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | if( !empty( $tooltip ) ){ |
| 91 | $field_html .= "<div class='sc-tt'><span class='dashicons dashicons-editor-help'></span><span class='sc-tt-text'>$tooltip</span></div>"; |
| 92 | } |
| 93 | |
| 94 | if( !empty( $helper ) ){ |
| 95 | $field_html .= "<p class='description'>$helper</p>"; |
| 96 | } |
| 97 | |
| 98 | return $field_html; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | public static function clean_attr( $a ){ |
| 103 | |
| 104 | foreach( $a as $k=>$v ){ |
| 105 | if( is_array( $v ) ){ |
| 106 | $a[ $k ] = self::clean_attr( $v ); |
| 107 | }else{ |
| 108 | |
| 109 | if( in_array( $k, array( 'custom', 'tooltip', 'helper', 'before_text', 'after_text' ) ) ){ |
| 110 | $a[ $k ] = wp_kses_post( $v ); |
| 111 | }else{ |
| 112 | $a[ $k ] = esc_attr( $v ); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return $a; |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | ?> |