PluginProbe
Floating Button – Easily Create Sticky, Fixed & Floating Buttons / 6.0.11
Floating Button – Easily Create Sticky, Fixed & Floating Buttons v6.0.11
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.11, at classes/Dashboard/Field.php

179 lines 5.0 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 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 ) . '">';
67 echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" class="checkbox-helper" id="' . esc_attr( $id ) . '">';
68 }
69
70 private static function get_name( $name, $order = '' ) {
71 if ( strpos( $name, '[' ) !== false ) {
72 if ( is_numeric( $order ) ) {
73 return 'param' . $name . '[]';
74 }
75
76 return 'param' . $name;
77 }
78
79 return $name;
80 }
81
82 private static function get_value( $name, $defval = '' ) {
83 $default = self::getDefault();
84
85 if ( strpos( $name, '[' ) !== false ) {
86 $value = self::get_param_value( 'param' . $name, $default );
87 if ( ! isset( $value ) && ! empty( $defval ) ) {
88 return $defval;
89 }
90
91 return $value;
92 }
93
94 if ( empty( $default[ $name ] ) && ! empty( $defval ) ) {
95 return $defval;
96 }
97
98 return $default[ $name ];
99 }
100
101 public static function get_id( $name ) {
102 return str_replace( array( '][', '[', ']' ), array( '_', '', '' ), $name );
103 }
104
105 private static function get_param_value( $name, $array ) {
106 $keys = preg_split( '/\[|\]/', $name, - 1, PREG_SPLIT_NO_EMPTY );
107 $value = $array;
108
109 foreach ( $keys as $key ) {
110 if ( isset( $value[ $key ] ) ) {
111 $value = $value[ $key ];
112 } else {
113 return null;
114 }
115 }
116
117 return $value;
118 }
119
120 private static function check_name( $name ) {
121 if ( empty( $name ) ) {
122 wp_die( esc_attr__( 'Field must have name', 'floating-button' ) );
123 }
124 }
125
126 public static function getDefault(): array {
127 $id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
128 $columns = DBManager::get_columns();
129
130 if ( empty( $id ) ) {
131 return self::get_data();
132 }
133
134 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : 'update';
135 $result = DBManager::get_data_by_id( $id );
136
137 if ( $action === 'update' ) {
138 return self::get_data( $result );
139 }
140
141 $data = self::get_data( $result );
142 $data['id'] = '';
143 $data['title'] = '';
144
145 return $data;
146 }
147
148 private static function get_data( $result = '' ): array {
149 $columns = DBManager::get_columns();
150 $data = [];
151 foreach ( $columns as $column ) {
152 $name = $column->Field;
153 if ( empty( $result ) ) {
154 if ( $name === 'param' ) {
155 $data[ $name ] = [];
156 continue;
157 }
158 $data[ $name ] = '';
159 } else {
160 $val = ! empty( $result->$name ) ? $result->$name : '';
161 if ( $name === 'param' ) {
162 $data[ $name ] = maybe_unserialize( $val );
163 continue;
164 }
165 $data[ $name ] = $val;
166 }
167 }
168
169 return $data;
170 }
171
172 public static function add_prefix( $prefix, $arr ): array {
173 foreach ( $arr as $key => $val ) {
174 $arr[ $key ]['name'] = $prefix . $val['name'];
175 }
176
177 return $arr;
178 }
179 }