PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.1
WP Coder – Insert & Manage Code Snippets v4.1
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / classes / Dashboard / Field.php

Field.php in WP Coder – Insert & Manage Code Snippets 4.1, at classes/Dashboard/Field.php

198 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $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 radio( $name = '', $default = '', $options = [], $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 foreach ( $options as $key => $val ) {
50 echo '<input type="radio" name="' . esc_attr( $name ) . '" value="' . esc_attr( $key ) . '"' . checked( $value, $key, false ) . ' id="' . esc_attr( $id ) . '_' . esc_attr($key) . '">';
51 echo '<label for="' . esc_attr( $id ) . '_' . esc_attr($key) . '">' . esc_html( $val ) . '</label>';
52 }
53 }
54
55 public static function text( $name = '', $default = '', $type = 'text', $order = '' ): void {
56 self::check_name( $name );
57 $pre_name = $name;
58 if ( is_numeric( $order ) ) {
59 $pre_name = $name . '[' . $order . ']';
60 }
61 $value = self::get_value( $pre_name, $default );
62 $name = self::get_name( $name , $order);
63 $id = self::get_id( $pre_name );
64 $class = ( $type === 'color' ) ? 'wowp-field-color' : '';
65 if ( empty( $class ) ) {
66 echo '<input type="' . esc_attr( $type ) . '" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" id="'.esc_attr($id).'">';
67
68 } else {
69 echo '<input type="text" data-alpha-enabled="true" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" class="' . esc_attr( $class ) . '" id="'.esc_attr($id).'">';
70
71 }
72 }
73
74 public static function checkbox( $name = '', $order = '' ): void {
75 self::check_name( $name );
76 $pre_name = $name;
77 if ( is_numeric( $order ) ) {
78 $pre_name = $name . '[' . $order . ']';
79 }
80 $value = self::get_value( $pre_name );
81 $name = self::get_name( $name , $order);
82 $id = self::get_id( $pre_name );
83 echo '<input type="checkbox" value="1" id="checkbox_'.esc_attr($id).'"' . checked( '1', $value, false ) . '>';
84 echo '<input type="hidden" name="' . esc_attr( $name ) . '" value="'.esc_attr($value).'" class="checkbox-helper" id="'.esc_attr($id).'">';
85 }
86
87 private static function get_name( $name, $order = '' ) {
88 if ( strpos( $name, '[' ) !== false ) {
89 if ( is_numeric( $order ) ) {
90 return 'param' . $name . '[]';
91 }
92 return 'param' . $name;
93 }
94
95 return $name;
96 }
97
98 private static function get_value( $name, $defval = '' ) {
99
100 $default = self::getDefault();
101
102 if ( strpos( $name, '[' ) !== false ) {
103
104 $value = self::get_param_value( 'param' . $name, $default );
105 if ( ! isset( $value ) && ! empty( $defval ) ) {
106 return $defval;
107 }
108
109 return $value;
110
111 }
112
113 if ( empty( $default[ $name ] ) && ! empty( $defval ) ) {
114 return $defval;
115 }
116
117 return $default[ $name ];
118 }
119
120 public static function get_id( $name ) {
121 return str_replace( array( '][', '[', ']' ), array( '_', '', '' ), $name );
122 }
123
124 private static function get_param_value( $name, $array ) {
125 $keys = preg_split( '/\[|\]/', $name, - 1, PREG_SPLIT_NO_EMPTY );
126 $value = $array;
127
128 foreach ( $keys as $key ) {
129 if ( isset( $value[ $key ] ) ) {
130 $value = $value[ $key ];
131 } else {
132 return null;
133 }
134 }
135
136 return $value;
137 }
138
139 private static function check_name( $name ) {
140 if ( empty( $name ) ) {
141 wp_die( esc_attr__( 'Field must have name', 'wp-coder' ) );
142 }
143 }
144
145 public static function getDefault(): array {
146 $id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
147 $columns = DBManager::get_columns();
148
149 if ( empty( $id ) ) {
150 return self::get_data();
151 }
152
153 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : 'update';
154 $result = DBManager::get_data_by_id( $id );
155
156 if ( $action === 'update' ) {
157
158 return self::get_data( $result );
159 }
160
161 $data = self::get_data( $result );
162 $data['id'] = '';
163 $data['title'] = '';
164
165 return $data;
166 }
167
168 private static function get_data( $result = '' ): array {
169 $columns = DBManager::get_columns();
170 $data = [];
171 foreach ( $columns as $column ) {
172 $name = $column->Field;
173 if ( empty( $result ) ) {
174 if ( $name === 'param' ) {
175 $data[ $name ] = [];
176 continue;
177 }
178 $data[ $name ] = '';
179 } else {
180 $val = ! empty( $result->$name ) ? $result->$name : '';
181 if ( $name === 'param' ) {
182 $data[ $name ] = maybe_unserialize( $val );
183 continue;
184 }
185 $data[ $name ] = $val;
186 }
187 }
188
189 return $data;
190 }
191
192 public static function add_prefix($prefix, $arr): array {
193 foreach ($arr as $key => $val) {
194 $arr[$key]['name'] = $prefix. $val['name'];
195 }
196 return $arr;
197 }
198 }