<?php
/**
 * Theme Options
 *
 * @package Personal_Trainer
 */

/**
 * Add theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function personaltrainer_theme_options( $wp_customize ) {
	$wp_customize->add_panel( 'personaltrainer_theme_options', array(
		'title'    => esc_html__( 'Theme Options', 'personal-trainer' ),
		'priority' => 130,
	) );

	// Breadcrumb Option.
	$wp_customize->add_section( 'personaltrainer_breadcrumb_options', array(
		'description'   => esc_html__( 'Breadcrumbs are a great way of letting your visitors find out where they are on your site with just a glance.', 'personal-trainer' ),
		'panel'         => 'personaltrainer_theme_options',
		'title'         => esc_html__( 'Breadcrumb', 'personal-trainer' ),
	) );

	personaltrainer_register_option( $wp_customize, array(
			'name'              =>'personaltrainer_breadcrumb_option',
			'default'           => 1,
			'sanitize_callback' => 'personaltrainer_sanitize_checkbox',
			'label'             => esc_html__( 'Check to enable Breadcrumb', 'personal-trainer' ),
			'section'           => 'personaltrainer_breadcrumb_options',
			'type'              => 'checkbox',
	    )
	);
    // Breadcrumb Option End

	// Layout Options
	$wp_customize->add_section( 'personaltrainer_layout_options', array(
		'title' => esc_html__( 'Layout Options', 'personal-trainer' ),
		'panel' => 'personaltrainer_theme_options',
		)
	);

	/* Layout Type */
	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_layout_type',
			'default'           => 'fluid',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_site_layout_option_enabled',
			'label'             => esc_html__( 'Site Layout', 'personal-trainer' ),
			'section'           => 'personaltrainer_layout_options',
			'type'              => 'radio',
			'choices'           => array(
				'fluid' => esc_html__( 'Fluid', 'personal-trainer' ),
				'boxed' => esc_html__( 'Boxed', 'personal-trainer' ),
			),
		)
	);

	/* Default Layout */
	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_default_layout',
			'default'           => 'right-sidebar',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'label'             => esc_html__( 'Default Layout', 'personal-trainer' ),
			'section'           => 'personaltrainer_layout_options',
			'type'              => 'radio',
			'choices'           => array(
				'right-sidebar'         => esc_html__( 'Right Sidebar ( Content, Primary Sidebar )', 'personal-trainer' ),
				'no-sidebar-full-width' => esc_html__( 'No Sidebar: Full Width', 'personal-trainer' ),
			),
		)
	);

	/* Homepage/Archive Layout */
	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_homepage_archive_layout',
			'default'           => 'no-sidebar-full-width',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'label'             => esc_html__( 'Homepage/Archive Layout', 'personal-trainer' ),
			'section'           => 'personaltrainer_layout_options',
			'type'              => 'radio',
			'choices'           => array(
				'right-sidebar'         => esc_html__( 'Right Sidebar ( Content, Primary Sidebar )', 'personal-trainer' ),
				'no-sidebar-full-width' => esc_html__( 'No Sidebar: Full Width', 'personal-trainer' ),
			),
		)
	);

	// Single Page/Post Image Layout
	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_single_layout',
			'default'           => 'disabled',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_single_page_post_image_option_enabled',
			'label'             => esc_html__( 'Single Page/Post Image', 'personal-trainer' ),
			'section'           => 'personaltrainer_layout_options',
			'type'              => 'radio',
			'choices'           => array(
				'disabled'                 => esc_html__( 'Disabled', 'personal-trainer' ),
				'post-thumbnail'           => esc_html__( 'Enable', 'personal-trainer' ),
			),
		)
	);


	// Excerpt Options.
	$wp_customize->add_section( 'personaltrainer_excerpt_options', array(
		'panel' => 'personaltrainer_theme_options',
		'title' => esc_html__( 'Excerpt Options', 'personal-trainer' ),
	) );

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_excerpt_length',
			'default'           => '55',
			'sanitize_callback' => 'absint',
			'description' => esc_html__( 'Excerpt length. Default is 55 words', 'personal-trainer' ),
			'input_attrs' => array(
				'min'   => 10,
				'max'   => 200,
				'step'  => 5,
				'style' => 'width: 60px;',
			),
			'label'    => esc_html__( 'Excerpt Length (words)', 'personal-trainer' ),
			'section'  => 'personaltrainer_excerpt_options',
			'type'     => 'number',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_excerpt_more_text',
			'default'           => esc_html__( 'Continue reading &raquo;', 'personal-trainer' ),
			'sanitize_callback' => 'sanitize_text_field',
			'label'             => esc_html__( 'Read More Text', 'personal-trainer' ),
			'section'           => 'personaltrainer_excerpt_options',
			'type'              => 'text',
		)
	);

	// Excerpt Options.
	$wp_customize->add_section( 'personaltrainer_search_options', array(
		'panel'     => 'personaltrainer_theme_options',
		'title'     => esc_html__( 'Search Options', 'personal-trainer' ),
	) );

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_search_text',
			'default'           => esc_html__( 'Search ...', 'personal-trainer' ),
			'sanitize_callback' => 'wp_kses_data',
			'label'             => esc_html__( 'Search Text', 'personal-trainer' ),
			'section'           => 'personaltrainer_search_options',
			'type'              => 'text',
		)
	);

	// Contact Option
	$wp_customize->add_section( 'personaltrainer_contact_info', array(
			'active_callback' => 'personaltrainer_is_contact_info_enabled',
			'panel'       	  => 'personaltrainer_theme_options',
			'title'       	  => esc_html__( 'Contact Info', 'personal-trainer' ),
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_contact_info_phone',
			'sanitize_callback' => 'sanitize_text_field',
			'default'           => '(123) 456-7890',
			'label'             => esc_html__( 'Phone', 'personal-trainer' ),
			'section'           => 'personaltrainer_contact_info',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_contact_info_email',
			'default'           => 'something@domain.com',
			'sanitize_callback' => 'sanitize_email',
			'label'             => esc_html__( 'Email', 'personal-trainer' ),
			'section'           => 'personaltrainer_contact_info',
		)
	);

	// Homepage / Frontpage Options.
	$wp_customize->add_section( 'personaltrainer_homepage_options', array(
		'description' => esc_html__( 'Only posts that belong to the categories selected here will be displayed on the front page', 'personal-trainer' ),
		'panel'       => 'personaltrainer_theme_options',
		'title'       => esc_html__( 'Homepage / Frontpage Options', 'personal-trainer' ),
	) );

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_recent_posts_heading',
			'sanitize_callback' => 'sanitize_text_field',
			'active_callback'   => 'personaltrainer_is_recent_post_heading_enabled',
			'default'           => esc_html__( 'Recent Posts', 'personal-trainer' ),
			'label'             => esc_html__( 'Recent Posts Heading', 'personal-trainer' ),
			'section'           => 'personaltrainer_homepage_options',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_front_page_category',
			'sanitize_callback' => 'personaltrainer_sanitize_category_list',
			'custom_control'    => 'Personaltrainer_Multi_Cat',
			'label'             => esc_html__( 'Categories', 'personal-trainer' ),
			'section'           => 'personaltrainer_homepage_options',
			'type'              => 'dropdown-categories',
		)
	);

	// Pagination Options.
	$pagination_type = get_theme_mod( 'personaltrainer_pagination_type', 'default' );

	$nav_desc = '';

	/**
	* Check if navigation type is Jetpack Infinite Scroll and if it is enabled
	*/
	$nav_desc = sprintf(
		wp_kses(
			__( 'For infinite scrolling, use %1$sCatch Infinite Scroll Plugin%2$s with Infinite Scroll module Enabled.', 'personal-trainer' ),
			array(
				'a' => array(
					'href' => array(),
					'target' => array(),
				),
				'br'=> array()
			)
		),
		'<a target="_blank" href="https://wordpress.org/plugins/catch-infinite-scroll/">',
		'</a>'
	);

	$wp_customize->add_section( 'personaltrainer_pagination_options', array(
		'description' => $nav_desc,
		'panel'       => 'personaltrainer_theme_options',
		'title'       => esc_html__( 'Pagination Options', 'personal-trainer' ),
	) );

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_pagination_type',
			'default'           => 'default',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'choices'           => personaltrainer_get_pagination_types(),
			'label'             => esc_html__( 'Pagination type', 'personal-trainer' ),
			'section'           => 'personaltrainer_pagination_options',
			'type'              => 'select',
		)
	);

	/* Scrollup Options */
	$wp_customize->add_section( 'personaltrainer_scrollup', array(
		'panel'    => 'personaltrainer_theme_options',
		'title'    => esc_html__( 'Scrollup Options', 'personal-trainer' ),
	) );

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_disable_scrollup',
			'sanitize_callback' => 'personaltrainer_sanitize_checkbox',
			'label'             => esc_html__( 'Disable Scroll Up', 'personal-trainer' ),
			'section'           => 'personaltrainer_scrollup',
			'type'              => 'checkbox',
		)
	);
}
add_action( 'customize_register', 'personaltrainer_theme_options' );

/** Active Callback Functions */
if( ! function_exists( 'personaltrainer_is_header_top_enabled' ) ) :
	/**
	* Return true if header top is enabled
	*
	* @since Personal Trainer 0.1
	*/
	function personaltrainer_is_header_top_enabled( $control ) {
		return ( ! $control->manager->get_setting( 'personaltrainer_disable_header_top' )->value() );
	}
endif;

if( ! function_exists( 'personaltrainer_is_contact_info_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_contact_info_enabled( $control ) {
		$enable_phone = $control->manager->get_setting( 'personaltrainer_contact_info_phone' )->value();
		$enable_email = $control->manager->get_setting( 'personaltrainer_contact_info_email' )->value();
		return ( ($enable_phone != $control->manager->get_setting( 'personaltrainer_contact_info_phone' )->default) || ($enable_email != $control->manager->get_setting( 'personaltrainer_contact_info_email' )->default) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_recent_post_heading_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_recent_post_heading_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_recent_posts_heading' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_recent_posts_heading' )->default) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_site_layout_option_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_site_layout_option_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_layout_type' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_layout_type' )->default) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_single_page_post_image_option_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_single_page_post_image_option_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_single_layout' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_single_layout' )->default) );
	}
endif;
