PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.6
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.6
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.6, at assets/lib/class-mustache-templates/class-mustache-templates.php

605 lines 23.0 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 = '<span class="description">' . $desc . '</span>';
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="' . esc_attr( $id ) . ' ' . esc_attr( $type ) . '">';
344 if( $type != 'header' && !empty( $label ) ){
345 echo '<label for="' . esc_attr( $id ) . '" class="wppb_mustache_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 echo '<input type="checkbox" name="' . esc_attr( $id ) . '" id="' . esc_attr( $id ) . '"' . checked( esc_attr( $meta ), 'on', false ) . ' value="on" />
362 <label for="' . esc_attr( $id ) . '">' . esc_html( $desc ) . '</label>';
363 break;
364 // select
365 case 'select':
366 echo '<select name="' . esc_attr( $id ) . '" id="' . esc_attr( $id ) . '">';
367 foreach ( $options as $option )
368 echo '<option' . selected( esc_attr( $meta ), $option['value'], false ) . ' value="' . esc_attr( $option['value'] ) . '">' . esc_html( $option['label'] ) . '</option>';
369 echo '</select><br />' . wp_kses_post( $desc );
370 break;
371 // radio
372 case 'radio':
373 foreach ( $options as $option )
374 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 ) . ' />
375 <label for="' . esc_attr( $id ) . '-' . esc_attr( $option['value'] ) . '">' . esc_html( $option['label'] ) . '</label><br />';
376 echo '' . wp_kses_post( $desc );
377 break;
378 // checkbox_group
379 case 'checkbox_group':
380 foreach ( $options as $option )
381 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"' : '' , ' />
382 <label for="' . esc_attr( $id ) . '-' . esc_attr( $option['value'] ) . '">' . esc_html( $option['label'] ) . '</label><br />';
383 echo '' . wp_kses_post( $desc );
384 break;
385 // text
386 case 'header':
387 echo '<h4>'. esc_html( $default ) .'</h4>';
388 break;
389 } //end switch
390
391 if( $type == 'textarea' ){
392 ?>
393 <div class="stp-extra">
394 <?php
395 if( !empty( $this->mustache_vars ) ){
396 foreach( $this->mustache_vars as $mustache_var_group ){
397 ?>
398 <h4><?php echo esc_html( $mustache_var_group['group-title'] ); ?></h4>
399 <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>
400 <?php
401 }
402 }
403 ?>
404 </div>
405 <?php
406 }
407 echo '</td></tr>';
408 } // end foreach
409 echo '</table>'; // end table
410 }
411
412 /**
413 * Function that ads a side metabox on pages ( not post types ) with a save button
414 *
415 *
416 * @since 2.0.0
417 *
418 */
419 function page_save_meta_box() {
420 ?>
421 <input type="submit" value="<?php esc_html_e( 'Save Changes', 'profile-builder' ); ?>" class="button button-primary button-large mustache-save">
422 <?php
423 }
424
425 /**
426 * Function that ads a form start on pages ( not post types ) and also the 'save_post' action
427 *
428 *
429 * @since 2.0.0
430 *
431 */
432 function wppb_mustache_page_before( $hook ){
433 global $started_mustache_form;
434
435 if( $started_mustache_form )
436 return;
437
438 if( isset( $_GET['page'] ) && $hook == $this->page[0] ){
439 /* if we are saving do the action 'save_post' */
440 if( isset( $_GET['mustache_action'] ) && $_GET['mustache_action'] == 'save' ) {
441 /* to avoid conflicts with other plugins we send an empty post object as the second parameter with a fake post type */
442 $post = new WP_Post( new stdClass() );
443 $post->post_type = 'wppb-mustache-settings-page';
444 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
445 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 );
446 do_action( 'save_post', $this->id, $post, false );
447 }
448
449 echo '<form action="'. esc_url( add_query_arg( array('mustache_action' => 'save') ) ) .'" method="post">';
450 $started_mustache_form = true;
451 }
452 }
453
454 /**
455 * Function that ads a form end on pages ( not post types )
456 *
457 *
458 * @since 2.0.0
459 *
460 */
461 function wppb_mustache_page_after( $hook ){
462 global $ended_mustache_form;
463 if( $ended_mustache_form )
464 return;
465
466 if( isset( $_GET['page'] ) && $hook == $this->page[0] ){
467 echo '</form>';
468 $ended_mustache_form = true;
469 }
470 }
471
472
473 /**
474 * Function that transforms and echoes the $mustache_vars into despayable forms. It takes care of nested levels and adds {{}} as well
475 *
476 * @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:
477 * 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 )
478 * @param int $level the current level of the nested variables.
479 *
480 * @since 2.0.0
481 *
482 */
483 function display_mustache_available_vars( $mustache_vars, $level ){
484 if( !empty( $mustache_vars ) ){
485 foreach( $mustache_vars as $var ){
486 if ( $var[ 'name' ] != 'password' ) {
487 if ( empty( $var[ 'children' ] ) ) {
488 echo str_repeat( "&nbsp;&nbsp;", $level );//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
489 if ( !empty( $var[ 'label' ] ) )
490 echo esc_html( apply_filters( 'wppb_variable_label', $var[ 'label' ] . ':' ) );
491 if ( !empty( $var[ 'unescaped' ] ) && $var[ 'unescaped' ] === true )
492 echo '{{{';
493 else
494 echo '{{';
495 echo esc_html( $var[ 'name' ] );
496 if ( !empty( $var[ 'unescaped' ] ) && $var[ 'unescaped' ] === true )
497 echo '}}}';
498 else
499 echo '}}';
500 echo PHP_EOL;
501 } else {
502 echo str_repeat( "&nbsp;&nbsp;", $level ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
503 echo '{{#' . esc_html( $var[ 'name' ] ) . '}}' . PHP_EOL;
504 $level++;
505 $this->display_mustache_available_vars( $var[ 'children' ], $level );
506 $level--;
507 echo str_repeat( "&nbsp;&nbsp;", $level ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
508 echo '{{/' . esc_html( $var[ 'name' ] ) . '}}' . PHP_EOL;
509 }
510 }
511 }
512 }
513 }
514
515 /**
516 * Function that saves the values from the metabox
517 *
518 * @param int $post_id the post id
519 * @param post object $post
520 *
521 * @since 2.0.0
522 *
523 */
524 function save_box( $post_id, $post ){
525 global $post_type;
526 /* addition to save as option if we are not on a post type */
527 if( !is_numeric( $post_id ) && isset( $_POST['_meta_box_nonce'] ) && @wp_verify_nonce( sanitize_text_field( $_POST['_meta_box_nonce'] ), basename( __FILE__ ) ) ){
528 foreach ( $this->fields as $field ) {
529
530 if ( isset( $_POST[$field['id']] ) ) {
531 update_option( $field['id'], wp_unslash( $_POST[$field['id']] ) );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
532 }
533
534 }
535 }
536
537 // verify nonce
538 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__ ) ) ) )
539 return $post_id;
540 // check autosave
541 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
542 return $post_id;
543 // check permissions
544 if ( !current_user_can( 'edit_page', $post_id ) )
545 return $post_id;
546
547 // loop through fields and save the data
548 foreach ( $this->fields as $field ) {
549 if( $field['type'] == 'tax_select' ) {
550 // save taxonomies
551 if ( isset( $_POST[$field['id']] ) )
552 $term = $_POST[$field['id']]; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
553 wp_set_object_terms( $post_id, $term, $field['id'] );
554 }
555 else {
556 // save the rest
557 $old = get_post_meta( $post_id, $field['id'], true );
558 if ( isset( $_POST[$field['id']] ) ) {
559 $new = $_POST[$field['id']]; /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
560 } else {
561 $new = '';
562 }
563
564 if ( $new && $new != $old ) {
565 if ( is_array( $new ) ) {
566 foreach ( $new as &$item ) {
567 //$item = esc_attr( $item );
568 }
569 unset( $item );
570 } else {
571 //$new = esc_attr( $new );
572 }
573 update_post_meta( $post_id, $field['id'], $new );
574 } elseif ( '' == $new && $old ) {
575 delete_post_meta( $post_id, $field['id'], $old );
576 }
577 }
578 } // end foreach
579 }
580
581
582 /**
583 * Function that saves the default values for each field in the database for options
584 *
585 *
586 * @since 2.0.0
587 *
588 */
589
590 function save_default_values(){
591 /* only do it on pages where we save as options and we have fields */
592 if( !empty( $this->fields ) && !post_type_exists($this->page[0]) ){
593 foreach( $this->fields as $field ){
594 if( !empty( $field['default'] ) ){
595 /* see if we have an option with this name, if we have don't do anything */
596 $field_saved_value = get_option( $field['id'] );
597 if( empty( $field_saved_value ) ) {
598 update_option( $field['id'], $field['default'] );
599 }
600 }
601 }
602 }
603 }
604 }
605