<?php 
add_action( 'after_setup_theme', 'teo_setup' );
if ( ! function_exists( 'teo_setup' ) ){
	function teo_setup(){
		load_theme_textdomain('Aruna', get_template_directory() .'/languages');
		require get_template_directory() . '/teoPanel/redux-framework.php';
		require get_template_directory() . '/teoPanel/Redux/ReduxCore/framework.php';
		require get_template_directory() . '/teoPanel/options-config.php';
		// require get_template_directory() . '/teoPanel/sample-config.php';
		require get_template_directory() . '/teoPanel/custom-functions.php';
		require get_template_directory() . '/includes/comments.php';
		require get_template_directory() . '/includes/widgets.php';
		require get_template_directory() . '/teoPanel/custom_metaboxes/meta_boxes.php';
	}
}

add_action('init', 'teo_thumbnails');
if( !function_exists('teo_thumbnails')) {
	function teo_thumbnails() {
		add_theme_support( 'post-thumbnails');
	}
}

/**
 * Save post metadata when a post is saved.
 *
 * @param int $post_id The ID of the post.
 * @param post $post the post.
 */
function teo_create_video_thumbnail( $post_id, $post ) {

	if ($post->post_date != $post->post_modified) {
		return;
	}

    // - Update the post's metadata.

    $format = get_post_format($post_id);
	global $current_user; get_currentuserinfo();
	$userid = $current_user->ID;
    if($format == 'video') {
    	$video = get_post_meta($post_id, '_video_externalvideo', true);
		if($video != '') {
			if (strpos($video,'youtube') !== false) {
				//it's youtube, we need toget the thumbnail! :)
				//we get the id of the video first
				$videotemp = explode('/watch?v=', $video);
				if(isset($videotemp[1]) && $videotemp[1] != '') {
				    $idvideo = $videotemp[1];
				    //we have the id, now we put the thumbnail
					$url_video = 'http://i1.ytimg.com/vi/' . $idvideo . '/hqdefault.jpg';
				}
			}
			elseif (strpos($video,'vimeo') !== false) {
				//it's youtube, we need toget the thumbnail! :)
				//we get the id of the video first
				$videotemp = explode('vimeo.com/', $video);
				if(isset($videotemp[1]) && $videotemp[1] != '') {
				    $idvideo = $videotemp[1];
				    //we have the id, now we put the thumbnail
				    $json_vimeo = 'http://vimeo.com/api/v2/video/' . $idvideo . '.json';
					$ch = curl_init();
					$timeout = 5;
					curl_setopt($ch, CURLOPT_URL, $json_vimeo);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
					$json_vimeo = curl_exec($ch);
					curl_close($ch);
					$jsontemp = json_decode($json_vimeo, true);
					if(isset($jsontemp[0]) && isset($jsontemp[0]['thumbnail_large']) && $jsontemp[0]['thumbnail_large'] != '') {
			 			$url_video = $jsontemp[0]['thumbnail_large'];
			 		}
				}
			}
			if(isset($url_video) && $url_video != '') {
				//we have an external youtube image :) we save it with the cURL library
				$filedetails = pathinfo($url_video);
				$mime = '';
				if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
					$mime = 'image/jpeg';
				}
				elseif($filedetails['extension'] == 'png') {
					$mime = 'image/png';
				}
				elseif($filedetails['extension'] == 'gif') {
					$mime = 'image/gif';
				}
				if($mime != '') {
				    require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
					$ch = curl_init($url_video);
					$uploaddir = wp_upload_dir();
					$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
					//we add some random digits to make sure it's unique
					$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
					curl_setopt($ch, CURLOPT_FILE, $fp);
					curl_setopt($ch, CURLOPT_HEADER, 0);
					curl_exec($ch);
					curl_close($ch);
					fclose($fp);

					$guid = trailingslashit($uploaddir['url'])  . $filename;
					$attachment = array(
						'post_mime_type' => $mime,
						'guid' => $guid,
						'post_title' => '[External image] : ' . $filedetails['filename'] ,
						'post_content' => '',
						'post_author' => $userid,
						'post_status' => 'inherit',
						'post_date' => date( 'Y-m-d H:i:s' ),
						'post_date_gmt' => date( 'Y-m-d H:i:s' )
					);

					// Add the file to the media library and generate thumbnail.
					$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['path']) . $filename, $post_id );
					$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					wp_update_attachment_metadata( $attach_id,  $attach_data );
				}
				update_post_meta($post_id, '_thumbnail_id', $attach_id);
			}
		}
    }
}
add_action( 'save_post', 'teo_create_video_thumbnail', 11, 2 );


