<?php
/*
 * Shows the User Login panel. When the user is logged in serveral user links
 * are shown.
 */

if ( !class_exists('WP_Widget_MABP_User_Login') ) {
  class WP_Widget_MABP_User_Login extends WP_Widget {

    // Constructor
    function WP_Widget_MABP_User_Login() {

      $widget_ops   = array('description' => __('Shows the user login and the user panel.', 'myarcadetheme'));

      $this->WP_Widget('MABP_User_Login', __('MyArcade User Login Panel', 'myarcadetheme'), $widget_ops);

      add_action('wp_print_scripts', array($this, 'js_load_scripts'));

    }

    function js_load_scripts () {
      if ( defined('WDFB_PLUGIN_URL')) {
        if (!is_admin()) wp_enqueue_script('wdfb_connect_widget', WDFB_PLUGIN_URL . '/js/wdfb_connect_widget.js');
      }
    }

    // Display Widget
    function widget($args, $instance) {
      global $user_ID, $current_user, $user_identity;

      // Populate user vars
      get_currentuserinfo();

      extract($args);

      $title = apply_filters('widget_title', esc_attr($instance['title']));

      echo $before_widget.$before_title.$title.$after_title;

      // <-- START --> HERE COMES THE OUPUT
      if ( $user_ID ) {
        // user is logged in
        global $mngl_options, $mngl_message;
        ?>
        <ul class="leabrd">
          <li>
            <figure><?php echo get_avatar( $user_ID, $size = '85'); ?></figure>
            <p><?php _e('Hello', 'myarcadetheme'); ?>, <strong><?php echo $user_identity; ?></strong>!</p>
            <ul class="menusr">
              <li class="mycnt">
                <a class="botn" href="<?php if(defined('BP_VERSION')){ global $bp; echo $bp->loggedin_user->domain; }else{ echo home_url().'/wp-admin/profile.php'; } ?>" title="<?php _e('MY ACCOUNT', 'myarcadetheme'); ?>"><?php _e('MY ACCOUNT', 'myarcadetheme'); ?></a>
                <ul>
                  <?php if ( defined('MNGL_PLUGIN_NAME') ) : ?>
                    <?php
                    $unread_count = $mngl_message->get_unread_count();
                    $unread_count_str = '[0]';
                    if($unread_count) $unread_count_str = " [{$unread_count}]";
                    ?>
                    <li><a href="<?php echo get_permalink($mngl_options->activity_page_id); ?>"><?php _e('Activity', 'myarcadetheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink($mngl_options->profile_page_id); ?>"><?php _e('Profile', 'myarcadetheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink($mngl_options->profile_edit_page_id); ?>"><?php _e('Settings', 'myarcadetheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink($mngl_options->friends_page_id); ?>"><?php _e('Friends', 'myarcadetheme'); ?></a></li>
                    <li><a href="<?php echo get_permalink($mngl_options->friend_requests_page_id); ?>"><?php _e('Friend Requests', 'myarcadetheme'); ?><?php echo $request_count_str; ?></a></li>
                    <li><a href="<?php echo get_permalink($mngl_options->inbox_page_id); ?>"><?php _e('Inbox', 'myarcadetheme'); ?> <?php echo $unread_count_str; ?></a></li>

                  <?php elseif ( defined('BP_VERSION') ) : ?>
                    <?php global $bp; ?>
                    <?php if( bp_is_active('activity') ) : ?>
                      <li><a href="<?php echo $bp->loggedin_user->domain . BP_ACTIVITY_SLUG . '/'; ?>"><?php _e('Activity', 'myarcadetheme'); ?></a></li>
                    <?php endif; ?>
                    <li><a href="<?php echo site_url( bp_get_members_root_slug() ); ?>"><?php _e('Members', 'myarcadetheme'); ?></a></li>
                    <?php if( bp_is_active('groups') ) : ?>
                      <li><a href="<?php echo $bp->loggedin_user->domain . BP_GROUPS_SLUG . '/'; ?>"><?php _e('Groups', 'myarcadetheme'); ?></a></li>
                    <?php endif; ?>
                    <?php if ( bp_is_active( 'friends' ) ) : ?>
                      <li><a href="<?php echo $bp->loggedin_user->domain . BP_FRIENDS_SLUG . '/'; ?>"><?php _e('Friends', 'myarcadetheme'); ?></a></li>
                    <?php endif; ?>
                    <li><a href="<?php echo $bp->loggedin_user->domain ?>"><?php _e('Profile', 'myarcadetheme'); ?></a></li>
                    <?php if ( isset($bp->myscore) ) : ?>
                      <li><a href="<?php echo $bp->loggedin_user->domain . $bp->myscore->slug . '/'; ?>"><?php _e('My Scores', 'myarcadetheme'); ?></a></li>
                    <?php endif; ?>
                  <?php else: ?>
                    <li><a href="<?php echo home_url(); ?>/wp-admin/index.php"><?php _e('Go to Dashboard', 'myarcadetheme'); ?></a></li>
                    <li><a href="<?php echo home_url(); ?>/wp-admin/profile.php"><?php _e('Edit My Profile', 'myarcadetheme'); ?></a></li>
                  <?php endif; ?>
                </ul>
              </li>
              <li><a class="botn logout" href="<?php echo wp_logout_url( home_url() ); ?>" title="<?php _e('LOGOUT', 'myarcadetheme'); ?>"><?php _e('LOGOUT', 'myarcadetheme'); ?></a></li>
            </ul>
          </li>
        </ul>
        <?php if(function_exists('wpfp_list_favorite_posts')) { ?>
        <h4><?php _e('Your Favorite Games', 'myarcadetheme'); ?></h4>
        <?php wpfp_list_favorite_posts(); ?>
        <?php } ?>
        <?php
      } else {
        // user isn't logged in
        ?>
        <div class="blk-cn bbp_widget_login">
          <fieldset id="loginBox">
            <form action="<?php echo wp_login_url(); ?>" method="post">
              <label><input autocomplete="off" type="text" name="log" id="log" placeholder="<?php _e('username', 'myarcadetheme'); ?>"></label>
              <label><input autocomplete="off" type="password" name="pwd" id="pwd" placeholder="<?php _e('password', 'myarcadetheme'); ?>"></label>
              <input type="hidden" name="redirect_to" value="<?php echo home_url(); ?>/">
              <?php if ( function_exists( 'new_fb_sign_button' ) ) : ?>
                <a class="botn-flgn fa-facebook" href="<?php echo wp_login_url(); ?>?loginFacebook=1" onclick="window.location = '<?php echo wp_login_url(); ?>?loginFacebook=1; return false;"><?php _e('LOGIN WITH', 'myarcadetheme'); ?></a>
              <?php endif; ?>
              <input type="submit" name="Login" value="<?php _e('Login', 'myarcadetheme'); ?>" class="logininp">
            </form>
            <?php
            if( get_option('users_can_register') ) : ?>
              <p><a href="<?php echo wp_registration_url(); ?>"><?php _e('Register', 'myarcadetheme'); ?></a></p>
            <?php endif; ?>
            <p><a href="<?php echo wp_lostpassword_url(); ?>"><?php _e('Lost password?', 'myarcadetheme'); ?></a></p>

            </fieldset>
          </div>
          <?php
        }
      // <-- END --> HERE COMES THE OUPUT

        echo $after_widget;
      }

    // Update Widget
      function update($new_instance, $old_instance) {

        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);

        return $instance;
      }

    // Display Widget Control Form
      function form($instance) {
        global $wpdb;

        $instance = wp_parse_args((array) $instance, array('title' => __('User Panel', 'myarcadetheme')));

        $title = esc_attr($instance['title']);

        ?>

        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>">
            <?php _e('Title', 'myarcadetheme'); ?>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
          </label>
        </p>
        <?php
      }
    }
  }
  ?>