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