| @@ -8,73 +8,50 @@ | ||
| 8 | 8 | |
| 9 | 9 | public static function textarea( $name = '', $default = '' ): void { |
| 10 | 10 | self::check_name( $name ); |
| 11 | 11 | $value = self::get_value( $name, $default ); |
| 12 | + $id = self::get_id( $name ); | |
| 12 | 13 | $name = self::get_name( $name ); |
| 13 | - $id = self::get_id( $name ); | |
| 14 | 14 | echo '<textarea name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '">' . esc_html( $value ) . '</textarea>'; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | - public static function select( $name = '', $default = '', $options = [], $order = '' ): void { | |
| 17 | + public static function select( $name = '', $default = '', $options = [] ): void { | |
| 18 | 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 ); | |
| 19 | + $value = self::get_value( $name, $default ); | |
| 20 | + $id = self::get_id( $name ); | |
| 21 | + $name = self::get_name( $name ); | |
| 26 | 22 | echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '">'; |
| 27 | 23 | foreach ( $options as $key => $val ) { |
| 24 | + | |
| 28 | 25 | if ( strrpos( $key, '_start' ) ) { |
| 29 | 26 | echo '<optgroup label="' . esc_attr( $val ) . '">'; |
| 30 | 27 | } elseif ( strrpos( $key, '_end' ) ) { |
| 31 | 28 | echo '</optgroup>'; |
| 32 | 29 | } else { |
| 33 | - echo '<option value="' . esc_attr( $key ) . '"' . selected( $value, $key, | |
| 34 | - false ) . '>' . esc_html( $val ) . '</option>'; | |
| 30 | + echo '<option value="' . esc_attr( $key ) . '"' . selected( $value, $key, false ) . '>' . esc_html( $val ) . '</option>'; | |
| 35 | 31 | } |
| 36 | 32 | } |
| 37 | 33 | echo '</select>'; |
| 38 | 34 | } |
| 39 | 35 | |
| 40 | - public static function text( $name = '', $default = '', $type = 'text', $order = '' ): void { | |
| 36 | + public static function text( $name = '', $default = '', $type = 'text' ): void { | |
| 41 | 37 | 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 | - } | |
| 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 ) . '">'; | |
| 55 | 42 | } |
| 56 | 43 | |
| 57 | - public static function checkbox( $name = '', $order = '' ): void { | |
| 44 | + public static function checkbox( $name = '' ): void { | |
| 58 | 45 | 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 ) . '">'; | |
| 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 ) . '>'; | |
| 69 | 50 | } |
| 70 | 51 | |
| 71 | - private static function get_name( $name, $order = '' ) { | |
| 52 | + private static function get_name( $name ) { | |
| 72 | 53 | if ( strpos( $name, '[' ) !== false ) { |
| 73 | - if ( is_numeric( $order ) ) { | |
| 74 | - return 'param' . $name . '[]'; | |
| 75 | - } | |
| 76 | - | |
| 77 | 54 | return 'param' . $name; |
| 78 | 55 | } |
| 79 | 56 | |
| 80 | 57 | return $name; |
| @@ -80,11 +57,13 @@ | ||
| 80 | 57 | return $name; |
| 81 | 58 | } |
| 82 | 59 | |
| 83 | 60 | private static function get_value( $name, $defval = '' ) { |
| 61 | + | |
| 84 | 62 | $default = self::getDefault(); |
| 85 | 63 | |
| 86 | 64 | if ( strpos( $name, '[' ) !== false ) { |
| 65 | + | |
| 87 | 66 | $value = self::get_param_value( 'param' . $name, $default ); |
| 88 | 67 | if ( ! isset( $value ) && ! empty( $defval ) ) { |
| 89 | 68 | return $defval; |
| 90 | 69 | } |
| @@ -89,8 +68,9 @@ | ||
| 89 | 68 | return $defval; |
| 90 | 69 | } |
| 91 | 70 | |
| 92 | 71 | return $value; |
| 72 | + | |
| 93 | 73 | } |
| 94 | 74 | |
| 95 | 75 | if ( empty( $default[ $name ] ) && ! empty( $defval ) ) { |
| 96 | 76 | return $defval; |
| @@ -95,16 +75,12 @@ | ||
| 95 | 75 | if ( empty( $default[ $name ] ) && ! empty( $defval ) ) { |
| 96 | 76 | return $defval; |
| 97 | 77 | } |
| 98 | 78 | |
| 99 | - if ( empty( $default[ $name ] ) ) { | |
| 100 | - return ''; | |
| 101 | - } | |
| 102 | - | |
| 103 | 79 | return $default[ $name ]; |
| 104 | 80 | } |
| 105 | 81 | |
| 106 | - public static function get_id( $name ) { | |
| 82 | + private static function get_id( $name ) { | |
| 107 | 83 | return str_replace( array( '][', '[', ']' ), array( '_', '', '' ), $name ); |
| 108 | 84 | } |
| 109 | 85 | |
| 110 | 86 | private static function get_param_value( $name, $array ) { |
| @@ -123,9 +99,9 @@ | ||
| 123 | 99 | } |
| 124 | 100 | |
| 125 | 101 | private static function check_name( $name ) { |
| 126 | 102 | if ( empty( $name ) ) { |
| 127 | - wp_die( __( 'Field must have name', 'floating-button' ) ); | |
| 103 | + wp_die( __( 'Field must have name', 'wpcoder' ) ); | |
| 128 | 104 | } |
| 129 | 105 | } |
| 130 | 106 | |
| 131 | 107 | public static function getDefault(): array { |
| @@ -139,8 +115,9 @@ | ||
| 139 | 115 | $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : 'update'; |
| 140 | 116 | $result = DBManager::get_data_by_id( $id ); |
| 141 | 117 | |
| 142 | 118 | if ( $action === 'update' ) { |
| 119 | + | |
| 143 | 120 | return self::get_data( $result ); |
| 144 | 121 | } |
| 145 | 122 | |
| 146 | 123 | $data = self::get_data( $result ); |
| @@ -171,14 +148,6 @@ | ||
| 171 | 148 | } |
| 172 | 149 | } |
| 173 | 150 | |
| 174 | 151 | 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 | 152 | } |
| 184 | 153 | } |