<?php 
global $page, $paged, $user_ID, $user_identity; 

$username=$_REQUEST['user']; 

// Work out which user we are trying to show the WL for
$zon_user_ID = -1;
$zon_user_identity = $user_identity;
$zon_user_name = '';
$user_logged_in = false;
 
if(!empty($username)) {

   $username_query = new WP_User_Query( array( 'search' => $username, 'search_columns' => array('user_login'),'number' => '1' ) );
   $results = $username_query->get_results();

   if(!empty($results)) {
      $zon_user_ID = $results[0]->ID;
      $zon_user_identity = $results[0]->display_name;
      $zon_user_name = $username;
   }
   else {
      wp_redirect( home_url() ); 
      exit;   
   }
}
else if(!is_user_logged_in()) {
   wp_redirect( home_url() ); 
   exit;   
}

if(is_user_logged_in()) {
   if($zon_user_ID == $user_ID || $zon_user_ID == -1) {
      $zon_user_ID = $user_ID;
      $user_logged_in = true;
      $current_user = wp_get_current_user();
      $zon_user_name = $current_user->user_login;
   }
}

$wishlist_url = get_bloginfo( 'url' ).'?action=wishlist&user='.$zon_user_name;
get_header();  

?>
<div class="container-fluid posts-wrapper">
 <div class="row-fluid">

<?php if(imwb_zonpress_get_option('wishlistsidebar') == '1' && is_active_sidebar("sidebar-wishlist-widget")) { ?>
<div id="post-sidebar" class="span4 content_sidebar left">
   <?php dynamic_sidebar("sidebar-wishlist-widget"); ?>
</div>
<?php } ?>

<div class="<?php echo (imwb_zonpress_get_option('wishlistsidebar') == '0' || !is_active_sidebar("sidebar-wishlist-widget")) ? 'span12' : 'span8';?> the-post wishlist-post"> 
	<header class="archive-header">
		<h1 class="archive-title"><?php echo $zon_user_identity.' '.imwb_zonpress_get_option('wishlisttext');?></h1>
	</header>
<?php

if($user_logged_in) { ?>

   <span class="share-wishlist alert alert-success alert-block"><h4>
   <?php 
   _e("Share This", "imwb_zonpress"); echo ' '.imwb_zonpress_get_option('wishlisttext').'</h4>'.$wishlist_url;
   
   if(imwb_zonpress_get_option('hideptlikestats') == 'N') { 
   
   $shareoptions = imwb_zonpress_get_option('wlshareoptions');
   if(!empty($shareoptions)) $shareoptions = explode(",", imwb_zonpress_get_option('wlshareoptions'));
   else $shareoptions = false;
   
   if($shareoptions !== false) {
   ?>   
    <div class="social-actions">
         <ul class="social-buttons">
         <?php if(in_array("T", $shareoptions)) { ?>
         <li class="story-share-tweet"><a href="javascript:imwb_zonpress_window_open('<?php echo 'http://twitter.com/share?url={URL}&text='.esc_js(imwb_zonpress_get_option('wlsharetext')); ?>', '<?php echo urlencode($wishlist_url);?>')"><?php _e("Tweet this", "imwb_zonpress");?></a></li>
         <?php } if(in_array("F", $shareoptions)) { ?>
         <li class="story-share-fb"><a href="javascript:imwb_zonpress_window_open('http://www.facebook.com/sharer.php?u={URL}&p[title]=test', '<?php echo urlencode($wishlist_url);?>')"><?php _e("Share to Facebook", "imwb_zonpress");?></a></li>
         <?php } if(in_array("P", $shareoptions)) { ?>
         <li class="story-share-pin"><a href="javascript:imwb_zonpress_window_open('<?php echo 'http://pinterest.com/pin/create/bookmarklet/?media='.imwb_zonpress_post_thumbnail_img_src().'&url={URL}&description='.esc_js(imwb_zonpress_get_option('wlsharetext')) ; ?>', '<?php echo urlencode($wishlist_url);?>')"><?php _e("Pin It", "imwb_zonpress");?></a></li>
         <?php } if(in_array("L", $shareoptions)) { ?>
         <li class="story-share-linkin"><a href="javascript:imwb_zonpress_window_open('<?php echo 'http://www.linkedin.com/shareArticle?mini=true&url={URL}&source='.esc_js(imwb_zonpress_get_option('wlsharetext')).'&token=&isFramed=false'; ?>', '<?php echo urlencode($wishlist_url);?>')"><?php _e("LinkedIn", "imwb_zonpress");?></a></li>
         <?php } if(in_array("G", $shareoptions)) { ?>
         <li class="story-share-gplus"><a href="javascript:imwb_zonpress_window_open('http://plus.google.com/share?url={URL}&authuser=0', '<?php echo urlencode($wishlist_url);?>')"><?php _e("Google+", "imwb_zonpress");?></a></li>
         <?php } ?>      
         </ul>
    </div>
   <?php } 
   
   } ?>
   </span>
<?php }
// The Query
$result = get_user_meta( $zon_user_ID, '_imwb_zonpress_user_wishlist', true );
$saved_list = explode(",", $result);

$args =  array( 'post_type' => 'post', 'ignore_sticky_posts' => 1, 'post__in' => $saved_list, 'posts_per_page' => -1 );
$the_query = new WP_Query( $args );

if ( have_posts() ) : ?>

   <ul class="wish-post-list">
   <?php
   // The Loop
   while ( $the_query->have_posts() ) :
   	$the_query->the_post();
   ?>
      <li>
      <a class="title" href="<?php echo imwb_zonpress_get_post_permalink('title'); ?>" alt="" <?php echo imwb_zonpress_get_post_link_target();?>><?php the_title(); ?></a>
      <a class="image" href="<?php echo imwb_zonpress_get_post_permalink('image'); ?>" alt="" <?php echo imwb_zonpress_get_post_link_target();?>><?php imwb_zonpress_the_post_thumbnail(get_the_ID(), "300", "250"); ?></a>
      <?php if($user_logged_in) { ?>
         <a class="del-wishlistlink btn btn-info" href="" data-post-item='<?php echo get_the_ID(); ?>'><?php echo imwb_zonpress_get_option('delwishlisttext');?></a>
      <?php } else { ?>
      	<div class="description">
      	   <?php echo do_shortcode(get_the_excerpt()); ?>
      	</div>
         <?php if(imwb_zonpress_is_product()) { ?>
         	<div class="actions clearfix">
         	  <a class="btn btn-success pull-right zp-ctr-track" data-post-item='<?php echo get_the_ID(); ?>' href="<?php echo imwb_zonpress_get_post_permalink('button'); ?>" title="<?php the_title_attribute(); ?>" <?php echo imwb_zonpress_get_post_link_target();?>><?php echo imwb_zonpress_get_option('postprdbtntxt');?></a>
         	  <?php if(imwb_zonpress_get_option('fphideprice') != "Y") imwb_zonpress_post_price();?>       
         	</div>
         <?php }    
      }?>
      
      </li>
   <?php endwhile; ?>
   </ul>
<?php endif; 

/* Restore original Post Data 
 * NB: Because we are using new WP_Query we aren't stomping on the 
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
?>
</div>
<?php if(imwb_zonpress_get_option('wishlistsidebar') == '2' && is_active_sidebar("sidebar-wishlist-widget")) { ?>
   <div id="post-sidebar" class="span4 content_sidebar right">
	   <?php dynamic_sidebar("sidebar-wishlist-widget"); ?>
   </div>
<?php } ?>
</div>
</div>
<?php get_footer(); ?>