← All changes
|
Libs/adminify-framework/classes/profile-options.class.php
+158
-214
4.2.11
→
3.2.4.1
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | -<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { | |
| 2 | + die; } // Cannot access directly. | |
| 2 | 3 | /** |
| 3 | 4 | * |
| 4 | 5 | * Profile Option Class |
| 5 | 6 | * |
| @@ -4,256 +5,199 @@ | ||
| 4 | 5 | * Profile Option Class |
| 5 | 6 | * |
| 6 | 7 | * @since 1.0.0 |
| 7 | 8 | * @version 1.0.0 |
| 8 | - * | |
| 9 | 9 | */ |
| 10 | 10 | if ( ! class_exists( 'ADMINIFY_Profile_Options' ) ) { |
| 11 | - class ADMINIFY_Profile_Options extends ADMINIFY_Abstract{ | |
| 11 | + class ADMINIFY_Profile_Options extends ADMINIFY_Abstract { | |
| 12 | 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 | - ); | |
| 13 | + // constans | |
| 14 | + public $unique = ''; | |
| 15 | + public $abstract = 'profile'; | |
| 16 | + public $sections = []; | |
| 17 | + public $args = [ | |
| 18 | + 'data_type' => 'serialize', | |
| 19 | + 'class' => '', | |
| 20 | + 'defaults' => [], | |
| 21 | + ]; | |
| 23 | 22 | |
| 24 | - // run profile construct | |
| 25 | - public function __construct( $key, $params ) { | |
| 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 ); | |
| 26 | 28 | |
| 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 ); | |
| 29 | + add_action( 'admin_init', [ $this, 'add_profile_options' ] ); | |
| 30 | + } | |
| 31 | 31 | |
| 32 | - add_action( 'admin_init', array( $this, 'add_profile_options' ) ); | |
| 32 | + // instance | |
| 33 | + public static function instance( $key, $params ) { | |
| 34 | + return new self( $key, $params ); | |
| 35 | + } | |
| 33 | 36 | |
| 34 | - } | |
| 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' ] ); | |
| 35 | 41 | |
| 36 | - // instance | |
| 37 | - public static function instance( $key, $params ) { | |
| 38 | - return new self( $key, $params ); | |
| 39 | - } | |
| 42 | + add_action( 'personal_options_update', [ $this, 'save_profile' ] ); | |
| 43 | + add_action( 'edit_user_profile_update', [ $this, 'save_profile' ] ); | |
| 44 | + } | |
| 40 | 45 | |
| 41 | - // add profile add/edit fields | |
| 42 | - public function add_profile_options() { | |
| 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; | |
| 43 | 50 | |
| 44 | - add_action( 'show_user_profile', array( $this, 'render_profile_form_fields' ) ); | |
| 45 | - add_action( 'edit_user_profile', array( $this, 'render_profile_form_fields' ) ); | |
| 51 | + return $default; | |
| 52 | + } | |
| 46 | 53 | |
| 47 | - add_action( 'personal_options_update', array( $this, 'save_profile' ) ); | |
| 48 | - add_action( 'edit_user_profile_update', array( $this, 'save_profile' ) ); | |
| 54 | + // get meta value | |
| 55 | + public function get_meta_value( $user_id, $field ) { | |
| 56 | + $value = null; | |
| 49 | 57 | |
| 50 | - } | |
| 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 | + } | |
| 51 | 67 | |
| 52 | - // get default value | |
| 53 | - public function get_default( $field ) { | |
| 68 | + $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; | |
| 69 | + $value = ( isset( $value ) ) ? $value : $default; | |
| 54 | 70 | |
| 55 | - $default = ( isset( $field['default'] ) ) ? $field['default'] : ''; | |
| 56 | - $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default; | |
| 71 | + return $value; | |
| 72 | + } | |
| 57 | 73 | |
| 58 | - return $default; | |
| 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'] : ''; | |
| 59 | 81 | |
| 60 | - } | |
| 82 | + if ( ! empty( $errors ) ) { | |
| 83 | + delete_user_meta( $profile_id, '_adminify_errors_' . $this->unique ); | |
| 84 | + } | |
| 61 | 85 | |
| 62 | - // get meta value | |
| 63 | - public function get_meta_value( $user_id, $field ) { | |
| 86 | + echo '<div class="adminify adminify-profile-options adminify-onload' . esc_attr( $class ) . '">'; | |
| 64 | 87 | |
| 65 | - $value = null; | |
| 88 | + wp_nonce_field( 'adminify_profile_nonce', 'adminify_profile_nonce' . $this->unique ); | |
| 66 | 89 | |
| 67 | - if ( ! empty( $user_id ) && ! empty( $field['id'] ) ) { | |
| 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'] : ''; | |
| 68 | 93 | |
| 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 | - } | |
| 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>' : ''; | |
| 76 | 96 | |
| 77 | - } | |
| 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 | + } | |
| 78 | 102 | |
| 79 | - $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; | |
| 80 | - $value = ( isset( $value ) ) ? $value : $default; | |
| 103 | + if ( ! empty( $field['id'] ) ) { | |
| 104 | + $field['default'] = $this->get_default( $field ); | |
| 105 | + } | |
| 81 | 106 | |
| 82 | - return $value; | |
| 107 | + ADMINIFY::field( $field, $this->get_meta_value( $profile_id, $field ), $this->unique, 'profile' ); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + } | |
| 83 | 111 | |
| 84 | - } | |
| 112 | + echo '</div>'; | |
| 113 | + } | |
| 85 | 114 | |
| 86 | - // render profile add/edit form fields | |
| 87 | - public function render_profile_form_fields( $profileuser ) { | |
| 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 ] ) ) : ''; | |
| 88 | 122 | |
| 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'] : ''; | |
| 123 | + if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_profile_nonce' ) ) { | |
| 124 | + return $user_id; | |
| 125 | + } | |
| 94 | 126 | |
| 95 | - if ( ! empty( $errors ) ) { | |
| 96 | - delete_user_meta( $profile_id, '_adminify_errors_'. $this->unique ); | |
| 97 | - } | |
| 127 | + if ( ! empty( $_POST[ $this->unique ] ) ) { | |
| 128 | + $request = sanitize_text_field( wp_unslash( $_POST[ $this->unique ] ) ); | |
| 129 | + } else { | |
| 130 | + $request = []; | |
| 131 | + } | |
| 98 | 132 | |
| 99 | - echo '<div class="adminify adminify-profile-options adminify-onload'. esc_attr( $class ) .'">'; | |
| 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 ] : ''; | |
| 100 | 140 | |
| 101 | - wp_nonce_field( 'adminify_profile_nonce', 'adminify_profile_nonce'. $this->unique ); | |
| 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 | + } | |
| 102 | 153 | |
| 103 | - foreach ( $this->sections as $section ) { | |
| 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 ); | |
| 104 | 157 | |
| 105 | - $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : ''; | |
| 106 | - $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : ''; | |
| 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 | + } | |
| 107 | 167 | |
| 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>' : ''; | |
| 168 | + $count++; | |
| 169 | + } | |
| 170 | + } | |
| 110 | 171 | |
| 111 | - if ( ! empty( $section['fields'] ) ) { | |
| 172 | + $data = apply_filters( "adminify_{$this->unique}_save", $data, $user_id, $this ); | |
| 112 | 173 | |
| 113 | - foreach ( $section['fields'] as $field ) { | |
| 174 | + do_action( "adminify_{$this->unique}_save_before", $data, $user_id, $this ); | |
| 114 | 175 | |
| 115 | - if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) { | |
| 116 | - $field['_error'] = $errors['fields'][$field['id']]; | |
| 117 | - } | |
| 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 | + } | |
| 118 | 192 | |
| 119 | - if ( ! empty( $field['id'] ) ) { | |
| 120 | - $field['default'] = $this->get_default( $field ); | |
| 121 | - } | |
| 193 | + if ( ! empty( $errors ) ) { | |
| 194 | + update_user_meta( $user_id, '_adminify_errors_' . $this->unique, $errors ); | |
| 195 | + } | |
| 196 | + } | |
| 122 | 197 | |
| 123 | - ADMINIFY::field( $field, $this->get_meta_value( $profile_id, $field ), $this->unique, 'profile' ); | |
| 198 | + do_action( "adminify_{$this->unique}_saved", $data, $user_id, $this ); | |
| 124 | 199 | |
| 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 | - } | |
| 200 | + do_action( "adminify_{$this->unique}_save_after", $data, $user_id, $this ); | |
| 201 | + } | |
| 202 | + } | |
| 259 | 203 | } |