//teo_update_thumbnails();
function teo_update_thumbnails() {

	$myposts = get_posts('posts_per_page=-1');
	foreach ( $myposts as $post ) : setup_postdata( $post );
		$format = get_post_format($post->ID);
		if($format == 'video') {
			$video = get_post_meta($post->ID, '_video_externalvideo', true);
			if($video != '') {
				if (strpos($video,'youtube') !== false) {
					//it's youtube, we need toget the thumbnail! :)
					//we get the id of the video first
					$videotemp = explode('/watch?v=', $video);
					if(isset($videotemp[1]) && $videotemp[1] != '') {
					    $idvideo = $videotemp[1];
					    //we have the id, now we put the thumbnail
					    $url_video = 'http://i1.ytimg.com/vi/' . $idvideo . '/hqdefault.jpg';
					}
				}
				elseif (strpos($video,'vimeo') !== false) {
					//it's youtube, we need toget the thumbnail! :)
					//we get the id of the video first
					$videotemp = explode('vimeo.com/', $video);
					if(isset($videotemp[1]) && $videotemp[1] != '') {
					    $idvideo = $videotemp[1];
					    //we have the id, now we put the thumbnail
					    $json_vimeo = 'http://vimeo.com/api/v2/video/' . $idvideo . '.json';
					    $ch = curl_init();
						$timeout = 5;
						curl_setopt($ch, CURLOPT_URL, $json_vimeo);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
						curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
						$json_vimeo = curl_exec($ch);
						curl_close($ch);
					    $jsontemp = json_decode($json_vimeo, true);
					    if(isset($jsontemp[0]) && isset($jsontemp[0]['thumbnail_large']) && $jsontemp[0]['thumbnail_large'] != '') {
			 				$url_video = $jsontemp[0]['thumbnail_large'];
			 			}
					}
				}
				if(isset($url_video) && $url_video != '') {
					//we have an external youtube image :) we save it with the cURL library
					$filedetails = pathinfo($url_video);
					$mime = '';
					if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
						$mime = 'image/jpeg';
					}
					elseif($filedetails['extension'] == 'png') {
						$mime = 'image/png';
					}
					elseif($filedetails['extension'] == 'gif') {
						$mime = 'image/gif';
					}
					if($mime != '') {
					    require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
						$ch = curl_init($url_video);
						$uploaddir = wp_upload_dir();
						$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
						//we add some random digits to make sure it's unique
						$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
						curl_setopt($ch, CURLOPT_FILE, $fp);
						curl_setopt($ch, CURLOPT_HEADER, 0);
						curl_exec($ch);
						curl_close($ch);
						fclose($fp);

						$guid = trailingslashit($uploaddir['url'])  . $filename;
						$attachment = array(
							'post_mime_type' => $mime,
							'guid' => $guid,
							'post_title' => '[External image] : ' . $filedetails['filename'] ,
							'post_content' => '',
							'post_author' => $user_id,
							'post_status' => 'inherit',
							'post_date' => date( 'Y-m-d H:i:s' ),
							'post_date_gmt' => date( 'Y-m-d H:i:s' )
						);

						// Add the file to the media library and generate thumbnail.
						$attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['path']) . $filename, $post->ID );
						$attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
						wp_update_attachment_metadata( $attach_id,  $attach_data );
					}
					update_post_meta($post->ID, '_thumbnail_id', $attach_id);
				}
			}
		}
	endforeach;
}


add_action('init', 'teo_sidebars') ;
function teo_sidebars() {
	$args = array(
				'name'          => 'Right sidebar',
				'before_widget' => '<div id="%1$s" class="widget %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<h2>',
				'after_title'   => '</h2><hr />' );
	register_sidebar($args);

	$args = array(
				'name'          => 'Left sidebar',
				'before_widget' => '<div id="%1$s" class="widget %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<h2>',
				'after_title'   => '</h2><hr />' );
	register_sidebar($args);

	$args = array(
				'name'          => 'Footer sidebar',
				'before_widget' => '<div id="%1$s" class="col-sm-4 %2$s"><div class="footer-widget"><div class="widget-content">',
				'after_widget'  => '</div></div></div>',
				'before_title'  => '<h2>',
				'after_title'   => '</h2><hr />' );
	register_sidebar($args);
}
// Loading js files into the theme
add_action('wp_head', 'teo_scripts');
if ( !function_exists('teo_scripts') ) {
	function teo_scripts() {
		global $redux_demo;
		wp_enqueue_script( 'libraries', get_template_directory_uri() . '/js/libraries.js', '1.0');
		wp_enqueue_script( 'main', get_template_directory_uri() . '/js/main.js', '1.0');
		wp_localize_script( 'main', 'MyAjax', array( 'ajaxurl' => admin_url('admin-ajax.php') ) );
		if($redux_demo['enable_keyboard'] != '2') {
			wp_enqueue_script( 'key-shortcuts', get_template_directory_uri() . '/js/key_shortcuts.js', '1.0');
		}

		if ( is_singular() && get_option( 'thread_comments' ) )
    		wp_enqueue_script( 'comment-reply' );
	}

}

add_action('wp_head', 'teo_opengraph');
if ( !function_exists('teo_opengraph') ) {
	function teo_opengraph() {
		if(is_single() || is_page() ) {
			global $post;
			//we have a single post so we set open graph tags
			if(isset($post) && is_object($post) ) {
				$type = get_post_format($post->ID);
				$thumb = '';
	            if($type == 'image' || $type == 'standard') {
	                $thumb = get_post_meta($post->ID, '_standard_image', true);
	            }
	            elseif($type == 'gallery') {
	                for($i=1; $i<=10; $i++) {
	                    $thumb = get_post_meta($post->ID, '_slider_image' . $i, true);
	                    if($thumb != '') {
	                        //image found, we exit the for statement
	                        break;
	                    }
	                }
	            }
	            elseif($type == 'quote') {
	                $thumb = get_post_meta($post->ID, '_quote_image', true);
	            }
	            if($thumb == '') {
	               $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
	            }
				if($thumb != '') {
					echo '<meta property="og:image" content="' . $thumb . '" />' . "\n";
				}
				echo '<meta property="og:title" content="' . get_the_title() . '" />' . "\n";
				echo '<meta property="og:description" content="Check out this awesome post: ' . get_the_title() . '" />' . "\n";
				echo '<meta property="og:url" content="' . get_permalink() . '" />' . "\n";
			}
		}
	}
}

//Loading the CSS files into the theme
add_action('wp_enqueue_scripts', 'teo_load_css');
if( !function_exists('teo_load_css') ) {
	function teo_load_css() {
		global $teo_options;
		wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/custom.css', array(), '1.0');
		wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css', array(), '1.0');
		wp_enqueue_style( 'Dosis-font', 'http://fonts.googleapis.com/css?family=Dosis:300,400,500,600,700,800');
		wp_enqueue_style( 'Nunito-font', 'http://fonts.googleapis.com/css?family=Nunito:400,300,500,700');


		wp_enqueue_script( 'jquery');
		wp_enqueue_script( 'jquery-ui-core');
		wp_enqueue_script( 'jquery-ui-widget');
		wp_enqueue_script( 'modernizr-respond.min', get_template_directory_uri() . '/js/modernizr-2.6.2-respond-1.1.0.min.js', '1.0');
	}
}

