PluginProbe
Floating Button – Easily Create Sticky, Fixed & Floating Buttons / 6.0.3
Floating Button – Easily Create Sticky, Fixed & Floating Buttons v6.0.3
trunk 2.0.2 3.0 4.0 4.1.2 4.3 5.0 5.0.1 5.1 5.1.1 5.2 5.3 5.3.1 6.0 6.0.1 6.0.10 6.0.11 6.0.12 6.0.13 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 All 31 releases
floating-button / classes / Dashboard / Field.php

Field.php in Floating Button – Easily Create Sticky, Fixed & Floating Buttons 6.0.3, at classes/Dashboard/Field.php

183 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FloatingButton\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
29 if ( strrpos( $key, '_start' ) ) {
30 echo '<optgroup label="' . esc_attr( $val ) . '">';
31 } elseif ( strrpos( $key, '_end' ) ) {
32 echo '</optgroup>';
33 } else {
34 echo '<option value="' . esc_attr( $key ) . '"' . selected( $value, $key, 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
53 } else {
54 echo '<input type="text" data-alpha-enabled="true" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" class="' . esc_attr( $class ) . '" id="'.esc_attr($id).'">';
55
56 }
57 }
58
59 public static function checkbox( $name = '', $order = '' ): void {
60 self::check_name( $name );
61 $pre_name = $name;
62 if ( is_numeric( $order ) ) {
63 $pre_name = $name . '[' . $order . ']';
64 }
65 $value = self::get_value( $pre_name );
66 $name = self::get_name( $name , $order);
67 $id = self::get_id( $pre_name );
68 echo '<input type="checkbox" value="1" id="checkbox_'.esc_attr($id).'">';
69 echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="'.esc_attr($value).'" class="checkbox-helper" id="'.esc_attr($id).'">';
70 }
71
72 private static function get_name( $name, $order = '' ) {
73 if ( strpos( $name, '[' ) !== false ) {
74 if ( is_numeric( $order ) ) {
75 return 'param' . $name . '[]';
76 }
77 return 'param' . $name;
78 }
79
80 return $name;
81 }
82
83 private static function get_value( $name, $defval = '' ) {
84
85 $default = self::getDefault();
86
87 if ( strpos( $name, '[' ) !== false ) {
88
89 $value = self::get_param_value( 'param' . $name, $default );
90 if ( ! isset( $value ) && ! empty( $defval ) ) {
91 return $defval;
92 }
93
94 return $value;
95
96 }
97
98 if ( empty( $default[ $name ] ) && ! empty( $defval ) ) {
99 return $defval;
100 }
101
102 return $default[ $name ];
103 }
104
105 public static function get_id( $name ) {
106 return str_replace( array( '][', '[', ']' ), array( '_', '', '' ), $name );
107 }
108
109 private static function get_param_value( $name, $array ) {
110 $keys = preg_split( '/\[|\]/', $name, - 1, PREG_SPLIT_NO_EMPTY );
111 $value = $array;
112
113 foreach ( $keys as $key ) {
114 if ( isset( $value[ $key ] ) ) {
115 $value = $value[ $key ];
116 } else {
117 return null;
118 }
119 }
120
121 return $value;
122 }
123
124 private static function check_name( $name ) {
125 if ( empty( $name ) ) {
126 wp_die( esc_attr__( 'Field must have name', 'floating-button' ) );
127 }
128 }
129
130 public static function getDefault(): array {
131 $id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
132 $columns = DBManager::get_columns();
133
134 if ( empty( $id ) ) {
135 return self::get_data();
136 }
137
138 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : 'update';
139 $result = DBManager::get_data_by_id( $id );
140
141 if ( $action === 'update' ) {
142
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 return $arr;
182 }
183 }