<?php
/*
Plugin Name: flexo-facebook-manager
Version: 1.0008
Contributors: flexostudio
Tags: facebook, comments, like, posts
Author: Grigor Grigorov, Mariela Stefanova, Flexo Studio Team
Plugin URI: http://www.flexostudio.com/wordpress-plugins-flexo-utils.html
*/

class flexoFBManager {
	public static $weight_comments = 1.5;
	public static $weight_likes		 = 1;
	/* =_ functions
	-------------------------------------------------------------------------------------- */		
	public static function get_url($post_id=null){
	  $permalink	=	get_permalink($post_id);
		$rurl = urlencode($permalink);
		return $rurl;	
	}
	
	/* =public functions
	-------------------------------------------------------------------------------------- */		
	public static function the_facebook(){
		
		echo get_facebook($post_id=null);
		
	}
	
	public static function get_facebook($post_id=null, $case='default'){

		return self::get_form($post_id,$case);
	}
	public static function get_fb_likes_field_name(){
		return "fb_likes";
	}
	
	
	/* =LIKES section
	-------------------------------------------------------------------------------------- */	
	public static function get_data($post_id=null){
		
		//ako e da
		$post_meta	=	get_option(fb_post_meta);
		if ($post_meta	==	'yes'){
			$graph 	= "http://graph.facebook.com/?id=".self::get_url($post_id);
			//ako e ne
			$obj		=	json_decode(file_get_contents($graph));
		}
		else {$obj		=	false;}
		return $obj;	
	}
	
	public static function get_likes($post_id=null){
		$obj	=	self::get_data($post_id);
		return $obj->shares;
	}
	
	public static function update_likes($post_id=null){
		if($post_id == null):
			$post_id = get_the_ID();
		endif;

		$data		=	self::get_data($post_id);
		$likes	=	intval($data->shares);//flexoFBManager::get_likes($post_id);
		$comments	=	intval($data->comments);
		
		$vars		=	self::post_vars($post_id);
		
		$update_score = false;
		
		self::save_custom($post_id,$field = "fb_likes" , $value = $likes);
		self::save_custom($post_id,$field = "fb_comments" , $value = $comments);
		self::save_custom($post_id,$field = "fb_score" , $likes * self::$weight_likes + $comments * self::$weight_comments + 1);
		self::save_custom($post_id,$field = "fb_need_update" , "no");
		
		return $likes;
	}
	
	public static function reset_data($post_id=null){
		if($post_id == null):
			$post_id = get_the_ID();
		endif;
		self::save_custom($post_id,$field = "fb_need_update" , $value = "yes");
	}
	
	/* =COMMENTS section
	-------------------------------------------------------------------------------------- */	
	public static function get_comments($post_id=null){
		$graph 	= "http://graph.facebook.com/comments/?ids=".self::get_url($post_id);
		$obj		=	json_decode(file_get_contents($graph));
		$url=urldecode(self::get_url($post_id));
	//	echo $url;
//		echo '<pre>';print_r($obj -> $url-> data);
		return count($obj -> $url-> data);
	}

	public static function update_comments($post_id=null){
		if($post_id == null):
			$post_id = get_the_ID();
		endif;

		$comments_raw	=	self::get_comments($post_id);
		$vars		=	self::post_vars($post_id);
		
		$comments	=	count($comments_raw);
		if($vars['comments'] !== $comments):
			self::save_custom($post_id,$field = "fb_comments" , $value = $comments);
		endif;
		
		return $comments;
	}	

	public static function update_all($post_id = null){
		self::update_likes($post_id);
		self::update_comments($post_id);
	}
	
	public static function reset_all($post_id = null) {
		self::reset_data($post_id);
	}
	
