<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Xarxaprod_theme
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function xarxaprod_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'sidebar-funds' ) ) {
		$classes[] = 'no-sidebar';
	}

	return $classes;
}
add_filter( 'body_class', 'xarxaprod_body_classes' );

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function xarxaprod_pingback_header() {
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
	}
}
add_action( 'wp_head', 'xarxaprod_pingback_header' );


/** 
 * Detect if the "xarxaprod" plugin is active
 */

function xarxaprod_plugin_active() {
  // Detect plugin. For use on Front End only.
	include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  // check for plugin using plugin name
  if ( is_plugin_active( 'xarxaprod-wp-plugin/xarxaprod-wp-plugin.php' ) ) {
  //plugin is activated do
	}
	//do stuff here
} //closing if is_plugin_active 

//add_action( 'wp_head', 'xarxaprod_plugin_active' );


/** 
 * Echo the post type slug
 */
function xarxaprod_class_posttype (){
	if( is_post_type_archive( array('xarxaprod-ajut') )) {
		echo 'archive-custom-post-type-';
	  printf(get_post_type_object( get_post_type() )->rewrite['slug'] );
	}
}
/**
 * Displays the FAQS subcategories from category id=1 
 */

if ( ! function_exists( 'xarxaprod_display_faqs_labels' ) ) :
  function xarxaprod_display_faqs_labels() {  
?>
    <nav id="tipus-faqs" class="xarxaprod-faqs-types">
      <ul class="xarxaprod-label">
      <?php 
        // https://developer.wordpress.org/reference%2Ffunctions%2Fwp_list_categories%2F/
        wp_list_categories(
          array(
            'child_of'            => 38, // category FAQ id=1
            'hide_empty'          => 1,
            'hide_title_if_empty' => true,
            'separator'           => '<li>',
            'style'               => 'list',
            'taxonomy'            => 'category',
            'title_li'            => '',
          )
        ); ?>
      </ul>
    </nav>
<?php
  }
endif; //end xarxaprod_display_faqs_labels

/**
 * Displays categories for each FAQ  
 */

if ( ! function_exists( 'xarxaprod_display_each_faq_categories' ) ) :
  function xarxaprod_display_each_faq_categories() {  

    $categories = get_the_category();
    if ( ! empty( $categories ) ) {  
      echo '    <nav id="tipus-faqs" class="xarxaprod-faqs-types">';
      echo '     <ul class="xarxaprod-label">';
      foreach ( $categories as $category ) {
        if ( ! ($category->cat_ID == '1') ) { //cat_ID '1' is defined   as default FAQ
          echo '<li class="cat-item"><a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a></li>';
        }                   
      }                     
      echo '     </ul>';    
      echo '    </nav>';    
      }                       
  }
endif; //end xarxaprod_display_each_faq_categories

/**
 * Display search form for FAQs
 */

if ( ! function_exists( 'xarxaprod_display_faq_search' ) ) :
  function xarxaprod_display_faqs_search() {  
    // search form for category
    // https://developer.wordpress.org/reference/functions/get_search_form/#comment-369
    // https://wordpress.stackexchange.com/questions/313037/restrict-a-search-to-a-custom-post-type
?> 
      <form class="xarxaprod-search-form" role="search" method="get" action="/">
        <label class="screen-reader-text" for="search">Search in ajuts</label>
        <input type="search" id="search" class="search-field" placeholder="cerca faqs..." value="<?php the_search_query();?>" name="s" />
        <input type="submit" id="searchsubmit" class="search-submit button button-search" value="cerca">
        <span class="xarxaprod-search-submit xarxaprod-icon-search"></span>
        <input type="hidden" value="posts" name="post_type" id="post_type" />
        <input type="hidden" value="faq" name="category_name" id="category_name" />
      </form>
<?php
  }
endif; //end xarxaprod_display_each_faq_categories