function teo_load_adminscripts() {
 	if(!is_admin() ) {
 		return ;
 	}
 	global $pagenow;
 	if(in_array( $pagenow, array('post.php', 'post-new.php' ) ) ) {
		echo '
		<script type="text/javascript">
			jQuery(document).ready(function(){
				function meta_hide() {
					jQuery("#imagepost_metabox").hide();
					jQuery("#quotepost_metabox").hide();
					jQuery("#videopost_metabox").hide();
					jQuery("#sliderpost_metabox").hide();
					jQuery("#audiopost_metabox").hide();
				}
				function meta_format(ptval) {
					meta_hide();	
					if(ptval == "quote") {
						jQuery("#quotepost_metabox").show();
					}
					else if(ptval == "gallery") {
						jQuery("#sliderpost_metabox").show();
					}
					else if(ptval == "video") {
						jQuery("#videopost_metabox").show();
					}
					else if(ptval == "audio") {
						jQuery("#audiopost_metabox").show();
					}
					else if(ptval == "image" || ptval == 0) {
						jQuery("#imagepost_metabox").show();
					}
				}
				var post_format = jQuery("input.post-format");
				var post_format_checked = jQuery("input.post-format:checked");
				meta_format(post_format_checked.val());

				post_format.on("change", function() {				
					var ptval = jQuery(this).val();
					meta_format(ptval);
				});
			});
		</script>';
	}
}
add_action('admin_head', 'teo_load_adminscripts');


//Loading the custom CSS from the theme options panel with a priority of 11, so it loads after the other css files

add_action('wp_head', 'teo_custom_css', 11);
function teo_custom_css() {
	global $redux_demo;
	if(isset($redux_demo['css-code']) && $redux_demo['css-code'] != '')
			echo '<style type="text/css">' . $redux_demo['css-code'] . '</style>';
}

function teo_register_menus() {
	register_nav_menus( array( 'top-menu' => 'Top primary menu',
							 )
						);
}
add_action('init', 'teo_register_menus');

add_action('init', 'teo_misc');
function teo_misc() {
	remove_action('wp_head', 'wp_generator'); 
	add_filter('show_admin_bar', '__return_false');
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'post-formats', array( 'gallery', 'image', 'quote', 'video', 'audio' ) );
	
}

function teo_list_pings($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment; ?>
		<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?> - <?php comment_excerpt(); ?>
	<?php 
}
if ( ! isset( $content_width ) ) $content_width = 990;

add_action( 'wp_ajax_bookmark-submit', 'teo_bookmark_submit' );
add_action( 'wp_ajax_bookmark-remove', 'teo_bookmark_remove' );
 
function teo_bookmark_submit() {
	// get the submitted parameters
	$postID = $_POST['post_id'];
 
	$current_user = wp_get_current_user();
    $bookmarks = get_user_meta($current_user->ID, 'bookmarks', true);
    if(empty($bookmarks) ) {
    	$bookmarks = array();
    }
    else {
    	$bookmarks = unserialize($bookmarks);
    }
    if(!in_array($postID, $bookmarks) ) {
    	$bookmarks[] = $postID;
    }
    $bookmarks = serialize($bookmarks);
    update_user_meta($current_user->ID, 'bookmarks', $bookmarks);
	exit;
}

function teo_bookmark_remove() {
	// get the submitted parameters
	$postID = $_POST['post_id'];
 
	$current_user = wp_get_current_user();
    $bookmarks = get_user_meta($current_user->ID, 'bookmarks', true);
    $bookmarks = unserialize($bookmarks);

    if(in_array($postID, $bookmarks) ) {
    	$index = array_search($postID, $bookmarks);
    	unset($bookmarks[$index]);
    }

    $bookmarks = serialize($bookmarks);
    update_user_meta($current_user->ID, 'bookmarks', $bookmarks);
	exit;
}

add_action( 'wp_ajax_rate-submit', 'teo_rate_submit' );
 
function teo_rate_submit() {
	// get the submitted parameters
	$postID = $_POST['post_id'];
	$vote = (int)$_POST['vote_id'];
	//$vote = 1 if the user rates up, 2 if the user rates the post down
	//$ratings is stored in the _ratings_ custom field of the post
	//$userratings stores for each individual user the posts on which he voted on
 
 	$current_user = wp_get_current_user();

	//------------Saving ratings on the post using custom fields ------------
    $ratings = get_post_meta($postID, '_ratings', true);

    if(empty($ratings) ) {
    	$ratings = array();
    }
    else {
    	$ratings = unserialize($ratings);
    }

    teo_updateRatings($postID, $vote, isset($ratings[$current_user->ID]) );
    $ratings[$current_user->ID] = array($vote, current_time('timestamp') );
   
    $ratings = serialize($ratings);

    update_post_meta($postID, '_ratings', $ratings);
    //------------The end for the data related to the post----------

    //------------Updating the list of rated posts by the individual user --------------

    $userratings = get_user_meta($current_user->ID, 'userratings', true);
    if(empty($userratings) ) {
    	$userratings = array();
    }
    else {
    	$userratings = unserialize($userratings);
    }
    if(!in_array($postID, $userratings) ) {
    	$userratings[] = $postID;
    }
    $userratings = serialize($userratings);
    update_user_meta($current_user->ID, 'userratings', $bookmarks);
    //---------End of the update process for the individual user ------------------

    
	exit;
}

function teo_updateRatings($post_ID, $vote, $extra=FALSE) {
	//if $extra is set, the user already voted but decided to change it
	//we must update both likes and dislikes accordingly
	if($vote == 1) {
		$customfield1 = '_ratings_up';
		$customfield2 = '_ratings_down'; //in case the user changes his vote, we must decrease the value from here
	}
	else {
		$customfield1 = '_ratings_down';
		$customfield2 = '_ratings_up'; //in case the user changes his vote, we must decrease the value from here
	}
	if($extra === FALSE ) {
		//the user votes for the first time
		$val = (int)get_post_meta($post_ID, $customfield1, true);
		update_post_meta($post_ID, $customfield1, $val+1);
	}
	else {
		//the user changed his vote
		$val1 = (int)get_post_meta($post_ID, $customfield1, true);
		$val2 = (int)get_post_meta($post_ID, $customfield2, true);

		update_post_meta($post_ID, $customfield1, $val1+1);
		update_post_meta($post_ID, $customfield2, $val2-1);
	}
}