	/* =WP Section
	-------------------------------------------------------------------------------------- */		
	public static function post_vars($post_id=null){
		//todo if custom post_id
		$custom = get_post_custom();
		$ret			=	array(
			'likes'						=> 	$custom["fb_likes"][0], 
			'comments'				=> 	$custom["fb_comments"][0],
			'fb_check'				=>	$custom["fb_check"][0],
			'fb_pic'					=>	$custom["fb_pic"][0],
			'pic'							=>	$custom["pic"][0],
			'fb_need_update' 	=>	$custom["fb_need_update"][0],
			'fb_score'				=>	$custom["fb_score"][0],
			'url_pic'					=>	$custom["url_pic"][0],
			'fb_text'					=>	$custom["fb_text"][0],
			'custom_text'			=>	$custom["custom_text"][0],
		);
		return $ret;		
	}	
	
	public static function save_custom($post_id,$field,$value){
		update_post_meta($post_id, $field, $value);
	}
	  
	public static $moreJS;
	public static function js_unload($post_id=null){
		if($post_id == null):
			$post_id = get_the_ID();
		endif;
		$url = plugins_url( 'update.php' , __FILE__ );
		self::$moreJS .= '<script type="text/javascript">
			jQuery(window).unload(function() {
				jQuery.ajax({
						type: "GET",
						url: "'.$url.'",
						data: "p='.$post_id.'",
						success: function(msg){
						}				
				});
		});
		</script>';
	}
	
	public static function wp_footer(){
		//todo da se proveri zasto ne raboti pri chrome
		//echo self::$moreJS;
	}

	public static function get_cache_likes($post_id=null){
		$ret	=	0;
		$vars	=	self::post_vars($post_id);
		if($vars['likes'] == null || $vars['fb_need_update'] == 'yes'){
			$ret 	=	self::update_likes();
		}else{
			$ret	=	$vars['likes'];
		}
		return $ret;
	}
	
	
	public static function get_cache_comments($post_id=null){
		$ret	=	0;
		$vars	=	self::post_vars($post_id);
		if($vars['comments'] == null){
			$ret 	=	self::update_comments();
		}else{
			$ret	=	$vars['comments'];
		}
		return $vars['comments'];
	}	

	public static	function admin_on_post_show(){
	 	add_meta_box('add_fb', 'Add Facebook ', 'flexoFBManager::show_fb_manager', 'post', 'normal', 'high' );
	}
	public static	function admin_on_page_show(){
	 	add_meta_box('add_fb', 'Add Facebook ', 'flexoFBManager::show_fb_manager', 'page', 'normal', 'high' );
	}
	public static function show_fb_manager (){
		global $post;
		$data		=	self::post_vars($post_id=null);
		$check	=	$data['fb_check'];
		$fb_pic	=	$data['fb_pic'];
		$pic		=	$data['pic'];
		$url_pic=	$data['url_pic'];
		$fb_text			=	$data['fb_text'];
		$custom_text	=	$data['custom_text'];
		
		?>
		
		<div class="fb-manager">
		<ul>
			<li>
				<input type="radio" name="fb_check" value="no" checked <?php echo $check == 'no' ? 'checked' : ''; ?>/>no
			</li><li>
				<input type="radio" name="fb_check" value="yes" <?php echo $check == 'yes' ? 'checked' : ''; ?>/>All
			</li><li>
				<input type="radio" name="fb_check" value="like" <?php echo $check == 'like' ? 'checked' : ''; ?>/>Like
			</li><li>
				<input type="radio" name="fb_check" value="comments" <?php echo $check == 'comments' ? 'checked' :'';?>/>Comments
			</li>
		</ul>
		<br /><br /><br />
		<h3>Choose the text to show in Facebook:</h3>
		<ul>
			<li>
				<input type="radio" name="fb_text" value="content" checked <?php echo $fb_text == 'content' ? 'checked' : ''; ?>/>the content
			</li><li>
				<input type="radio" name="fb_text" value="excerpt" <?php echo $fb_text == 'excerpt' ? 'checked' : ''; ?>/>the excerpt
			</li><li>
				<input type="radio" name="fb_text" value="custom" <?php echo $fb_text == 'custom' ? 'checked' : ''; ?>/>custom text
			</li>
		</ul>
		<div class="clear"></div>
		<textarea name="custom_text"><?php if (!$custom_text){echo 'custom text';} else{ echo $custom_text;} ?></textarea>
		<div class="clear"></div>
		<h3>Choose which picture to show in Facebook:</h3>
			<ul>
				<li>
					<input type="radio" name="fb_pic" value="first_pic" checked <?php echo $fb_pic == 'first_pic' ? 'checked' : ''; ?>/>First image from the content
		
				</li><li>
					<input type="radio" name="fb_pic" value="thumb" <?php echo $fb_pic == 'thumb' ? 'checked' : ''; ?>/>thumbnill
				</li><li>
					<input type="radio" name="fb_pic" value="url_pic" <?php echo $fb_pic == 'url_pic' ? 'checked' : ''; ?>/>URL to picture
			</li>
					
						<textarea name="url_pic"  placeholder="URL to picture"><?php if (!$url_pic){echo 'URL to picture';} else{echo $url_pic;} ?> </textarea>
					
				<li style="border: none;">
					<input type="radio" name="fb_pic" value="attach" <?php echo $fb_pic == 'attach' ? 'checked' : ''; ?>/>attach new
							Picture
							<?php if(strlen($pic) > 0)echo "<img style='max-width:90%;' src='".$pic."'  />"; ?>
							<input class="file" type="file" style="width:100%;height: 15px;" size="0" name="pic"  ID="pic" value="<?php echo $pic; ?>" />
				</li><li style="clear: left;">
							<input type="checkbox" name="del" value="del picture">Delete Picture
				</li>
			</ul>
					<div class="clear"></div>
		</div>
		<?php 
	
		

	}
	
