| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
add_shortcode('user_meta', 'wppb_toolbox_usermeta_handler'); |
| 6 |
function wppb_toolbox_usermeta_handler( $atts, $content=null){ |
| 7 |
|
| 8 |
$user_id = ''; |
| 9 |
|
| 10 |
if( isset( $atts['user_id'] ) ){ |
| 11 |
|
| 12 |
if( ( !is_multisite() && current_user_can( 'edit_users' ) ) || ( is_multisite() && ( current_user_can( 'remove_users' ) || current_user_can( 'manage_options' ) ) ) ) |
| 13 |
$user_id = absint( $atts['user_id'] ); |
| 14 |
|
| 15 |
} |
| 16 |
|
| 17 |
if( empty( $user_id ) ){ |
| 18 |
$user = wp_get_current_user(); |
| 19 |
$user_id = $user->ID; |
| 20 |
} |
| 21 |
|
| 22 |
if ( !isset( $atts['size'] ) ){ |
| 23 |
$atts['size'] = '50'; |
| 24 |
} |
| 25 |
if ( !isset( $atts['pre'] ) ) { |
| 26 |
$atts['pre'] = ''; |
| 27 |
} |
| 28 |
if ( !isset( $atts['post'] ) ) { |
| 29 |
$atts['post'] = ''; |
| 30 |
} |
| 31 |
if ( !isset( $atts['wpautop'] ) ) { |
| 32 |
$atts['wpautop'] = ''; |
| 33 |
} |
| 34 |
|
| 35 |
if( isset( $atts['key'] ) && in_array( $atts['key'], array( 'user_pass', 'user_activation_key' ) ) ) |
| 36 |
return; |
| 37 |
|
| 38 |
$user = new WP_User( $user_id ); |
| 39 |
|
| 40 |
if ( !$user->exists() ) return; |
| 41 |
|
| 42 |
if ( !array_key_exists( 'key', $atts ) ) return; |
| 43 |
|
| 44 |
if( $atts['key'] == 'avatar' ){ |
| 45 |
return wp_kses_post( $atts['pre'] ) . get_avatar( $user->ID, $atts['size']) . wp_kses_post( $atts['post'] ) ; |
| 46 |
} |
| 47 |
|
| 48 |
if( $atts['key'] === 'id' ){ |
| 49 |
$atts['key'] = 'ID'; |
| 50 |
} |
| 51 |
|
| 52 |
$value = ''; |
| 53 |
|
| 54 |
if ( $user->has_prop( $atts['key'] ) ){ |
| 55 |
|
| 56 |
if ($atts['wpautop'] == 'on'){ |
| 57 |
$value = wpautop( $user->get( $atts['key'] ) ); |
| 58 |
} else { |
| 59 |
$value = $user->get( $atts['key'] ); |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
// Verify if key is a WYSIWYG field |
| 65 |
$escape_value = true; |
| 66 |
$manage_fields = get_option( 'wppb_manage_fields' ); |
| 67 |
|
| 68 |
if ( !empty( $manage_fields ) ){ |
| 69 |
foreach ( $manage_fields as $field ){ |
| 70 |
if ( $field['meta-name'] == $atts['key'] && $field['field'] == 'WYSIWYG' ){ |
| 71 |
$escape_value = false; |
| 72 |
break; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if( $escape_value === true && !empty( $value ) ){ |
| 78 |
$value = esc_html( $value ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( !empty( $value ) ){ |
| 82 |
return wp_kses_post( $atts['pre'] ) . $value . wp_kses_post( $atts['post'] ) ; |
| 83 |
} |
| 84 |
|
| 85 |
if( $atts['key'] === 'role' ){ |
| 86 |
$roles = !empty( $user->roles ) ? $user->roles : array(); |
| 87 |
|
| 88 |
if( !empty( $roles ) ){ |
| 89 |
$value_roles = implode( ', ', $roles ); |
| 90 |
return wp_kses_post( $atts['pre'] ) . esc_html( $value_roles ) . wp_kses_post( $atts['post'] ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return; |
| 95 |
} |
| 96 |
|