| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Field { |
| 8 |
|
| 9 |
public static function textarea( $name = '', $default = '' ): void { |
| 10 |
self::check_name( $name ); |
| 11 |
$value = self::get_value( $name, $default ); |
| 12 |
$name = self::get_name( $name ); |
| 13 |
$id = self::get_id( $name ); |
| 14 |
echo '<textarea name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '">' . esc_html( $value ) . '</textarea>'; |
| 15 |
} |
| 16 |
|
| 17 |
public static function select( $name = '', $default = '', $options = [], $order = '' ): void { |
| 18 |
self::check_name( $name ); |
| 19 |
$pre_name = $name; |
| 20 |
if ( is_numeric( $order ) ) { |
| 21 |
$pre_name = $name . '[' . $order . ']'; |
| 22 |
} |
| 23 |
$value = self::get_value( $pre_name, $default ); |
| 24 |
$name = self::get_name( $name, $order ); |
| 25 |
$id = self::get_id( $pre_name ); |
| 26 |
echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '">'; |
| 27 |
foreach ( $options as $key => $val ) { |
| 28 |
if ( strrpos( $key, '_start' ) ) { |
| 29 |
echo '<optgroup label="' . esc_attr( $val ) . '">'; |
| 30 |
} elseif ( strrpos( $key, '_end' ) ) { |
| 31 |
echo '</optgroup>'; |
| 32 |
} else { |
| 33 |
echo '<option value="' . esc_attr( $key ) . '"' . selected( $value, $key, |
| 34 |
false ) . '>' . esc_html( $val ) . '</option>'; |
| 35 |
} |
| 36 |
} |
| 37 |
echo '</select>'; |
| 38 |
} |
| 39 |
|
| 40 |
public static function text( $name = '', $default = '', $type = 'text', $order = '' ): void { |
| 41 |
self::check_name( $name ); |
| 42 |
$pre_name = $name; |
| 43 |
if ( is_numeric( $order ) ) { |
| 44 |
$pre_name = $name . '[' . $order . ']'; |
| 45 |
} |
| 46 |
$value = self::get_value( $pre_name, $default ); |
| 47 |
$name = self::get_name( $name, $order ); |
| 48 |
$id = self::get_id( $pre_name ); |
| 49 |
$class = ( $type === 'color' ) ? 'wowp-field-color' : ''; |
| 50 |
if ( empty( $class ) ) { |
| 51 |
echo '<input type="' . esc_attr( $type ) . '" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" id="' . esc_attr( $id ) . '">'; |
| 52 |
} else { |
| 53 |
echo '<input type="text" data-alpha-enabled="true" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" class="' . esc_attr( $class ) . '" id="' . esc_attr( $id ) . '">'; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public static function checkbox( $name = '', $order = '' ): void { |
| 58 |
self::check_name( $name ); |
| 59 |
$pre_name = $name; |
| 60 |
if ( is_numeric( $order ) ) { |
| 61 |
$pre_name = $name . '[' . $order . ']'; |
| 62 |
} |
| 63 |
$value = self::get_value( $pre_name ); |
| 64 |
$name = self::get_name( $name, $order ); |
| 65 |
$id = self::get_id( $pre_name ); |
| 66 |
echo '<input type="checkbox" value="1" id="checkbox_' . esc_attr( $id ) . '"' . checked( '1', $value, |
| 67 |
false ) . '>'; |
| 68 |
echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" class="checkbox-helper" id="' . esc_attr( $id ) . '">'; |
| 69 |
} |
| 70 |
|
| 71 |
private static function get_name( $name, $order = '' ) { |
| 72 |
if ( strpos( $name, '[' ) !== false ) { |
| 73 |
if ( is_numeric( $order ) ) { |
| 74 |
return 'param' . $name . '[]'; |
| 75 |
} |
| 76 |
|
| 77 |
return 'param' . $name; |
| 78 |
} |
| 79 |
|
| 80 |
return $name; |
| 81 |
} |
| 82 |
|
| 83 |
private static function get_value( $name, $defval = '' ) { |
| 84 |
$default = self::getDefault(); |
| 85 |
|
| 86 |
if ( strpos( $name, '[' ) !== false ) { |
| 87 |
$value = self::get_param_value( 'param' . $name, $default ); |
| 88 |
if ( ! isset( $value ) && ! empty( $defval ) ) { |
| 89 |
return $defval; |
| 90 |
} |
| 91 |
|
| 92 |
return $value; |
| 93 |
} |
| 94 |
|
| 95 |
if ( empty( $default[ $name ] ) && ! empty( $defval ) ) { |
| 96 |
return $defval; |
| 97 |
} |
| 98 |
|
| 99 |
if ( empty( $default[ $name ] ) ) { |
| 100 |
return ''; |
| 101 |
} |
| 102 |
|
| 103 |
return $default[ $name ]; |
| 104 |
} |
| 105 |
|
| 106 |
public static function get_id( $name ) { |
| 107 |
return str_replace( array( '][', '[', ']' ), array( '_', '', '' ), $name ); |
| 108 |
} |
| 109 |
|
| 110 |
private static function get_param_value( $name, $array ) { |
| 111 |
$keys = preg_split( '/\[|\]/', $name, - 1, PREG_SPLIT_NO_EMPTY ); |
| 112 |
$value = $array; |
| 113 |
|
| 114 |
foreach ( $keys as $key ) { |
| 115 |
if ( isset( $value[ $key ] ) ) { |
| 116 |
$value = $value[ $key ]; |
| 117 |
} else { |
| 118 |
return null; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $value; |
| 123 |
} |
| 124 |
|
| 125 |
private static function check_name( $name ) { |
| 126 |
if ( empty( $name ) ) { |
| 127 |
wp_die( __( 'Field must have name', 'floating-button' ) ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public static function getDefault(): array { |
| 132 |
$id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0; |
| 133 |
$columns = DBManager::get_columns(); |
| 134 |
|
| 135 |
if ( empty( $id ) ) { |
| 136 |
return self::get_data(); |
| 137 |
} |
| 138 |
|
| 139 |
$action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : 'update'; |
| 140 |
$result = DBManager::get_data_by_id( $id ); |
| 141 |
|
| 142 |
if ( $action === 'update' ) { |
| 143 |
return self::get_data( $result ); |
| 144 |
} |
| 145 |
|
| 146 |
$data = self::get_data( $result ); |
| 147 |
$data['id'] = ''; |
| 148 |
$data['title'] = ''; |
| 149 |
|
| 150 |
return $data; |
| 151 |
} |
| 152 |
|
| 153 |
private static function get_data( $result = '' ): array { |
| 154 |
$columns = DBManager::get_columns(); |
| 155 |
$data = []; |
| 156 |
foreach ( $columns as $column ) { |
| 157 |
$name = $column->Field; |
| 158 |
if ( empty( $result ) ) { |
| 159 |
if ( $name === 'param' ) { |
| 160 |
$data[ $name ] = []; |
| 161 |
continue; |
| 162 |
} |
| 163 |
$data[ $name ] = ''; |
| 164 |
} else { |
| 165 |
$val = ! empty( $result->$name ) ? $result->$name : ''; |
| 166 |
if ( $name === 'param' ) { |
| 167 |
$data[ $name ] = maybe_unserialize( $val ); |
| 168 |
continue; |
| 169 |
} |
| 170 |
$data[ $name ] = $val; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $data; |
| 175 |
} |
| 176 |
|
| 177 |
public static function add_prefix( $prefix, $arr ): array { |
| 178 |
foreach ( $arr as $key => $val ) { |
| 179 |
$arr[ $key ]['name'] = $prefix . $val['name']; |
| 180 |
} |
| 181 |
|
| 182 |
return $arr; |
| 183 |
} |
| 184 |
} |