	public static function admin_on_post_save () {
		global $post;
	

	//	echo "<pre>";		print_r($_POST['fb_pic']);		echo "</pre>";exit;
				$files = array('pic');
	  foreach($files as $fname):
	  if(!empty($_FILES[$fname]['name'])){ //New upload
	   if(floatval($_FILES[$fname]['error']) > 0)
	    continue;
	
	   require_once( ABSPATH . 'wp-admin/includes/file.php' );
	   require_once(ABSPATH . 'wp-admin/includes/image.php');
	   require_once(ABSPATH . 'wp-admin/includes/media.php');
		
		 $override['action'] = 'editpost';
	   $overrdie['test_form'] = false;
	
	   
	   $uploaded_file = wp_handle_upload($_FILES[$fname], $override);
	   $filename   = $uploaded_file['file'];
	   
	   $post_id = $post->ID;
	   $attachment = array(
	    'post_title'    => $_FILES[$fname]['name'],
	    'post_content'   => '',
	    'post_type'    => 'attachment',
	    'post_parent'   => $post_id,
	    'post_mime_type'  => $_FILES[$fname]['type'],
	    'guid'       => $uploaded_file['url']
	    );
	    
	    $url   =  sprintf("%s", $uploaded_file['url']);
	    
	     if(strlen($url)>0){
			    update_post_meta($post->ID, $fname, $url);
			    $id = wp_insert_attachment( $attachment,$filename, $post_id );
			    wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $filename ) );
	   		}
	   		
	  }
		endforeach;
		if($_POST['fb_check']):
	   update_post_meta($post->ID, "fb_check", $_POST['fb_check']);
	  endif;
	  if($_POST['fb_pic']):
	   update_post_meta($post->ID, "fb_pic", $_POST['fb_pic']);
	  endif;
	  if($_POST['pic']):
	   update_post_meta($post->ID, "pic", $_POST['pic']);
	  endif;
	  if($_POST['del']):
	   update_post_meta($post->ID, "pic", '');
	  endif;
	  
	  if($_POST['url_pic']):
	   update_post_meta($post->ID, "url_pic", $_POST['url_pic']);
	  endif;
	   if($_POST['fb_text']):
	   update_post_meta($post->ID, "fb_text", $_POST['fb_text']);
	  endif;
	  if($_POST['custom_text']):
	   update_post_meta($post->ID, "custom_text", $_POST['custom_text']);
	  endif;
	}
	
	public static function include_facebook($content = ''){
		global $post;
		$post_id	=	$post->ID;
		$data			=	self::post_vars($post_id);
		$more			=	"";
		$l				=	"";
		$layout		=	get_option(fb_layout);
		$gallery_check = self::gallery_filter();
		
		if($gallery_check) {
			$ret	=	$content;
		}
		
		else {
					if(is_singular()){
						switch($data['fb_check']){
							case "yes"			: 
								if ($layout	==	'top'){
								$l		=	self::get_facebook($post_id,'like');
								$more = self::get_facebook($post_id,'comments'); 
								}
								else {
									 $more = self::get_facebook($post_id); 
								}
							break;
							
							case "like"			: $more = self::get_facebook($post_id,'like'); 			break;
							case "comments"	: $more = self::get_facebook($post_id,'comments'); 	break;
						}
			
						self::reset_all();
					
						}
						$ret =	$l.$content.$more;
					}
		return $ret;
	}
	
