<?php
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');

add_action( 'wp_enqueue_scripts', 'my_enqueue_styles' );
add_action( 'after_setup_theme', 'my_child_theme_setup' );
add_filter('edit_post_link', 'wpse_remove_edit_post_link');
add_theme_support('category-thumbnails');


 function mysql_real_escape_string($string){
// $con = new mysqli("localhost", "Administrador", "Administrador", "iwatch_ITP");
  $con = new mysqli("localhost", "Administrador", "Administrador", "iwatchecoembes");
 return mysqli_real_escape_string($con,$string);
 }
function remove_core_updates(){
	global $wp_version;
	return(object) array('last_checked'=> time(),'version_checked'=> $wp_version,);
}
add_filter('pre_site_transient_update_core','remove_core_updates');
add_filter('pre_site_transient_update_plugins','remove_core_updates');
add_filter('pre_site_transient_update_themes','remove_core_updates');

function wpse_remove_edit_post_link( $link ) {
	return '';
}


function my_child_theme_setup() {
	load_child_theme_textdomain( 'ainia_bloggr_child', get_stylesheet_directory() . '/languages' );
}


function my_enqueue_styles() {

	/* If using a child theme, auto-load the parent theme style. */
	if ( is_child_theme() ) {
		//wp_enqueue_style( 'parent-style', trailingslashit( get_template_directory_uri() ) . 'style.css' );
	}

	/* Always load active theme's style.css. */
	wp_enqueue_style( 'style', get_stylesheet_uri() );
	wp_enqueue_style( 'bloggr-google-fonts', '//fonts.googleapis.com/css?family=Material+Icons' );
	
}
if ( ! function_exists( 'bloggr_entry_footer_ID' ) ) :
/**
 * Prints HTML with meta information for the categories, tags and comments.
*/
function bloggr_entry_footer_ID($ID) {
	// Hide category and tag text for pages.

	?>
		<p class="action_favorito">
		<?php
		
		$rel = get_user_relation_id( get_current_user_id() , 0, $ID, 'favorite' );
		if($rel):?>
				<i  id="icon_favourite_<?php echo $ID;?>" class="fa fa-heart" style="margin-left:10px;font-size:24px;color:red;cursor:pointer;vertical-align:middle;" onclick="marcaPostFavorito(<?php echo '\''.$ID.'\'';?>)"></i>			 					
		 <?php else: ?>
				<i id="icon_favourite_<?php echo $ID;?>" class="fa fa-heart" style="margin-left:10px;font-size:24px;color:#404040;cursor:pointer;vertical-align:middle;" onclick="marcaPostFavorito(<?php echo '\''.$ID.'\'';?>)"></i>
						 	    
		 <?php endif; 
		 	 $url_article = get_post_meta( $ID, 'url_article', true );?>
		 
		  <i class="material-icons" style="margin-left:10px;font-size:24px;color:#404040;cursor:pointer;vertical-align:middle;" onclick="mostrar_enviar_a(<?php echo '\''.$ID.'\'';?>)">email</i>
		  
		  <a href="<?php echo $url_article;?>" target="_blank">
		  	<i id="icon_share_<?php echo $ID;?>" class="fa fa-share-square-o" style="margin-left:10px;font-size:24px;color:#404040;cursor:pointer;vertical-align:middle;" ></i>
			</a>
		
			 <p id="server_message-<?php echo $ID;?>" style="display:none">Esto es un mensaje</p>

		<?php
}
endif;

if ( ! function_exists( 'bloggr_entry_footer_reduced_ID' ) ) :
/**
 * Prints HTML with meta information for the categories, tags and comments.
*/
function bloggr_entry_footer_reduced_ID($ID) {
	// Hide category and tag text for pages.

	?>
		<p class="action_favorito">
		<?php
		

		 	 $url_article = get_post_meta( $ID, 'url_article', true );?>
		 
		  <i class="material-icons" style="margin-left:10px;font-size:24px;color:#404040;cursor:pointer;vertical-align:middle;" onclick="mostrar_enviar_a(<?php echo '\''.$ID.'\'';?>)">email</i>
		   <i id="icon_share_<?php echo $ID;?>" class="fa fa-share-alt" style="margin-left:10px;font-size:24px;color:#404040;cursor:pointer;vertical-align:middle;" onclick="mostrar_recomendar_a(<?php echo '\''.$ID.'\'';?>)"></i>
				
			 <p id="server_message-<?php echo $ID;?>" style="display:none">Esto es un mensaje</p>

		<?php
}
endif;
function bloggr_entry_footer() {
	// Hide category and tag text for pages.
	if ( 'post' == get_post_type() ) {
		/* translators: used between list items, there is a space after the comma */
		$categories_list = get_the_category_list( __( ', ', 'ainia_bloggr_child' ) );
		if ( $categories_list && bloggr_categorized_blog() ) {
			printf( '<span class="cat-links"><i class="fa fa-pencil"></i></span>', $categories_list );
		}

		/* translators: used between list items, there is a space after the comma */
		$tags_list = get_the_tag_list( '', __( ', ', 'ainia_bloggr_child' ) );
		if ( $tags_list ) {
			printf( '<span class="tags-links"><i class="fa fa-tag"></i> ' . __( 'Tagged %1$s', 'ainia_bloggr_child' ) . '</span>', $tags_list );
		}
	}

	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
		echo '<span class="comments-link"><i class="fa fa-comments-o"></i> ';
		comments_popup_link( __( 'Leave a comment', 'ainia_bloggr_child' ), __( '1 Comment', 'ainia_bloggr_child' ), __( '% Comments', 'ainia_bloggr_child' ) );
		echo '</span>';
	}

	edit_post_link( __( 'Edit', 'ainia_bloggr_child' ), '<span class="edit-link">', '</span>' );
}

