PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.4
WP Coder – Insert & Manage Code Snippets v3.4
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 3.4, at classes/Dashboard/Field.php

184 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 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 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 ) . '"' . 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 ) . '">';
69 }
70
71 private static function get_name( $name, $order = '' ) {
72 if ( strpos( $name, '[' ) !== false ) {
73 if ( is_numeric( $order ) ) {
74 return 'param' . $name . '[]';
75 }
76
77 return 'param' . $name;
78 }
79
80 return $name;
81 }
82
83 private static function get_value( $name, $defval = '' ) {
84 $default = self::getDefault();
85
86 if ( strpos( $name, '[' ) !== false ) {
87 $value = self::get_param_value( 'param' . $name, $default );
88 if ( ! isset( $value ) && ! empty( $defval ) ) {
89 return $defval;
90 }
91
92 return $value;
93 }
94
95 if ( empty( $default[ $name ] ) && ! empty( $defval ) ) {
96 return $defval;
97 }
98
99 if ( empty( $default[ $name ] ) ) {
100 return '';
101 }
102
103 return $default[ $name ];
104 }
105
106 public static function get_id( $name ) {
107 return str_replace( array( '][', '[', ']' ), array( '_', '', '' ), $name );
108 }
109
110 private static function get_param_value( $name, $array ) {
111 $keys = preg_split( '/\[|\]/', $name, - 1, PREG_SPLIT_NO_EMPTY );
112 $value = $array;
113
114 foreach ( $keys as $key ) {
115 if ( isset( $value[ $key ] ) ) {
116 $value = $value[ $key ];
117 } else {
118 return null;
119 }
120 }
121
122 return $value;
123 }
124
125 private static function check_name( $name ) {
126 if ( empty( $name ) ) {
127 wp_die( __( 'Field must have name', 'floating-button' ) );
128 }
129 }
130
131 public static function getDefault(): array {
132 $id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
133 $columns = DBManager::get_columns();
134
135 if ( empty( $id ) ) {
136 return self::get_data();
137 }
138
139 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( $_REQUEST['action'] ) : 'update';
140 $result = DBManager::get_data_by_id( $id );
141
142 if ( $action === 'update' ) {
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
182 return $arr;
183 }
184 }