PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.3.0
Adminify – White Label, Admin Menu Editor, Login Customizer v4.3.0
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Libs / adminify-framework / classes / profile-options.class.php

profile-options.class.php in Adminify – White Label, Admin Menu Editor, Login Customizer 4.3.0, at Libs/adminify-framework/classes/profile-options.class.php

260 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
2 /**
3 *
4 * Profile Option Class
5 *
6 * @since 1.0.0
7 * @version 1.0.0
8 *
9 */
10 if ( ! class_exists( 'ADMINIFY_Profile_Options' ) ) {
11 class ADMINIFY_Profile_Options extends ADMINIFY_Abstract{
12
13 // constans
14 public $unique = '';
15 public $abstract = 'profile';
16 public $sections = array();
17 public $pre_fields = array();
18 public $args = array(
19 'data_type' => 'serialize',
20 'class' => '',
21 'defaults' => array(),
22 );
23
24 // run profile construct
25 public function __construct( $key, $params ) {
26
27 $this->unique = $key;
28 $this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
29 $this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this );
30 $this->pre_fields = $this->pre_fields( $this->sections );
31
32 add_action( 'admin_init', array( $this, 'add_profile_options' ) );
33
34 }
35
36 // instance
37 public static function instance( $key, $params ) {
38 return new self( $key, $params );
39 }
40
41 // add profile add/edit fields
42 public function add_profile_options() {
43
44 add_action( 'show_user_profile', array( $this, 'render_profile_form_fields' ) );
45 add_action( 'edit_user_profile', array( $this, 'render_profile_form_fields' ) );
46
47 add_action( 'personal_options_update', array( $this, 'save_profile' ) );
48 add_action( 'edit_user_profile_update', array( $this, 'save_profile' ) );
49
50 }
51
52 // get default value
53 public function get_default( $field ) {
54
55 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
56 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
57
58 return $default;
59
60 }
61
62 // get meta value
63 public function get_meta_value( $user_id, $field ) {
64
65 $value = null;
66
67 if ( ! empty( $user_id ) && ! empty( $field['id'] ) ) {
68
69 if ( $this->args['data_type'] !== 'serialize' ) {
70 $meta = get_user_meta( $user_id, $field['id'] );
71 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
72 } else {
73 $meta = get_user_meta( $user_id, $this->unique, true );
74 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
75 }
76
77 }
78
79 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
80 $value = ( isset( $value ) ) ? $value : $default;
81
82 return $value;
83
84 }
85
86 // render profile add/edit form fields
87 public function render_profile_form_fields( $profileuser ) {
88
89 $is_profile = ( is_object( $profileuser ) && isset( $profileuser->ID ) ) ? true : false;
90 $profile_id = ( $is_profile ) ? $profileuser->ID : 0;
91 $errors = ( ! empty( $profile_id ) ) ? get_user_meta( $profile_id, '_adminify_errors_'. $this->unique, true ) : array();
92 $errors = ( ! empty( $errors ) ) ? $errors : array();
93 $class = ( $this->args['class'] ) ? ''. $this->args['class'] : '';
94
95 if ( ! empty( $errors ) ) {
96 delete_user_meta( $profile_id, '_adminify_errors_'. $this->unique );
97 }
98
99 echo '<div class="adminify adminify-profile-options adminify-onload'. esc_attr( $class ) .'">';
100
101 wp_nonce_field( 'adminify_profile_nonce', 'adminify_profile_nonce'. $this->unique );
102
103 foreach ( $this->sections as $section ) {
104
105 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
106 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
107
108 echo ( $section_title || $section_icon ) ? '<h2>'. wp_kses_post( $section_icon ) . esc_html( $section_title ) .'</h2>' : '';
109 echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. wp_kses_post( $section['description'] ) .'</div>' : '';
110
111 if ( ! empty( $section['fields'] ) ) {
112
113 foreach ( $section['fields'] as $field ) {
114
115 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
116 $field['_error'] = $errors['fields'][$field['id']];
117 }
118
119 if ( ! empty( $field['id'] ) ) {
120 $field['default'] = $this->get_default( $field );
121 }
122
123 ADMINIFY::field( $field, $this->get_meta_value( $profile_id, $field ), $this->unique, 'profile' );
124
125 }
126
127 }
128
129 }
130
131 echo '</div>';
132
133 }
134
135 // save profile form fields
136 public function save_profile( $user_id ) {
137
138 $count = 1;
139 $data = array();
140 $errors = array();
141 $noncekey = 'adminify_profile_nonce'. $this->unique;
142 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
143
144 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_profile_nonce' ) ) {
145 return $user_id;
146 }
147
148 // Authorization: a valid nonce proves intent, not permission.
149 if ( ! current_user_can( 'edit_user', $user_id ) ) {
150 return $user_id;
151 }
152
153 // XSS ok.
154 // No worries, This "POST" requests is sanitizing in the below foreach.
155 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? wp_unslash( $_POST[ $this->unique ] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each field is sanitized individually by the framework's per-field sanitize handlers.
156
157 if ( ! empty( $request ) ) {
158
159 foreach ( $this->sections as $section ) {
160
161 if ( ! empty( $section['fields'] ) ) {
162
163 foreach ( $section['fields'] as $field ) {
164
165 if ( ! empty( $field['id'] ) ) {
166
167 $field_id = $field['id'];
168 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
169
170 // Sanitize "post" request of field.
171 if ( ! isset( $field['sanitize'] ) ) {
172
173 if( is_array( $field_value ) ) {
174 $data[$field_id] = wp_kses_post_deep( $field_value );
175 } else {
176 $data[$field_id] = wp_kses_post( $field_value );
177 }
178
179 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
180
181 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
182
183 } else {
184
185 // A sanitize callback was declared but is not callable; never store raw input.
186 if ( is_array( $field_value ) ) {
187 $data[$field_id] = wp_kses_post_deep( $field_value );
188 } else {
189 $data[$field_id] = wp_kses_post( $field_value );
190 }
191
192 }
193
194 // Validate "post" request of field.
195 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
196
197 $has_validated = call_user_func( $field['validate'], $field_value );
198
199 if ( ! empty( $has_validated ) ) {
200
201 $errors['sections'][$count] = true;
202 $errors['fields'][$field_id] = $has_validated;
203 $data[$field_id] = $this->get_meta_value( $user_id, $field );
204
205 }
206
207 }
208
209 }
210
211 }
212
213 }
214
215 $count++;
216
217 }
218
219 }
220
221 $data = apply_filters( "adminify_{$this->unique}_save", $data, $user_id, $this );
222
223 do_action( "adminify_{$this->unique}_save_before", $data, $user_id, $this );
224
225 if ( empty( $data ) ) {
226
227 if ( $this->args['data_type'] !== 'serialize' ) {
228 foreach ( $this->pre_fields as $field ) {
229 if ( ! empty( $field['id'] ) ) {
230 delete_user_meta( $user_id, $field['id'] );
231 }
232 }
233 } else {
234 delete_user_meta( $user_id, $this->unique );
235 }
236
237 } else {
238
239 if ( $this->args['data_type'] !== 'serialize' ) {
240 foreach ( $data as $key => $value ) {
241 update_user_meta( $user_id, $key, $value );
242 }
243 } else {
244 update_user_meta( $user_id, $this->unique, $data );
245 }
246
247 if ( ! empty( $errors ) ) {
248 update_user_meta( $user_id, '_adminify_errors_'. $this->unique, $errors );
249 }
250
251 }
252
253 do_action( "adminify_{$this->unique}_saved", $data, $user_id, $this );
254
255 do_action( "adminify_{$this->unique}_save_after", $data, $user_id, $this );
256
257 }
258 }
259 }
260