<?php
class Category_Dropdown_Custom_Control extends WP_Customize_Control {

    /**
     * Control type.
     *
     * @access public
     * @var string
     */
    public $type = 'dropdown-category';

    /**
     * Taxonomy.
     *
     * @access public
     * @var string
     */
    public $taxonomy = '';

    /**
     * Constructor.
     *
     * @since 1.0.0
     *
     * @param WP_Customize_Manager $manager Customizer bootstrap instance.
     * @param string               $id      Control ID.
     * @param array                $args    Optional. Arguments to override class property defaults.
     */
    public function __construct( $manager, $id, $args = array() ) {

        $news25_taxonomy = 'category';
        if ( isset( $args['taxonomy'] ) ) {
            $taxonomy_exist = taxonomy_exists( $args['taxonomy']  );
            if ( true === $taxonomy_exist ) {
                $news25_taxonomy =  $args['taxonomy'];
            }
        }
        $args['taxonomy'] = $news25_taxonomy;
        $this->taxonomy =  $news25_taxonomy;

        parent::__construct( $manager, $id, $args );
    }

    /**
     * Render content.
     *
     * @since 1.0.0
     */
    public function render_content() {

        $tax_args = array(
            'hierarchical' => 0,
            'taxonomy'     => $this->taxonomy,
        );
        $cats = get_categories( $tax_args );

        ?>
        <label>
            <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
            <?php if ( ! empty( $this->description ) ) : ?>
                <span class="description customize-control-description"><?php echo $this->description; ?></span>
            <?php endif; ?>
            <select  <?php $this->link(); ?>>
                <?php
                printf( '<option value="%s" %s>%s</option>', 'All', selected( $this->value(), '', false ), __( 'All', 'news-25' )  );7
                ?>
                <?php if ( ! empty( $cats ) ) :  ?>
                    <?php foreach ( $cats as $key => $cat ) :  ?>
                        <?php
						$selected = is_array($this->value()) ? $this->value()[0] : $this->value();
                        printf( '<option value="%s" %s>%s</option>', esc_attr( $cat->name ), selected( $selected, $cat->name, false ), esc_html( $cat->name ) );
                        ?>
                    <?php endforeach ?>
                <?php endif ?>
            </select>
        </label>
        <?php
    }
}

class News25_Category_Dropdown_Custom_Control extends WP_Customize_Control {
    private $cats = false;

    public function __construct($manager, $id, $args = array(), $options = array()) {
        // Ensure all categories are loaded
        $this->cats = get_categories(wp_parse_args($options, array(
            'hide_empty' => false,
            'number'     => 0, // no limit
        )));
        
        // Set sanitize callback to the function from the external file
        $this->sanitize_callback = 'sanitize_categories'; // Reference the external sanitize function

        parent::__construct($manager, $id, $args);
    }

    public function render_content() {
        if ( ! empty( $this->cats ) ) {
            ?>
            <label>
                <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
                <?php if ( ! empty( $this->description ) ) : ?>
                    <span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span>
                <?php endif; ?>
                <select multiple <?php $this->link(); ?> style="height: 200px;">
                    <?php
                    foreach ( $this->cats as $cat ) {
                        printf(
                            '<option value="%s" %s>%s</option>',
                            esc_attr( $cat->term_id ),
                            selected( in_array( $cat->term_id, (array) $this->value() ), true, false ),
                            esc_html( $cat->name )
                        );
                    }
                    ?>
                </select>
            </label>
            <?php
        }
    }
}