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