/**
* WSL wordpress_social_login_meta shortcode
*
* Note:
*   This shortcode is experimental and might change in future versions.
*
*   [wordpress_social_login_meta
*        user_id="215"
*        meta="wsl_current_user_image"
*        display="html"
*        css_class="my_style_is_better"
*   ]
*/
function wsl_shortcode_wordpress_social_login_meta($args = array())
{
    // wordpress user id default to current user connected
    $user_id = isset($args['user_id']) && $args['user_id'] ? $args['user_id'] : get_current_user_id();
    // display default to plain text
    $display = isset($args['display']) && $args['display'] ? $args['display'] : 'plain';
    // when display is set to html, css_class will be used for the main dom el
    $css_class = isset($args['css_class']) && $args['css_class'] ? $args['css_class'] : '';
    // wsl user meta to display
    $meta = isset($args['meta']) && $args['meta'] ? $args['meta'] : null;
    if (!is_numeric($user_id)) {
        return;
    }
    if (!$meta) {
        return;
    }
    $assets_base_url = WORDPRESS_SOCIAL_LOGIN_PLUGIN_URL . 'assets/img/16x16/';
    $assets_base_url = isset($args['assets_base_url']) && $args['assets_base_url'] ? $args['assets_base_url'] : $assets_base_url;
    $return = '';
    if ('current_avatar' == $meta) {
        if ('plain' == $display) {
            $return = wsl_get_user_custom_avatar($user_id);
        } else {
            $return = '<img class="wordpress_social_login_meta_user_avatar ' . $css_class . '" src="' . wsl_get_user_custom_avatar($user_id) . '" />';
        }
    }
    if ('current_provider' == $meta) {
        $provider = get_user_meta($user_id, 'wsl_current_provider', true);
        if ('plain' == $display) {
            $return = $provider;
        } else {
            $return = '<img class="wordpress_social_login_meta_user_provider ' . $css_class . '" src="' . $assets_base_url . strtolower($provider) . '.png"> ' . $provider;
        }
    }
    if ('user_identities' == $meta) {
        ob_start();
        $linked_accounts = wsl_get_stored_hybridauth_user_profiles_by_user_id($user_id);
        if ($linked_accounts) {
            ?>
<table class="wp-social-login-linked-accounts-list <?php 
            echo $css_class;
            ?>
"><?php 
            foreach ($linked_accounts as $item) {
                $identity = $item->profileurl;
                $photourl = $item->photourl;
                if (!$identity) {
                    $identity = $item->identifier;
                }
                ?>
<tr><td><?php 
                if ($photourl) {
                    ?>
<img  style="vertical-align: top;width:16px;height:16px;" src="<?php 
                    echo $photourl;
                    ?>
"> <?php 
                } else {
                    ?>
<img src="<?php 
                    echo $assets_base_url . strtolower($item->provider) . '.png';
                    ?>
" /> <?php 
                }
                echo ucfirst($item->provider);
                ?>
 </td><td><?php 
                echo $identity;
                ?>
</td></tr><?php 
                echo "\n";
            }
            ?>
</table><?php 
        }
        $return = ob_get_clean();
        if ('plain' == $display) {
            $return = strip_tags($return);
        }
    }
    return $return;
}
 function wsl_get_bp_user_custom_avatar($html, $args)
 {
     //Buddypress component should be enabled
     if (!wsl_is_component_enabled('buddypress')) {
         return $html;
     }
     //Check if avatars display is enabled
     if (!get_option('wsl_settings_users_avatars')) {
         return $html;
     }
     //Check arguments
     if (is_array($args)) {
         //User Object
         if (!empty($args['object']) and strtolower($args['object']) == 'user') {
             //User Identifier
             if (!empty($args['item_id']) and is_numeric($args['item_id'])) {
                 $user_id = $args['item_id'];
                 //Only Overwrite gravatars
                 # https://wordpress.org/support/topic/buddypress-avatar-overwriting-problem?replies=1
                 if (bp_get_user_has_avatar($user_id)) {
                     return $html;
                 }
                 $wsl_avatar = wsl_get_user_custom_avatar($user_id);
                 //Retrieve Avatar
                 if ($wsl_avatar) {
                     $img_class = 'class="' . (!empty($args['class']) ? $args['class'] . ' ' : '') . 'avatar-wordpress-social-login" ';
                     $img_width = !empty($args['width']) ? 'width="' . $args['width'] . '" ' : 'width="' . bp_core_avatar_full_width() . '" ';
                     $img_height = !empty($args['height']) ? 'height="' . $args['height'] . '" ' : 'height="' . bp_core_avatar_full_height() . '" ';
                     $img_alt = !empty($args['alt']) ? 'alt="' . esc_attr($args['alt']) . '" ' : '';
                     //Replace
                     $wsl_html = preg_replace('#<img[^>]+>#i', '<img src="' . $wsl_avatar . '" ' . $img_alt . $img_class . $img_height . $img_width . '/>', $html);
                     // HOOKABLE:
                     return apply_filters('wsl_hook_alter_get_bp_user_custom_avatar', $wsl_html, $user_id, $wsl_avatar, $html, $args);
                 }
             }
         }
     }
     return $html;
 }