public static function fb_menu_display() {
					if (isset($_POST['submit'])):
							$fb_width1 = addslashes(htmlspecialchars($_POST['width']));
							$fb_appId1 = addslashes(htmlspecialchars($_POST['appId']));
							$fb_num_posts1 = addslashes(htmlspecialchars($_POST['num_posts']));
							$fb_feed1 = addslashes(htmlspecialchars($_POST['feed']));
							$fb_type1 = addslashes(htmlspecialchars($_POST['type']));
							$fb_xid1 = addslashes(htmlspecialchars($_POST['xid']));
							$fb_face1 = addslashes(htmlspecialchars($_POST['face']));
							$fb_send1 = addslashes(htmlspecialchars($_POST['send']));
							$fb_layout1 = addslashes(htmlspecialchars($_POST['layout']));
							$fb_header1 = (($_POST['header']));
							$fb_post_meta1 = addslashes(htmlspecialchars($_POST['post_meta']));
							
							update_option(fb_width, $fb_width1);
							update_option(fb_appId, $fb_appId1);
							update_option(fb_num_posts, $fb_num_posts1);
							update_option(fb_feed, $fb_feed1);
							update_option(fb_type, $fb_type1);
							update_option(fb_xid, $fb_xid1);
							update_option(fb_face, $fb_face1);
							update_option(fb_send, $fb_send1);
							update_option(fb_layout, $fb_layout1);
							update_option(fb_header, $fb_header1);
							update_option(fb_post_meta, $fb_post_meta1);
					endif;
	?>
	<style>
	.fb_admin  {
		padding:10px;
		}
		.fb_admin input{
			margin:10px;
		}
		
		.fb_admin  .layout {
			height:46px;
			width: 46px;
			background-repeat:no-repeat;
		}
		.fb_admin  .top {
			background:url('<?php echo $url = plugins_url( 'layout-2.png' , __FILE__ ); ?>');
		}
		.fb_admin  .bottom {
			background:url('<?php echo $url = plugins_url( 'layout-1.png' , __FILE__ ); ?>');
		}
		
		.fb_admin .left{
			float:left;
		}
		
		.fb_admin .clear{
			width:100%;
			clear:both;
		}
		</style>
		
		<div class="fb_admin">
			
		<form method="POST">
			</br>width<input type="text" name="width" value="<?php echo get_option(fb_width); ?>"/>
	
			</br>appId<input type="text" name="appId" value="<?php echo get_option(fb_appId); ?>"/>
	
			</br>num_posts<input type="text" name="num_posts" value="<?php echo get_option(fb_num_posts); ?>"/>
	
			</br>feed		<input type="radio" name="feed" value="true" <?php echo get_option(fb_feed) == 'true' ? 'checked' : ''; ?>/>true
									<input type="radio" name="feed" value="false" <?php echo get_option(fb_feed) == 'false' ? 'checked' : ''; ?>/>false
			</br>Faces	<input type="radio" name="face" value="true" <?php echo get_option(fb_face) == 'true' ? 'checked' : ''; ?>/>true
									<input type="radio" name="face" value="false" <?php echo get_option(fb_face) == 'false' ? 'checked' : ''; ?>/>false
			
			</br>Send	<input type="radio" name="send" value="true" <?php echo get_option(fb_send) == 'true' ? 'checked' : ''; ?>/>true
									<input type="radio" name="send" value="false" <?php echo get_option(fb_send) == 'false' ? 'checked' : ''; ?>/>false
			</br> iframe don't supported Send Button
							
			</br>Comments	Header<input type="text" name="header" value="<?php echo get_option(fb_header); ?>"/>
			
			
		</br>Layout 
					<div class="clear"></div> 
							<div class="left">
								<div class="layout bottom"></div>
								<input  type="radio" name="layout" value="top" <?php echo get_option(fb_layout) == 'top' ? 'checked' : ''; ?>/> top
							</div>

							<div class="left">
								<div class="layout top" ></div>
								<input  type="radio" name="layout" value="bottom" <?php echo get_option(fb_layout) == 'bottom' ? 'checked' : ''; ?>/>bottom
							</div>
							
				<div class="clear"></div> 
								 
		</br>Write "Likes" and "Comments" as Post Meta Vars<input type="radio" name="post_meta" value="yes" <?php echo get_option(fb_post_meta) == 'yes' ? 'checked' : ''; ?>/>yes
								 <input type="radio" name="post_meta" value="no" <?php echo get_option(fb_post_meta) == 'no' ? 'checked' : ''; ?>/>no
			
			</br>Type: 	
				<input type="radio" name="type" value="html" <?php echo get_option(fb_type) == 'html' ? 'checked' : ''; ?>/>HTML5
				<input type="radio" name="type" value="xfbml" <?php echo get_option(fb_type) == 'xfbml' ? 'checked' : ''; ?>/>XFBML
				<input type="radio" name="type" value="iframe" <?php echo get_option(fb_type) == 'iframe' ? 'checked' : ''; ?>/>IFRAME
			
				</br>XID (only for iframe): 	
				<input type="radio" name="xid" value="url" <?php echo get_option(fb_xid) == 'url' ? 'checked' : ''; ?>/>Post url
				<input type="radio" name="xid" value="p_id" <?php echo get_option(fb_xid) == 'p_id' ? 'checked' : ''; ?>/>Post ID
			</br><input type="submit" name="submit" value="Save">
	</form>
		</div>

	<?php
			
					
				
	}

	public static function fb_id(){
		$fb_appId1=get_option(fb_appId);
		return $fb_appId1;
	}
	
	public static function get_form($post_id=null,$type='default',$url="") {
		
		if (!$url)
 			$rurl	= self::get_url($post_id);
		else
			$rurl = urlencode($url);
		
 		$fb_width1		=	get_option(fb_width);
 		$fb_appId1		=	self::fb_id();
 		$fb_num_posts1=	get_option(fb_num_posts);
 		$fb_feed1			=	get_option(fb_feed);
 		$fb_type1			=	get_option(fb_type);
 		$fb_send1			=	get_option(fb_send);
 		$fb_face1			=	get_option(fb_face);
 		$fb_header1		=	get_option(fb_header);
 		$arg					=	array(
 		 		'fb_width1'			=>	$fb_width1,
		 		'fb_appId1'			=>	$fb_appId1,
		 		'fb_num_posts1'	=>	$fb_num_posts1,
		 		'fb_feed1'			=>	$fb_feed1,
		 		'fb_type1'			=>	$fb_type1,
		 		'fb_face1'			=>	$fb_face1,
		 		'fb_send1'			=>	$fb_send1,
		 		'fb_header1'		=>	$fb_header1,
		 		'post_id'				=>	$post_id,
 		);
 		//echo '<pre>';print_r($arg);echo '</pre>';
 		switch($fb_type1):
 			case 'html':
 				$form = self::get_html_form($rurl,$arg,$type);
			break;
			
			case 'xfbml':
					$form = self::get_xfbml_form($rurl,$arg,$type);
			break;
			case 'iframe':
		 		 $form = self::get_iframe_form($rurl,$arg,$type);
				break;
			
 		endswitch;
	return $form;
		
	}
	
	public static function get_html_form ($rurl,$arg,$case){
			//echo '<pre>';print_r($arg);echo '</pre>';
		switch($case):
			case 'default':
				$form = '<div class="fb_header">'.$arg['fb_header1'].'</div>
						<div id="fb-root"></div>
						<script>(function(d, s, id) {
					  var js, fjs = d.getElementsByTagName(s)[0];
					  if (d.getElementById(id)) {return;}
					  js = d.createElement(s); js.id = id;
					  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId='.$arg['fb_appId1'].'";
					  fjs.parentNode.insertBefore(js, fjs);
						}(document, "script", "facebook-jssdk"));</script>
						<div class="fb-like" data-send="'.$arg['fb_send1'].'" data-width="'.$arg['fb_width1'].'" data-show-faces="'.$arg['fb_face1'].'"></div> 
						<div class="fb-comments" data-href="'.$rurl.'" data-num-posts="'.$arg['fb_num_posts1'].'" data-width="'.$arg['fb_width1'].'"></div>';
					break;
					
				case 'comments':
		 			$form ='<div class="fb_header">'.$arg['fb_header1'].'</div>
		 				<div id="fb-root"></div>
							<script>(function(d, s, id) {
						  var js, fjs = d.getElementsByTagName(s)[0];
						  if (d.getElementById(id)) {return;}
						  js = d.createElement(s); js.id = id;
						  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId='.$arg['fb_appId1'].'";
						  fjs.parentNode.insertBefore(js, fjs);
						}(document, "script", "facebook-jssdk"));</script> 
						<div class="fb-comments" data-href="'.$rurl.'" data-num-posts="'.$arg['fb_num_posts1'].'" data-width="'.$arg['fb_width1'].'"></div>';
				break;	
				case 'like':
					$form = '<div id="fb-root"></div>
							<script>(function(d, s, id) {
						  var js, fjs = d.getElementsByTagName(s)[0];
						  if (d.getElementById(id)) {return;}
						  js = d.createElement(s); js.id = id;
						  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId='.$arg['fb_appId1'].'";
						  fjs.parentNode.insertBefore(js, fjs);
							}(document, "script", "facebook-jssdk"));</script>
							<div class="fb-like" data-send="'.$arg['fb_send1'].'" data-width="'.$arg['fb_width1'].'" data-show-faces="'.$arg['fb_face1'].'"></div> ';
					break;	
 			endswitch;
		return $form;
	}
	
	public static function get_xfbml_form($rurl,$arg,$case) {
		
		switch($case):
			case 'default':
				$form = '<div class="fb_header">'.$arg['fb_header1'].'</div>
						<div id="fb-root"></div>
						<script>(function(d, s, id) {
					  var js, fjs = d.getElementsByTagName(s)[0];
					  if (d.getElementById(id)) {return;}
					  js = d.createElement(s); js.id = id;
					  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId='.$arg['fb_appId1'].'";
					  fjs.parentNode.insertBefore(js, fjs);
						}(document, "script", "facebook-jssdk"));</script>
						<html xmlns:fb="http://ogp.me/ns/fb#">
							<fb:like href="'.$rurl.'" send="'.$arg['fb_send1'].'" width="'.$arg['fb_width1'].'" show_faces="'.$arg['fb_face1'].'"></fb:like>
							<fb:comments href="'.$rurl.'" num_posts="'.$arg['fb_num_posts1'].'" width="'.$arg['fb_width1'].'"></fb:comments>';
					break;
					
				case 'comments':
		 			$form ='<div class="fb_header">'.$arg['fb_header1'].'</div>
		 			<div id="fb-root"></div>
						<script>(function(d, s, id) {
						  var js, fjs = d.getElementsByTagName(s)[0];
						  if (d.getElementById(id)) {return;}
						  js = d.createElement(s); js.id = id;
						  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId='.$arg['fb_appId1'].'";
						  fjs.parentNode.insertBefore(js, fjs);
						}(document, "script", "facebook-jssdk"));</script>
						<html xmlns:fb="http://ogp.me/ns/fb#"> 
						<fb:comments href="'.$rurl.'" num_posts="'.$arg['fb_num_posts1'].'" width="'.$arg['fb_width1'].'"></fb:comments>';
				break;	
				case 'like':
					$form = '<div id="fb-root"></div>
							<script>(function(d, s, id) {
						  var js, fjs = d.getElementsByTagName(s)[0];
						  if (d.getElementById(id)) {return;}
						  js = d.createElement(s); js.id = id;
						  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId='.$arg['fb_appId1'].'";
						  fjs.parentNode.insertBefore(js, fjs);
							}(document, "script", "facebook-jssdk"));</script>
							<html xmlns:fb="http://ogp.me/ns/fb#">
							<fb:like href="'.$rurl.'" send="'.$arg['fb_send1'].'" width="'.$arg['fb_width1'].'" show_faces="'.$arg['fb_face1'].'"></fb:like>';
					break;	
 			endswitch;
		return $form;
	}

	public static function  get_iframe_form ($rurl,$arg,$case) {
	
		$fb_xid1=get_option(fb_xid);
		if ($fb_xid1 == 'p_id')
				$xid=$arg['post_id'];
		else 
				$xid=$rurl;
		
		
		if ($arg['fb_face1'] == 'true') $height='70';
		else 	$height = '35';
		
		switch($case):
			case 'default':
				
				$form = '<div class="fb_header">'.$arg['fb_header1'].'</div>
				<iframe src="//www.facebook.com/plugins/like.php?href='.$rurl.'&amp;send='.$arg['fb_send1'].'&amp;layout=standard&amp;width='.$arg['fb_width1'].'&amp;show_faces='.$arg['fb_face1'].'&amp;action=like&amp;colorscheme=light&amp;font=tahoma&amp;height='.$height.'px&amp;appId='.$arg['fb_appId1'].'" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:'.$arg['fb_width1'].'; height:'.$height.'px;" allowTransparency="true"></iframe><div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId='.$arg['fb_appId1'].'&amp;xfbml=1"></script>
				<fb:comments xid="'.$xid.'" numposts="'.$arg['fb_num_posts1'].'" width="'.$arg['fb_width1'].'" publish_feed="'.$arg['fb_feed1'].'"></fb:comments>';
				
			break;
			case 'like':
			
				$form = '<iframe src="//www.facebook.com/plugins/like.php?href='.$rurl.'&amp;send='.$arg['fb_send1'].'&amp;layout=standard&amp;width='.$arg['fb_width1'].'&amp;show_faces='.$arg['fb_face1'].'&amp;action=like&amp;colorscheme=light&amp;font=tahoma&amp;height='.$height.'px&amp;appId='.$arg['fb_appId1'].'" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:'.$arg['fb_width1'].'; height:'.$height.'px;" allowTransparency="true"></iframe>';
				
				
			break;
			case 'comments':
					$form = '<div class="fb_header">'.$arg['fb_header1'].'</div>
					<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#appId='.$arg['fb_appId1'].'&amp;xfbml=1"></script><fb:comments xid="'.$xid.'" numposts="'.$arg['fb_num_posts1'].'" width="'.$arg['fb_width1'].'" publish_feed="'.$arg['fb_feed1'].'"></fb:comments>';
			break;
		endswitch;
	return $form;
	}
	public static	function catch_first_image() {
		 global $post, $posts;
		 $first_img = '';
		 ob_start();
		 ob_end_clean();
		 $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		 $first_img = $matches [1] [0];
	
		 return $first_img;
	}
	public static function fb_menu () {
			add_options_page('fb settings','fb manager','manage_options','fb-manager','flexoFBManager::fb_menu_display','');
	
	}
	 
	 public static function gallery_filter(){
	 	
	 	$pattern	=	"?sgimage=";
	 	$url	='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	 	$ret = strpos($url, $pattern) === false ? false : true;	
	 return $ret; 
	}
		public static function wp_head(){
			self::open_graph_check();
		}
		
		public static function admin_head(){
			include('admin.css.php');
		}
		public static function open_graph_check(){

			global $post;
			$post_id	=	$post->ID;
			$data			=	self::post_vars($post_id);
			
			$perm	='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		
			if ($data['fb_pic']			== 'attach') 			{$pic_url	=	$data['pic'];}
			elseif ($data['fb_pic'] == 'first_pic') 	{$pic_url	=	self::catch_first_image();}
			elseif ($data['fb_pic'] == 'thumb')				{
				
					if(has_post_thumbnail($post_id)) {
					 $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
					}
				$pic_url	=	 $thumb[0];
			}
			else 																			{$pic_url	=	$data['url_pic'];}
			
			if ($data['fb_text'] 		 == 'content') 		{$text1	=	$post-> post_content;}
			elseif ($data['fb_text'] == 'excerpt')		{$text1	=	$post-> post_excerpt;}
			else 																			{$text1	=	$data['custom_text'];}
				$text = strip_tags($text1); 
			echo '<link rel="canonical" href="'.$perm.'"/>'."\n";
			remove_action('wp_head','rel_canonical');
	//	echo'<pre>';	print_r($post);	echo'</pre>';
			?>
		<meta property="og:title" content="<?php echo  $post->post_title; ?>" />
    <meta property="og:description" content="<?php echo $text; ?>" />			
    <meta property="og:locale" content="<?php echo self::parse_locale(get_bloginfo('language')); ?>" />	
    <meta property="og:type" content="blog"/>
    <meta property="og:url" content="<?php echo $perm; ?>"/>
    <meta property="og:image" content="<?php echo $pic_url; ?>"/>
    <meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>
    <meta property="fb:app_id" content="<?php echo flexoFBManager::fb_id(); ?>"/>
    <?php /*<meta property="fb:admins" content="1423356739"/> */ ?>
			<?php
		}
	
	public static function parse_locale($_lang){
		//$lang	=	"en_US";
		$lang = "bg_BG";
		
		return $lang;
	}
}//class