add_action( 'wp_ajax_repost-data', 'teo_bookmark_repost_data' );
add_action( 'wp_ajax_nopriv_repost-data', 'teo_bookmark_repost_data' );

function teo_bookmark_repost_data() {
	$id = $_POST['post_id'];
	$post = get_post($id);
	$var = array();
	$var['post_format'] = get_post_format($id);
	$var['title'] = get_the_title($id);
	$var['thumb'] = get_post_meta($id, '_standard_image', true);
	$var['image1'] = get_post_meta($id, '_slider_image1', true);
	$var['image2'] = get_post_meta($id, '_slider_image2', true);
	$var['image3'] = get_post_meta($id, '_slider_image3', true);
	$var['image4'] = get_post_meta($id, '_slider_image4', true);
	$var['image5'] = get_post_meta($id, '_slider_image5', true);
	$var['image6'] = get_post_meta($id, '_slider_image6', true);
	$var['image7'] = get_post_meta($id, '_slider_image7', true);
	$var['image8'] = get_post_meta($id, '_slider_image8', true);
	$var['image9'] = get_post_meta($id, '_slider_image9', true);
	$var['image10'] = get_post_meta($id, '_slider_image10', true);
	$var['title1'] = get_post_meta($id, '_slider_title1', true);
	$var['title2'] = get_post_meta($id, '_slider_title2', true);
	$var['title3'] = get_post_meta($id, '_slider_title3', true);
	$var['title4'] = get_post_meta($id, '_slider_title4', true);
	$var['title5'] = get_post_meta($id, '_slider_title5', true);
	$var['title6'] = get_post_meta($id, '_slider_title6', true);
	$var['title7'] = get_post_meta($id, '_slider_title7', true);
	$var['title8'] = get_post_meta($id, '_slider_title8', true);
	$var['title9'] = get_post_meta($id, '_slider_title9', true);
	$var['title10'] = get_post_meta($id, '_slider_title10', true);
	$var['quote_text'] = get_post_meta($id, '_quote_text', true);
	$var['quote_author'] = get_post_meta($id, '_quote_author', true);
	$var['quote_source'] = get_post_meta($id, '_quote_source', true);
	$var['quote_image'] = get_post_meta($id, '_quote_image', true);
	$var['localvideo'] = get_post_meta($id, '_video_localvideo', true);
	$var['externalvideo'] = get_post_meta($id, '_video_externalvideo', true);
	$var['standard_nsfw'] = get_post_meta($id, '_standard_nsfw', true);
	$var['slider_nsfw'] = get_post_meta($id, '_slider_nsfw', true);
	$var['quote_nsfw'] = get_post_meta($id, '_quote_nsfw', true);
	$var['video_nsfw'] = get_post_meta($id, '_video_nsfw', true);

	$var['categories'] = array();
	$categories = get_the_category($id);
	if($categories) {
		foreach($categories as $category) {
			$var['categories'][] = $category->term_id;
		}
	}

	$var['tags'] = '';
	$tags = get_the_tags($id);
	if($tags) {
		for($i=0; $i<count($tags); $i++) {
			$var['tags']  .= $tags[$i]->name . ',';
		}
	}
	$var = json_encode($var);
	echo $var;
	exit;
}

add_action( 'wp_ajax_random_post', 'teo_random_post' );
add_action( 'wp_ajax_nopriv_random_post', 'teo_random_post' );

function teo_random_post() {
	$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
    foreach($posts as $post) {
        $link = get_permalink($post);
    }
    echo $link;
    exit;
}

/** Enqueue Color Picker **/
function teo_colorpicker_enqueue() {
    wp_enqueue_style( 'wp-color-picker' );
    wp_enqueue_script( 'colorpicker-js', get_stylesheet_directory_uri() . '/js/colorpicker.js', array( 'wp-color-picker' ) );
}
add_action( 'admin_enqueue_scripts', 'teo_colorpicker_enqueue' );

function teo_get_kudos($id = 0) {
	if($id == 0) {
		$current_user = wp_get_current_user();
		$id = $current_user->ID;
	}
	$kudos = 0;
	$args = array(
		'no_found_rows' => true, // counts posts, remove if pagination required
		'update_post_term_cache' => false, // grabs terms, remove if terms required (category, tag...)
		'update_post_meta_cache' => false, // grabs post meta, remove if post meta required)
		'author' => $id	
	);
	$query = new WP_Query($args);
	while($query->have_posts() ) {
		$query->the_post();
		global $post;
		$votesup = (int)get_post_meta($post->ID, '_ratings_up', true);
		//$votesdown = (int)get_post_meta($post->ID, '_ratings_down', true);
		$kudos += $votesup;
	}
	wp_reset_postdata();
	return $kudos;
}

/**
 * Sort by positive votes - negative votes.
 *
 * @param $orderby original order by string
 * @return custom order by positive votes - negative votes 
 */
function teo_ordervotes($orderby) {
    return 'mt1.meta_value-mt2.meta_value DESC';
}

add_action( 'save_post', 'teo_save_post', 10, 2 );
function teo_save_post( $post_id, WP_Post $post ) {
	
    if ( !get_post_meta( $post_id, '_ratings_up', true ) ) {
        update_post_meta( $post_id, '_ratings_up', 0 );
    }

    if ( !get_post_meta( $post_id, '_ratings_down', true ) ) {
        update_post_meta( $post_id, '_ratings_down', 0 );
    }
}

add_action('init','teo_redirect');
function teo_redirect() {
       if (isset($_GET['editprofile']) && $_GET['editprofile'] == 1 && is_user_logged_in() ) {
            get_template_part('profile', 'edit');
            die;
       }
}