if ( ! function_exists( 'bloggr_post_nav' ) ) :
/**
 * Display navigation to next/previous post when applicable.
*/
function bloggr_post_nav() {
	// Don't print empty markup if there's nowhere to navigate.
	$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
	$next     = get_adjacent_post( false, '', false );

	if ( ! $next && ! $previous ) {
		return;
	}
	?>
	<nav class="navigation post-navigation" role="navigation">
		<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'ainia_bloggr_child' ); ?></h1>
		<div id="nav-links" class="nav-links">
			<?php
				previous_post_link( '<div id="nav-previous" class="nav-previous"><span class="valign">%link</span></div>', _x( '<span class="meta-nav">&larr;</span>&nbsp;%title', 'Previous post link', 'ainia_bloggr_child' ) );
				next_post_link(     '<div id="nav-next" class="nav-next">%link</div>',     _x( '%title&nbsp;<span class="meta-nav">&rarr;</span>', 'Next post link',     'ainia_bloggr_child' ) );
			?>
		</div><!-- .nav-links -->
	</nav><!-- .navigation -->
	<?php
}
endif;

if ( ! function_exists( 'bloggr_paging_nav' ) ) :
/**
 * Display navigation to next/previous set of posts when applicable.
*/
function bloggr_paging_nav() {
	// Don't print empty markup if there's only one page.
	if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
		return;
	}
	?>
	<nav class="navigation paging-navigation" role="navigation">
		<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'ainia_bloggr_child' ); ?></h1>
		<div class="nav-links">

			<?php if ( get_next_posts_link() ) : ?>
			<div  id="nav-previous" class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'ainia_bloggr_child' ) ); ?></div>
			<?php endif; ?>

			<?php if ( get_previous_posts_link() ) : ?>
			<div id="nav-next" class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'ainia_bloggr_child' ) ); ?></div>
			<?php endif; ?>

		</div><!-- .nav-links -->
	</nav><!-- .navigation -->
	<?php
}
endif;
if ( ! function_exists( 'bloggr_child_posted_on' ) ) :
/**
 * Prints HTML with meta information for the current post-date/time and author.
*/
function bloggr_child_posted_on() {
	$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
		$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
	}

	$time_string = sprintf( $time_string,
			esc_attr( get_the_date( 'c' ) ),
			esc_html( get_the_date() ),
			esc_attr( get_the_modified_date( 'c' ) ),
			esc_html( get_the_modified_date() )
			);

	$posted_on = sprintf(
			_x( '<i class="fa fa-clock-o"></i> %s', 'post date', 'ainia_bloggr_child' ),
			'<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
			);

	$byline = sprintf(
			_x( '<span class="hide-on-mobile">by %s</span>', 'post author', 'ainia_bloggr_child' ),
			'<span class="author vcard">' . esc_html( get_the_author() ) . '</span>'
			);

	echo '<span class="posted-on">' . $posted_on . '</span><span class="byline"> ' . $byline . '</span>';

}
endif;

function get_loading_spiner ($ID){
	$html='<div class="floatingBarsG" id="floatingBar'.$ID.'" style="width:22px;
	height:27px;position:relative;left:45%;
	top:45%;visibility:hidden;">
	<div class="blockG" id="rotateG_01"></div>
	<div class="blockG" id="rotateG_02"></div>
	<div class="blockG" id="rotateG_03"></div>
	<div class="blockG" id="rotateG_04"></div>
	<div class="blockG" id="rotateG_05"></div>
	<div class="blockG" id="rotateG_06"></div>
	<div class="blockG" id="rotateG_07"></div>
	<div class="blockG" id="rotateG_08"></div>
</div>';
	return $html;
}