/* =Add admin menu
------------------------------------------*/
if(function_exists('add_action')):
	add_action('admin_menu', 'flexoFBManager::fb_menu');
	add_option('fb_width','600','fb_width');
	add_option('fb_appId','','fb_appId');
	add_option('fb_num_posts','10','fb_num_posts');
	add_option('fb_feed','true','fb_feed');
	add_option('fb_type','html','fb_type');
	add_option('fb_xid','url','fb_xid');
	add_option('fb_face','false','fb_face');
	add_option('fb_send','false','fb_send');
	add_option('fb_layout','bottom','fb_layout');
	add_option('fb_header','','fb_header');
	add_option('fb_post_meta','yes','fbpost_meta');
	
	
	add_filter('the_content', 'flexoFBManager::include_facebook', 1);
	add_action('admin_menu', 'flexoFBManager::admin_on_post_show' );
	add_action('admin_menu', 'flexoFBManager::admin_on_page_show' );
	add_action('save_post', 'flexoFBManager::admin_on_post_save');
	add_action('save_page', 'flexoFBManager::admin_on_post_save');
	add_action('wp_footer', 'flexoFBManager::wp_footer');
	add_action('wp_head'	,	'flexoFBManager::wp_head',1);
	add_action('admin_head', 'flexoFBManager::admin_head');
endif;

?>