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