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

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