<?php

/**
 * Custom footer items on the Theme customizer admin page.
 * 
 * @package Camp Maine
 */

/**
 * Adds the Footer section, footer item settings, and controls to the theme customizer.
 *
 * @param WP_Customize_Manager $wp_customize instance of the WP_Customize_Manager class.
 */
function campmaine_footer_customizer( $wp_customize ) {
    $wp_customize->add_section(
        'footer_section',
        array(
            'title' => 'Footer',
            'description' => 'This theme supports setting up to ' . MAX_FOOTER_ITEMS . ' footer items.',
            'priority' => 100
        )
    );

    for ($i=0; $i<MAX_FOOTER_ITEMS;$i++) {
    	$wp_customize->add_setting('footer_item_' . $i, 
		array('default' => '',
		      'sanitize_callback' => 'campmaine_sanitize_footer_item',)
		);
    }

    for ($i=0; $i<MAX_FOOTER_ITEMS;$i++) {
        $wp_customize->add_control(
        'footer_item_' . $i,
            array(
                'label' => 'Footer item #' . (1 + $i),
                'section' => 'footer_section',
                'type' => 'text',
                'priority' => $i,
            )
	);    
    }

    $wp_customize->add_setting('show_wordpress_branding', array('sanitize_callback' => 'campmaine_sanitize_checkbox'));
    $wp_customize->add_control('show_wordpress_branding',
        array(
    	    'type' => 'checkbox',
            'label' => 'Show Proudly powered by WordPress',
	    'default' => '1',
            'section' => 'footer_section',
	)
    );

    $wp_customize->add_setting('show_design_team_branding', array('sanitize_callback' => 'campmaine_sanitize_checkbox'));
    $wp_customize->add_control('show_design_team_branding',
        array(
            'type' => 'checkbox',
            'label' => "Show Design by Web Development @ TAM'S TRADING POST",
	    'default' => '0',
            'section' => 'footer_section',
        )
    );

}
add_action( 'customize_register', 'campmaine_footer_customizer' );

/**
 * Callback for footer item textfield input.
 *
 * @uses wp_kses_post() to sanitize content for allowed HTML tags for input.
 * @uses force_balance_tags() to balance unclosed html tags.
 * @param string $input user input from footer item textfield.
 * @return string sanitized input.
 */
function campmaine_sanitize_footer_item ($input) {
    return wp_kses_post( force_balance_tags( $input ) );
}

/**
 * Callback to check checkbox input.
 *
 * @uses strcmp to compare input
 * @param string $input user input from checkbox
 * @return string sanitized input
 */
function campmaine_sanitize_checkbox ($input) {
    return strcmp('1', $input) == 0 ? '1' : '';
}