PluginProbe
WP Ultimate Post Grid / 4.0.1
WP Ultimate Post Grid v4.0.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / bv-settings / includes / class-bvs-saver.php

class-bvs-saver.php in WP Ultimate Post Grid 4.0.1, at vendor/bv-settings/includes/class-bvs-saver.php

132 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle the settings saving.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 1.0.0
7 *
8 * @package BV_Settings
9 * @author Brecht Vandersmissen <[email protected]>
10 */
11
12 class BV_Saver {
13 private $bvs;
14
15 /**
16 * Store main instance and initialize.
17 *
18 * @since 1.0.0
19 */
20 public function __construct( $bvs ) {
21 $this->bvs = $bvs;
22 }
23
24 /**
25 * Update the settings.
26 *
27 * @since 1.0.0
28 * @param array $settings_to_update Settings toB update.
29 */
30 public function update_settings( $settings_to_update ) {
31 $old_settings = $this->bvs->get_settings();
32
33 if ( is_array( $settings_to_update ) ) {
34 $settings_to_update = $this->sanitize_settings( $settings_to_update );
35 $new_settings = array_merge( $old_settings, $settings_to_update );
36
37 $new_settings = apply_filters( $this->bvs->atts['uid'] . '_settings_update', $new_settings, $old_settings );
38
39 update_option( $this->bvs->atts['uid'] . '_settings', $new_settings );
40 $this->bvs->helpers['structure']->set_settings( $new_settings );
41 }
42
43 return $this->bvs->get_settings();
44 }
45
46 /**
47 * Sanitize the settings.
48 *
49 * @since 1.0.0
50 * @param array $settings Settings to sanitize.
51 */
52 public function sanitize_settings( $settings ) {
53 $sanitized_settings = array();
54 $settings_details = $this->bvs->helpers['structure']->get_details();
55
56 foreach ( $settings as $id => $value ) {
57 if ( array_key_exists( $id, $settings_details ) ) {
58 $details = $settings_details[ $id ];
59
60 $sanitized_value = NULL;
61
62 // Check for custom sanitization function.
63 if ( isset( $details['sanitize'] ) && is_callable( $details['sanitize'] ) ) {
64 $sanitized_value = call_user_func( $details['sanitize'], $value );
65 }
66
67 // Options callback.
68 if ( isset( $details['optionsCallback'] ) ) {
69 $details['options'] = call_user_func( $details['optionsCallback'], $details );
70 }
71
72 // Default sanitization based on type.
73 if ( is_null( $sanitized_value ) && isset( $details['type'] ) ) {
74 switch ( $details['type'] ) {
75 case 'code':
76 $sanitized_value = wp_kses_post( $value );
77
78 // Fix for CSS code.
79 $sanitized_value = str_replace( '&gt;', '>', $sanitized_value );
80 break;
81 case 'color':
82 $sanitized_value = sanitize_text_field( $value );
83 break;
84 case 'dropdown':
85 if ( array_key_exists( $value, $details['options'] ) ) {
86 $sanitized_value = $value;
87 }
88 break;
89 case 'dropdownMultiselect':
90 $sanitized_value = array();
91
92 if ( is_array( $value ) ) {
93 foreach ( $value as $option ) {
94 if ( array_key_exists( $option, $details['options'] ) ) {
95 $sanitized_value[] = $option;
96 }
97 }
98 }
99 break;
100 case 'email':
101 $sanitized_value = sanitize_email( $value );
102 break;
103 case 'number':
104 $sanitized_value = sanitize_text_field( $value );
105 break;
106 case 'richTextarea':
107 $sanitized_value = wp_kses_post( $value );
108 break;
109 case 'text':
110 $sanitized_value = sanitize_text_field( $value );
111 break;
112 case 'textarea':
113 $sanitized_value = wp_kses_post( $value );
114 break;
115 case 'toggle':
116 $sanitized_value = $value ? true : false;
117 break;
118 }
119 }
120
121 $sanitized_value = apply_filters( $this->bvs->atts['uid'] . '_sanitized', $sanitized_value, $value, $id, $details );
122
123 if ( ! is_null( $sanitized_value ) ) {
124 $sanitized_settings[ $id ] = $sanitized_value;
125 }
126 }
127 }
128
129 return $sanitized_settings;
130 }
131 }
132