PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / assets / lib / class-mustache-templates / class-mustache-templates.php

class-mustache-templates.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at assets/lib/class-mustache-templates/class-mustache-templates.php

609 lines 23.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @param array() $mustache_vars is the array containing the names of the variable that must be proccessed by mustache in the template. The array must be in this form:
4 * array( array( 'name' => '', 'type' => '' ) ... ), and for loop tags it also contains a 'children' element which contains other mustache_vars array( array( 'name' => 'users', 'type' => 'loop_tag', 'children' => $merge_tags )
5 * @param string $template the template that needs to be procesed
6 * @param array $extra_values in this array we can pass any variables that are required for that specific implementation of the mustache processing system
7 *
8 */
9 class PB_Mustache_Generate_Template{
10 var $mustache_vars = array( );
11 var $template = '';
12 var $extra_values = array( );
13
14 /**
15 * constructor for the class
16 *
17 * @param array $mustache_vars the array of variables
18 * @param string $template the html template
19 * @param $extra_values in this array we can pass any variables that are required for that specific implementation of the mustache processing system
20 *
21 * @since 2.0.0
22 *
23 */
24 function __construct( $mustache_vars, $template, $extra_values ){
25
26 // Include Mustache Templates
27 if( !class_exists( 'Mustache_Autoloader' ) ){
28
29 if( !defined( 'WPPB_PAID_PLUGIN_DIR' ) || ( defined( 'WPPB_PAID_PLUGIN_DIR' ) && defined( 'PROFILE_BUILDER_PAID_VERSION' ) ) ){
30 if( file_exists( WPPB_PLUGIN_DIR . '/assets/lib/Mustache/Autoloader.php' ) )
31 require_once( WPPB_PLUGIN_DIR.'/assets/lib/Mustache/Autoloader.php' );
32 } else if( defined( 'WPPB_PAID_PLUGIN_DIR' ) ){
33 if( file_exists( WPPB_PAID_PLUGIN_DIR . '/assets/lib/Mustache/Autoloader.php' ) )
34 require_once( WPPB_PAID_PLUGIN_DIR.'/assets/lib/Mustache/Autoloader.php' );
35 }
36
37 }
38
39 Mustache_Autoloader::register();
40
41 $this->mustache_vars = $mustache_vars;
42 $this->template = $template;
43 $this->extra_values = $extra_values;
44
45 $this->process_mustache_vars();
46 }
47
48 /**
49 * construct the mustache_vars_array from $mustache_vars through filters
50 *
51 * @since 2.0.0
52 *
53 */
54 function process_mustache_vars(){
55 if( !empty( $this->mustache_vars ) ){
56 foreach( $this->mustache_vars as $var ){
57 foreach( $var['variables'] as $variables ){
58 if( empty( $variables['children'] ) )
59 $variables['children'] = array();
60 $this->mustache_vars_array[ $variables['name'] ] = apply_filters( 'mustache_variable_'. $variables['type'], '', $variables['name'], $variables['children'], $this->extra_values );
61 }
62 }
63 }
64 }
65
66
67 /**
68 * Process the mustache template from the html template and the processed mustache variable array.
69 *
70 * @since 2.0.0
71 *
72 * @return $content the proccessed template ready for output.
73 */
74 function process_template(){
75 $m = new Mustache_Engine;
76 try {
77 if( !empty( $this->mustache_vars_array ) ){
78 foreach( $this->mustache_vars_array as $key => $value ){
79 if( is_string( $value ) ) {
80 $this->mustache_vars_array[$key] = str_replace('[', '&#91;', $value);
81 $this->mustache_vars_array[$key] = str_replace(']', '&#93;', $value);
82 }
83 }
84 }
85 $content = do_shortcode( $m->render( $this->template, $this->mustache_vars_array ) );
86 } catch (Exception $e) {
87 $content = $e->getMessage();
88 }
89
90 return apply_filters( 'wppb_mustache_template', $content );
91 }
92
93 /**
94 * Handle toString method
95 *
96 * @since 2.0.0
97 *
98 * @return string $html html for the template.
99 */
100 public function __toString() {
101 $html = $this->process_template();
102 return "{$html}";
103 }
104
105
106 }
107
108
109 class PB_Mustache_Generate_Admin_Box{
110
111 var $id; // string meta box id
112 var $title; // string title
113 var $page; // string|array post type to add meta box to
114 var $priority;
115 var $mustache_vars;
116 var $default_value;
117
118
119 /**
120 * constructor for the class
121 *
122 * @param string $id the meta box id
123 * @param string $title the title of the metabox
124 * @param string|array $page post type to add meta box to
125 * @param string $priority the priority within the context where the boxes should show ('high', 'core', 'default' or 'low')
126 * @param array $mustache_vars is the array containing the names of the variable that must be proccessed by mustache in the template. The array must be in this form:
127 * array( array( 'name' => '', 'type' => '' ) ... ), and for loop tags it also contains a 'children' element which contains other mustache_vars array( array( 'name' => 'users', 'type' => 'loop_tag', 'children' => $merge_tags )
128 * @param string $default_value the default template that populates the codemirror box.
129 *
130 * @since 2.0.0
131 *
132 */
133 function __construct( $id, $title, $page, $priority, $mustache_vars, $default_value = '', $fields = array() ){
134
135 $this->mustache_vars = $mustache_vars;
136
137 $this->id = $id;
138 $this->title = $title;
139 $this->page = $page;
140 $this->priority = $priority;
141 $this->default_value = $default_value;
142
143 if(!is_array($this->page)) {
144 $this->page = array($this->page);
145 }
146
147 if( empty( $fields ) ){
148 $this->fields = array(
149 array( // Textarea
150 'label' => '', // <label>
151 'desc' => '', // description
152 'id' => $id, // field id and name
153 'type' => 'textarea', // type of field
154 'default' => $default_value, // type of field
155 )
156 );
157 }
158 else{
159 $this->fields = $fields;
160 }
161
162 $this->save_default_values();
163
164 if( defined( 'DOING_AJAX' ) && DOING_AJAX )
165 return;
166
167 add_action( 'add_meta_boxes', array( $this, 'add_box' ) );
168 add_action( 'save_post', array( $this, 'save_box' ), 11, 2);
169 add_action( 'wp_insert_post', array( $this, 'save_box' ), 11, 2);
170 add_action( 'admin_enqueue_scripts', array( $this, 'wppb_print_mustache_script' ) );
171 add_action( 'admin_head', array( $this, 'wppb_print_codemirror_script' ) );
172
173 add_action( 'wck_before_meta_boxes', array( $this, 'wppb_mustache_page_before' ) );
174 add_action( 'wck_after_meta_boxes', array( $this, 'wppb_mustache_page_after' ) );
175 }
176
177 /**
178 * Function that print the required scripts for the mustache templates
179 *
180 * @param string $hook the page hook
181 *
182 * @since 2.0.0
183 *
184 */
185 function wppb_print_mustache_script( $hook ){
186 if ( isset( $_GET['post_type'] ) || isset( $_GET['post'] ) || isset( $_GET['page'] ) ){
187 if ( isset( $_GET['post_type'] ) )
188 $post_type = sanitize_text_field( $_GET['post_type'] );
189
190 elseif ( isset( $_GET['post'] ) )
191 $post_type = get_post_type( absint( $_GET['post'] ) );
192 else if( isset( $_GET['page'] ) ){
193 $screen = get_current_screen();
194 $post_type = $screen->id;
195 }
196
197 if ( ( $this->page[0] == $post_type ) ){
198 if( file_exists( WPPB_PLUGIN_DIR . 'assets/lib/codemirror/lib/codemirror.css' ) )
199 wp_enqueue_style( 'codemirror-style', WPPB_PLUGIN_URL . 'assets/lib/codemirror/lib/codemirror.css', false, PROFILE_BUILDER_VERSION );
200 else if( defined( 'WPPB_PAID_PLUGIN_URL' ) )
201 wp_enqueue_style( 'codemirror-style', WPPB_PAID_PLUGIN_URL . 'assets/lib/codemirror/lib/codemirror.css', false, PROFILE_BUILDER_VERSION );
202
203 if( !wp_script_is( 'codemirror', 'registered' ) && !wp_script_is( 'codemirror', 'enqueued' ) ) {
204
205 if( file_exists( WPPB_PLUGIN_DIR . 'assets/lib/codemirror/lib/codemirror.js' ) ){
206 wp_enqueue_script('codemirror', WPPB_PLUGIN_URL . 'assets/lib/codemirror/lib/codemirror.js', array(), PROFILE_BUILDER_VERSION);
207 wp_enqueue_script('codemirror-mode-overlay-js', WPPB_PLUGIN_URL . 'assets/lib/codemirror/addon/mode/overlay.js', array(), '1.0');
208 wp_enqueue_script('codemirror-mode-xml-js', WPPB_PLUGIN_URL . 'assets/lib/codemirror/mode/xml/xml.js', array(), '1.0');
209 wp_enqueue_script('codemirror-fullscreen-js', WPPB_PLUGIN_URL . 'assets/lib/codemirror/addon/display/fullscreen.js', array(), '1.0');
210 } else if ( defined( 'WPPB_PAID_PLUGIN_URL' ) ){
211 wp_enqueue_script('codemirror', WPPB_PAID_PLUGIN_URL . 'assets/lib/codemirror/lib/codemirror.js', array(), PROFILE_BUILDER_VERSION);
212 wp_enqueue_script('codemirror-mode-overlay-js', WPPB_PAID_PLUGIN_URL . 'assets/lib/codemirror/addon/mode/overlay.js', array(), '1.0');
213 wp_enqueue_script('codemirror-mode-xml-js', WPPB_PAID_PLUGIN_URL . 'assets/lib/codemirror/mode/xml/xml.js', array(), '1.0');
214 wp_enqueue_script('codemirror-fullscreen-js', WPPB_PAID_PLUGIN_URL . 'assets/lib/codemirror/addon/display/fullscreen.js', array(), '1.0');
215 }
216
217 }
218
219 wp_enqueue_script('jquery-ui-accordion');
220 }
221 }
222 }
223
224 /**
225 * Function that prints the codemirror initialization and the css
226 *
227 * @param string $hook the page hook
228 *
229 * @since 2.0.0
230 *
231 */
232 function wppb_print_codemirror_script(){
233 global $printed_codemirror_scripts;
234
235 if( $printed_codemirror_scripts )
236 return;
237
238 $post_type = NULL;
239
240 if ( isset( $_GET['post_type'] ) || isset( $_GET['post'] ) || isset( $_GET['page'] ) ){
241 if ( isset( $_GET['post_type'] ) )
242 $post_type = sanitize_text_field( $_GET['post_type'] );
243 elseif ( isset( $_GET['post'] ) )
244 $post_type = get_post_type( absint( $_GET['post'] ) );
245 else if( isset( $_GET['page'] ) ){
246 $screen = get_current_screen();
247 if( $screen !== null && is_object( $screen ) ) {
248 $post_type = $screen->id;
249 }
250 }
251
252 if ( ( $this->page[0] == $post_type ) ){
253 if( file_exists( WPPB_PLUGIN_DIR . 'assets/lib/class-mustache-templates/class-mustache-templates.css' ) )
254 wp_enqueue_style( 'class-mustache-css', WPPB_PLUGIN_URL . 'assets/lib/class-mustache-templates/class-mustache-templates.css', false, PROFILE_BUILDER_VERSION );
255 else if( defined( 'WPPB_PAID_PLUGIN_URL' ) )
256 wp_enqueue_style( 'class-mustache-css', WPPB_PAID_PLUGIN_URL . 'assets/lib/class-mustache-templates/class-mustache-templates.css', false, PROFILE_BUILDER_VERSION );
257
258 if( wp_script_is( 'codemirror-mode-overlay-js', 'enqueued' ) ) {
259 if( file_exists( WPPB_PLUGIN_DIR . 'assets/lib/class-mustache-templates/class-mustache-templates.css' ) )
260 wp_enqueue_script( 'class-mustache-js', WPPB_PLUGIN_URL . 'assets/lib/class-mustache-templates/class-mustache-templates.js', array("jquery"), PROFILE_BUILDER_VERSION );
261 else if( defined( 'WPPB_PAID_PLUGIN_URL' ) )
262 wp_enqueue_script( 'class-mustache-js', WPPB_PAID_PLUGIN_URL . 'assets/lib/class-mustache-templates/class-mustache-templates.js', array("jquery"), PROFILE_BUILDER_VERSION );
263 }
264
265 $printed_codemirror_scripts = true;
266 }
267 }
268 }
269
270 /**
271 * Function that adds the mustache metabox
272 *
273 * @since 2.0.0
274 *
275 */
276 function add_box() {
277 global $post_type;
278
279 foreach ( $this->page as $page ) {
280 add_meta_box( $this->id, $this->title, array( $this, 'meta_box_callback' ), $page, 'normal', $this->priority, array( 'post_type' => $post_type ) );
281
282 /* if it's not a post type add a side metabox with a save button */
283 if( isset( $_GET['page'] ) )
284 add_meta_box( 'page-save-metabox', __( 'Save', 'profile-builder' ), array( $this, 'page_save_meta_box' ), $page, 'side' );
285 }
286 }
287
288 /**
289 * Function output the content for the metabox
290 *
291 *
292 * @since 2.0.0
293 *
294 */
295 function meta_box_callback( $post, $metabox ) {
296 global $post;
297 if( isset( $_GET['page'] ) )
298 $post_type = NULL;
299 else
300 $post_type = $metabox['args']['post_type'];
301
302 /* save default values in the database as post meta for post types */
303 if( !empty( $this->fields ) && !empty( $post->ID ) ){
304 foreach( $this->fields as $field ){
305 if( !empty( $field['default'] ) ){
306 /* see if we have an option with this name, if we have don't do anything */
307 $field_saved_value = get_post_meta( $post->ID, $field['id'], true );
308 if( empty( $field_saved_value ) ){
309 add_post_meta( $post->ID, $field['id'], $field['default'], true );
310 }
311 }
312 }
313 }
314
315 // Use nonce for verification
316 echo '<input type="hidden" name="' . esc_attr( $post_type ) . '_meta_box_nonce" value="' . esc_attr( wp_create_nonce( basename( __FILE__) ) ) . '" />';
317
318 // Begin the field table and loop
319 echo '<table class="form-table meta_box mustache-box">';
320 foreach ( $this->fields as $field ) {
321
322 // get data for this field
323 extract( $field );
324 if ( !empty( $desc ) )
325 $desc = '<p class="cozmoslabs-description cozmoslabs-description-space-left">' . $desc . '</p>';
326
327 // get value of this field if it exists for this post
328 if( isset( $_GET['page'] ) ){
329 $meta = get_option( $id );
330 }
331 else{
332 $meta = get_post_meta( $post->ID, $id, true );
333 }
334
335 //try to set a default value
336 if( empty( $meta ) && !empty( $default ) ){
337 $meta = $default;
338 }
339
340
341 // begin a table row with
342 echo '<tr>
343 <td class="cozmoslabs-form-field-wrapper ' . esc_attr( $id ) . ' ' . esc_attr( $type ) . '">';
344 if( $type != 'header' && $type != 'checkbox' && !empty( $label ) ){
345 echo '<label for="' . esc_attr( $id ) . '" class="wppb_mustache_label cozmoslabs-form-field-label">' . esc_html( $label ) . '</label>';
346 }
347 switch( $type ) {
348 // text
349 case 'text':
350 echo '<input type="text" name="' . esc_attr( $id ) . '" id="' . esc_attr( $id ) . '" value="' . esc_attr( $meta ) . '" size="30" />
351 <br />' . wp_kses_post( $desc ) ;
352 break;
353 // textarea
354 case 'textarea':
355
356 echo '<textarea name="' . esc_attr( $id ) . '" id="' . esc_attr( $id ) . '" cols="220" rows="4" class="wppb_mustache_template">' . esc_textarea( $meta ) . '</textarea>';
357 echo wp_kses_post( $desc ) ;
358 break;
359 // checkbox
360 case 'checkbox':
361
362 echo ' <div class="cozmoslabs-toggle-container">
363 <input type="checkbox" name="' . esc_attr( $id ) . '" id="' . esc_attr( $id ) . '"' . checked( esc_attr( $meta ), 'on', false ) . ' value="on" />
364 <label class="cozmoslabs-toggle-track" for="' . esc_attr( $id ) . '">' . esc_html( $desc ) . '</label>
365 </div>
366 ';
367 break;
368 // select
369 case 'select':
370 echo '<select name="' . esc_attr( $id ) . '" id="' . esc_attr( $id ) . '">';
371 foreach ( $options as $option )
372 echo '<option' . selected( esc_attr( $meta ), $option['value'], false ) . ' value="' . esc_attr( $option['value'] ) . '">' . esc_html( $option['label'] ) . '</option>';
373 echo '</select><br />' . wp_kses_post( $desc );
374 break;
375 // radio
376 case 'radio':
377 foreach ( $options as $option )
378 echo '<input type="radio" name="' . esc_attr( $id ) . '" id="' . esc_attr( $id ) . '-' . esc_attr( $option['value'] ) . '" value="' . esc_attr( $option['value'] ) . '"' . checked( esc_attr( $meta ), $option['value'], false ) . ' />
379 <label for="' . esc_attr( $id ) . '-' . esc_attr( $option['value'] ) . '">' . esc_html( $option['label'] ) . '</label><br />';
380 echo '' . wp_kses_post( $desc );
381 break;
382 // checkbox_group
383 case 'checkbox_group':
384 foreach ( $options as $option )
385 echo '<input type="checkbox" value="' . esc_attr( $option['value'] ) . '" name="' . $id . '[]" id="' . $id . '-' . $option['value'] . '"' , is_array( $meta ) && in_array( $option['value'], $meta ) ? ' checked="checked"' : '' , ' />
386 <label for="' . esc_attr( $id ) . '-' . esc_attr( $option['value'] ) . '">' . esc_html( $option['label'] ) . '</label><br />';
387 echo '' . wp_kses_post( $desc );
388 break;
389 // text
390 case 'header':
391 echo '<h4 class="cozmoslabs-description">'. esc_html( $default ) .'</h4>';
392 break;
393 } //end switch
394
395 if( $type == 'textarea' ){
396 ?>
397 <div class="stp-extra">
398 <?php
399 if( !empty( $this->mustache_vars ) ){
400 foreach( $this->mustache_vars as $mustache_var_group ){
401 ?>
402 <h4><?php echo esc_html( $mustache_var_group['group-title'] ); ?></h4>
403 <pre><?php do_action( 'wppb_before_mustache_vars_display', $mustache_var_group, $id, $post_type ); $this->display_mustache_available_vars( $mustache_var_group['variables'], 0 ); ?></pre>
404 <?php
405 }
406 }
407 ?>
408 </div>
409 <?php
410 }
411 echo '</td></tr>';
412 } // end foreach
413 echo '</table>'; // end table
414 }
415
416 /**
417 * Function that ads a side metabox on pages ( not post types ) with a save button
418 *
419 *
420 * @since 2.0.0
421 *
422 */
423 function page_save_meta_box() {
424 ?>
425 <input type="submit" value="<?php esc_html_e( 'Save Changes', 'profile-builder' ); ?>" class="button button-primary button-large mustache-save">
426 <?php
427 }
428
429 /**
430 * Function that ads a form start on pages ( not post types ) and also the 'save_post' action
431 *
432 *
433 * @since 2.0.0
434 *
435 */
436 function wppb_mustache_page_before( $hook ){
437 global $started_mustache_form;
438
439 if( $started_mustache_form )
440 return;
441
442 if( isset( $_GET['page'] ) && $hook == $this->page[0] ){
443 /* if we are saving do the action 'save_post' */
444 if( isset( $_GET['mustache_action'] ) && $_GET['mustache_action'] == 'save' ) {
445 /* to avoid conflicts with other plugins we send an empty post object as the second parameter with a fake post type */
446 $post = new WP_Post( new stdClass() );
447 $post->post_type = 'wppb-mustache-settings-page';
448 remove_all_actions('save_post', 10 );//remove all actions that could run on the save_post hook on priority 10 than could expect a numeric post id instead of a string like $this->id. Ex: add_action( 'save_post', array( $this, 'refresh_post_word_count' ) ); in WPML translation management
449 remove_all_actions('save_post', PHP_INT_MAX );//same for PHP_INT_MAX priority: add_action( 'save_post', array( $this, 'queue_save_post_actions' ), PHP_INT_MAX, 2 );
450 do_action( 'save_post', $this->id, $post, false );
451 }
452
453 echo '<form action="'. esc_url( add_query_arg( array('mustache_action' => 'save') ) ) .'" method="post">';
454 $started_mustache_form = true;
455 }
456 }
457
458 /**
459 * Function that ads a form end on pages ( not post types )
460 *
461 *
462 * @since 2.0.0
463 *
464 */
465 function wppb_mustache_page_after( $hook ){
466 global $ended_mustache_form;
467 if( $ended_mustache_form )
468 return;
469
470 if( isset( $_GET['page'] ) && $hook == $this->page[0] ){
471 echo '</form>';
472 $ended_mustache_form = true;
473 }
474 }
475
476
477 /**
478 * Function that transforms and echoes the $mustache_vars into despayable forms. It takes care of nested levels and adds {{}} as well
479 *
480 * @param array $mustache_vars is the array containing the names of the variable that must be proccessed by mustache in the template. The array must be in this form:
481 * array( array( 'name' => '', 'type' => '' ) ... ), and for loop tags it also contains a 'children' element which contains other mustache_vars array( array( 'name' => 'users', 'type' => 'loop_tag', 'children' => $merge_tags )
482 * @param int $level the current level of the nested variables.
483 *
484 * @since 2.0.0
485 *
486 */
487 function display_mustache_available_vars( $mustache_vars, $level ){
488 if( !empty( $mustache_vars ) ){
489 foreach( $mustache_vars as $var ){
490 if ( $var[ 'name' ] != 'password' ) {
491 if ( empty( $var[ 'children' ] ) ) {
492 echo str_repeat( "&nbsp;&nbsp;", $level );//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
493 if ( !empty( $var[ 'label' ] ) )
494 echo esc_html( apply_filters( 'wppb_variable_label', $var[ 'label' ] . ':' ) );
495 if ( !empty( $var[ 'unescaped' ] ) && $var[ 'unescaped' ] === true )
496 echo '{{{';
497 else
498 echo '{{';
499 echo esc_html( $var[ 'name' ] );
500 if ( !empty( $var[ 'unescaped' ] ) && $var[ 'unescaped' ] === true )
501 echo '}}}';
502 else
503 echo '}}';
504 echo PHP_EOL;
505 } else {
506 echo str_repeat( "&nbsp;&nbsp;", $level ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
507 echo '{{#' . esc_html( $var[ 'name' ] ) . '}}' . PHP_EOL;
508 $level++;
509 $this->display_mustache_available_vars( $var[ 'children' ], $level );
510 $level--;
511 echo str_repeat( "&nbsp;&nbsp;", $level ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
512 echo '{{/' . esc_html( $var[ 'name' ] ) . '}}' . PHP_EOL;
513 }
514 }
515 }
516 }
517 }
518
519 /**
520 * Function that saves the values from the metabox
521 *
522 * @param int $post_id the post id
523 * @param post object $post
524 *
525 * @since 2.0.0
526 *
527 */
528 function save_box( $post_id, $post ){
529 global $post_type;
530 /* addition to save as option if we are not on a post type */
531 if( !is_numeric( $post_id ) && isset( $_POST['_meta_box_nonce'] ) && @wp_verify_nonce( sanitize_text_field( $_POST['_meta_box_nonce'] ), basename( __FILE__ ) ) ){
532 foreach ( $this->fields as $field ) {
533
534 if ( isset( $_POST[$field['id']] ) ) {
535 update_option( $field['id'], wp_unslash( $_POST[$field['id']] ) );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
536 }
537
538 }
539 }
540
541 // verify nonce
542 if ( ! ( in_array( $post_type, $this->page ) && isset( $_POST[$post_type . '_meta_box_nonce'] ) && @wp_verify_nonce( sanitize_text_field( $_POST[$post_type . '_meta_box_nonce'] ), basename( __FILE__ ) ) ) )
543 return $post_id;
544 // check autosave
545 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
546 return $post_id;
547 // check permissions
548 if ( !current_user_can( 'edit_page', $post_id ) )
549 return $post_id;
550
551 // loop through fields and save the data
552 foreach ( $this->fields as $field ) {
553 if( $field['type'] == 'tax_select' ) {
554 // save taxonomies
555 if ( isset( $_POST[$field['id']] ) )
556 $term = $_POST[$field['id']]; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
557 wp_set_object_terms( $post_id, $term, $field['id'] );
558 }
559 else {
560 // save the rest
561 $old = get_post_meta( $post_id, $field['id'], true );
562 if ( isset( $_POST[$field['id']] ) ) {
563 $new = $_POST[$field['id']]; /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
564 } else {
565 $new = '';
566 }
567
568 if ( $new && $new != $old ) {
569 if ( is_array( $new ) ) {
570 foreach ( $new as &$item ) {
571 //$item = esc_attr( $item );
572 }
573 unset( $item );
574 } else {
575 //$new = esc_attr( $new );
576 }
577 update_post_meta( $post_id, $field['id'], $new );
578 } elseif ( '' == $new && $old ) {
579 delete_post_meta( $post_id, $field['id'], $old );
580 }
581 }
582 } // end foreach
583 }
584
585
586 /**
587 * Function that saves the default values for each field in the database for options
588 *
589 *
590 * @since 2.0.0
591 *
592 */
593
594 function save_default_values(){
595 /* only do it on pages where we save as options and we have fields */
596 if( !empty( $this->fields ) && !post_type_exists($this->page[0]) ){
597 foreach( $this->fields as $field ){
598 if( !empty( $field['default'] ) ){
599 /* see if we have an option with this name, if we have don't do anything */
600 $field_saved_value = get_option( $field['id'] );
601 if( empty( $field_saved_value ) ) {
602 update_option( $field['id'], $field['default'] );
603 }
604 }
605 }
606 }
607 }
608 }
609