add_action('init','teo_processdata', 1);
function teo_processdata() {
	//processing the edit profile form
	if(isset($_POST['editprofile']) ) {
		if(!is_user_logged_in() ) {
			die;
		}
		require get_template_directory() . '/includes/mediaUpload.php';
		$tmp = new MediaUpload;
		global $current_user; get_currentuserinfo();
		$id = $current_user->ID;
		$email = sanitize_email($_POST['email']);
		$password = $_POST['password'];
		$password2 = $_POST['password2'];
		$username = esc_attr($_POST['display']);
		if(isset($_POST['nsfw'] ) ) {
			$nsfw = 1;
		}
		else {
			$nsfw = 0;
		}
		$error = '';
		$success = '';
		if($username == '' || $email == '') {
			$error = __("You can't leave the username or the e-mail field empty", 'Aruna');
		}
		else if($password != '' && $password != $password2) {
			$error = __("The two passwords you've entered do not match!", 'Aruna');
		}
		else {
			if($_FILES['cover']['error'] == 0) {
                $cover = $tmp->saveUpload( 'cover' );
                update_user_meta($id, '_cover', wp_get_attachment_url($cover['attachment_id']));
            }
            if($_FILES['avatar']['error'] == 0) {
                $cover = $tmp->saveUpload( 'avatar' );
                update_user_meta($id, '_avatar', wp_get_attachment_url($cover['attachment_id']));
            }
            update_user_meta($id, '_nsfw', $nsfw);
            if($password != '') {
            	wp_update_user( array ( 'ID' => $id, 'display_name' => $username, 'user_pass' => $password, 'user_email' => $email ) ) ;
            }
            else {
            	wp_update_user( array ( 'ID' => $id, 'display_name' => $username, 'user_email' => $email) ) ;
            }
            $success = 1;
        }
        if($success == 1) {
        	wp_redirect(get_home_url() . '/?editprofile=1&success=1');
        	exit; 
        }
        elseif($error != '') {
        	wp_redirect(get_home_url() . '/?editprofile=1&error=' . urlencode($error) );
        	exit; 
        }
        die;
	}

	//processing the front-end upload
	if(isset($_POST['frontend_upload']) ) {
		if(!is_user_logged_in() ) {
			die;
		}
		global $redux_demo;
		require get_template_directory() . '/includes/mediaUpload.php';
		$tmp = new MediaUpload;
		global $current_user; get_currentuserinfo();
		$id = $current_user->ID;
		$post_format = $_POST['post_type'];
		$category = $_POST['post_category'];
		if(!isset($redux_demo['enable_autoapproval']) || $redux_demo['enable_autoapproval'] == '1') {
			$status = 'publish';
		}
		else {
			$status = 'pending';
		}
		switch($post_format) {
			case "window-slider" :
				$format = 'gallery';
				//processing slider post
				$title = esc_html($_POST['title_slider']);
				$tags = $_POST['tags_slider'];

				$comments = isset($_POST['comments_slider']) ? 1 : 0;
				$anonymous = isset($_POST['anonymous_slider']) ? 1 : 0;

				$comment_status = $comments == 1 ? 'open' : 'closed';

				$user_id = $id;

				$image1 = esc_html($_POST['image1']);
				$image2 = esc_html($_POST['image2']);
				$image3 = esc_html($_POST['image3']);
				$image4 = esc_html($_POST['image4']);
				$image5 = esc_html($_POST['image5']);
				$image6 = esc_html($_POST['image6']);
				$image7 = esc_html($_POST['image7']);
				$image8 = esc_html($_POST['image8']);
				$image9 = esc_html($_POST['image9']);
				$image10 = esc_html($_POST['image10']);

				if( ($_FILES['slide1']['error'] != 0 && $image1 == '') || $title == '') {
					wp_redirect(home_url('/') . '?posterror=1');
					exit; 
				}

				$post = array(
		          'comment_status' => $comment_status,
		          'ping_status'    => 'open',
		          'post_author'    => $user_id,
		          'post_content'   => '',
		          'post_status'    => $status,
		          'post_title'     => $title,
		          'post_type'      => 'post',
		        );  
		        $post_id = wp_insert_post($post);

				$title1 = isset($_POST['title1']) ? esc_html($_POST['title1']) : '';
				add_post_meta($post_id, '_slider_title1', $title1);
				if($image1 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image1) ) {
			        	add_post_meta($post_id, '_slider_image1', $image1);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image1);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image1);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image1', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide1']['error'] == 0) {
						$slide1 = $tmp->saveUpload( 'slide1' );
	                	add_post_meta($post_id, '_slider_image1', wp_get_attachment_url($slide1['attachment_id']) );
					}
				}

				$title2 = isset($_POST['title2']) ? esc_html($_POST['title2']) : '';
				add_post_meta($post_id, '_slider_title2', $title2);
				if($image2 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image2) ) {
			        	add_post_meta($post_id, '_slider_image2', $image2);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image2);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image2);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image2', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide2']['error'] == 0) {
						$slide2 = $tmp->saveUpload( 'slide2' );
	                	add_post_meta($post_id, '_slider_image2', wp_get_attachment_url($slide2['attachment_id']) );
					}
				}

				$title3 = isset($_POST['title3']) ? esc_html($_POST['title3']) : '';
				add_post_meta($post_id, '_slider_title3', $title3);
				if($image3 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image3) ) {
			        	add_post_meta($post_id, '_slider_image3', $image3);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image3);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image3);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image3', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide3']['error'] == 0) {
						$slide3 = $tmp->saveUpload( 'slide3' );
	                	add_post_meta($post_id, '_slider_image3', wp_get_attachment_url($slide3['attachment_id']) );
					}
				}

				$title4 = isset($_POST['title4']) ? esc_html($_POST['title4']) : '';
				add_post_meta($post_id, '_slider_title4', $title4);
				if($image4 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image4) ) {
			        	add_post_meta($post_id, '_slider_image4', $image4);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image4);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image4);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image4', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide4']['error'] == 0) {
						$slide4 = $tmp->saveUpload( 'slide4' );
	                	add_post_meta($post_id, '_slider_image4', wp_get_attachment_url($slide4['attachment_id']) );
					}
				}

				$title5 = isset($_POST['title5']) ? esc_html($_POST['title5']) : '';
				add_post_meta($post_id, '_slider_title5', $title5);
				if($image5 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image5) ) {
			        	add_post_meta($post_id, '_slider_image5', $image5);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image5);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image5);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image5', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide5']['error'] == 0) {
						$slide5 = $tmp->saveUpload( 'slide5' );
	                	add_post_meta($post_id, '_slider_image5', wp_get_attachment_url($slide5['attachment_id']) );
					}
				}

				$title6 = isset($_POST['title6']) ? esc_html($_POST['title6']) : '';
				add_post_meta($post_id, '_slider_title6', $title6);
				if($image6 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image6) ) {
			        	add_post_meta($post_id, '_slider_image6', $image6);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image6);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image6);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image6', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide6']['error'] == 0) {
						$slide6 = $tmp->saveUpload( 'slide6' );
	                	add_post_meta($post_id, '_slider_image6', wp_get_attachment_url($slide6['attachment_id']) );
					}
				}

				$title7 = isset($_POST['title7']) ? esc_html($_POST['title7']) : '';
				add_post_meta($post_id, '_slider_title7', $title7);
				if($image7 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image7) ) {
			        	add_post_meta($post_id, '_slider_image7', $image7);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image7);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image7);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image7', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide7']['error'] == 0) {
						$slide7 = $tmp->saveUpload( 'slide7' );
	                	add_post_meta($post_id, '_slider_image7', wp_get_attachment_url($slide7['attachment_id']) );
					}
				}

				$title8 = isset($_POST['title8']) ? esc_html($_POST['title8']) : '';
				add_post_meta($post_id, '_slider_title8', $title8);
				if($image8 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image8) ) {
			        	add_post_meta($post_id, '_slider_image8', $image8);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image8);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image8);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image8', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide8']['error'] == 0) {
						$slide8 = $tmp->saveUpload( 'slide8' );
	                	add_post_meta($post_id, '_slider_image8', wp_get_attachment_url($slide8['attachment_id']) );
					}
				}

				$title9 = isset($_POST['title9']) ? esc_html($_POST['title9']) : '';
				add_post_meta($post_id, '_slider_title9', $title9);
				if($image9 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image9) ) {
			        	add_post_meta($post_id, '_slider_image9', $image9);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image9);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image9);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image9', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide9']['error'] == 0) {
						$slide9 = $tmp->saveUpload( 'slide9' );
	                	add_post_meta($post_id, '_slider_image9', wp_get_attachment_url($slide9['attachment_id']) );
					}
				}

				$title10 = isset($_POST['title10']) ? esc_html($_POST['title10']) : '';
				add_post_meta($post_id, '_slider_title10', $title10);
				if($image10 != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image10) ) {
			        	add_post_meta($post_id, '_slider_image10', $image10);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image10);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image10);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_slider_image10', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['slide10']['error'] == 0) {
						$slide10 = $tmp->saveUpload( 'slide10' );
	                	add_post_meta($post_id, '_slider_image10', wp_get_attachment_url($slide10['attachment_id']) );
					}
				}

				$nsfw = isset($_POST['nsfw_slider']) ? 1 : 2;
				add_post_meta($post_id, '_slider_nsfw', $nsfw);
				add_post_meta($post_id, '_anonymous', $anonymous);

				break;
			case "window-video":
				$format = 'video';
				$title = esc_html($_POST['title_video']);
				$tags = $_POST['tags_video'];

				if($title == '') {
					wp_redirect(home_url('/') . '?posterror=1');
					exit; 
				}

				$comments = isset($_POST['comments_video']) ? 1 : 0;
				$anonymous = isset($_POST['anonymous_video']) ? 1 : 0;

				$comment_status = $comments == 1 ? 'open' : 'closed';

				$user_id = $id;

				$post = array(
		          'comment_status' => $comment_status,
		          'ping_status'    => 'open',
		          'post_author'    => $user_id,
		          'post_content'   => '',
		          'post_status'    => $status,
		          'post_title'     => $title,
		          'post_type'      => 'post',
		        );  
		        $post_id = wp_insert_post($post);
		        if(isset($_FILES['video_file']) && $_FILES['video_file']['error'] == 0) {
					$video_file = $tmp->saveUpload( 'video_file' );
	               	add_post_meta($post_id, '_video_localvideo', wp_get_attachment_url($video_file['attachment_id']) );
				}
				else {
		        	$video = esc_url($_POST['video']);
		        	if (strpos($video,'youtube') !== false) {
		        		//it's youtube, we need toget the thumbnail! :)
		        		//we get the id of the video first
		        		$videotemp = explode('/watch?v=', $video);
		        		if(isset($videotemp[1]) && $videotemp[1] != '') {
		        			$idvideo = $videotemp[1];
		        			//we have the id, now we put the thumbnail
		        			$url_video = 'http://i1.ytimg.com/vi/' . $idvideo . '/hqdefault.jpg';
		        		}
		        	}
		        	elseif (strpos($video,'vimeo') !== false) {
		        		//it's youtube, we need toget the thumbnail! :)
		        		//we get the id of the video first
		        		$videotemp = explode('vimeo.com/', $video);
		        		if(isset($videotemp[1]) && $videotemp[1] != '') {
		        			$idvideo = $videotemp[1];
		        			//we have the id, now we put the thumbnail
		        			$json_vimeo = 'http://vimeo.com/api/v2/video/' . $idvideo . '.json';
		        			$ch = curl_init();
							$timeout = 5;
							curl_setopt($ch, CURLOPT_URL, $json_vimeo);
							curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
							curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
							$json_vimeo = curl_exec($ch);
							curl_close($ch);
		        			$jsontemp = json_decode($json_vimeo, true);
		        			if(isset($jsontemp[0]) && isset($jsontemp[0]['thumbnail_large']) && $jsontemp[0]['thumbnail_large'] != '') {
 								$url_video = $jsontemp[0]['thumbnail_large'];
 							}
		        		}
		        	}
		        	if(isset($url_video) && $url_video != '') {
		        		//we have an external youtube image :) we save it with the cURL library
			        	$filedetails = pathinfo($url_video);
			        	$mime = '';
			       		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
			       			$mime = 'image/jpeg';
			       		}
			       		elseif($filedetails['extension'] == 'png') {
			       			$mime = 'image/png';
			       		}
			       		elseif($filedetails['extension'] == 'gif') {
			       			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
				        	$ch = curl_init($url_video);
				        	$uploaddir = wp_upload_dir();
				       		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
				       		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
						        'post_status' => 'inherit',
						        'post_date' => date( 'Y-m-d H:i:s' ),
						        'post_date_gmt' => date( 'Y-m-d H:i:s' )
						    );

						    // Add the file to the media library and generate thumbnail.
						    $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
						    $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
						    wp_update_attachment_metadata( $attach_id,  $attach_data );

							update_post_meta($post_id, '_thumbnail_id', $attach_id);
						}
		        	}

		        	add_post_meta($post_id, '_video_externalvideo', $video);
		        }

				$nsfw = isset($_POST['nsfw_video']) ? 1 : 2;
				add_post_meta($post_id, '_video_nsfw', $nsfw);
				add_post_meta($post_id, '_anonymous', $anonymous);

				break;
			case "window-audio":
				$format = 'audio';
				$title = esc_html($_POST['title_audio']);

				if($title == '') {
					wp_redirect(home_url('/') . '?posterror=1');
					exit; 
				}
				$tags = $_POST['tags_audio'];

				$comments = isset($_POST['comments_audio']) ? 1 : 0;
				$anonymous = isset($_POST['anonymous_audio']) ? 1 : 0;

				$comment_status = $comments == 1 ? 'open' : 'closed';

				$user_id = $id;

				$post = array(
		          'comment_status' => $comment_status,
		          'ping_status'    => 'open',
		          'post_author'    => $user_id,
		          'post_content'   => '',
		          'post_status'    => $status,
		          'post_title'     => $title,
		          'post_type'      => 'post',
		        );  
		        $post_id = wp_insert_post($post);

		        if(isset($_FILES['audio_file']) && $_FILES['audio_file']['error'] == 0) {
					$audio_file = $tmp->saveUpload( 'audio_file' );
	               	add_post_meta($post_id, '_audio_localaudio', wp_get_attachment_url($audio_file['attachment_id']) );
				}
		        else {
		        	$audio = esc_url($_POST['audio']);
		        	add_post_meta($post_id, '_audio_externalaudio', $audio);
		        }

				$nsfw = isset($_POST['nsfw_audio']) ? 1 : 2;
				add_post_meta($post_id, '_audio_nsfw', $nsfw);
				add_post_meta($post_id, '_anonymous', $anonymous);

				break;
			case "window-quote":
				$format = 'quote';
				$quote_title = esc_html($_POST['quote_title']);
				$quote_author = esc_html($_POST['quote_author']);
				$quote_source = esc_html($_POST['quote_source']);
				$title = esc_html($_POST['quote']);
				if($quote_title == '') {
					$quote_title = $title;
				}
				if($quote_title == '') {
					wp_redirect(home_url('/') . '?posterror=1');
					exit; 
				}
				$tags = $_POST['tags_quote'];
				$quoteimage = esc_url($_POST['quoteimage']);

				$comments = isset($_POST['comments_quote']) ? 1 : 0;
				$anonymous = isset($_POST['anonymous_quote']) ? 1 : 0;

				$comment_status = $comments == 1 ? 'open' : 'closed';

				$user_id = $id;

				$post = array(
		          'comment_status' => $comment_status,
		          'ping_status'    => 'open',
		          'post_author'    => $user_id,
		          'post_content'   => '',
		          'post_status'    => $status,
		          'post_title'     => $quote_title,
		          'post_type'      => 'post',
		        );  
		        $post_id = wp_insert_post($post);

		        add_post_meta($post_id, '_quote_text', $title);
		        add_post_meta($post_id, '_quote_author', $quote_author);
		        add_post_meta($post_id, '_quote_source', $quote_source);

		        if($quoteimage != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $quoteimage) ) {
			        	add_post_meta($post_id, '_quote_image', $quoteimage);
			        }
		        	else {
		        		//we have an external quote image :) we save it with the cURL library
		        		$filedetails = pathinfo($quoteimage);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($quoteimage);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_quote_image', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['image_quote']['error'] == 0) {
						$image_quote = $tmp->saveUpload( 'image_quote' );
	                	add_post_meta($post_id, '_quote_image', wp_get_attachment_url($image_quote['attachment_id']) );
					}
				}

				$nsfw = isset($_POST['nsfw_image']) ? 1 : 2;
				add_post_meta($post_id, '_quote_nsfw', $nsfw);
				add_post_meta($post_id, '_anonymous', $anonymous);

				break;
			case "window-image":
				$format = 'image';
				$title = esc_html($_POST['title_image']);
				$image = esc_url($_POST['postimage']);
				$tags = $_POST['tags_image'];
				$comments = isset($_POST['comments_image']) ? 1 : 0;
				$anonymous = isset($_POST['anonymous_image']) ? 1 : 0;

				$comment_status = $comments == 1 ? 'open' : 'closed';

				$user_id = $id;

				if(($_FILES['image_imagepost']['error'] != 0 && $image == '') || $title == '') {
					wp_redirect(home_url('/') . '?posterror=1');
					exit; 
				}

				$post = array(
		          'comment_status' => $comment_status,
		          'ping_status'    => 'open',
		          'post_author'    => $user_id,
		          'post_content'   => '',
		          'post_status'    => $status,
		          'post_title'     => $title,
		          'post_type'      => 'post',
		        );  
		        $post_id = wp_insert_post($post);

		        if($image != '') {
		        	$pattern = '~' . get_bloginfo('url') . '~';
			        if(preg_match($pattern, $image) ) {
			        	add_post_meta($post_id, '_standard_image', $image);
			        }
		        	else {
		        		//we have an external image :) we save it with the cURL library
		        		$filedetails = pathinfo($image);
		        		$mime = '';
		        		if($filedetails['extension'] == 'jpe' || $filedetails['extension'] == 'jpg' || $filedetails['extension'] == 'jpeg') {
		        			$mime = 'image/jpeg';
		        		}
		        		elseif($filedetails['extension'] == 'png') {
		        			$mime = 'image/png';
		        		}
		        		elseif($filedetails['extension'] == 'gif') {
		        			$mime = 'image/gif';
		        		}
		        		if($mime != '') {
		        			require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
			        		$ch = curl_init($image);
			        		$uploaddir = wp_upload_dir();
			        		$filename = $filedetails['filename'] . rand(1, 9999) . '.' . $filedetails['extension']; 
			        		//we add some random digits to make sure it's unique
							$fp = fopen(trailingslashit($uploaddir['path']) . $filename, 'wb');
							curl_setopt($ch, CURLOPT_FILE, $fp);
							curl_setopt($ch, CURLOPT_HEADER, 0);
							curl_exec($ch);
							curl_close($ch);
							fclose($fp);

							$guid = trailingslashit($uploaddir['url'])  . $filename;
							$attachment = array(
					            'post_mime_type' => $mime,
					            'guid' => $guid,
					            'post_title' => '[External image] : ' . $filedetails['filename'] ,
					            'post_content' => '',
					            'post_author' => $user_id,
					            'post_status' => 'inherit',
					            'post_date' => date( 'Y-m-d H:i:s' ),
					            'post_date_gmt' => date( 'Y-m-d H:i:s' )
					        );

					        // Add the file to the media library and generate thumbnail.
					        $attach_id = wp_insert_attachment( $attachment, trailingslashit($uploaddir['url']) . $filename, $post_id );
					        $attach_data = wp_generate_attachment_metadata( $attach_id, trailingslashit($uploaddir['path']) . $filename );
					        wp_update_attachment_metadata( $attach_id,  $attach_data );

							add_post_meta($post_id, '_standard_image', trailingslashit($uploaddir['url']) . $filename);
						}

		        	}
		        }
		        else {
		        	if($_FILES['image_imagepost']['error'] == 0) {
						$image = $tmp->saveUpload( 'image_imagepost' );
	                	add_post_meta($post_id, '_standard_image', wp_get_attachment_url($image['attachment_id']) );
					}
				}

				$nsfw = isset($_POST['nsfw_image']) ? 1 : 2;
				add_post_meta($post_id, '_standard_nsfw', $nsfw);
				add_post_meta($post_id, '_anonymous', $anonymous);

				break;
			default:
				die;
		}
		//setting post format
		set_post_format($post_id, $format);

		//setting category
		wp_set_post_terms($post_id, (array)$category, 'category');
			
		//setting tags
		wp_set_post_terms($post_id, $tags, 'post_tag');

		wp_redirect(home_url('/') . '?success=1');
		exit; 
	}
}

