<?php

// [bartag foo="foo-value"]
add_filter('widget_text', 'do_shortcode');
add_shortcode( 'pinterest_gallery', 'wp_pinterest_to_wp_shortcode_func' );


function wp_pinterest_to_wp_shortcode_func( $atts ) {
	
	$cont='';
	
	extract( shortcode_atts( array(
			'id' => 'something',
			
	), $atts ) );

	//var $id contains the id of the custom post type . let's extract needed
	//setup new WP_Query
	$wp_query = new WP_Query(
			array(
					'posts_per_page'    =>    -1,
					'post_type'            =>    'wp_pinterest_to_wp',
					'p' => $id
			)
	);
	
	 
	//begine loop
	//while ($wp_query->have_posts()) : $wp_query->the_post();
	
	//reading post meta
	global $post;
	$pid=$id;
	
	 
	if(! is_numeric($pid)) {
	
		$pid = preg_replace('{&.*?;}', '', $pid);
		
		//not numberic extract numbers
		preg_match_all('{\d+}', $pid,$matches);
		
		
		$ids = $matches[0];
		@$pid=$ids[0];
	
	}

	 
	$gallery_user=get_post_meta($pid,'gallery_user',1);
	$gallery_type=get_post_meta($pid,'gallery_type',1);
	$gallery_max=get_post_meta($pid,'gallery_max',1);
	$gallery_padding=get_post_meta($pid,'gallery_padding',1);
	$gallery_bg=get_post_meta($pid,'gallery_bg',1);
	$gallery_itm_width=get_post_meta($pid,'gallery_itm_width',1);
	$gallery_itm_height=get_post_meta($pid,'gallery_itm_height',1);
	$wp_pinterest_to_wp_ops=get_post_meta($pid,'wp_pinterest_to_wp_ops',1);
	$gallery_color=get_post_meta($pid,'gallery_color',1);
	$gallery_board=get_post_meta($pid,'gallery_board',1);
	$gallery_itms=get_post_meta($pid,'gallery_itms',1);
	
	 
	//link target
	if(in_array('OPT_LINK_BLANK', $wp_pinterest_to_wp_ops)){
		$target = ' target="_blank" ';
	}else{
		$target = '';
	}
	
	
 
	if(!is_array($gallery_itms)) $gallery_itms=array();
	 
	if(! is_array($wp_pinterest_to_wp_ops))$wp_pinterest_to_wp_ops=array();
	
	//get pins
	require_once 'core.php'; 
	$pinterest=new pinterest();
	
	if($gallery_type == 'pins' || $gallery_type == 'board'){
		
			if($gallery_type == 'pins'){
				$pins=$pinterest->wp_pins_get($pid,$gallery_user);
			}else{
				$pins=$pinterest->wp_pins_get($pid,$gallery_board,true);
			}
			
			 
		
		
		//echo count($pins[0]).'  pins found .';
		
		//<div style="background-size: 100% auto;width:200px;height:120px;border:1px solid #000;background-image: url('http://distilleryimage0.s3.amazonaws.com/899f276eec7f11e292eb22000a1fbd89_6.jpg')"></div>
		$ids =$pins[0];
		$imgs=$pins[1];
		$ttls=$pins[2];
		$pin_imgs_big=$pins[3];
		
		// if wide thumbnails,use full image
		if($gallery_itm_width > 300) $imgs = $pin_imgs_big;
		
		$i=0;
		$p = 0 ; // posted counter
		$coumn_width=$gallery_itm_width + 20 + 2 * $gallery_padding ;
		
		$cont.= '<div style="color:#'.$gallery_color.';background-image: url(\''. plugins_url('images/patterns/pattern'.$gallery_bg.'.png', __FILE__).'\');" class="wp_pinterest_to_wp_wraper"><div class="wp_pinterest_to_wp_container js-masonry"   data-masonry-options=\'{ "columnWidth": '.$coumn_width.', "itemSelector": ".wp_pinterest_to_wp_itm_contain" }\' >';
		
		if(in_array('OPT_GALLERY',$wp_pinterest_to_wp_ops)){
			$gallery_id=' rel="prettyPhoto[pp_'.$pid.']" ';
			$gallery_class="";
		}else{
			$gallery_class="wp_pinterest_to_wp_itm_pop";
			$gallery_id = '';
		}
		
		
		foreach($ids as $id){
			
			//check if checked to be viewed
			if(in_array($id,$gallery_itms,true)){
			 
				if($gallery_max > $p){
					
					//building description dev
					
					if(in_array('OPT_DESC', $wp_pinterest_to_wp_ops)){
						$display='block' ;
					}else{
						$display='none';
					}
					
					$desc='<div style="display:'.$display.';width:'.$gallery_itm_width.'px" class="wp_pinterest_to_wordpress_itm_desc">'.str_replace('/search', 'http://pinterest.com/search', urldecode($ttls[$i]) ) .'</div>';
					
				
					
					$linkme='';
					if(in_array('OPT_LINK_POP',$wp_pinterest_to_wp_ops)){
						$linkme='<a class="wp_pinterest_to_wordpress_link" '.$target.' href="http://pinterest.com/pin/'.$ids[$i].'"></a>';
						
						$desc=$desc.'<div class="wp_pinterest_to_wp_itm_lnk" style="display:none">'.$linkme.'</div>';
					}
					
					//building link 
					if(in_array('OPT_LINK', $wp_pinterest_to_wp_ops)){
						$href=' '.$target.' href= "http://pinterest.com/pin/'.$id.'"';
					}else{
						$href=' '.$gallery_id.' href="'.stripslashes($pin_imgs_big[$i]).'" link="'.stripslashes($pin_imgs_big[$i]).'" class="'.$gallery_class.'"  target="blank"';
					}
					
					$cont.= '<div style="margin:'.$gallery_padding.'px"    class="wp_pinterest_to_wp_itm_contain">  <a '.$href.' > <div class="wp_pinterest_to_wp_itm" style="width:'.$gallery_itm_width.'px;height:'.$gallery_itm_height.'px;background-image: url(\''.$imgs[$i].'\')"></div><div style="clear:both"></div></a> '.$desc.' </div>';
					$p++;
				}//gallery max
				
			}//check
				
			$i++;
			
		}//foreach id
		
		$cont.= '<div class="clear"></div></div></div>';
		return $cont;
		
	}else{
		//boards
		$boards=$pinterest->wp_boards_get($pid,$gallery_user);
		//echo count($boards[0]).'  boards found .';

		
		
		$ids=$boards[0];
		$ttls=$boards[1];
		$links=$boards[2];
		$imgs=$boards[3];
		
		$i=0;
		$p=0;
		$coumn_width=$gallery_itm_width + 20 + 2 * $gallery_padding ;
		
		$cont.= '<div style="background-image: url(\''. plugins_url('images/patterns/pattern'.$gallery_bg.'.png', __FILE__).'\');" class="wp_pinterest_to_wp_wraper"><div class="wp_pinterest_to_wp_container js-masonry"   data-masonry-options=\'{ "columnWidth": '.$coumn_width.', "itemSelector": ".wp_pinterest_to_wp_itm_gallery" }\' >';
		
		foreach($ids as $id){
		 
			if(in_array($id,$gallery_itms,true)){
				 
			
				if($gallery_max > $p){
			
					//building description dev
			
					if(in_array('OPT_DESC', $wp_pinterest_to_wp_ops)){
						
						//count enabled ?
						if(in_array('OPT_COUNT', $wp_pinterest_to_wp_ops)){
							
							
							$query="SELECT count(*) as count FROM wp_pinterest_to_wp_pins where pin_board ='".$links[$i]."'    and pin_gallery='$pid'";
							 
							$rows=$pinterest->db->get_results($query);
							
							if(count($rows)>0){
								$count=$rows[0]->count;
							}else{
								$count='';
							}
							
							 
							$count='<small>('.$count.') photos</small>';
						}else{
							$count='';
						}
						
						$desc='<strong>'.$ttls[$i].'</strong>'.$count;
					
					}else{
						$desc='';
					}
			
					//building link
					if(in_array('OPT_LINK', $wp_pinterest_to_wp_ops)){
						$href='"http://pinterest.com'.$links[$i].'" '.$target;
						$jsclass='';
					}else{
						$href='"#" ';
						$jsclass='wp_pinterest_to_wp_itm_gallery_pop';
					}
			
					//echo '<div style="margin:'.$gallery_padding.'px"  title="'.urldecode($ttls[$i]).'" class="wp_pinterest_to_wp_itm_contain">  <a href='.$href.' > <div class="wp_pinterest_to_wp_itm" style="width:'.$gallery_itm_width.'px;height:'.$gallery_itm_height.'px;background-image: url(\''.$imgs[$i].'\')"></div><div style="clear:both"></div></a> '.$desc.' </div>';
					$cont.= '<a id="'.$id.'" style="color:#'.$gallery_color.';width:'.$gallery_itm_width.'px;margin:'.$gallery_padding.'px;padding-top:'.($gallery_itm_height + 20) .'px" href='.$href.' class="'.$jsclass.' wp_pinterest_to_wp_gallery wp_pinterest_to_wp_itm_gallery"><div class="outer" ><div style="width:'.$gallery_itm_width.'px; height:'.$gallery_itm_height.'px "></div></div><div class="outer"><div style="width:'.$gallery_itm_width.'px; height:'.$gallery_itm_height.'px "></div></div><div class="outer" ><div style="width:'.$gallery_itm_width.'px; height:'.$gallery_itm_height.'px "></div></div><div class="outer" ><div style="background-image: url(\''.$imgs[$i].'\');width:'.$gallery_itm_width.'px; height:'.$gallery_itm_height.'px "></div></div>'.$desc.'</a>';
					
					$p++;
					
				}//gallery max >i
			}// in array
			
			$i++;
		}
		
		$cont.= '<div class="clear"></div></div></div>';
		
		// getting pins for displayed boards to show in gallery 
		$i=0;
		$p=0;
		$allpins=array();
		$allpinsjs='';
		foreach ($ids as $id){
			if(in_array($id,$gallery_itms,true)){ 
			if($gallery_max >$p ){
				
				$pins=$pinterest->wp_pins_get($pid,$links[$i],true,$id);

				$n=0;
				
				if(is_array($pins[0])){
				
					foreach($pins[0]as $pin){
						
						//check link
						$linkme=''; 
						if(in_array('OPT_LINK_POP',$wp_pinterest_to_wp_ops)){
							$linkme='<a '.$target.' class="wp_pinterest_to_wordpress_link" href="http://pinterest.com/pin/'.$pins[0][$n].'"></a>';
						}
						
						//echo '<a href="'.$pin_imgs_big[$n].'" rel="prettyPhoto[pp_gal_'.$id.']" ><img src ="'.$imgs[$n].'"/></a>';
						if(in_array('OPT_DESC_POP', $wp_pinterest_to_wp_ops)){
							$desc=urldecode($pins[2][$n]);
							 
						}else{
							$desc='';
						}
						
						$pins[2][$n] = $desc.$linkme;
						$pins[3][$n] = stripslashes($pins[3][$n]);
						
						
						$n++;
					}
					
				
				$allpins[$id]=$pins;
				$allpinsjs.='wp_pinterest_to_wp_pins['.$id.']=addpins['.$id.'];';
				
				$p++;
				}
			}//gallery max
		}//in array
		$i++;
		}
		
		
		
		$cont.='<script type="text/javascript">
		
		var addpins='.json_encode($allpins).';
		
		if(typeof wp_pinterest_to_wp_pins === "undefined" ) {
		    wp_pinterest_to_wp_pins =  addpins  ;
		}else{
		    
			'.$allpinsjs.'	
		    
		}</script>'; 
		
		return $cont;
		 
	}
	
	
	
	 
	
	 
	
}


add_filter( 'the_content', 'wp_pinterest_sc_check' , 99999998);
function wp_pinterest_sc_check($cont){
		
	if(stristr($cont,'[pinterest_gallery')){
		//shortcode exists
		add_shortcode( 'pinterest_gallery', 'wp_pinterest_to_wp_shortcode_func' );
		 
		return do_shortcode($cont);
		
	}else{
		return $cont;
	}
}

