<?php
/**
 * Single Posts and their fields within the Innerpage Settings panel.
 * 
 * Provides options for configuring the appearance and behavior of single post pages.
 *
 * @package Yaatra
 */


// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'customize_register', 'yaatra_register_single_posts_options' );

if ( ! function_exists( 'yaatra_register_single_posts_options' ) ) :

    /**
     * Registers theme options for the Single Posts section.
     * 
     * Adds and configures settings for single post pages within the WordPress Customizer.
     *
     * @param WP_Customize_Manager $wp_customize The Customizer object managing theme options.
     * @since 1.0.0
     */

    function yaatra_register_single_posts_options( $wp_customize ) {

        /*
         * Failsafe is safe
         */
        if ( ! isset( $wp_customize ) ) {
            return;
        }

        /**
         * Single Posts Section
         * 
         * Innerpage Settings > Single Posts
         * 
         * @uses $wp_customize->add_section() https://developer.wordpress.org/reference/classes/wp_customize_manager/add_section/
         * @since 1.0.0
         */
        $wp_customize->add_section( new Yaatra_Customize_Section (
            $wp_customize, 'yaatra_section_post_single',
                array(
                    'priority'  => 15,
                    'panel'     => 'yaatra_panel_innerpage',
                    'title'     => __( 'Single Posts', 'yaatra' ),
                )
            )
        );

        /**
         * Heading field for related posts
         *
         * Innerpage Settings > Single Posts
         *
         * @since 1.2.0
         */
        $wp_customize->add_setting( 'single_posts_related_heading',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Yaatra_Control_Heading(
            $wp_customize, 'single_posts_related_heading',
                array(
                    'priority'          => 45,
                    'section'           => 'yaatra_section_post_single',
                    'settings'          => 'single_posts_related_heading',
                    'label'             => __( 'Related Posts', 'yaatra' )
                )
            )
        );

        /**
         * Toggle option for single posts related posts
         *
         * Innerpage Settings > Single Posts
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'yaatra_enable_related_posts',
            array(
                'default'           => yaatra_get_customizer_default( 'yaatra_enable_related_posts' ),
                'sanitize_callback' => 'yaatra_sanitize_checkbox'
            )
        );
        $wp_customize->add_control( new Yaatra_Control_Toggle(
            $wp_customize, 'yaatra_enable_related_posts',
                array(
                    'priority'          => 50,
                    'section'           => 'yaatra_section_post_single',
                    'settings'          => 'yaatra_enable_related_posts',
                    'label'             => __( 'Enable Related Posts', 'yaatra' )
                )
            )
        );

        /**
         * Text field for related posts section title
         *
         * Innerpage Settings > Single Posts
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'related_posts_title',
            array(
                'default'           => yaatra_get_customizer_default( 'related_posts_title' ),
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( 'related_posts_title',
            array(
                'priority'          => 55,
                'section'           => 'yaatra_section_post_single',
                'settings'          => 'related_posts_title',
                'label'             => __( 'Enter Section Title', 'yaatra' ),
                'type'              => 'text',
                'active_callback'   => 'yaatra_cb_has_enable_single_posts_related'
            )
        );

        /**
         * Number field for related posts count
         *
         * Innerpage Settings > Single Posts
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'yaatra_related_posts_count',
            array(
                'default'           => yaatra_get_customizer_default( 'yaatra_related_posts_count' ),
                'sanitize_callback' => 'absint'
            )
        );
        $wp_customize->add_control( 'yaatra_related_posts_count',
            array(
                'priority'          => 60,
                'section'           => 'yaatra_section_post_single',
                'settings'          => 'yaatra_related_posts_count',
                'label'             => __( 'Number of Related Posts.', 'yaatra' ),
                'type'              => 'number',
                'active_callback'   => 'yaatra_cb_has_enable_single_posts_related'
            )
        );

        /**
         * Select field for related posts orderby
         *
         * Innerpage Settings > Single Posts
         *
         * @since 1.0.0
         */
        $wp_customize->add_setting( 'yaatra_related_posts_orderby',
            array(
                'default'           => yaatra_get_customizer_default( 'yaatra_related_posts_orderby' ),
                'sanitize_callback' => 'yaatra_sanitize_select'
            )
        );
        $wp_customize->add_control( 'yaatra_related_posts_orderby',
            array(
                'priority'          => 65,
                'section'           => 'yaatra_section_post_single',
                'settings'          => 'yaatra_related_posts_orderby',
                'label'             => __( 'Related Posts Orderby.', 'yaatra' ),
                'type'              => 'select',
                'choices'           => yaatra_single_posts_related_orderby_choices(),
                'active_callback'   => 'yaatra_cb_has_enable_single_posts_related'
            )
        );

        /**
         * Upgrade field for single post section
         * 
         * General Settings > Single Posts
         *
         * @since 1.2.0
         */ 
        $wp_customize->add_setting( 'upgrade_single_post',
            array(
                'sanitize_callback' => 'sanitize_text_field'
            )
        );
        $wp_customize->add_control( new Yaatra_Control_Upgrade(
            $wp_customize, 'upgrade_single_post',
                array(
                    'priority'      => 70,
                    'section'       => 'yaatra_section_post_single',
                    'settings'      => 'upgrade_single_post',
                    'label'         => __( 'More features with Yaatra Pro', 'yaatra' ),
                    'choices'       => yaatra_upgrade_choices( 'yaatra_single_post' )
                )
            )
        );

    }

endif;