PluginProbe
Cool Timeline (Horizontal & Vertical Timeline) / trunk
Cool Timeline (Horizontal & Vertical Timeline) vtrunk
3.4.0 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 2.2.3 2.3.3 2.4 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.6.1 2.7.1 2.8.3 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 All 79 releases
cool-timeline / admin / codestar-framework / classes / profile-options.class.php

profile-options.class.php in Cool Timeline (Horizontal & Vertical Timeline) trunk, at admin/codestar-framework/classes/profile-options.class.php

256 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( 'CSF_Profile_Options' ) ) {
11 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
12 class CSF_Profile_Options extends CSF_Abstract{
13
14 // constans
15 public $unique = '';
16 public $abstract = 'profile';
17 public $sections = 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 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
29 $this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
30 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
31 $this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
32
33 add_action( 'admin_init', array( $this, 'add_profile_options' ) );
34
35 }
36
37 // instance
38 public static function instance( $key, $params ) {
39 return new self( $key, $params );
40 }
41
42 // add profile add/edit fields
43 public function add_profile_options() {
44
45 add_action( 'show_user_profile', array( $this, 'render_profile_form_fields' ) );
46 add_action( 'edit_user_profile', array( $this, 'render_profile_form_fields' ) );
47
48 add_action( 'personal_options_update', array( $this, 'save_profile' ) );
49 add_action( 'edit_user_profile_update', array( $this, 'save_profile' ) );
50
51 }
52
53 // get default value
54 public function get_default( $field ) {
55
56 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
57 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
58
59 return $default;
60
61 }
62
63 // get meta value
64 public function get_meta_value( $user_id, $field ) {
65
66 $value = null;
67
68 if ( ! empty( $user_id ) && ! empty( $field['id'] ) ) {
69
70 if ( $this->args['data_type'] !== 'serialize' ) {
71 $meta = get_user_meta( $user_id, $field['id'] );
72 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
73 } else {
74 $meta = get_user_meta( $user_id, $this->unique, true );
75 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
76 }
77
78 }
79
80 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
81 $value = ( isset( $value ) ) ? $value : $default;
82
83 return $value;
84
85 }
86
87 // render profile add/edit form fields
88 public function render_profile_form_fields( $profileuser ) {
89
90 $is_profile = ( is_object( $profileuser ) && isset( $profileuser->ID ) ) ? true : false;
91 $profile_id = ( $is_profile ) ? $profileuser->ID : 0;
92 $errors = ( ! empty( $profile_id ) ) ? get_user_meta( $profile_id, '_csf_errors_'. $this->unique, true ) : array();
93 $errors = ( ! empty( $errors ) ) ? $errors : array();
94 $class = ( $this->args['class'] ) ? ''. $this->args['class'] : '';
95
96 if ( ! empty( $errors ) ) {
97 delete_user_meta( $profile_id, '_csf_errors_'. $this->unique );
98 }
99
100 echo '<div class="csf csf-profile-options csf-onload'. esc_attr( $class ) .'">';
101
102 wp_nonce_field( 'csf_profile_nonce', 'csf_profile_nonce'. $this->unique );
103
104 foreach ( $this->sections as $section ) {
105
106 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
107 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
108
109 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
110 echo ( $section_title || $section_icon ) ? '<h2>'. $section_icon . $section_title .'</h2>' : '';
111 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
112 echo ( ! empty( $section['description'] ) ) ? '<div class="csf-field csf-section-description">'. $section['description'] .'</div>' : '';
113
114 if ( ! empty( $section['fields'] ) ) {
115
116 foreach ( $section['fields'] as $field ) {
117
118 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
119 $field['_error'] = $errors['fields'][$field['id']];
120 }
121
122 if ( ! empty( $field['id'] ) ) {
123 $field['default'] = $this->get_default( $field );
124 }
125
126 CSF::field( $field, $this->get_meta_value( $profile_id, $field ), $this->unique, 'profile' );
127
128 }
129
130 }
131
132 }
133
134 echo '</div>';
135
136 }
137
138 // save profile form fields
139 public function save_profile( $user_id ) {
140
141 $count = 1;
142 $data = array();
143 $errors = array();
144 $noncekey = 'csf_profile_nonce'. $this->unique;
145 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
146
147 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'csf_profile_nonce' ) ) {
148 return $user_id;
149 }
150
151 // XSS ok.
152 // No worries, This "POST" requests is sanitizing in the below foreach.
153 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
154 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? wp_unslash( $_POST[ $this->unique ] ) : array();
155
156 if ( ! empty( $request ) ) {
157
158 foreach ( $this->sections as $section ) {
159
160 if ( ! empty( $section['fields'] ) ) {
161
162 foreach ( $section['fields'] as $field ) {
163
164 if ( ! empty( $field['id'] ) ) {
165
166 $field_id = $field['id'];
167 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
168
169 // Sanitize "post" request of field.
170 if ( ! isset( $field['sanitize'] ) ) {
171
172 if( is_array( $field_value ) ) {
173 $data[$field_id] = wp_kses_post_deep( $field_value );
174 } else {
175 $data[$field_id] = wp_kses_post( $field_value );
176 }
177
178 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
179
180 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
181
182 } else {
183
184 $data[$field_id] = $field_value;
185
186 }
187
188 // Validate "post" request of field.
189 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
190
191 $has_validated = call_user_func( $field['validate'], $field_value );
192
193 if ( ! empty( $has_validated ) ) {
194
195 $errors['sections'][$count] = true;
196 $errors['fields'][$field_id] = $has_validated;
197 $data[$field_id] = $this->get_meta_value( $user_id, $field );
198
199 }
200
201 }
202
203 }
204
205 }
206
207 }
208
209 $count++;
210
211 }
212
213 }
214
215 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
216 $data = apply_filters( "csf_{$this->unique}_save", $data, $user_id, $this );
217
218 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
219 do_action( "csf_{$this->unique}_save_before", $data, $user_id, $this );
220
221 if ( empty( $data ) ) {
222
223 if ( $this->args['data_type'] !== 'serialize' ) {
224 foreach ( $data as $key => $value ) {
225 delete_user_meta( $user_id, $key );
226 }
227 } else {
228 delete_user_meta( $user_id, $this->unique );
229 }
230
231 } else {
232
233 if ( $this->args['data_type'] !== 'serialize' ) {
234 foreach ( $data as $key => $value ) {
235 update_user_meta( $user_id, $key, $value );
236 }
237 } else {
238 update_user_meta( $user_id, $this->unique, $data );
239 }
240
241 if ( ! empty( $errors ) ) {
242 update_user_meta( $user_id, '_csf_errors_'. $this->unique, $errors );
243 }
244
245 }
246
247 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
248 do_action( "csf_{$this->unique}_saved", $data, $user_id, $this );
249
250 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
251 do_action( "csf_{$this->unique}_save_after", $data, $user_id, $this );
252
253 }
254 }
255 }
256