<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Madd_Magazine
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function madd_magazine_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	return $classes;
}
add_filter( 'body_class', 'madd_magazine_body_classes' );

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function madd_magazine_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'madd_magazine_pingback_header' );

function maddwp_pagination()
{
		global $wp_query;
		$big = 999999999;
		echo paginate_links(array(
				'base' => str_replace($big, '%#%', get_pagenum_link($big)),
				'format' => '?paged=%#%',
				'current' => max(1, get_query_var('paged')),
				'total' => $wp_query->max_num_pages,
				'prev_text'          => '&#8592;',
				'next_text'          => '&#8594;'
		));
}
add_action('init', 'maddwp_pagination');


add_action( 'add_meta_boxes', 'slide_post_box' );
add_action( 'save_post', 'slide_post_save_postdata' );

function slide_post_box() {
    add_meta_box( 
        'slider-post-id',
        'Show In Slider',
        'slide_post_callback',
        'post',
        'side',
        'default'
    );
}

function slide_post_callback($post)
{
    wp_nonce_field( 'slide_post_field_nonce', 'slide_post_noncename' );
    $saved = get_post_meta( $post->ID, 'home_slide_post', true);
    if( !$saved )
        $saved = 'default';

    $fields = array(
        'yes'       => __('Yes', 'madd-magazine'),
        'default'   => __('No', 'madd-magazine'),
    );

    foreach($fields as $key => $label)
    {
        printf(
            '<input type="radio" name="home_slide_post" value="%1$s" id="home_slide_post[%1$s]" %3$s />'.
            '<label for="home_slide_post[%1$s]"> %2$s ' .
            '</label><br>',
            esc_attr($key),
            esc_html($label),
            checked($saved, $key, false)
        );
    }
}

function slide_post_save_postdata( $post_id ) 
{
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return;

    if ( isset($_POST['slide_post_noncename']) && !wp_verify_nonce( $_POST['slide_post_noncename'], 'slide_post_field_nonce' ) )
        return;

    if ( isset($_POST['home_slide_post']) && $_POST['home_slide_post'] != "" ){
          update_post_meta( $post_id, 'home_slide_post', $_POST['home_slide_post'] );
    } 
}

function category_posts_pagination( $query ) 
{
	if ( is_category() && $query->is_main_query() ) 
	{
		$default_posts_per_page = get_option( 'posts_per_page' );
		$query->query_vars['posts_per_page'] = $default_posts_per_page + 1;
		return;
	}
	}
add_action( 'pre_get_posts', 'category_posts_pagination', 1 );