CompLibFormHandler.php
3 weeks ago
ElementGenerator.php
3 weeks ago
ShowUserMetadata.php
2 months ago
ShowUserMetadata.php
91 lines
| 1 | <?php |
| 2 | namespace KirkiComponentLib; |
| 3 | |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | class ShowUserMetadata { |
| 9 | public function __construct() { |
| 10 | add_action( 'show_user_profile', array( $this, 'show_custom_user_metadata' ) ); |
| 11 | add_action( 'edit_user_profile', array( $this, 'show_custom_user_metadata' ) ); |
| 12 | add_action( 'personal_options_update', array( $this, 'save_custom_user_metadata' ) ); |
| 13 | add_action( 'edit_user_profile_update', array( $this, 'save_custom_user_metadata' ) ); |
| 14 | } |
| 15 | |
| 16 | private function get_meta_list( $user_id ) { |
| 17 | $meta_list = array(); |
| 18 | $metadata = get_user_meta( $user_id ); |
| 19 | $meta_list = array(); |
| 20 | foreach ( $metadata as $name => $value ) { |
| 21 | if ( str_starts_with( $name, KIRKI_COMPONENT_LIBRARY_APP_PREFIX ) ) { |
| 22 | $meta_list[ substr( $name, strlen( KIRKI_COMPONENT_LIBRARY_APP_PREFIX ) + 1 ) ] = $value[0]; |
| 23 | } |
| 24 | } |
| 25 | return $meta_list; |
| 26 | } |
| 27 | |
| 28 | private function transform_key( $string ) { |
| 29 | $string = str_replace( '_', ' ', $string ); |
| 30 | $string = ucwords( $string ); |
| 31 | return $string; |
| 32 | } |
| 33 | |
| 34 | public function show_custom_user_metadata( $user ) { |
| 35 | $meta_list = $this->get_meta_list( $user->ID ); |
| 36 | if ( ! count( $meta_list ) ) { |
| 37 | return; |
| 38 | } |
| 39 | $can_edit = current_user_can( 'edit_user', $user->ID ); |
| 40 | ?> |
| 41 | <h3>Kirki User Information</h3> |
| 42 | <table class="form-table"> |
| 43 | <?php |
| 44 | foreach ( $meta_list as $meta_key => $meta_value ) : |
| 45 | ?> |
| 46 | <tr> |
| 47 | <th><label |
| 48 | for="<?php echo esc_attr( $meta_key ); ?>"><?php echo esc_html( $this->transform_key( $meta_key ) ); ?></label> |
| 49 | </th> |
| 50 | <td> |
| 51 | <input <?php echo $can_edit ? '' : 'disabled'; ?> type="text" |
| 52 | name="custom_meta[<?php echo esc_attr( $meta_key ); ?>]" id="<?php echo esc_attr( $meta_key ); ?>" |
| 53 | value="<?php echo esc_attr( $meta_value ); ?>" class="regular-text" /> |
| 54 | </td> |
| 55 | </tr> |
| 56 | <?php endforeach; ?> |
| 57 | </table> |
| 58 | <?php |
| 59 | } |
| 60 | |
| 61 | |
| 62 | public function save_custom_user_metadata( $user_id ) { |
| 63 | |
| 64 | if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | // PHPCS:ignore WordPress.Security.NonceVerification.Missing |
| 69 | if ( ! isset( $_POST['custom_meta'] ) || ! is_array( $_POST['custom_meta'] ) ) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | $meta_list = $this->get_meta_list( $user_id ); |
| 74 | // PHPCS:ignore PHPCS(WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 75 | $custom_meta = wp_unslash( $_POST['custom_meta'] ); |
| 76 | foreach ( $meta_list as $meta_key => $meta_label ) { |
| 77 | if ( ! isset( $custom_meta[ $meta_key ] ) ) { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | $value = $custom_meta[ $meta_key ]; |
| 82 | |
| 83 | if ( strpos( $meta_key, 'address' ) !== false || strlen( $value ) > 50 ) { |
| 84 | $meta_value = sanitize_textarea_field( $value ); |
| 85 | } else { |
| 86 | $meta_value = sanitize_text_field( $value ); |
| 87 | } |
| 88 | update_user_meta( $user_id, KIRKI_COMPONENT_LIBRARY_APP_PREFIX . '_' . $meta_key, $meta_value ); |
| 89 | } |
| 90 | } |
| 91 | } |