// Create a new filtering function that will add our where clause to the query
function teo_filter_where( $where = '' ) {
    // posts in the last 7 days
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-7 days')) . "'";
    return $where;
}

function teo_filter_trending( $where = '' ) {
    $where .= " AND post_date > '" . date('Y-m-d', strtotime('-2 days')) . "'";
    return $where;
}

function convertBytes( $value ) {
    if ( is_numeric( $value ) ) {
        return $value;
    } else {
        $value_length = strlen( $value );
        $qty = substr( $value, 0, $value_length - 1 );
        $unit = strtolower( substr( $value, $value_length - 1 ) );
        switch ( $unit ) {
            case 'k':
                $qty *= 1024;
                break;
            case 'm':
                $qty *= 1048576;
                break;
            case 'g':
                $qty *= 1073741824;
                break;
        }
        return $qty;
    }
}

add_action('wp_login_failed', 'teo_front_end_login_fail'); 
 
function teo_front_end_login_fail($username){
    wp_redirect(home_url('/') . '?login=failed'); 
    exit; 
}

add_action('register_post', 'binda_register_fail_redirect', 99, 3);

function binda_register_fail_redirect( $sanitized_user_login, $user_email, $errors ){
    $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    if ( $errors->get_error_code() ){
        wp_redirect(home_url('/') . '?register=failed'); 
        exit;   
    }
}
if ( !function_exists('teo_get_avatar') ) {
	function teo_get_avatar($userid, $width = 27, $height = 27) {
		$avatar = get_user_meta($userid, '_avatar', true);
		if($avatar == '') {
			//no avatar is set so we return the one from gravatar, with the correct dimensions
			return get_avatar($userid, $width);
		}
		else {
			//we have a custom avatar so we resize it accordingly
			$returnimg = aq_resize($avatar, $width, $height, true);
			if($returnimg != '') {
				//we could resize the image(it was bigger) so we return the resized image
				return '<img src="' . $returnimg . '" alt="' . get_the_author_meta('display_name', $userid) . '" />';
			}
			else {
				//we couldn't resize the image so we return the original
				return '<img src="' . $image . '" alt="' . get_the_author_meta('display_name', $userid) . '" />';
			}
		}
	}
}

//hide anonymous posts from author page.
// add_action( 'pre_get_posts','teo_hide_anonymous' );
// function teo_hide_anonymous($query) {
// 	if( $query->is_main_query() && !is_admin() && $query->is_author) {
// 		$meta_query[] = array(
// 			'key' => '_anonymous',
// 			'value' => 0,
// 			'compare' => '=',
// 			'type' => 'NUMERIC'
// 		);
// 		$query->set('meta_query', $meta_query);
// 	}
// }

?>