PluginProbe
Bricksable for Bricks Builder / 1.3.0
Bricksable for Bricks Builder v1.3.0
1.2.9 1.3.0 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.31 1.4.32 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 All 132 releases
bricksable / includes / lib / class-bricksable-admin-api.php

class-bricksable-admin-api.php in Bricksable for Bricks Builder 1.3.0, at includes/lib/class-bricksable-admin-api.php

360 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post type Admin API file.
4 *
5 * @package Bricksable/Includes
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Admin API class.
14 */
15 class Bricksable_Admin_API {
16
17 /**
18 * Constructor function
19 */
20 public function __construct() {
21 add_action( 'save_post', array( $this, 'save_meta_boxes' ), 10, 1 );
22 }
23
24 /**
25 * Generate HTML for displaying fields.
26 *
27 * @param array $data Data array.
28 * @param object $post Post object.
29 * @param boolean $echo Whether to echo the field HTML or return it.
30 * @return string
31 */
32 public function display_field( $data = array(), $post = null, $echo = true ) {
33
34 // Get field info.
35 if ( isset( $data['field'] ) ) {
36 $field = $data['field'];
37 } else {
38 $field = $data;
39 }
40
41 // Check for prefix on option name.
42 $option_name = '';
43 if ( isset( $data['prefix'] ) ) {
44 $option_name = $data['prefix'];
45 }
46
47 // Get saved data.
48 $data = '';
49 if ( $post ) {
50
51 // Get saved field data.
52 $option_name .= $field['id'];
53 $option = get_post_meta( $post->ID, $field['id'], true );
54
55 // Get data to display in field.
56 if ( isset( $option ) ) {
57 $data = $option;
58 }
59 } else {
60
61 // Get saved option.
62 $option_name .= $field['id'];
63 $option = get_option( $option_name );
64
65 // Get data to display in field.
66 if ( isset( $option ) ) {
67 $data = $option;
68 }
69 }
70
71 // Show default data if no option saved and default is supplied.
72 if ( false === $data && isset( $field['default'] ) ) {
73 $data = $field['default'];
74 } elseif ( false === $data ) {
75 $data = '';
76 }
77
78 $html = '';
79
80 switch ( $field['type'] ) {
81
82 case 'text':
83 case 'url':
84 case 'email':
85 $html .= '<input id="' . esc_attr( $field['id'] ) . '" type="text" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . esc_attr( $data ) . '" />' . "\n";
86 break;
87
88 case 'password':
89 case 'number':
90 case 'hidden':
91 $min = '';
92 if ( isset( $field['min'] ) ) {
93 $min = ' min="' . esc_attr( $field['min'] ) . '"';
94 }
95
96 $max = '';
97 if ( isset( $field['max'] ) ) {
98 $max = ' max="' . esc_attr( $field['max'] ) . '"';
99 }
100 $html .= '<input id="' . esc_attr( $field['id'] ) . '" type="' . esc_attr( $field['type'] ) . '" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . esc_attr( $data ) . '"' . $min . '' . $max . '/>' . "\n";
101 break;
102
103 case 'text_secret':
104 $html .= '<input id="' . esc_attr( $field['id'] ) . '" type="text" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="" />' . "\n";
105 break;
106
107 case 'textarea':
108 $html .= '<textarea id="' . esc_attr( $field['id'] ) . '" rows="5" cols="50" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '">' . $data . '</textarea><br/>' . "\n";
109 break;
110
111 case 'checkbox':
112 $checked = '';
113 if ( $data && 'on' === $data ) {
114 $checked = 'checked="checked"';
115 }
116 $html .= '<input id="' . esc_attr( $field['id'] ) . '" type="' . esc_attr( $field['type'] ) . '" name="' . esc_attr( $option_name ) . '" ' . $checked . '/>' . "\n";
117 break;
118
119 case 'checkbox_multi':
120 foreach ( $field['options'] as $k => $v ) {
121 $checked = false;
122 if ( in_array( $k, (array) $data, true ) ) {
123 $checked = true;
124 }
125 $html .= '<p><label for="' . esc_attr( $field['id'] . '_' . $k ) . '" class="checkbox_multi"><input type="checkbox" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '[]" value="' . esc_attr( $k ) . '" id="' . esc_attr( $field['id'] . '_' . $k ) . '" /> ' . $v . '</label></p> ';
126 }
127 break;
128
129 case 'radio':
130 foreach ( $field['options'] as $k => $v ) {
131 $checked = false;
132 if ( $k === $data ) {
133 $checked = true;
134 }
135 $html .= '<label for="' . esc_attr( $field['id'] . '_' . $k ) . '"><input type="radio" ' . checked( $checked, true, false ) . ' name="' . esc_attr( $option_name ) . '" value="' . esc_attr( $k ) . '" id="' . esc_attr( $field['id'] . '_' . $k ) . '" /> ' . $v . '</label> ';
136 }
137 break;
138
139 case 'select':
140 $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '">';
141 foreach ( $field['options'] as $k => $v ) {
142 $selected = false;
143 if ( $k === $data ) {
144 $selected = true;
145 }
146 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '">' . $v . '</option>';
147 }
148 $html .= '</select> ';
149 break;
150
151 case 'select_multi':
152 $html .= '<select name="' . esc_attr( $option_name ) . '[]" id="' . esc_attr( $field['id'] ) . '" multiple="multiple">';
153 foreach ( $field['options'] as $k => $v ) {
154 $selected = false;
155 if ( in_array( $k, (array) $data, true ) ) {
156 $selected = true;
157 }
158 $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '">' . $v . '</option>';
159 }
160 $html .= '</select> ';
161 break;
162
163 case 'image':
164 $image_thumb = '';
165 if ( $data ) {
166 $image_thumb = wp_get_attachment_thumb_url( $data );
167 }
168 $html .= '<img id="' . $option_name . '_preview" class="image_preview" src="' . $image_thumb . '" /><br/>' . "\n";
169 $html .= '<input id="' . $option_name . '_button" type="button" data-uploader_title="' . __( 'Upload an image', 'bricksable' ) . '" data-uploader_button_text="' . __( 'Use image', 'bricksable' ) . '" class="image_upload_button button" value="' . __( 'Upload new image', 'bricksable' ) . '" />' . "\n";
170 $html .= '<input id="' . $option_name . '_delete" type="button" class="image_delete_button button" value="' . __( 'Remove image', 'bricksable' ) . '" />' . "\n";
171 $html .= '<input id="' . $option_name . '" class="image_data_field" type="hidden" name="' . $option_name . '" value="' . $data . '"/><br/>' . "\n";
172 break;
173
174 case 'color':
175 //phpcs:disable
176 ?><div class="color-picker" style="position:relative;">
177 <input type="text" name="<?php esc_attr_e( $option_name ); ?>" class="color" value="<?php esc_attr_e( $data ); ?>" />
178 <div style="position:absolute;background:#FFF;z-index:99;border-radius:100%;" class="colorpicker"></div>
179 </div>
180 <?php
181 //phpcs:enable
182 break;
183
184 case 'editor':
185 wp_editor(
186 $data,
187 $option_name,
188 array(
189 'textarea_name' => $option_name,
190 )
191 );
192 break;
193
194 }
195
196 switch ( $field['type'] ) {
197
198 case 'checkbox_multi':
199 case 'radio':
200 case 'select_multi':
201 $html .= '<br/><span class="description">' . $field['description'] . '</span>';
202 break;
203
204 default:
205 if ( ! $post ) {
206 $html .= '<label for="' . esc_attr( $field['id'] ) . '">' . "\n";
207 }
208
209 $html .= '<span class="description">' . $field['description'] . '</span>' . "\n";
210
211 if ( ! $post ) {
212 $html .= '</label>' . "\n";
213 }
214 break;
215 }
216
217 if ( ! $echo ) {
218 return $html;
219 }
220
221 $sanitation = new Bricksable_Settings( $this );
222 echo wp_kses( $html, $sanitation->allowed_htmls );
223
224 }
225
226 /**
227 * Validate form field
228 *
229 * @param string $data Submitted value.
230 * @param string $type Type of field to validate.
231 * @return string Validated value
232 */
233 public function validate_field( $data = '', $type = 'text' ) {
234
235 switch ( $type ) {
236 case 'text':
237 $data = sanitize_text_field( $data );
238 break;
239 case 'url':
240 $data = esc_url( $data );
241 break;
242 case 'email':
243 $data = sanitize_email( $data );
244 break;
245 }
246
247 return $data;
248 }
249
250 /**
251 * Add meta box to the dashboard.
252 *
253 * @param string $id Unique ID for metabox.
254 * @param string $title Display title of metabox.
255 * @param array $post_types Post types to which this metabox applies.
256 * @param string $context Context in which to display this metabox ('advanced' or 'side').
257 * @param string $priority Priority of this metabox ('default', 'low' or 'high').
258 * @param array $callback_args Any axtra arguments that will be passed to the display function for this metabox.
259 * @return void
260 */
261 public function add_meta_box( $id = '', $title = '', $post_types = array(), $context = 'advanced', $priority = 'default', $callback_args = null ) {
262
263 // Get post type(s).
264 if ( ! is_array( $post_types ) ) {
265 $post_types = array( $post_types );
266 }
267
268 // Generate each metabox.
269 foreach ( $post_types as $post_type ) {
270 add_meta_box( $id, $title, array( $this, 'meta_box_content' ), $post_type, $context, $priority, $callback_args );
271 }
272 }
273
274 /**
275 * Display metabox content
276 *
277 * @param object $post Post object.
278 * @param array $args Arguments unique to this metabox.
279 * @return void
280 */
281 public function meta_box_content( $post, $args ) {
282
283 $fields = apply_filters( $post->post_type . '_custom_fields', array(), $post->post_type );
284
285 if ( ! is_array( $fields ) || 0 === count( $fields ) ) {
286 return;
287 }
288
289 echo '<div class="custom-field-panel">' . "\n";
290
291 foreach ( $fields as $field ) {
292
293 if ( ! isset( $field['metabox'] ) ) {
294 continue;
295 }
296
297 if ( ! is_array( $field['metabox'] ) ) {
298 $field['metabox'] = array( $field['metabox'] );
299 }
300
301 if ( in_array( $args['id'], $field['metabox'], true ) ) {
302 $this->display_meta_box_field( $field, $post );
303 }
304 }
305
306 echo '</div>' . "\n";
307
308 }
309
310
311 /**
312 * Dispay field in metabox
313 *
314 * @param array $field Field data.
315 * @param object $post Post object.
316 * @return void
317 */
318 public function display_meta_box_field( $field = array(), $post = null ) {
319
320 if ( ! is_array( $field ) || 0 === count( $field ) ) {
321 return;
322 }
323
324 $field = '<p class="form-field"><label for="' . $field['id'] . '">' . $field['label'] . '</label>' . $this->display_field( $field, $post, false ) . '</p>' . "\n";
325
326 $sanitation = new Bricksable_Settings( $this );
327 echo wp_kses( $field, $sanitation->allowed_htmls );
328 }
329
330 /**
331 * Save metabox fields.
332 *
333 * @param integer $post_id Post ID.
334 * @return void
335 */
336 public function save_meta_boxes( $post_id = 0 ) {
337
338 if ( ! $post_id ) {
339 return;
340 }
341
342 $post_type = get_post_type( $post_id );
343
344 $fields = apply_filters( $post_type . '_custom_fields', array(), $post_type );
345
346 if ( ! is_array( $fields ) || 0 === count( $fields ) ) {
347 return;
348 }
349
350 foreach ( $fields as $field ) {
351 if ( isset( $_REQUEST[ $field['id'] ] ) ) { //phpcs:ignore
352 update_post_meta( $post_id, $field['id'], $this->validate_field( $_REQUEST[ $field['id'] ], $field['type'] ) ); //phpcs:ignore
353 } else {
354 update_post_meta( $post_id, $field['id'], '' );
355 }
356 }
357 }
358
359 }
360