| 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 |
$id = self::get_id( $name ); |
| 13 |
$name = self::get_name( $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 = [] ): void { |
| 18 |
self::check_name( $name ); |
| 19 |
$value = self::get_value( $name, $default ); |
| 20 |
$id = self::get_id( $name ); |
| 21 |
$name = self::get_name( $name ); |
| 22 |
echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '">'; |
| 23 |
foreach ( $options as $key => $val ) { |
| 24 |
|
| 25 |
if ( strrpos( $key, '_start' ) ) { |
| 26 |
echo '<optgroup label="' . esc_attr( $val ) . '">'; |
| 27 |
} elseif ( strrpos( $key, '_end' ) ) { |
| 28 |
echo '</optgroup>'; |
| 29 |
} else { |
| 30 |
echo '<option value="' . esc_attr( $key ) . '"' . selected( $value, $key, false ) . '>' . esc_html( $val ) . '</option>'; |
| 31 |
} |
| 32 |
} |
| 33 |
echo '</select>'; |
| 34 |
} |
| 35 |
|
| 36 |
public static function text( $name = '', $default = '', $type = 'text' ): void { |
| 37 |
self::check_name( $name ); |
| 38 |
$value = self::get_value( $name, $default ); |
| 39 |
$id = self::get_id( $name ); |
| 40 |
$name = self::get_name( $name ); |
| 41 |
echo '<input type="' . esc_attr( $type ) . '" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . esc_attr( $value ) . '">'; |
| 42 |
} |
| 43 |
|
| 44 |
public static function checkbox( $name = '' ): void { |
| 45 |
self::check_name( $name ); |
| 46 |
$value = self::get_value( $name ); |
| 47 |
$id = self::get_id( $name ); |
| 48 |
$name = self::get_name( $name ); |
| 49 |
echo '<input type="checkbox" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="1"' . checked( $value, "1", false ) . '>'; |
| 50 |
} |
| 51 |
|
| 52 |
private static function get_name( $name ) { |
| 53 |
if ( strpos( $name, '[' ) !== false ) { |
| 54 |
return 'param' . $name; |
| 55 |
} |
| 56 |
|
| 57 |
return $name; |
| 58 |
} |
| 59 |
|
| 60 |
private static function get_value( $name, $defval = '' ) { |
| 61 |
|
| 62 |
$default = self::getDefault(); |
| 63 |
|
| 64 |
if ( strpos( $name, '[' ) !== false ) { |
| 65 |
|
| 66 |
$value = self::get_param_value( 'param' . $name, $default ); |
| 67 |
if ( ! isset( $value ) && ! empty( $defval ) ) { |
| 68 |
return $defval; |
| 69 |
} |
| 70 |
|
| 71 |
return $value; |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
if ( empty( $default[ $name ] ) && ! empty( $defval ) ) { |
| 76 |
return $defval; |
| 77 |
} |
| 78 |
|
| 79 |
return $default[ $name ]; |
| 80 |
} |
| 81 |
|
| 82 |
private static function get_id( $name ) { |
| 83 |
return str_replace( array( '][', '[', ']' ), array( '_', '', '' ), $name ); |
| 84 |
} |
| 85 |
|
| 86 |
private static function get_param_value( $name, $array ) { |
| 87 |
$keys = preg_split( '/\[|\]/', $name, - 1, PREG_SPLIT_NO_EMPTY ); |
| 88 |
$value = $array; |
| 89 |
|
| 90 |
foreach ( $keys as $key ) { |
| 91 |
if ( isset( $value[ $key ] ) ) { |
| 92 |
$value = $value[ $key ]; |
| 93 |
} else { |
| 94 |
return null; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $value; |
| 99 |
} |
| 100 |
|
| 101 |
private static function check_name( $name ) { |
| 102 |
if ( empty( $name ) ) { |
| 103 |
wp_die( __( 'Field must have name', 'wpcoder' ) ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public static function getDefault(): array { |
| 108 |
$id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0; |
| 109 |
$columns = DBManager::get_columns(); |
| 110 |
|
| 111 |
if ( empty( $id ) ) { |
| 112 |
return self::get_data(); |
| 113 |
} |
| 114 |
|
| 115 |
$action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : 'update'; |
| 116 |
$result = DBManager::get_data_by_id( $id ); |
| 117 |
|
| 118 |
if ( $action === 'update' ) { |
| 119 |
|
| 120 |
return self::get_data( $result ); |
| 121 |
} |
| 122 |
|
| 123 |
$data = self::get_data( $result ); |
| 124 |
$data['id'] = ''; |
| 125 |
$data['title'] = ''; |
| 126 |
|
| 127 |
return $data; |
| 128 |
} |
| 129 |
|
| 130 |
private static function get_data( $result = '' ): array { |
| 131 |
$columns = DBManager::get_columns(); |
| 132 |
$data = []; |
| 133 |
foreach ( $columns as $column ) { |
| 134 |
$name = $column->Field; |
| 135 |
if ( empty( $result ) ) { |
| 136 |
if ( $name === 'param' ) { |
| 137 |
$data[ $name ] = []; |
| 138 |
continue; |
| 139 |
} |
| 140 |
$data[ $name ] = ''; |
| 141 |
} else { |
| 142 |
$val = ! empty( $result->$name ) ? $result->$name : ''; |
| 143 |
if ( $name === 'param' ) { |
| 144 |
$data[ $name ] = maybe_unserialize( $val ); |
| 145 |
continue; |
| 146 |
} |
| 147 |
$data[ $name ] = $val; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return $data; |
| 152 |
} |
| 153 |
} |