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

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

	// Get the colorscheme or the default if there isn't one.
	$colors = equable_lite_sanitize_colorscheme( get_theme_mod( 'colorscheme', 'default' ) );
	$classes[] = 'colors-' . $colors;

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

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function equable_lite_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'equable_lite_pingback_header' );


/**
 * Returns true if a blog has more than 1 category.
 * @return bool*/
function equable_lite_categorized_blog() {
	$category_count = get_transient( 'equable_lite_categories' );

	if ( false === $category_count ) {
		// Create an array of all the categories that are attached to posts.
		$categories = get_categories( array(
			'fields'     => 'ids',
			'hide_empty' => 1,
			// We only need to know if there is more than one category.
			'number'     => 2,
		) );

		// Count the number of categories that are attached to the posts.
		$category_count = count( $categories );

		set_transient( 'equable_lite_categories', $category_count );
	}

	// Allow viewing case of 0 or 1 categories in post preview.
	if ( is_preview() ) {
		return true;
	}

	return $category_count > 1;
}


/**
 * Flush out the transients used in equable_lite_categorized_blog.*/
function equable_lite_category_transient_flusher() {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	// Like, beat it. Dig?
	delete_transient( 'equable_lite_categories' );
}
add_action( 'edit_category', 'equable_lite_category_transient_flusher' );
add_action( 'save_post',     'equable_lite_category_transient_flusher' );