function add_category_checklist_filter_to_post_administration(){

    //execute only on the 'post' content type
    global $post_type;
    if($post_type == 'post'){

    $args = array(
	'show_option_all'    => 'Todas las categor&iacute;as',
	'show_option_none'   => '',
	'option_none_value'  => '-1',
	'orderby'            => 'name',
	'order'              => 'ASC',
	'show_count'         => 0,
	'hide_empty'         => 1,
	'child_of'           => 0,
	'exclude'            => '',
	'include'            => '',
	'echo'               => 0,
	'selected'           => 0,
	'hierarchical'       => 0,
	'name'               => 'cat1[]',
	'id'                 => '',
	'class'              => 'postform',
	'depth'              => 0,
	'tab_index'          => 0,
	'taxonomy'           => 'category',
	'hide_if_empty'      => false,
	'value_field'	     => 'term_id',
);

        //if we have a post format already selected, ensure that its value is set to be selected
        $selected=array();
        if(isset($_GET['cat1'])){
            $args['selected'] = sanitize_text_field($_GET['cat1']);
            $selected[]=$_GET['cat1'];
        }
     
       /* if(isset($_GET['post_category'])){
            
            $cats=$_GET['post_category'];
            foreach ($cats as $cat){
            	  //echo 'sssssssssssssssssssss'.$cat;
            	  $selected[]=$cat;
            }
        }*/
		$checked_ontop = false;
         //wp_dropdown_categories($args);
         $dropdown = wp_dropdown_categories( $args );

/** insert "multiple" using str_replace **/
$multi = str_replace( '<select', '<select multiple ', $dropdown );
 foreach ($selected as $cat){
 	 foreach ($cat as $singleCat){
 		$multi =str_replace('value="'.$singleCat.'"','value="'.$singleCat.'" selected',$multi);
 	}
 }


/** output result **/
echo $multi;
     

    }
}
add_action('restrict_manage_posts','add_category_checklist_filter_to_post_administration');
//restrict the posts by the chosen post format
function add_post_format_filter_to_posts($query){

    global $post_type, $pagenow;

    //if we are currently on the edit screen of the post type listings
    if($pagenow == 'edit.php' && $post_type == 'post'){
           $selected=array();
        if(isset($_GET['cat']) && $_GET['cat']>0){
            
            $selected[]=$_GET['cat'];
         }
        if(isset($_GET['cat1'])&& $_GET['cat1']>0){
            
            $selected[]=$_GET['cat1'];
         }
         if(isset($_GET['cat2'])&& $_GET['cat2']>0){
            
            $selected[]=$_GET['cat2'];
         }
  
            if(sizeof($selected) > 0){
                $query->query_vars['category__in']=$selected[0];
 				echo 'SQL SQL '.$GLOBALS['wp_query']->request;
            }
            
       
    }   
    return $query;
}
add_action('pre_get_posts','add_post_format_filter_to_posts');

function save_user_fields ($user_id) {
  		 $taxonomy='category';			
		$terms = get_categories(array(
    				'taxonomy' => 'category',
    'hide_empty' => false,
) );
		foreach($terms as $term)$conceptos[]=$term->term_id;
		
		ainia_set_user_terms($user_id, $conceptos);
		//clean_object_term_cache($user_id, $taxonomy);	
		$user=get_user_by('id', $user_id);
		$meta_value=md5($user->user_login);
		
		add_user_meta($user_id, 'pkg_autologin_code', $meta_value);
 
    do_action('users_register_form_save_data');
}
add_action('user_register', 'save_user_fields');
add_filter( 'get_terms_args', 'filtrar_lista_terminos', 1, 2 );
 function filtrar_lista_terminos( $args, $taxonomies ) {
	$user_id=get_current_user_id();
 
    if ( is_admin() && 'concepto_seguimiento'==$taxonomies[0] ){
        $ids=get_user_conceptos_seguimiento2($user_id);
       // echo sizeof($ids);
        $conceptos=array();
        if (sizeof($conceptos)>0){
          foreach($ids as $id){
        	$conceptos[]=$id->term_id;
        	//echo $id->term_id.'-';
        	}
        }
        else{
        	$conceptos[]=0;
        
        }
        	    
        //echo implode(",",$conceptos);
		//$ids=array(1329,301,321,300,305,317,308,328);
    	$args['exclude'] = 5768;
    	     $args['orderby']        = 'name';
             $args['order']          = 'DESC';

    }
 
    return $args;
}

