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

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function ofisuport_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', 'ofisuport_body_classes' );

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


/** 
 * Detect if the Oficina de Suport "ofisuport" plugin is active
 */

function ofisuport_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( 'oficinasuport-wp-plugin/ofisuport-wp-plugin.php' ) ) {
  //plugin is activated do
	}
	//do stuff here
} //closing if is_plugin_active 

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


/** 
 * Echo the post type slug
 */
function ofisuport_class_posttype (){
	if( is_post_type_archive( array('ofisuport-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( 'ofisuport_display_faqs_labels' ) ) :
  function ofisuport_display_faqs_labels() {  
?>
    <nav id="tipus-faqs" class="ofisuport-faqs-types">
      <ul class="ofisuport-label">
      <?php 
        // https://developer.wordpress.org/reference%2Ffunctions%2Fwp_list_categories%2F/
        wp_list_categories(
          array(
            'child_of'            => 1, // 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 ofisuport_display_faqs_labels

/**
 * Displays categories for each FAQ  
 */

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

    $categories = get_the_category();
    if ( ! empty( $categories ) ) {  
      echo '    <nav id="tipus-faqs" class="ofisuport-faqs-types">';
      echo '     <ul class="ofisuport-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 ofisuport_display_each_faq_categories

/**
 * Display search form for FAQs
 */

if ( ! function_exists( 'ofisuport_display_faq_search' ) ) :
  function ofisuport_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
?> 
    <aside id="search-form-faqs" class="ofisuport-search-faqs">
      <form class="ofisuport-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="ofisuport-search-submit ofisuport-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>
    </aside>
<?php
  }
endif; //end ofisuport_display_each_faq_categories
