<?php
/**
 * Plugin Name: Easy Testimonials Carousel
 * Description: This plugin enables you to generate multiple testimonials categorized by type. You can create separate shortcodes for each category and effortlessly integrate them into your templates.   
 * Plugin URI: 
 * Version: 1.7
 * Author: Zluck Solutions
 * Author URI: https://zluck.com
 * License: GPLv3 or later
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain: easy-testimonials-carousel
 * Requires at least: 6.0
 * Tested up to: 6.9
 * Requires PHP: 8.0
 * Network: false
 */

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

// Define the absolute path to the includes file
$deactivation_feedback_file = plugin_dir_path(__FILE__) . 'includes/deactivation-feedback.php';

// Check if the file exists before including it
if (file_exists($deactivation_feedback_file)) {
    require_once $deactivation_feedback_file;
} else {
    // Optional: Log an error or handle the missing file scenario gracefully
    error_log('Required file "deactivation-feedback.php" is missing.');
}

// Session handling removed - using WordPress alternatives
//  view details link at plugin author area starts
function zl_add_view_details_link($plugin_meta, $plugin_file) {
    if (plugin_basename(__FILE__) === $plugin_file) {
        $zl_plugin_slug = 'easy-testimonials-carousel'; 
        $view_details_link = '<a href="' . admin_url('plugin-install.php?tab=plugin-information&plugin=' . $zl_plugin_slug . '&TB_iframe=true&width=600&height=550') . '" class="thickbox" aria-label="View plugin details">View Details</a>';
        // Add the link next to the Author
        array_splice($plugin_meta, 2, 3, $view_details_link);
    }
    return $plugin_meta;
}
add_filter('plugin_row_meta', 'zl_add_view_details_link', 10, 2);
//  view details link at plugin author area ends

function zl_enqueue_custom_scripts($post_id) {
    // Enqueue jQuery and jQuery Migrate
    wp_enqueue_script('jquery');
    wp_enqueue_script('jquery-migrate');
    wp_enqueue_style('dashicons');
    if (is_page() || is_single()) {
        // Enqueue slick slider styles
        wp_enqueue_style('slick-css', plugin_dir_url(__FILE__) . '/assets/slick/slick.css',array(),'1.0.0');
        wp_enqueue_style('slick-theme-css', plugin_dir_url(__FILE__) . '/assets/slick/slick-theme.css',array(),'1.0.0');
        // Enqueue font-awesome styles
        wp_enqueue_style('font-awesome', plugin_dir_url(__FILE__) . '/assets/css/font-awesome.css', array(),'6.4.0');

        wp_enqueue_style('my-plugin-style', plugin_dir_url(__FILE__) . '/assets/css/testimonial-style.css',array(),'1.0.0');
        

        // Enqueue slick slider script
        wp_enqueue_script('slick-js', plugin_dir_url(__FILE__) . 'assets/slick/slick.min.js', array('jquery'), '1.0.0');
        wp_enqueue_script('solid-js', plugin_dir_url(__FILE__) . '/assets/js/solid.min.js', array('jquery'), '6.5.1');
        // Enqueue extraSlickOptions script
        wp_enqueue_script('extra-slick-options', plugin_dir_url(__FILE__) . '/assets/js/extraSlickOptions.js', array('jquery', 'slick-js'), '1.0.0');
        // Enqueue the CSS and slider js file 
        wp_enqueue_script('custom-slider', plugin_dir_url(__FILE__) . '/assets/js/testimonial-slider.js', array('jquery'), '1.0.0');        
    }

    // Retrieve the saved font-family values for the three post meta fields
    $select_titlefont_family = get_post_meta($post_id, 'testimo_title_font_family', true);
    $select_contentfont_family = get_post_meta($post_id, 'testimo_content_font_family', true);
    $select_designationfont_family = get_post_meta($post_id, 'testimo_designation_font_family', true);

    // Create an array to hold the font families
    $font_family = array();

    // Add font families to the array if they are not empty
    if (!empty($select_titlefont_family) && is_string($select_titlefont_family)) {
        $font_family[] = urlencode($select_titlefont_family);
    }

    if (!empty($select_contentfont_family) && is_string($select_contentfont_family)) {
        $font_family[] = urlencode($select_contentfont_family);
    }

    if (!empty($select_designationfont_family) && is_string($select_designationfont_family)) {
        $font_family[] = urlencode($select_designationfont_family);
    }
    //Check if there are any font families to enqueue
    if (!empty($font_family)) {
        // Enqueue the Google Font stylesheet for the combined font families
        $font_family_string = implode('|', $font_family);
        $local_google_fonts_url = plugin_dir_url(__FILE__) . '/assets/css/font-family-css/'.$font_family_string.'.css';
        $style_version = '1.0.0'; 
        wp_enqueue_style('google-fonts', $local_google_fonts_url, array(), $style_version);
    }
    // Custom CSS Enqueue
    $css = get_option('custom_css');
    if (!empty($css)) {
        // Enqueue your custom CSS file
        wp_enqueue_style('my-plugin-style', plugin_dir_url(__FILE__) . '/assets/css/testimonial-style.css');
        
        wp_add_inline_style('my-plugin-style', $css);
    }
}
add_action('wp_enqueue_scripts', 'zl_enqueue_custom_scripts'); 

function zl_enqueue_admin_scripts() {
    // CodeMirror core and CSS mode
    wp_enqueue_script('codemirror-js', plugin_dir_url(__FILE__).'/assets/js/codemirror.min.js', array(), null, true);
    wp_enqueue_script('codemirror-mode-css', plugin_dir_url(__FILE__).'/assets/js/css.min.js', array('codemirror-js'), null, true);
    wp_enqueue_style('codemirror-css', plugin_dir_url(__FILE__).'/assets/css/codemirror.min.css');
    // Enqueue your custom stylesheet
    wp_enqueue_style('my-plugin-style', plugin_dir_url(__FILE__) . '/assets/css/testimonial-admin.css','1.0.0',true);  

    // Enqueue toaster library 
    wp_enqueue_script('toastr', plugin_dir_url(__FILE__). '/assets/js/toaster-js.js', array('jquery'), '2.1.3', true);
    wp_enqueue_style('toastr', plugin_dir_url(__FILE__) . '/assets/css/toaster-css.css', array(), '1.0.0');

    // Enqueue jQuery UI CSS
    wp_enqueue_style('jquery-ui-css', plugin_dir_url(__FILE__). '/assets/css/jquery-ui.css',array(),'1.12.1');

    // Reorder
    wp_enqueue_script('jquery-ui-sortable');
    // wp_enqueue_style('testimonial-admin', plugins_url('admin-style.css', __FILE__));
    
    // Enqueue Select2 CSS
    wp_enqueue_style('select2-css', plugin_dir_url(__FILE__).'/assets/css/select2.min.css','4.1.0');

    // Enqueue Select2 JS
    wp_enqueue_script('select2-js', plugin_dir_url(__FILE__).'/assets/js/select2.min.js', ['jquery'], null, true);

    // Enqueue jQuery UI scripts
    wp_enqueue_script('design-custom-script', plugin_dir_url(__FILE__) . '/assets/js/zl-script.js', array('jquery', 'jquery-ui-core', 'jquery-ui-dialog','jquery-ui-tabs','jquery-ui-button','jquery-ui-slider'), '1.0.0', true );

    // Pass PHP variables to JavaScript
    $post_id = get_the_id(); 
    $active_tab = get_post_meta($post_id, 'active_tab', true);
    $selected_design = get_post_meta($post_id, 'testimonial_design', true); // Default to 'design1'
    $design1 = plugin_dir_url(__FILE__) . 'assets/Images/slickslideone.png';
    $design2 = plugin_dir_url(__FILE__) . 'assets/Images/slickslidetwo.png';
    $design3 = plugin_dir_url(__FILE__) . 'assets/Images/slickslidethree.png';
    $design_options = array(
        'design1' => array(
            'label' => 'Design 1',
            'image' => $design1,
        ),
        'design2' => array(
            'label' => 'Design 2',
            'image' => $design2,
        ),
        'design3' => array(
            'label' => 'Design 3',
            'image' => $design3,
        ),
    );
    $zl_script_data = array(
        'designOptions' => $design_options,
        'postId' => $post_id,
        'activeTab' => $active_tab,
        'security' => wp_create_nonce('save_active_tab_nonce'),
        'deactivationNonce' => wp_create_nonce('deactivation_nonce')
    );
    wp_localize_script('design-custom-script', 'scriptData', $zl_script_data);
}
add_action('admin_enqueue_scripts', 'zl_enqueue_admin_scripts');

 // This is a function used to create custom post types and taxonomy.
function zl_custom_post_types_and_taxonomy() {
    // Register the Testimonials custom post type
    $zl_testimonial_labels = array(
        'name'               => _x( 'Testimonial', 'post type general name' , 'easy-testimonials-carousel'),
        'singular_name'      => _x( 'Testimonial', 'post type singular name', 'easy-testimonials-carousel'),
        'add_new'            => _x( 'Add New Testimonial', 'Testimonial','easy-testimonials-carousel'),
        'add_new_item'       => __( 'Add New Testimonial', 'easy-testimonials-carousel'),
        'edit_item'          => __( 'Edit Testimonial', 'easy-testimonials-carousel'),
        'new_item'           => __( 'New Testimonial', 'easy-testimonials-carousel'),
        'all_items'          => __( 'All Testimonials', 'easy-testimonials-carousel'),
        'view_item'          => __( 'View Testimonial', 'easy-testimonials-carousel'),
        'search_items'       => __( 'Search Testimonial', 'easy-testimonials-carousel'),
        'not_found'          => __( 'No Testimonial found', 'easy-testimonials-carousel'),
        'not_found_in_trash' => __( 'No Testimonial found in the Trash', 'easy-testimonials-carousel'),
        'menu_name'          => 'Easy Testimonials Carousel'
    );
    $zl_testimonial_args = array(
        'labels'        => $zl_testimonial_labels,
        'description'   => 'Holds our testimonials and testimonials specific data',
        'public'        => true,
        'menu_position' => 5,
        'menu_icon'     => 'dashicons-format-quote',
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt' ,'page-attributes'),
        'has_archive'   => true,
    );
    register_post_type( 'zl_testimonials', $zl_testimonial_args );

    // Register the Testimonial Category custom taxonomy
    $zl_taxonomy_labels = array(
        'name'              => _x( 'Testimonial Category', 'taxonomy general name', 'easy-testimonials-carousel' ),
        'singular_name'     => _x( 'Testimonial Category', 'taxonomy singular name', 'easy-testimonials-carousel' ),
        'search_items'      => __( 'Search Testimonial', 'easy-testimonials-carousel' ),
        'all_items'         => __( 'All Testimonial', 'easy-testimonials-carousel' ),
        'view_item'         => __( 'View Testimonial', 'easy-testimonials-carousel' ),
        'parent_item'       => __( 'Parent Testimonial', 'easy-testimonials-carousel' ),
        'parent_item_colon' => __( 'Parent Testimonial:', 'easy-testimonials-carousel' ),
        'edit_item'         => __( 'Edit Testimonial', 'easy-testimonials-carousel' ),
        'update_item'       => __( 'Update Testimonial', 'easy-testimonials-carousel' ),
        'add_new_item'      => __( 'Add New Testimonial', 'easy-testimonials-carousel' ),
        'new_item_name'     => __( 'New Testimonial Name', 'easy-testimonials-carousel' ),
        'not_found'         => __( 'No Testimonial Found', 'easy-testimonials-carousel' ),
        'back_to_items'     => __( 'Back to Testimonial', 'easy-testimonials-carousel' ),
        'menu_name'         => __( 'Testimonials Category', 'easy-testimonials-carousel' ),
    );
    $zl_taxonomy_args = array(
        'labels'            => $zl_taxonomy_labels,
        'hierarchical'      => true,
        'public'            => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'testimonial-category' ),
        'show_in_rest'      => true,
    );
    register_taxonomy( 'testimonial-category', 'zl_testimonials', $zl_taxonomy_args );

    // Register the Shortcode Settings custom post type
    $zl_shortcode_labels = array(
        'name'               => _x( 'Add Slider', 'post type general name', 'easy-testimonials-carousel'),
        'singular_name'      => _x( 'Create Slider', 'post type singular name', 'easy-testimonials-carousel'),
        'add_new'            => _x( 'Add New Slider', 'My Custom CPT', 'easy-testimonials-carousel'),
        'add_new_item'       => __( 'Add Slider', 'easy-testimonials-carousel'),
        'edit_item'          => __( 'Edit Slider', 'easy-testimonials-carousel'),
        'new_item'           => __( 'New My Slider', 'easy-testimonials-carousel'),
        'all_items'          => __( 'All Sliders', 'easy-testimonials-carousel'),
        'search_items'       => __( 'Search Slider', 'easy-testimonials-carousel'),
        'not_found'          => __( 'No Slider Found', 'easy-testimonials-carousel'),
        'not_found_in_trash' => __( 'No Slider Found', 'easy-testimonials-carousel'),
        'menu_name'          => 'Create Slider'
    );
    $zl_shortcode_args = array(
        'labels'        => $zl_shortcode_labels,
        'description'   => 'Holds your shortcode data',
        'public'        => true,
        'menu_position' => 6, // You can change the menu position as needed
        'supports'      => array( 'title' ),
        'has_archive'   => true,
        'show_in_menu'  => 'edit.php?post_type=zl_testimonials', // Display cpt under "zl_testimonials" in the admin menu
    );
    register_post_type( 'zl_shortc_setting', $zl_shortcode_args );
}
add_action( 'init', 'zl_custom_post_types_and_taxonomy' );

// create meta boxes for both cpt starts
function zl_add_testimonial_design_meta_box() {
    // meta box for post type zl_shortc_setting and in backend postype identify as all shortcode
    add_meta_box(
        'testimonial_position_meta_boxes', // Unique ID (should match the callback function name)
        'Testimonials Carousel Settings', // Box title
        'zl_render_testimonial_design_panel', // Callback function (should match the function name)
        'zl_shortc_setting', // Post type
        'normal', // Position
        'high' // Priority
    );
    add_meta_box(
    // meta box for post type zl_testimonials and in backend postype identify as easy testimonial carousel
        'testimonial_position_meta_box', 
        'Testimonial Information', 
        'zl_testimonial_title_meta_box', 
        'zl_testimonials', // Post type
        'normal', 
        'high'
    );
}
add_action('add_meta_boxes', 'zl_add_testimonial_design_meta_box');
// create meta boxes for both CPT ends.

function custom_post_type_add_thumbnail_support() {
    add_theme_support( 'post-thumbnails', array( 'zl_testimonials' ) );
}
add_action( 'after_setup_theme', 'custom_post_type_add_thumbnail_support' );

// Callback function for "zl_shortc_setting"(Shortcode Post type) Function starts.
function zl_render_testimonial_design_panel() {
    $post_id = get_the_id(); // Get the current post ID
    $selected_post_limit = get_post_meta($post_id, 'testimonial_post_limit', true); 
    $selected_desktop_col = get_post_meta($post_id, 'testimonial_desktop_meta', true); 
    $selected_laptop_col = get_post_meta($post_id, 'testimonial_laptop_meta', true); 
    $selected_tablet_col = get_post_meta($post_id, 'testimonial_tablet_meta', true); 
    $selected_category = get_post_meta($post_id, 'testimonial_category', true);
    $selected_bg_color = get_post_meta($post_id, 'testimonial_bg_color', true); 
    $selected_content_color = get_post_meta($post_id, 'testimonial_text_color', true); 
    $selected_title_color = get_post_meta($post_id, 'testimonial_title_color', true); 
    $selected_designation_color = get_post_meta($post_id, 'testimo_designa_color', true);
    $testimo_ratingicon_color = get_post_meta($post_id, 'testimo_ratingicon_color', true);
    $selected_feature_imagestyle = get_post_meta($post_id, 'testimoo_pic', true);
    $selected_layout = get_post_meta($post_id, 'testimoo_layout', true);
    $selected_arrowstyle = get_post_meta($post_id, 'testimoo_arrowstyle', true);
    $selected_columns = get_post_meta($post_id, 'testimoo_grid_columns', true);
    $selected_text_align = get_post_meta($post_id, 'testimo_text_align', true);
    $slidedelayvalue = get_post_meta($post_id, 'sliderange_field', true);
    $select_order_option = get_post_meta($post_id, 'testimopost_orderoption', true);
    $select_titlefont_family = get_post_meta($post_id, 'testimo_title_font_family', true);
    $select_contentfont_family = get_post_meta($post_id, 'testimo_content_font_family', true);
    $select_titlefont_style = get_post_meta($post_id, 'testimo_title_font_style', true);
    $select_contentfont_style = get_post_meta($post_id, 'testimo_content_font_style', true);
    $select_designationfont_style = get_post_meta($post_id, 'testimo_designation_font_style', true);
    $select_designationfont_family = get_post_meta($post_id, 'testimo_designation_font_family', true);
    $select_titlefont_size = get_post_meta($post_id, 'testimonial_fontsize_limit', true);
    $select_contentfont_size = get_post_meta($post_id, 'testimonial_content_fontsize_limit', true);
    $select_designationfont_size = get_post_meta($post_id, 'testimonial_designation_fontsize_limit', true);
    $select_column_gap = get_post_meta($post_id, 'testimonial_column_gap', true);
    $select_griditem_margin = get_post_meta($post_id, 'testimonial_griditem_margin', true);
    $select_griditem_padding = get_post_meta($post_id, 'testimonial_griditem_padding', true);
    $selected_testimonials = get_post_meta($post_id, 'testimonials_top_rated', true);
    if (!is_array($selected_testimonials)) {
        $selected_testimonials = [];
    }
    $toggle_value = get_post_meta($post_id, 'autoplaytoggle_field', true);
    if ($toggle_value === '') {
        $toggle_value = 'true'; // Default to true if not set
    }
    // Set the 'checked' attribute based on the value
    $checked_autoplay_attribute = $toggle_value === 'true' ? 'checked' : '';

    $slickslidesarrow = get_post_meta($post_id, 'slickslidesarrow', true);
    // Set the 'checked' attribute based on the value
    $checked_arrow_attributes = $slickslidesarrow === 'true' ? 'checked' : '';
    $slickslidesdots = get_post_meta($post_id, 'slickslidesdots', true);
    $slickslidesloop = get_post_meta($post_id, 'slickslidesloop', true);
    $slickslideshover = get_post_meta($post_id, 'slickslideshover', true);
    $slicksdisplayname= get_post_meta($post_id, 'toggle_display_name', true);
    $slicksdisplaypicture= get_post_meta($post_id, 'toggle_display_picture', true);
    $slicksdisplaydesignation= get_post_meta($post_id, 'toggle_display_designation', true);
    $slicksdisplayrating= get_post_meta($post_id, 'toggle_display_rating', true);
    $slicksdisplaycontent= get_post_meta($post_id, 'toggle_display_content', true);
    
    $slicksdisplay_drag_mt= get_post_meta($post_id, 'slickslides_drag_mt', true);
    $slicksdisplay_lazy_load= get_post_meta($post_id, 'slickslides_lazy_load', true);
    if ($slickslidesdots === '') {
        $slickslidesdots = 'true'; // Default to true if not set
    }
    // Set the 'checked' attribute based on the value
    $checked_dots_attributes = $slickslidesdots === 'true' ? 'checked' : '';
    $checked_loop_attributes = $slickslidesloop === 'true' ? 'checked' : '';
    $checked_hover_attributes = $slickslideshover === 'true' ? 'checked' : '';
    $checked_dname_attributes = $slicksdisplayname === 'true' ? 'checked' : '';
    $checked_dpicture_attributes = $slicksdisplaypicture === 'true' ? 'checked' : '';
    $checked_ddesignation_attributes = $slicksdisplaydesignation === 'true' ? 'checked' : '';
    $checked_drating_attributes = $slicksdisplayrating === 'true' ? 'checked' : '';
    $checked_dcontent_attributes = $slicksdisplaycontent === 'true' ? 'checked' : '';
    $checked_drag_mt_attributes = $slicksdisplay_drag_mt === 'true' ? 'checked' : '';
    $checked_lazy_load_attributes = $slicksdisplay_lazy_load === 'true' ? 'checked' : '';
    echo '<div class="wrap testimonial-wrap">'; ?>
    <?php
    // Retrieve the current testimonial design from the post meta (if available)
    $selected_design = get_post_meta($post_id, 'testimonial_design', true); // Default to 'design1'
    $design1 = plugin_dir_url(__FILE__) . 'assets/Images/slickslideone.png';
    $design2 = plugin_dir_url(__FILE__) . 'assets/Images/slickslidetwo.png';
    $design3 = plugin_dir_url(__FILE__) . 'assets/Images/slickslidethree.png';

    // Define the available designs
    $design_options = array(
        'design1' => array(
            'label' => 'Design 1',
            'image' => $design1,
        ),
        'design2' => array(
            'label' => 'Design 2',
            'image' => $design2,
        ),
        'design3' => array(
            'label' => 'Design 3',
            'image' => $design3,
        ),
    ); ?>

    <!-- Settings tabs and posts meta fields-->
    <div id="zl-tabs">
        <ul>
            <li><a href="#zl-tab-post-limit"><?php esc_html_e('Post Settings', 'easy-testimonials-carousel'); ?></a></li>
            <li><a href="#zl-tab-color-picker"><?php esc_html_e('Color Settings', 'easy-testimonials-carousel'); ?></a></li>
            <li><a href="#zl-tab-category"><?php esc_html_e('Category', 'easy-testimonials-carousel'); ?></a></li>
            <li><a href="#zl-tab-post-design"><?php esc_html_e('Slider Designs', 'easy-testimonials-carousel'); ?></a></li>
            <li><a href="#zl-tab-slick-slides"><?php esc_html_e('Slider Settings', 'easy-testimonials-carousel'); ?></a></li>
            <li><a href="#zl-tab-slick-display"><?php esc_html_e('Display Settings', 'easy-testimonials-carousel'); ?></a></li>
            <li><a href="#zl-tab-top-rated"><?php esc_html_e('Top Rated', 'easy-testimonials-carousel'); ?></a></li>
        </ul>

        <div id="zl-tab-post-limit">
            <label for="testimonial_post_limit"><?php esc_html_e('Posts Limit:', 'easy-testimonials-carousel'); ?></label>
            <input type="number" name="testimonial_post_limit" id="zl_testimonial_post_limit" value="<?php echo esc_attr(empty($selected_post_limit) ? 10 : $selected_post_limit); ?>" min="-1" max="50" />
            <?php $post_limit_nonce_action = 'testimonial_post_limit_action'; ?>
            <input type="hidden" name="testimonial_post_limit_nonce" value="<?php echo esc_attr(wp_create_nonce($post_limit_nonce_action)); ?>" />
            <p class="zl_shortnoteforall"><?php esc_html_e('Limit number of testimonials to show.'); ?></p><br>
            <div class="zl-testimo-post-orderstyle">
                <label for="custom-order-option"><?php esc_html_e('Order By:', 'easy-testimonials-carousel'); ?></label>
                <?php wp_nonce_field('zlorderoption_nonce_action', 'zlorderoption_nonce'); ?>
                <select name="testimopost_orderoption" id="zl-custom-order-option">
                    <option value="latest" <?php selected($select_order_option, 'latest'); ?>><?php esc_html_e('Latest', 'easy-testimonials-carousel'); ?></option>
                    <option value="asc" <?php selected($select_order_option, 'asc'); ?>><?php esc_html_e('Ascending', 'easy-testimonials-carousel'); ?></option>
                    <option value="desc" <?php selected($select_order_option, 'desc'); ?>><?php esc_html_e('Descending', 'easy-testimonials-carousel'); ?></option>
                    <option value="modified" <?php selected($select_order_option, 'modified'); ?>><?php esc_html_e('Modified', 'easy-testimonials-carousel'); ?></option>
                    <option value="date-wise" <?php selected($select_order_option, 'date-wise'); ?>><?php esc_html_e('Date', 'easy-testimonials-carousel'); ?></option>
                    <option value="random" <?php selected($select_order_option, 'random'); ?>><?php esc_html_e('Random', 'easy-testimonials-carousel'); ?></option>
                </select>
                <p class="zl_shortnoteforall"><?php esc_html_e('Display posts by order wise', 'easy-testimonials-carousel'); ?></p><br>
            </div><hr>

            <?php
            $zl_font_families = array(
                'inherit',
                'Arial',
                'Times New Roman',
                'Verdana',
                'Georgia',
                'Courier New',
                'Tahoma',
                'Trebuchet MS',
                'Arial Black',
                'Impact',
                'Comic Sans MS',
                'Palatino',
                'Garamond',
                'Book Antiqua',
                'Lucida Sans Unicode',
                'Lucida Console',
                'Helvetica',
                'Geneva',
                'Monaco',
                'Cursive',
                'fantasy',
                'serif',
                'sans-serif',
                'monospace',
                'Open+Sans', // Google Font with a space
                'Another+Google+Font', // Replace spaces with plus (+)
                'Roboto',
                'Lato',
                'Montserrat',
                'Oswald',
                'Raleway',
                'Poppins',
                'Ubuntu',
                'Noto+Sans',
                'Playfair+Display',
                'Source+Sans+Pro',
                'Merriweather',
                'Droid+Sans',
                'Cabin',
                'Quicksand',
                'Pacifico',
                'Alegreya',
                'Crimson+Text',
                'Inconsolata',
                'Fira+Sans',
                'PT+Sans',
                'Dosis',
                'Bitter',
                'Lobster',
                'Exo',
                'Dancing+Script',
                'Karla',
                'Muli',
                'Rubik',
                'Julius+Sans+One',
                'Varela+Round',
                'Rokkitt',
                'Nunito',
                'Cairo',
                'Catamaran',
                'Maven+Pro',
                'Cinzel',
                'Archivo',
                'Asap',
                'Abril+Fatface',
                'Coda',
                'Pacifico',
                'Fjalla+One',
                'Bungee',
                'Comfortaa',
                'Gloria+Hallelujah',
                'Great+Vibes',
                'Indie+Flower',
                'Kaushan+Script',
                'Lobster+Two',
                'Lora',
                'Marmelad',
                'Merriweather+Sans',
                'Orbitron',
                'Overpass',
                'Pangolin',
                'Piedra',
                'Quicksand',
                'Rakkas',
                'Righteous',
                'Roboto+Condensed',
                'Shadows+Into+Light',
                'Sunflower',
                'Syncopate',
                'Viga',
                'Zilla+Slab',
                // Add more Google Fonts here
            ); ?>

            <?php
            $font_styles = array(
               'normal',
               'italic',
               'oblique',
               'initial',
               'inherit',
            ); ?>

            <div class="zl_testimo-title_font_style">
                <label for="title_font_style"><?php esc_html_e('Font Style (Title):', 'easy-testimonials-carousel'); ?></label>
                <select name="testimo_title_font_style" id="zl_title_font_style">
                    <?php foreach ($font_styles as $family) : ?>
                        <option value="<?php echo esc_attr($family); ?>" <?php selected($select_titlefont_style, $family); ?>><?php echo esc_html($family); ?>
                        </option>
                        <?php endforeach; ?>
                </select>
                <input type="hidden" name="testimo_nonce_title_font_style" value="<?php echo esc_attr(wp_create_nonce('testimo_title_font_style_nonce')); ?>" />
                <p class="zl_shortnoteforall"><?php esc_html_e('Set the font style of title', 'easy-testimonials-carousel'); ?></p><br>
            </div>

            <div class="zl_testimo-content_font_style">
                <label for="content_font_style"><?php esc_html_e('Font Style (Content):', 'easy-testimonials-carousel'); ?></label>
                <select name="testimo_content_font_style" id="zl_content_font_style">
                    <?php foreach ($font_styles as $family) : ?>
                        <option value="<?php echo esc_attr($family); ?>" <?php selected($select_contentfont_style, $family); ?>><?php echo esc_html($family); ?></option>
                        <?php endforeach; ?>
                </select>
                <input type="hidden" name="testimo_nonce_content_font_style" value="<?php echo esc_attr(wp_create_nonce('testimo_content_font_style_nonce')); ?>" />
                <p class="zl_shortnoteforall"><?php esc_html_e('Set the font style of content', 'easy-testimonials-carousel'); ?></p><br>
            </div>
            
            <div class="zl_testimo_designation_font_style">
                <label for="designation_font_style"><?php esc_html_e('Font Style (Designation):', 'easy-testimonials-carousel'); ?></label>
                <select name="testimo_designation_font_style" id="zl_designation_font_style">
                    <?php foreach ($font_styles as $family) : ?>
                        <option value="<?php echo esc_attr($family); ?>" <?php selected($select_designationfont_style, $family); ?>><?php echo esc_html($family); ?></option>
                        <?php endforeach; ?>
                </select>
                <input type="hidden" name="testimo_nonce_designation_font_style" value="<?php echo esc_attr(wp_create_nonce('testimo_designation_font_style_nonce')); ?>" />
                <p class="zl_shortnoteforall"><?php esc_html_e('Set the font style of designation', 'easy-testimonials-carousel'); ?></p><br>
            </div><br><hr>

            <div class="zl_testimo_title_font_family-style">
                <label for="title_font_family"><?php esc_html_e('Font Family (Title):', 'easy-testimonials-carousel'); ?></label>
                <select name="testimo_title_font_family" id="zl_title_font_family">
                    <?php foreach ($zl_font_families as $family) : ?>
                        <option value="<?php echo esc_attr($family); ?>" <?php selected($select_titlefont_family, $family); ?>><?php echo esc_html($family); ?></option>
                    <?php endforeach; ?>
                </select>
                <input type="hidden" name="testimo_nonce_title_font_family" value="<?php echo esc_attr(wp_create_nonce('testimo_title_font_family_nonce')); ?>" />
            <p class="zl_shortnoteforall"><?php esc_html_e('Set the font family of title', 'easy-testimonials-carousel'); ?></p><br>
            </div>

            <div class="zl_testimo_content_font_family-style">
                <label for="content_font_family"><?php esc_html_e('Font Family (Content):', 'easy-testimonials-carousel'); ?></label>
                <select name="testimo_content_font_family" id="zl_content_font_family">
                    <?php foreach ($zl_font_families as $family) : ?>
                        <option value="<?php echo esc_attr($family); ?>" <?php selected($select_contentfont_family, $family); ?>><?php echo esc_html($family); ?></option>
                    <?php endforeach; ?>
                </select>
                <input type="hidden" name="testimo_nonce_content_font_family" value="<?php echo esc_attr(wp_create_nonce('testimo_content_font_family_nonce')); ?>" />
                <p class="zl_shortnoteforall"><?php esc_html_e('Set the font family of content', 'easy-testimonials-carousel'); ?></p><br>
            </div>

            <div class="zl_testimo_designation_font_family-style">
                <label for="designation_font_family"><?php esc_html_e('Font Family (Designation):', 'easy-testimonials-carousel'); ?></label>
                <select name="testimo_designation_font_family" id="zl_designation_font_family">
                    <?php foreach ($zl_font_families as $family) : ?>
                        <option value="<?php echo esc_attr($family); ?>" <?php selected($select_designationfont_family, $family); ?>><?php echo esc_html($family);?>  
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <input type="hidden" name="testimo_nonce_designation_font_family" value="<?php echo esc_attr(wp_create_nonce('testimo_designation_font_family_nonce')); ?>" />
            <p class="zl_shortnoteforall"><?php esc_html_e('Set the font family of designation', 'easy-testimonials-carousel'); ?></p><br><br><hr>

            <div class="zl_testimo_tile_font_size-style">
                <label for="title_font_size"><?php esc_html_e('Font Size (Title):', 'easy-testimonials-carousel'); ?></label>
                <?php wp_nonce_field('zltestimonialfontsizelimit_nonce', 'testimonial_nonce_fontsizelimit'); ?>
                <input type="text" name="testimonial_fontsize_limit" id="zl_testimonial_fontsize_limit" value="<?php echo esc_attr($select_titlefont_size); ?>" maxlength="2" /><br>
                <p class="zl_shortnoteforall"><?php esc_html_e('Set the font size of title within pixel i.e 15', 'easy-testimonials-carousel'); ?></p><br>
            </div>
            <div class="zl_testimo_comtent_font_size-style">
                <label for="content_font_size"><?php esc_html_e('Font Size (Content):', 'easy-testimonials-carousel'); ?></label>
                <?php wp_nonce_field('zltestimonial_content_fontsizelimit_nonce', 'testimonial_nonce_content_fontsizelimit'); ?>
                <input type="text" name="testimonial_content_fontsize_limit" id="zl_testimonial_content_fontsize_limit" value="<?php echo esc_attr($select_contentfont_size); ?>" maxlength="2" /><br>
                <p class="zl_shortnoteforall"><?php esc_html_e('Set the font size of content within pixel i.e 15', 'easy-testimonials-carousel'); ?></p><br>
            </div>

            <div class="testimo_designation_font_size-style">
                <label for="content_font_size"><?php esc_html_e('Font Size (Designation):', 'easy-testimonials-carousel'); ?></label>
                <?php wp_nonce_field('zltestimonial_designation_fontsizelimit_nonce', 'testimonial_nonce_designation_fontsizelimit'); ?>
                <input type="text" name="testimonial_designation_fontsize_limit" id="zl_testimonial_designation_fontsize_limit" value="<?php echo esc_attr($select_designationfont_size); ?>" maxlength="2"/><br>
                <p class="zl_shortnoteforall"><?php esc_html_e('Set the font size of designation within pixel i.e 15', 'easy-testimonials-carousel'); ?></p><br>
            </div>

            <div class="testimo-text-align">
                <label for="testimo_text_align"><?php esc_html_e('Text Align:', 'easy-testimonials-carousel'); ?></label>
                <select id="zl_testimo_text_align" name="testimo_text_align">
                    <option value="center" <?php echo selected($selected_text_align,'center'); ?>><?php esc_html_e('Center', 'easy-testimonials-carousel'); ?></option>
                    <option value="left"   <?php echo selected($selected_text_align,'left'); ?>><?php esc_html_e('Left', 'easy-testimonials-carousel'); ?></option>
                    <option value="right" <?php echo selected($selected_text_align,'right'); ?>><?php esc_html_e('Right', 'easy-testimonials-carousel'); ?></option>
                </select>
                <?php $textalign_nonce_action = 'testimoo_textalign_nonce_action'; ?>
                <input type="hidden" name="testimoo_textalign_nonce" value="<?php echo esc_attr(wp_create_nonce($textalign_nonce_action)); ?>" />
                <p class="zl_shortnoteforall zl_text_align_shortnote"><?php esc_html_e('Select the text align for text', 'easy-testimonials-carousel'); ?></p><br>
            </div><hr>

            <div class="testimo-image-style">
                <label for="testimoo_pic"><?php esc_html_e('Image Style:', 'easy-testimonials-carousel'); ?></label>
                <select id="zl_testimoo_pic" name="testimoo_pic">
                    <option value="50" <?php echo selected($selected_feature_imagestyle,'50'); ?>><?php esc_html_e('Round Image', 'easy-testimonials-carousel'); ?></option>
                    <option value="0"   <?php echo selected($selected_feature_imagestyle,'0'); ?>><?php esc_html_e('Square Image', 'easy-testimonials-carousel'); ?></option>
                </select>
                <?php $pic_nonce_action = 'testimoo_pic_nonce_action'; ?>
                <input type="hidden" name="testimoo_pic_nonce" value="<?php echo esc_attr(wp_create_nonce($pic_nonce_action)); ?>" />
                <p class="zl_shortnoteforall"><?php esc_html_e('Select the image style', 'easy-testimonials-carousel'); ?></p><br>
            </div>
        </div>
    
        <div id="zl-tab-color-picker">
            <div class="testimonial_title_color">
                <label for="testimonial_title_color"><?php esc_html_e('Change Title Color:', 'easy-testimonials-carousel'); ?></label>
                <input type="text" name="testimonial_title_color" id="testimonial_title_color"
                value="<?php echo esc_attr($selected_title_color); ?>" class="zl-my-color-field testimonial_title_color" data-default-color="#1e73be"/><br>
                <?php $title_color_nonce_action = 'testimonial_title_color_nonce_action'; ?>
                <input type="hidden" name="testimonial_title_color_nonce" value="<?php echo esc_attr(wp_create_nonce($title_color_nonce_action)); ?>" />
            </div>

            <div class="testimonial_content_color">
                <label for="testimonial_content_color"><?php esc_html_e('Change  Content Text Color:', 'easy-testimonials-carousel'); ?></label>
                <input type="text" name="testimonial_text_color" id="testimonial_content_color"
                value="<?php echo esc_attr($selected_content_color); ?>" class="zl-my-color-field" data-default-color="#7c7f85"/><br>
                <?php $text_color_nonce_action = 'testimonial_text_color_nonce_action'; ?>
                <input type="hidden" name="testimonial_text_color_nonce" value="<?php echo esc_attr(wp_create_nonce($text_color_nonce_action)); ?>" />
            </div>

            <div class="testimonial_bg_color">
                <label for="testimonial_bg_color"><?php esc_html_e('Change  Background Color:', 'easy-testimonials-carousel'); ?></label>
                <input type="text" name="testimonial_bg_color" id="testimonial_bg_color"
                value="<?php echo esc_attr($selected_bg_color); ?>" class="zl-my-color-field" data-default-color="#d0def7"/><br>
                <?php $bg_color_nonce_action = 'testimonial_bg_color_nonce_action'; ?>
                <input type="hidden" name="testimonial_bg_color_nonce" value="<?php echo esc_attr(wp_create_nonce($bg_color_nonce_action)); ?>" />
            </div>

            <div class="testimo_designa_color">
                <label for="testimo_designa_color"><?php esc_html_e('Change  Designation Color:', 'easy-testimonials-carousel'); ?></label>
                <input type="text" name="testimo_designa_color" id="testimo_designa_color"
                value="<?php echo esc_attr($selected_designation_color); ?>" class="zl-my-color-field" data-default-color="#37393c"/><br>
                <?php $designa_color_nonce_action = 'testimo_designa_color_nonce_action'; ?>
                <input type="hidden" name="testimo_designa_color_nonce" value="<?php echo esc_attr(wp_create_nonce($designa_color_nonce_action)); ?>" />
            </div>

            <div class="testimo_ratingicon_color">
                <label for="testimo_ratingicon_color"><?php esc_html_e('Change  Rating icon Color:', 'easy-testimonials-carousel'); ?></label>
                <input type="text" name="testimo_ratingicon_color" id="testimo_ratingicon_color"
                value="<?php echo esc_attr($testimo_ratingicon_color); ?>" class="zl-my-color-field" data-default-color="#e5902d"/>
                <?php $ratingicon_nonce_action = 'testimo_ratingicon_color_nonce_action'; ?>
                <input type="hidden" name="testimo_ratingicon_color_nonce" value="<?php echo esc_attr(wp_create_nonce($ratingicon_nonce_action)); ?>" />
            </div>
        </div>            
        <div id="zl-tab-category">
            <label for="testimonial_category"><?php esc_html_e('Select Category:', 'easy-testimonials-carousel'); ?></label>
            <select name="testimonial_category" id="testimonial_category">
                <option value=""><?php esc_html_e('All', 'easy-testimonials-carousel'); ?></option>
                <?php $categories = get_terms(array('taxonomy' => 'testimonial-category', 'hide_empty' => false));
                foreach ($categories as $category) {
                    $selected = ($selected_category == $category->term_id) ? 'selected' : '';
                    echo '<option value="' . esc_attr($category->term_id) . '" ' . esc_attr($selected) . '>' . esc_html($category->name) . '</option>';
                } ?>
            </select>
            <?php $category_nonce_action = 'testimonial_category_nonce_action'; ?>
    		<input type="hidden" name="testimonial_category_nonce" value="<?php echo esc_attr(wp_create_nonce($category_nonce_action)); ?>" />
            <p class="zl_shortnoteforall-category"><?php esc_html_e('Display posts by testimonial category wise', 'easy-testimonials-carousel'); ?></p>
        </div>

        <div id="zl-tab-post-design">
            <div class="zl-image-select-container">
                <p class="zl_shortnoteforall-designimage"><?php esc_html_e('Select a design layout to display the testimonials', 'easy-testimonials-carousel'); ?></p>
                <?php
                $first_option_selected = empty($selected_design) ? 'selected' : ''; // Check if none is selected
                $first_option = true;
                foreach ($design_options as $value => $label_info) : ?>
                    <?php
                    $label = $label_info['label'];
                    $selected = ($selected_design === $value || ($first_option && empty($selected_design))) ? 'selected' : '';
        
                    // Unset the first_option flag after the first option is processed
                    if ($first_option) {
                        $first_option = false;
                    } ?>
                    <label class="zl-image-select-label <?php echo $selected ? 'selected' : ''; ?>">
                        <input type="radio" name="testimonial_design" value="<?php echo esc_attr($value); ?>"
                            <?php echo esc_attr($selected); ?> style="display: none;" />
                        <?php echo esc_attr($label_info['label']); ?>
                    </label>
                <?php endforeach; ?>
                <?php $post_design_nonce_action = 'testimonial_design_nonce_action'; ?>
                <input type="hidden" name="testimonial_design_nonce" value="<?php echo esc_attr(wp_create_nonce($post_design_nonce_action)); ?>" />
            </div>
        
            <div id="zl_selected_design_image">
                <?php // Display the selected design image
                $selected_design_image_url = '';
                if (isset($design_options[$selected_design]['image'])) {
                    $selected_design_image_url = sanitize_text_field($design_options[$selected_design]['image']);
                }
                if (empty($selected_design_image_url)) {
                    // Use a static image URL if $selected_design_image_url is empty
                    $static_image_url = plugin_dir_url(__FILE__) . 'assets/Images/slickslideone.png';
                    echo '<img src="' . esc_url($static_image_url) . '" alt="Design 1" height="auto" width="700px"/>';
                } else {
                    // Display the selected design image if available
                    echo '<img src="' . esc_url($selected_design_image_url) . '" alt="'.esc_attr($label_info['label']).'" height="auto" width="700px"/>';
                } ?>
            </div>
        </div>

        <div id="zl-tab-slick-slides">
            <div class="zl-slickslide-delay">  
                <label for="range-field"><?php esc_html_e('Slide Delay:', 'easy-testimonials-carousel'); ?></label>
                <input type="range" id="zl-range-field" name="sliderange_field" value="<?php echo esc_attr($slidedelayvalue); ?>" min="100" max="5000" step="100" style="width:200px;" /><br>
                <input type="text" id="zl-range-value" name="sliderange_value" value="<?php echo esc_attr($slidedelayvalue); ?>" style="width: 69px;" readonly /><br>
                <?php $post_sliderange_nonce_action = 'sliderange_nonce_action'; ?>
                <input type="hidden" name="sliderange_nonce" value="<?php echo esc_attr(wp_create_nonce($post_sliderange_nonce_action)); ?>" />
                <p class="zl_shortnoteforall"><?php esc_html_e('Select a value for sliding speed.', 'easy-testimonials-carousel'); ?></p><br>
            </div> 

            <div class="zl-testimo-responsive">
                <div class="zl-res-fieldset">
                    <div class="res_title"><label for="testimo_desktop_col"><?php esc_html_e('Responsive Column:', 'easy-testimonials-carousel'); ?></label></div>
                        <div class="zl_column_desktop">
                        <span class="dashicons dashicons-desktop"></span>
                        <input type="number" name="testimonial_desktop_meta" id="testimonial_desktop_meta" class="zl_res_input" value="<?php echo esc_attr(empty($selected_desktop_col) ? 1 : $selected_desktop_col); ?>" min="-1" max="50" />
                        <?php $res_desktop_meta_nonce_action = 'testimonial_desktop_meta_nonce_action'; ?>
                        <input type="hidden" name="testimonial_desktop_meta_nonce" value="<?php echo esc_attr(wp_create_nonce($res_desktop_meta_nonce_action)); ?>" />

                        <span class="dashicons dashicons-laptop"></span>
                        <input type="number" name="testimonial_laptop_meta" id="testimonial_laptop_meta" class="zl_res_input" value="<?php echo esc_attr(empty($selected_laptop_col) ? 1 : $selected_laptop_col); ?>" min="-1" max="50" />
                        <?php $res_laptop_meta_nonce_action = 'testimonial_laptop_meta_nonce_action'; ?>
                        <input type="hidden" name="testimonial_laptop_meta_nonce" value="<?php echo esc_attr(wp_create_nonce($res_laptop_meta_nonce_action)); ?>" />

                        <span class="dashicons dashicons-tablet"></span>
                        <input type="number" name="testimonial_tablet_meta" id="testimonial_tablet_meta" class="zl_res_input" value="<?php echo esc_attr(empty($selected_tablet_col) ? 1 : $selected_tablet_col); ?>" min="-1" max="50" />
                        <?php $res_tablet_meta_nonce_action = 'testimonial_tablet_meta_nonce_action'; ?>
                        <input type="hidden" name="testimonial_tablet_meta_nonce" class="zl_res_input" value="<?php echo esc_attr(wp_create_nonce($res_tablet_meta_nonce_action)); ?>" />
                    </div>
                </div>
            </div><br>

            <!-- Add the toggle field inside the same div -->
            <div class="zl-toggle-container">
                <label for="toggle_field"><?php esc_html_e('Slide Autoplay:', 'easy-testimonials-carousel'); ?></label>
                <label class="zl_switch zl_autoplayswitch">
                    <input type="checkbox" name="autoplaytoggle_field" id="toggle_field" <?php echo esc_attr($checked_autoplay_attribute); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_autoplaytoggle_nonce_action = 'autoplaytoggle_nonce_action'; ?>
                <input type="hidden" name="autoplaytoggle_nonce" value="<?php echo esc_attr(wp_create_nonce($post_autoplaytoggle_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Select an option whether you want the slider autoplay or not.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="toggle_field"><?php esc_html_e('Navigation:', 'easy-testimonials-carousel'); ?></label>
                <label class="zl_switch zl_arrowswitch">
                    <input type="checkbox" name="slickslidesarrow" id="toggle_field_arrow" <?php echo esc_attr($checked_arrow_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_slickslidesarrow_nonce_action = 'slickslidesarrow_nonce_action'; ?>
                <input type="hidden" name="slickslidesarrow_nonce" value="<?php echo esc_attr(wp_create_nonce($post_slickslidesarrow_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Select an option whether you want navigation option or not.', 'easy-testimonials-carousel'); ?></p><br>

            <!-- Navigation Style -->
            <div class="zl-toggle-container">
                <label for="toggle_field"><?php esc_html_e('Navigation Style:', 'easy-testimonials-carousel'); ?></label>
                <label class="zl_arrowstyle">
                    <select id="zl_testimoo_arrowstyle" name="testimoo_arrowstyle">
                        <option value="outer-slider-container" <?php echo selected($selected_arrowstyle,'outer-slider-container'); ?>><?php esc_html_e('Outer Slider Container', 'easy-testimonials-carousel'); ?></option>
                        <option value="inner-slider-container" <?php echo selected($selected_arrowstyle,'inner-slider-container'); ?>><?php esc_html_e('Inner Slider Container', 'easy-testimonials-carousel'); ?></option>
                        <option value="below-slider"   <?php echo selected($selected_arrowstyle,'below-slider'); ?>><?php esc_html_e('Below Slider', 'easy-testimonials-carousel'); ?></option>
                    </select>
                    <?php $arrowstyle_nonce_action = 'testimoo_arrowstyle_nonce_action'; ?>
                    <input type="hidden" name="testimoo_arrowstyle_nonce" value="<?php echo esc_attr(wp_create_nonce($arrowstyle_nonce_action)); ?>" />
                </label>   
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Select an option for navigation style.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="toggle_field"><?php esc_html_e('Pagination:', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch zl_slidedotswitch">
                    <input type="checkbox" name="slickslidesdots" id="toggle_field_dots" <?php echo esc_attr($checked_dots_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_slickslidesdots_nonce_action = 'slickslidesdots_nonce_action'; ?>
                <input type="hidden" name="slickslidesdots_nonce" value="<?php echo esc_attr(wp_create_nonce($post_slickslidesdots_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Select an option whether you want pagination option or not.', 'easy-testimonials-carousel'); ?></p><br>
            
            <div class="zl-toggle-container">
                <label for="toggle_field"><?php esc_html_e('Loop :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch zl_slideloopswitch">
                    <input type="checkbox" name="slickslidesloop" id="toggle_field_loop" <?php echo esc_attr($checked_loop_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_slickslidesloop_nonce_action = 'slickslidesloop_nonce_action'; ?>
                <input type="hidden" name="slickslidesloop_nonce" value="<?php echo esc_attr(wp_create_nonce($post_slickslidesloop_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Select an option whether you want to loop the sliders.', 'easy-testimonials-carousel'); ?></p><br>

            <!-- slickslideshover -->
            <div class="zl-toggle-container">
                <label for="toggle_field"><?php esc_html_e('Slide Stop On Hover :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch zl_slidehoverswitch">
                    <input type="checkbox" name="slickslideshover" id="toggle_field_hover" <?php echo esc_attr($checked_hover_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_slickslideshover_nonce_action = 'slickslideshover_nonce_action'; ?>
                <input type="hidden" name="slickslideshover_nonce" value="<?php echo esc_attr(wp_create_nonce($post_slickslideshover_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Select an option whether you want to pause sliding on mouse hover.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="display_drag_mouse_touch"><?php esc_html_e('Mouse / Touch Drag :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch zl_slider_drag_mt">
                    <input type="checkbox" name="slickslides_drag_mt" id="toggle_field_drag_mt" <?php echo esc_attr($checked_drag_mt_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_slickslidesdrag_mt_nonce_action = 'slickslidesdrag_mt_nonce_action'; ?>
                <input type="hidden" name="slickslidesdrag_mt_nonce" value="<?php echo esc_attr(wp_create_nonce($post_slickslidesdrag_mt_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Its allows slide drag and drop.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="display_lazy_load_touch"><?php esc_html_e('Lazy Load :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch zl_slider_lazy_load">
                    <input type="checkbox" name="slickslides_lazy_load" id="toggle_field_lazy_load" <?php echo esc_attr($checked_lazy_load_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_slickslideslazy_load_nonce_action = 'slickslideslazy_load_nonce_action'; ?>
                <input type="hidden" name="slickslideslazy_load_nonce" value="<?php echo esc_attr(wp_create_nonce($post_slickslideslazy_load_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Its allows slide images lazy load.', 'easy-testimonials-carousel'); ?></p><br>
        </div>

        <div id="zl-tab-slick-display">
            <div class="zl-toggle-container">
                <label for="display_name"><?php esc_html_e('Hide Name(Title) :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch displaynameswitch">
                    <input type="checkbox" name="toggle_display_name" id="display_name_switch" <?php echo esc_attr($checked_dname_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_display_name_nonce_action = 'displayname_nonce_action'; ?>
                <input type="hidden" name="displayname_nonce" value="<?php echo esc_attr(wp_create_nonce($post_display_name_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Hide Testimonial name in frontend side.', 'easy-testimonials-carousel'); ?></p><br>
            
            <div class="zl-toggle-container">
                <label for="display_picture"><?php esc_html_e('Hide Image :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch displaypictureswitch">
                    <input type="checkbox" name="toggle_display_picture" id="display_picture_switch" <?php echo esc_attr($checked_dpicture_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_display_picture_nonce_action = 'displaypicture_nonce_action'; ?>
                <input type="hidden" name="displaypicture_nonce" value="<?php echo esc_attr(wp_create_nonce($post_display_picture_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Hide Testimonial Picture in frontend side.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="display_designation"><?php esc_html_e('Hide Designation :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch displaydesignationswitch">
                    <input type="checkbox" name="toggle_display_designation" id="display_designation_switch" <?php echo esc_attr($checked_ddesignation_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_display_designation_nonce_action = 'displaydesignation_nonce_action'; ?>
                <input type="hidden" name="displaydesignation_nonce" value="<?php echo esc_attr(wp_create_nonce($post_display_designation_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Hide Testimonial Designation in frontend side.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="display_rating"><?php esc_html_e('Hide Rating :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch displayratingswitch">
                    <input type="checkbox" name="toggle_display_rating" id="display_rating_switch" <?php echo esc_attr($checked_drating_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_display_rating_nonce_action = 'displayrating_nonce_action'; ?>
                <input type="hidden" name="displayrating_nonce" value="<?php echo esc_attr(wp_create_nonce($post_display_rating_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Hide Testimonial Rating in frontend side.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="display_content"><?php esc_html_e('Hide Content :', 'zl-easy-testimonials-carousel'); ?></label>
                <label class="zl_switch displaycontentswitch">
                    <input type="checkbox" name="toggle_display_content" id="display_content_switch" <?php echo esc_attr($checked_dcontent_attributes); ?>>
                    <span class="zl_slider"></span>
                </label>
                <?php $post_display_content_nonce_action = 'displaycontent_nonce_action'; ?>
                <input type="hidden" name="displaycontent_nonce" value="<?php echo esc_attr(wp_create_nonce($post_display_content_nonce_action)); ?>" />
            </div>
            <p class="zl_shortnoteforall"><?php esc_html_e('Hide Testimonial Content in frontend side.', 'easy-testimonials-carousel'); ?></p><br>

            <div class="zl-toggle-container">
                <label for="testimoo_layout"><?php esc_html_e('Layout:', 'easy-testimonials-carousel'); ?></label>
                <select id="zl_testimoo_layout" name="testimoo_layout">
                    <option value="slider"   <?php echo selected($selected_layout,'slider'); ?>><?php esc_html_e('Slider', 'easy-testimonials-carousel'); ?></option>
                    <option value="grid" <?php echo selected($selected_layout,'grid'); ?>><?php esc_html_e('Grid', 'easy-testimonials-carousel'); ?></option>
                </select>
                <?php $layout_nonce_action = 'testimoo_layout_nonce_action'; ?>
                <input type="hidden" name="testimoo_layout_nonce" value="<?php echo esc_attr(wp_create_nonce($layout_nonce_action)); ?>" />
                <div id="zl_grid_columns_select" style="display: <?php echo ($selected_layout == 'grid') ? 'block' : 'none'; ?>;">
                    <label for="testimoo_grid_columns"><?php esc_html_e('Grid Columns', 'easy-testimonials-carousel'); ?></label>
                    <select id="testimoo_grid_columns" name="testimoo_grid_columns">
                        <option value="1" <?php echo selected($selected_columns, '1'); ?>>1</option>
                        <option value="2" <?php echo selected($selected_columns, '2'); ?>>2</option>
                        <option value="3" <?php echo selected($selected_columns, '3'); ?>>3</option>
                        <option value="4" <?php echo selected($selected_columns, '4'); ?>>4</option>
                    </select>
                    <?php $grid_col_nonce_action = 'testimoo_grid_col_nonce_action'; ?>
                    <input type="hidden" name="testimoo_grid_col_nonce" value="<?php echo esc_attr(wp_create_nonce($grid_col_nonce_action)); ?>" />
                    <!-- Column Gap -->
                    <label for="testimoo_col_gap"><?php esc_html_e('Column Gap : ', 'easy-testimonials-carousel'); ?></label>  
                    <?php wp_nonce_field('zltestimonialcolumngap_nonce', 'testimonial_nonce_columngap'); ?>
                    <input type="number" name="testimonial_column_gap" id="zl_testimonial_column_gap" value="<?php echo esc_attr($select_column_gap); ?>" />
                    <!-- Grid Item Margin -->
                    <label for="testimoo_griditem_margin"><?php esc_html_e('Grid Item(Margin) : ', 'easy-testimonials-carousel'); ?></label> 
                    <?php wp_nonce_field('zltestimonialgriditem_margin_nonce', 'testimonial_nonce_griditemmargin'); ?>
                    <input type="number" name="testimonial_griditem_margin" id="testimonial_griditem_margin" value="<?php echo esc_attr($select_griditem_margin); ?>" /> 
                    <!-- Grid Item Padding -->
                    <label for="testimoo_griditem_padding"><?php esc_html_e('Grid Item(Padding) : ', 'easy-testimonials-carousel'); ?></label> 
                    <?php wp_nonce_field('zltestimonialgriditem_padding_nonce', 'testimonial_nonce_griditempadding'); ?>
                    <input type="number" name="testimonial_griditem_padding" id="testimonial_griditem_padding" value="<?php echo esc_attr($select_griditem_padding); ?>" /> 
                </div>
            </div> <br>
            <p class="zl_shortnoteforall"><?php esc_html_e('Select the Layout style from this option(i.e grid, slider)', 'easy-testimonials-carousel'); ?></p><br>
        </div>

        <div id="zl-tab-top-rated">
            <?php wp_nonce_field('testimonials_top_rated_nonce_action', 'testimonials_top_rated_nonce'); ?>
            <h4><?php esc_html_e('Top Rated Testimonials', 'easy-testimonials-carousel'); ?></h4>
            <select class="select2" id="zl_testimo_top_rated" name="testimonials_top_rated[]" multiple="multiple" style="width: 400px;">
                <?php
                $args = [
                    'post_type' => 'zl_testimonials',
                    'posts_per_page' => -1,
                    'post_status' => 'publish'
                ];

                if (!empty($selected_category)) {
                    $args['tax_query'] = [
                        [
                            'taxonomy' => 'testimonial-category',
                            'field' => 'term_id',
                            'terms' => $selected_category
                        ]
                    ];
                }
                $query = new WP_Query($args);
                if ($query->have_posts()) {
                    while ($query->have_posts()) {
                        $query->the_post();
                        $selected = in_array(get_the_ID(), (array)$selected_testimonials) ? 'selected' : '';
                        echo '<option value="' . esc_attr(get_the_ID()) . '" ' . esc_html($selected) . '>' . esc_html(get_the_title()) . '</option>';
                    }
                    wp_reset_postdata();
                } ?>
            </select> 
            <p class="shortnote_toprated"><?php esc_html_e('Top Rated options display based on category selection in "category" tab section. ', 'easy-testimonials-carousel'); ?></p><br>
        </div>
        <!-- Top Rated Ends Here -->
    </div>
    <?php echo '</div>';
}
// Callback function for "zl_shortc_setting"(Shortcode Post type) Function ends

// tab selected after post published, post updated or page refresh  
function zl_save_active_tab_callback() {
    check_ajax_referer('save_active_tab_nonce', 'security');
    
    // Check user capabilities
    if (!current_user_can('edit_posts')) {
        wp_die('Insufficient permissions.');
    }
    
    // if (isset($_POST['post_id']) && isset($_POST['active_tab'])) {
    $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
    $active_tab = isset($_POST['active_tab']) ? sanitize_text_field($_POST['active_tab']) : '';
    // Save the active tab as post meta
    update_post_meta($post_id, 'active_tab', $active_tab);
    wp_die();
}
add_action('wp_ajax_save_active_tab', 'zl_save_active_tab_callback');
add_action('wp_ajax_nopriv_save_active_tab', 'zl_save_active_tab_callback');

// For backend Color Picker fields starts
function zl_enqueue_color_picker() {
    // For backend Color Picker fields ends
    wp_enqueue_style('wp-color-picker');
    wp_enqueue_script('wp-color-picker'); 
}
add_action('admin_head', 'zl_enqueue_color_picker');

// for save Shortcode meta fields save_post hook starts
function zl_save_testimonial_design_post_meta($post_id) {
    // Check if this is the right post type
    if (get_post_type($post_id) !== 'zl_shortc_setting') {
        return;
    }
    
    // Check user capabilities
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    // Save the values as post meta
    $post_design_nonce_action = 'testimonial_design_nonce_action'; // Define nonce action for post design
    if (isset($_POST['testimonial_design_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_design_nonce'])), $post_design_nonce_action)) {
        if (isset($_POST['testimonial_design'])) {
            $selected_design = sanitize_text_field(wp_unslash($_POST['testimonial_design']));
            update_post_meta($post_id, 'testimonial_design', $selected_design);
        }
    }
    $post_limit_nonce_action = 'testimonial_post_limit_action'; // Define nonce action for post limit
    if (isset($_POST['testimonial_post_limit_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_post_limit_nonce'])), $post_limit_nonce_action)) {
        if (isset($_POST['testimonial_post_limit'])) {
            $selected_post_limit = intval($_POST['testimonial_post_limit']);
            update_post_meta($post_id, 'testimonial_post_limit', $selected_post_limit);
        }
    }

    // Responsive Column
    $res_desktop_meta_nonce_action = 'testimonial_desktop_meta_nonce_action'; // Define nonce action for Responsive desktop
    if (isset($_POST['testimonial_desktop_meta_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_desktop_meta_nonce'])), $res_desktop_meta_nonce_action)) {
        if (isset($_POST['testimonial_desktop_meta'])) {
            $selected_desktop_col = intval($_POST['testimonial_desktop_meta']);
            update_post_meta($post_id, 'testimonial_desktop_meta', $selected_desktop_col);
        }
    }
    $res_laptop_meta_nonce_action = 'testimonial_laptop_meta_nonce_action'; // Define nonce action for Responsive Laptop
    if (isset($_POST['testimonial_laptop_meta_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_laptop_meta_nonce'])), $res_laptop_meta_nonce_action)) {
        if (isset($_POST['testimonial_laptop_meta'])) {
            $selected_laptop_col = intval($_POST['testimonial_laptop_meta']);
            update_post_meta($post_id, 'testimonial_laptop_meta', $selected_laptop_col);
        }
    }
    $res_tablet_meta_nonce_action = 'testimonial_tablet_meta_nonce_action'; // Define nonce action for Responsive Laptop
    if (isset($_POST['testimonial_tablet_meta_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_tablet_meta_nonce'])), $res_tablet_meta_nonce_action)) {
    if (isset($_POST['testimonial_tablet_meta'])) {
            $selected_tablet_col_res = intval($_POST['testimonial_tablet_meta']);
            update_post_meta($post_id, 'testimonial_tablet_meta', $selected_tablet_col_res);
        }
    }

    $category_nonce_action = 'testimonial_category_nonce_action';
	if (isset($_POST['testimonial_category_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_category_nonce'])), $category_nonce_action)) {
	    if (isset($_POST['testimonial_category'])) {
	        $selected_category = isset($_POST['testimonial_category']) ? sanitize_text_field($_POST['testimonial_category']) : '';
	        update_post_meta($post_id, 'testimonial_category', $selected_category);
	    }
	}

    if (!isset($_POST['testimonials_top_rated_nonce']) || !wp_verify_nonce($_POST['testimonials_top_rated_nonce'], 'testimonials_top_rated_nonce_action')) {
        return;
    }
    // Check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // Check permissions
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    // Sanitize and save the data
    if (isset($_POST['testimonials_top_rated'])) {
        $testimonials_top_rated = array_map('sanitize_text_field', $_POST['testimonials_top_rated']);
        update_post_meta($post_id, 'testimonials_top_rated', $testimonials_top_rated);
    } else {
        delete_post_meta($post_id, 'testimonials_top_rated');
    }

    // Save the color picker value
    $bg_color_nonce_action = 'testimonial_bg_color_nonce_action';
    if (isset($_POST['testimonial_bg_color_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_bg_color_nonce'])), $bg_color_nonce_action)) {
        if (isset($_POST['testimonial_bg_color'])) {
            $selected_color = sanitize_hex_color($_POST['testimonial_bg_color']);
            update_post_meta($post_id, 'testimonial_bg_color', $selected_color);
        }
    }
    $text_color_nonce_action = 'testimonial_text_color_nonce_action';
    if (isset($_POST['testimonial_text_color_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_text_color_nonce'])), $text_color_nonce_action)) {
        if (isset($_POST['testimonial_text_color'])) {
            $selected_text_color = sanitize_hex_color($_POST['testimonial_text_color']);
            update_post_meta($post_id, 'testimonial_text_color', $selected_text_color);
        }
    }
    $title_color_nonce_action = 'testimonial_title_color_nonce_action';
    if (isset($_POST['testimonial_title_color_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimonial_title_color_nonce'])), $title_color_nonce_action)) {
        if (isset($_POST['testimonial_title_color'])) {
            $selected_title_color = sanitize_hex_color($_POST['testimonial_title_color']);
            update_post_meta($post_id, 'testimonial_title_color', $selected_title_color);
        }
    }
    $designa_color_nonce_action = 'testimo_designa_color_nonce_action';
    if (isset($_POST['testimo_designa_color_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimo_designa_color_nonce'])), $designa_color_nonce_action)) {
        if (isset($_POST['testimo_designa_color'])) {
            $selected_designa_color = sanitize_hex_color($_POST['testimo_designa_color']);
            update_post_meta($post_id, 'testimo_designa_color', $selected_designa_color);
        }
    }
    $ratingicon_nonce_action = 'testimo_ratingicon_color_nonce_action';
    if (isset($_POST['testimo_ratingicon_color_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimo_ratingicon_color_nonce'])), $ratingicon_nonce_action)) {
        if (isset($_POST['testimo_ratingicon_color'])) {
            $selected_ratingicon_color = sanitize_hex_color($_POST['testimo_ratingicon_color']);
            update_post_meta($post_id, 'testimo_ratingicon_color', $selected_ratingicon_color);
        }
    }
    $pic_nonce_action = 'testimoo_pic_nonce_action';
    if (isset($_POST['testimoo_pic_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimoo_pic_nonce'])), $pic_nonce_action)) {
        if (isset($_POST['testimoo_pic'])) {
            $selected_pic = sanitize_text_field(wp_unslash($_POST['testimoo_pic']));
            update_post_meta($post_id, 'testimoo_pic', $selected_pic);
        }
    }
    $layout_nonce_action = 'testimoo_layout_nonce_action';
    if (isset($_POST['testimoo_layout_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimoo_layout_nonce'])), $layout_nonce_action)) {
        if (isset($_POST['testimoo_layout'])) {
            $selected_layout = sanitize_text_field(wp_unslash($_POST['testimoo_layout']));
            update_post_meta($post_id, 'testimoo_layout', $selected_layout);
        }
    }
    $arrowstyle_nonce_action = 'testimoo_arrowstyle_nonce_action';
    if (isset($_POST['testimoo_arrowstyle_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimoo_arrowstyle_nonce'])), $arrowstyle_nonce_action)) {
        if (isset($_POST['testimoo_arrowstyle'])) {
            $selected_arrowstyle = sanitize_text_field(wp_unslash($_POST['testimoo_arrowstyle']));
            update_post_meta($post_id, 'testimoo_arrowstyle', $selected_arrowstyle);
        }
    }

    if (!isset($_POST['testimoo_grid_col_nonce']) || !wp_verify_nonce($_POST['testimoo_grid_col_nonce'], 'testimoo_grid_col_nonce_action')) {
        return;
    }
    if (isset($_POST['testimoo_grid_columns'])) {
        update_post_meta($post_id, 'testimoo_grid_columns', sanitize_text_field($_POST['testimoo_grid_columns']));
    }
    

    $textalign_nonce_action = 'testimoo_textalign_nonce_action';
    if (isset($_POST['testimoo_textalign_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['testimoo_textalign_nonce'])), $textalign_nonce_action)) {
        if (isset($_POST['testimo_text_align'])) {
            $selected_text_align = sanitize_text_field(wp_unslash($_POST['testimo_text_align']));
            update_post_meta($post_id, 'testimo_text_align', $selected_text_align);
        }
    }
    // Check if the slider autoplay field is set and save it accordingly
    // if (isset($_POST['autoplaytoggle_field'])) {
    //     $autoplay_toggle = 'true';
    // } else {
    //     $autoplay_toggle = 'false';
    // }
    // update_post_meta($post_id, 'autoplaytoggle_field', $autoplay_toggle);
    $post_autoplaytoggle_nonce_action = 'autoplaytoggle_nonce_action'; // Define nonce action for autoplay toggle field
    if (isset($_POST['autoplaytoggle_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['autoplaytoggle_nonce'])), $post_autoplaytoggle_nonce_action)) {
        if (isset($_POST['autoplaytoggle_field'])) {
            $autoplay_toggle = 'true';
        } else {
            $autoplay_toggle = 'false';
        }
        update_post_meta($post_id, 'autoplaytoggle_field', $autoplay_toggle);
    }

    // Check if the slider arrow field is set and save it accordingly
    $post_slickslidesarrow_nonce_action = 'slickslidesarrow_nonce_action'; // Define nonce action for slick slides arrow field
    if (isset($_POST['slickslidesarrow_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['slickslidesarrow_nonce'])), $post_slickslidesarrow_nonce_action)) {
        if (isset($_POST['slickslidesarrow'])) {
            $arrow_toggle = 'true';
        } else {
            $arrow_toggle = 'false';
        }
        update_post_meta($post_id, 'slickslidesarrow', $arrow_toggle);
    }

    // Check if the slider dots field is set and save it accordingly
    $post_slickslidesdots_nonce_action = 'slickslidesdots_nonce_action'; // Define nonce action for slick slides dots field
    if (isset($_POST['slickslidesdots_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['slickslidesdots_nonce'])), $post_slickslidesdots_nonce_action)) {
        if (isset($_POST['slickslidesdots'])) {
            $dots_toggle = 'true';
        } else {
            $dots_toggle = 'false';
        }
        update_post_meta($post_id, 'slickslidesdots', $dots_toggle);
    }

    $post_slickslidesloop_nonce_action = 'slickslidesloop_nonce_action';
    if (isset($_POST['slickslidesloop_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['slickslidesloop_nonce'])), $post_slickslidesloop_nonce_action)) {
        if (isset($_POST['slickslidesloop'])) {
            $loop_toggle = 'true';
        }else {
            $loop_toggle = 'false';
        }
        update_post_meta($post_id, 'slickslidesloop', $loop_toggle);
    }

    $post_slickslideshover_nonce_action = 'slickslideshover_nonce_action';
    if (isset($_POST['slickslideshover_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['slickslideshover_nonce'])), $post_slickslideshover_nonce_action)) {
        if (isset($_POST['slickslideshover'])) {
            $hover_toggle = 'true';
        }else{
            $hover_toggle = 'false';
        }
        update_post_meta($post_id, 'slickslideshover', $hover_toggle);
    }

    $post_slickslides_drag_mt_nonce_action = 'slickslidesdrag_mt_nonce_action';
    if (isset($_POST['slickslidesdrag_mt_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['slickslidesdrag_mt_nonce'])), $post_slickslides_drag_mt_nonce_action)) {
        if (isset($_POST['slickslides_drag_mt'])) {
            $drag_mt_toggle = 'true';
        }else{
            $drag_mt_toggle = 'false';
        }
        update_post_meta($post_id, 'slickslides_drag_mt', $drag_mt_toggle);
    }
    $post_slickslides_lazy_load_nonce_action = 'slickslideslazy_load_nonce_action';
    if (isset($_POST['slickslideslazy_load_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['slickslideslazy_load_nonce'])), $post_slickslides_lazy_load_nonce_action)) {
        if (isset($_POST['slickslides_lazy_load'])) {
            $lazy_load_toggle = 'true';
        }else{
            $lazy_load_toggle = 'false';
        }
        update_post_meta($post_id, 'slickslides_lazy_load', $lazy_load_toggle);
    }

    $post_display_name_nonce_action = 'displayname_nonce_action';
    if (isset($_POST['displayname_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['displayname_nonce'])), $post_display_name_nonce_action)) {
        if (isset($_POST['toggle_display_name'])) {
            $dname_toggle = 'true';
        }else{
            $dname_toggle = 'false';
        }
        update_post_meta($post_id, 'toggle_display_name', $dname_toggle);
    }
    $post_display_picture_nonce_action = 'displaypicture_nonce_action';
    if (isset($_POST['displaypicture_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['displaypicture_nonce'])), $post_display_picture_nonce_action)) {
        if (isset($_POST['toggle_display_picture'])) {
            $dpicture_toggle = 'true';
        }else{
            $dpicture_toggle = 'false';
        }
        update_post_meta($post_id, 'toggle_display_picture', $dpicture_toggle);
    }
    $post_display_designation_nonce_action = 'displaydesignation_nonce_action';
    if (isset($_POST['displaydesignation_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['displaydesignation_nonce'])), $post_display_designation_nonce_action)) {
        if (isset($_POST['toggle_display_designation'])) {
            $d_designation_toggle = 'true';
        }else{
            $d_designation_toggle = 'false';
        }
        update_post_meta($post_id, 'toggle_display_designation', $d_designation_toggle);
    }

    $post_display_rating_nonce_action = 'displayrating_nonce_action';
    if (isset($_POST['displayrating_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['displayrating_nonce'])), $post_display_rating_nonce_action)) {
        if (isset($_POST['toggle_display_rating'])) {
            $drating_toggle = 'true';
        }else{
            $drating_toggle = 'false';
        }
        update_post_meta($post_id, 'toggle_display_rating', $drating_toggle);
    }
    $post_display_content_nonce_action = 'displaycontent_nonce_action';
    if (isset($_POST['displaycontent_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['displaycontent_nonce'])), $post_display_content_nonce_action)) {
        if (isset($_POST['toggle_display_content'])) {
            $dcontent_toggle = 'true';
        }else{
            $dcontent_toggle = 'false';
        }
        update_post_meta($post_id, 'toggle_display_content', $dcontent_toggle);
    }

    // Check if the slide delay field is set and save it accordingly
    $post_sliderange_nonce_action = 'sliderange_nonce_action'; // Define nonce action for range field
    if (isset($_POST['sliderange_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['sliderange_nonce'])), $post_sliderange_nonce_action)) {
        if (isset($_POST['sliderange_field'])) {
            $value = sanitize_text_field(wp_unslash($_POST['sliderange_field']));
            update_post_meta($post_id, 'sliderange_field', $value);
        }
    }
    // save sort post order meta
    if (isset($_POST['testimopost_orderoption']) && isset($_POST['zlorderoption_nonce'])) {
        // $nonce = sanitize_text_field($_POST['zlorderoption_nonce']);
       $orderoption_nonce = isset( $_POST['zlorderoption_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['zlorderoption_nonce'] ) ) : '';
       if (wp_verify_nonce($orderoption_nonce, 'zlorderoption_nonce_action')) {
           update_post_meta($post_id, 'testimopost_orderoption', sanitize_text_field($_POST['testimopost_orderoption']));
       } else {
           // Nonce verification failed, handle the error or reject the form submission
           echo 'Nonce verification failed. Form submission rejected.';
       }
   }
    // save title font family as post meta
    if (isset($_POST['testimo_title_font_family']) && isset($_POST['testimo_nonce_title_font_family'])) {
        $title_font_family_nonce = isset($_POST['testimo_nonce_title_font_family']) ? sanitize_text_field(wp_unslash($_POST['testimo_nonce_title_font_family'])) : '';
        if (!wp_verify_nonce($title_font_family_nonce, 'testimo_title_font_family_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimo_title_font_family', sanitize_text_field($_POST['testimo_title_font_family']));
        }
    }
    // save content font family as post meta
    if (isset($_POST['testimo_content_font_family']) && isset($_POST['testimo_nonce_content_font_family'])) {
        $content_font_family_nonce = isset($_POST['testimo_nonce_content_font_family']) ? sanitize_text_field(wp_unslash($_POST['testimo_nonce_content_font_family'])) : '';
        if (!wp_verify_nonce($content_font_family_nonce, 'testimo_content_font_family_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimo_content_font_family', sanitize_text_field($_POST['testimo_content_font_family']));
        }
    }
    // save title font style as post meta
    if (isset($_POST['testimo_title_font_style']) && isset($_POST['testimo_nonce_title_font_style'])) {
        $title_font_style_nonce = isset($_POST['testimo_nonce_title_font_style']) ? sanitize_text_field(wp_unslash($_POST['testimo_nonce_title_font_style'])) : '';
        if (!wp_verify_nonce($title_font_style_nonce, 'testimo_title_font_style_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimo_title_font_style', sanitize_text_field($_POST['testimo_title_font_style']));
        }
    }
    // save content font style as post meta
    if (isset($_POST['testimo_content_font_style']) && isset($_POST['testimo_nonce_content_font_style'])) {
        $content_font_style_nonce = isset($_POST['testimo_nonce_content_font_style']) ? sanitize_text_field(wp_unslash($_POST['testimo_nonce_content_font_style'])) : '';
        if (!wp_verify_nonce($content_font_style_nonce, 'testimo_content_font_style_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimo_content_font_style', sanitize_text_field($_POST['testimo_content_font_style']));
        }
    }
    // save designation font style as post meta
    if (isset($_POST['testimo_designation_font_style']) && isset($_POST['testimo_nonce_designation_font_style'])) {
        $designation_font_style_nonce = isset($_POST['testimo_nonce_designation_font_style']) ? sanitize_text_field(wp_unslash($_POST['testimo_nonce_designation_font_style'])) : '';
        if (!wp_verify_nonce($designation_font_style_nonce, 'testimo_designation_font_style_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimo_designation_font_style', sanitize_text_field($_POST['testimo_designation_font_style']));
        }
    }
    // save designation font family as post meta
    if (isset($_POST['testimo_designation_font_family']) && isset($_POST['testimo_nonce_designation_font_family'])) {
        $designation_font_family_nonce = isset($_POST['testimo_nonce_designation_font_family']) ? sanitize_text_field(wp_unslash($_POST['testimo_nonce_designation_font_family'])) : '';
        if (!wp_verify_nonce($designation_font_family_nonce, 'testimo_designation_font_family_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimo_designation_font_family', sanitize_text_field($_POST['testimo_designation_font_family']));
        }
    }
    // save title font size as post meta
    if (isset($_POST['testimonial_fontsize_limit']) && isset($_POST['testimonial_nonce_fontsizelimit'])) {
        // $nonce = $_POST['testimonial_nonce_fontsizelimit'];
        $fontsizelimit_nonce = isset( $_POST['testimonial_nonce_fontsizelimit'] ) ? sanitize_text_field( wp_unslash( $_POST['testimonial_nonce_fontsizelimit'] ) ) : '';
        if ( ! wp_verify_nonce($fontsizelimit_nonce, 'zltestimonialfontsizelimit_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            // update_post_meta($post_id, 'testimonial_fontsize_limit', sanitize_text_field($_POST['testimonial_fontsize_limit']));
            $title_fontsize_limit = sanitize_text_field($_POST['testimonial_fontsize_limit']);
            if ($title_fontsize_limit === '' || preg_match('/^\d{2}$/', $title_fontsize_limit)) {
                update_post_meta($post_id, 'testimonial_fontsize_limit', $title_fontsize_limit);
            } else {
                wp_die('Invalid Title font size. Please enter a two-digit number.');
            }
        }
    }
    // Save the Column Gap as post meta
    if (isset($_POST['testimonial_column_gap']) && isset($_POST['testimonial_nonce_columngap'])) {
        $column_gap_nonce = isset( $_POST['testimonial_nonce_columngap'] ) ? sanitize_text_field( wp_unslash( $_POST['testimonial_nonce_columngap'] ) ) : '';
        if ( ! wp_verify_nonce($column_gap_nonce, 'zltestimonialcolumngap_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimonial_column_gap', sanitize_text_field($_POST['testimonial_column_gap']));
        }
    }
    // Save the Grid Item Margin as Post Meta 
    if (isset($_POST['testimonial_griditem_margin']) && isset($_POST['testimonial_nonce_griditemmargin'])) {
        $column_gap_nonce = isset( $_POST['testimonial_nonce_griditemmargin'] ) ? sanitize_text_field( wp_unslash( $_POST['testimonial_nonce_griditemmargin'] ) ) : '';
        if ( ! wp_verify_nonce($column_gap_nonce, 'zltestimonialgriditem_margin_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimonial_griditem_margin', sanitize_text_field($_POST['testimonial_griditem_margin']));
        }
    }
    // Save the Grid Item Padding as Post Meta 
    if (isset($_POST['testimonial_griditem_padding']) && isset($_POST['testimonial_nonce_griditempadding'])) {
        $column_gap_nonce = isset( $_POST['testimonial_nonce_griditempadding'] ) ? sanitize_text_field( wp_unslash( $_POST['testimonial_nonce_griditempadding'] ) ) : '';
        if ( ! wp_verify_nonce($column_gap_nonce, 'zltestimonialgriditem_padding_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            update_post_meta($post_id, 'testimonial_griditem_padding', sanitize_text_field($_POST['testimonial_griditem_padding']));
        }
    }
    // zltestimonial_content_fontsizelimit_nonce
    if (isset($_POST['testimonial_content_fontsize_limit']) && isset($_POST['testimonial_nonce_content_fontsizelimit'])) {
        $content_fontsizelimit_nonce = isset( $_POST['testimonial_nonce_content_fontsizelimit'] ) ? sanitize_text_field( wp_unslash( $_POST['testimonial_nonce_content_fontsizelimit'] ) ) : '';
        if ( ! wp_verify_nonce($content_fontsizelimit_nonce, 'zltestimonial_content_fontsizelimit_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            // update_post_meta($post_id, 'testimonial_content_fontsize_limit', sanitize_text_field($_POST['testimonial_content_fontsize_limit']));
            $content_fontsize_limit = sanitize_text_field($_POST['testimonial_content_fontsize_limit']);
            if ($content_fontsize_limit === '' || preg_match('/^\d{2}$/', $content_fontsize_limit)) {
                update_post_meta($post_id, 'testimonial_content_fontsize_limit', $content_fontsize_limit);
            } else {
                wp_die('Invalid Title font size. Please enter a two-digit number.');
            }
        }
    }
    if (isset($_POST['testimonial_designation_fontsize_limit']) && isset($_POST['testimonial_nonce_designation_fontsizelimit'])) {
        $designation_fontsizelimit_nonce = isset( $_POST['testimonial_nonce_designation_fontsizelimit'] ) ? sanitize_text_field( wp_unslash( $_POST['testimonial_nonce_designation_fontsizelimit'] ) ) : '';
        if ( ! wp_verify_nonce($designation_fontsizelimit_nonce, 'zltestimonial_designation_fontsizelimit_nonce')) {
            wp_die('Security check failed. Please try again or contact support.');
        } else {
            // update_post_meta($post_id, 'testimonial_designation_fontsize_limit', sanitize_text_field($_POST['testimonial_designation_fontsize_limit']));
            $designation_fontsize_limit = sanitize_text_field($_POST['testimonial_designation_fontsize_limit']);
            if ($designation_fontsize_limit === '' || preg_match('/^\d{2}$/', $designation_fontsize_limit)) {
                update_post_meta($post_id, 'testimonial_designation_fontsize_limit', $designation_fontsize_limit);
            } else {
                wp_die('Invalid font size. Please enter a two-digit number.');
            }
        }
    }
}
add_action('save_post', 'zl_save_testimonial_design_post_meta');
// for save Shortcode meta fields save_post hook ends

// callback function of Testimonial post type's meta box starts
function zl_testimonial_title_meta_box($post) {
    wp_nonce_field('zl_testimonial_save_meta', 'zl_testimonial_nonce'); 
    $person_name = sanitize_text_field(get_post_meta($post->ID, 'testimonial_person_name', true)); 
    $position = sanitize_text_field(get_post_meta($post->ID, 'testimonial_positions', true)); 
    $dropdown_value = get_post_meta($post->ID, 'star_rating', true); ?>
    <div>
        <label for="testimonial-title-display"><?php esc_html_e('Person Name :', 'easy-testimonials-carousel'); ?></label>
        <?php $zl_person_name_nonce = wp_create_nonce('zl_person_name_nonce_action');
        echo '<input type="hidden" name="zl_person_name_nonce" value="' . esc_attr($zl_person_name_nonce) . '" />'; ?>
        <input type="text" name="testimonial_person_name" value="<?php echo esc_html($person_name); ?>" style="width: 50%; margin: 10px 5px;"> <br>

        <label for="identity-position"><?php esc_html_e('Designation :', 'easy-testimonials-carousel'); ?></label>
        <?php $zl_positions_nonce = wp_create_nonce('zl_positions_nonce_action');
        echo '<input type="hidden" name="zl_positions_nonce" value="' . esc_attr($zl_positions_nonce) . '" />'; ?>
        <input type="text" name="testimonial_positions" value="<?php echo esc_html($position); ?>" style="width: 50%; margin: 10px 15px;">
    </div>
    
    <label for="star_rating"><?php esc_html_e('Rating :', 'easy-testimonials-carousel'); ?></label>
    <?php $my_custom_nonce = wp_create_nonce('zl_rating_nonce_action');
    echo '<input type="hidden" name="zl_rating_nonce" value="' . esc_attr($my_custom_nonce) . '" />'; ?>
    <select id="star_rating" name="star_rating" style="width: 5%; margin:10px 46px;">
        <option value="0:0" <?php selected($dropdown_value, '0:0'); ?>><?php esc_html_e('0 star', 'easy-testimonials-carousel'); ?></option>
        <option value="1:1" <?php selected($dropdown_value, '1:1'); ?>><?php esc_html_e('1 star', 'easy-testimonials-carousel'); ?></option>
        <option value="2:2" <?php selected($dropdown_value, '2:2'); ?>><?php esc_html_e('2 star', 'easy-testimonials-carousel'); ?></option>
        <option value="3:3" <?php selected($dropdown_value, '3:3'); ?>><?php esc_html_e('3 star', 'easy-testimonials-carousel'); ?></option>
        <option value="4:4" <?php selected($dropdown_value, '4:4'); ?>><?php esc_html_e('4 star', 'easy-testimonials-carousel'); ?></option>
        <option value="5:5" <?php selected($dropdown_value, '5:5'); ?>><?php esc_html_e('5 star', 'easy-testimonials-carousel'); ?></option>
    </select>
    <?php
}
// callback function of Testimonial post type's meta box ends

// save testimonial's post type's meta fields starts
function zl_save_testimonial_data($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // Check if our nonce is set.
    if (!isset($_POST['zl_testimonial_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['zl_testimonial_nonce'])), 'zl_testimonial_save_meta')) {
        return;
    }

    if (isset($_POST['testimonial_positions'])) {
        update_post_meta($post_id, 'testimonial_positions', sanitize_text_field($_POST['testimonial_positions']));
    } else {
        delete_post_meta($post_id, 'testimonial_positions');
    }

    if (isset($_POST['testimonial_person_name'])) {
        update_post_meta($post_id, 'testimonial_person_name', sanitize_text_field($_POST['testimonial_person_name']));
    } else {
        delete_post_meta($post_id, 'testimonial_person_name');
    }

    if (isset($_POST['star_rating'])) {
        update_post_meta($post_id, 'star_rating', sanitize_text_field($_POST['star_rating']));
    } else {
        delete_post_meta($post_id, 'star_rating');
    }
}
add_action('save_post_zl_testimonials', 'zl_save_testimonial_data');
// save testimonial's post type's meta fields ends

// Handle quick edit save
function zl_quick_edit_save() {
    // Check the nonce for security
    if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'inlineeditnonce')) {
        return;
    }

    // Get the post ID
    $post_id = (int) $_POST['post_ID'];

    if (isset($_POST['testimonial_positions'])) {
        update_post_meta($post_id, 'testimonial_positions', sanitize_text_field($_POST['testimonial_positions']));
    }

    if (isset($_POST['testimonial_person_name'])) {
        update_post_meta($post_id, 'testimonial_person_name', sanitize_text_field($_POST['testimonial_person_name']));
    }

    if (isset($_POST['star_rating'])) {
        update_post_meta($post_id, 'star_rating', sanitize_text_field($_POST['star_rating']));
    }
}
add_action('wp_ajax_inline-save', 'zl_quick_edit_save');

// for adding extra column in shortcode post type starts
function zl_add_shortcode_column($columns) {
    // Create a new array to store column names
    $new_columns = array();
    // Add the existing columns (Title) first
    $new_columns['cb'] = $columns['cb'];
    $new_columns['title'] = $columns['title'];
    // Add the Shortcode column
    $new_columns['shortcode'] = 'Shortcode';
    // Add the existing columns (date) last
    $new_columns['date'] = $columns['date'];
    return $new_columns;
}
add_filter('manage_zl_shortc_setting_posts_columns', 'zl_add_shortcode_column');

// for display shortcodes in backend starts
function zl_custom_shortcode_column($column, $post_id) {
    if ($column === 'shortcode') {
        echo esc_html("[easy_testimonials id=\"$post_id\"]");
    } 
}
add_action('manage_zl_shortc_setting_posts_custom_column', 'zl_custom_shortcode_column', 10, 2);

// for make shortcode copyable starts
function zl_add_shortcode_column_styles() {
    echo '<style>.column-shortcode { user-select: all; }</style>';
}
add_action('admin_head', 'zl_add_shortcode_column_styles');

// shortcode for display testimonials starts.
function zl_testimonial_design_shortcode($atts) {
    global $post;
    $post_id = $post->ID;
    // Extract the 'id' attribute from the shortcode
    $atts = shortcode_atts(
        array('id' => '', 
            ), $atts,
        'testimonial_design' );

    $post_id = intval($atts['id']);
    if (!empty($post_id)) {
        $post = get_post($post_id);
        // Check if a valid post is found
        if ($post) {
            // You can access the post content, title, custom fields, etc., using $post
            $selected_design = get_post_meta($post_id, 'testimonial_design', true);
            $selected_category = get_post_meta($post_id, 'testimonial_category', true);
            $selected_testimonials = get_post_meta($post_id, 'testimonials_top_rated', true);
            $backend_posts_per_page = intval(get_post_meta($post_id, 'testimonial_post_limit', true));
            // Responsive data
            $selected_res_desktop_col = intval(get_post_meta($post_id, 'testimonial_desktop_meta', true)); 
            $selected_res_laptop_col = intval(get_post_meta($post_id, 'testimonial_laptop_meta', true)); 
            $selected_res_tablet_col = intval(get_post_meta($post_id, 'testimonial_tablet_meta', true)); 
            $selected_bg_color = get_post_meta($post_id, 'testimonial_bg_color', true);
            $selected_content_color = get_post_meta($post_id, 'testimonial_text_color', true);
            $selected_title_color = get_post_meta($post_id, 'testimonial_title_color', true);
            $selected_designation_color = get_post_meta($post_id, 'testimo_designa_color', true); 
            $selected_ratingicon_color = get_post_meta($post_id, 'testimo_ratingicon_color', true);
            $selected_sort_postorder_option = get_post_meta($post_id, 'testimopost_orderoption', true);
            $selected_feature_imagestyle = get_post_meta($post_id, 'testimoo_pic', true); // Default to true
            $selected_layout = get_post_meta($post_id, 'testimoo_layout', true); 
            $selected_arrowstyle = get_post_meta($post_id, 'testimoo_arrowstyle', true); 
            $selected_columns = get_post_meta($post_id, 'testimoo_grid_columns', true); 
            $selected_text_align = get_post_meta($post_id, 'testimo_text_align', true); 
            $data_a_attribute = get_post_meta($post_id, 'autoplaytoggle_field', true);
            $slickslidesarrow = get_post_meta($post_id, 'slickslidesarrow', true);
            $slickslidesdots = get_post_meta($post_id, 'slickslidesdots', true);
            $slickslidesloop = get_post_meta($post_id, 'slickslidesloop', true);
            $slickslideshover = get_post_meta($post_id, 'slickslideshover', true);
            $slickslidesdrag_mt = get_post_meta($post_id, 'slickslides_drag_mt', true);
            $slickslideslazy_load = get_post_meta($post_id, 'slickslides_lazy_load', true);

            $selected_display_name = get_post_meta($post_id, 'toggle_display_name', true);
            $selected_display_picture = get_post_meta($post_id, 'toggle_display_picture', true);
            $selected_display_designation = get_post_meta($post_id, 'toggle_display_designation', true);
            $selected_display_rating = get_post_meta($post_id, 'toggle_display_rating', true);
            $selected_display_content = get_post_meta($post_id, 'toggle_display_content', true);
            $slidedelayvalue = get_post_meta($post_id, 'sliderange_field', true); // Default to true
            $select_titlefont_family = get_post_meta($post_id, 'testimo_title_font_family', true);
            $select_contentfont_family = get_post_meta($post_id, 'testimo_content_font_family', true);
            $select_titlefont_style = get_post_meta($post_id, 'testimo_title_font_style', true);
            $select_contentfont_style = get_post_meta($post_id, 'testimo_content_font_style', true);
            $select_designationfont_style = get_post_meta($post_id, 'testimo_designation_font_style', true);
            $select_designationfont_family = get_post_meta($post_id, 'testimo_designation_font_family', true);
            $select_titlefont_size = get_post_meta($post_id, 'testimonial_fontsize_limit', true);
            $select_content_titlefont_size = get_post_meta($post_id, 'testimonial_content_fontsize_limit', true);
            $select_designation_titlefont_size = get_post_meta($post_id, 'testimonial_designation_fontsize_limit', true);
            $select_column_gap = get_post_meta($post_id, 'testimonial_column_gap', true);
            $select_griditem_margin = get_post_meta($post_id, 'testimonial_griditem_margin', true);
            $select_griditem_padding = get_post_meta($post_id, 'testimonial_griditem_padding', true);
            
            if (empty($selected_layout)) {
                $selected_layout = 'slider';
            }
            if (empty($selected_columns)) {
                $selected_columns = '1';
            }
            if (empty($selected_arrowstyle)) {
                $selected_arrowstyle = 'outer-slider-container';
            }
            if (empty($selected_design)) {
                // Set a default design here (you can change 'design1' to your preferred default)
                $selected_design = 'design1';
            }
            // Create WP_Query args based on retrieved post meta values
            if (!empty($selected_testimonials)) {
                $args = array(
                    'post_type' => 'zl_testimonials',
                    'post__in' => $selected_testimonials, // Array of IDs
                    'orderby' => 'post__in', // Maintain order of IDs
                    'posts_per_page' => $backend_posts_per_page,
                );
            }else{  
                $args = array(
                    'post_type' => 'zl_testimonials',
                    'posts_per_page' => $backend_posts_per_page,
                );
                if (!empty($selected_category)) {
                    $args['tax_query'] = array(
                        array(
                            'taxonomy' => 'testimonial-category',
                            'field' => 'id',
                            'terms' => $selected_category,
                        ),
                    );
                }

                // Add the order and orderby parameters based on the selected option
                switch ($selected_sort_postorder_option){
                    case 'latest':
                        $args['orderby'] = 'date';
                        $args['order'] = 'DESC';
                        break;

                    case 'random':
                        $args['orderby'] = 'rand';
                        break;

                    case 'asc':
                        $args['orderby'] = 'title';
                        $args['order'] = 'ASC';
                        break;

                    case 'desc':
                        $args['orderby'] = 'title';
                        $args['order'] = 'DESC';
                        break;

                    case 'modified':
                        $args['orderby'] = 'modified';
                        $args['order'] = 'DESC';
                        break;

                    case 'date-wise':
                        $args['orderby'] = 'date';
                        $args['order'] = 'ASC';
                        break;

                    default:
                        // Default to 'latest' if no valid option is selected
                        $args['orderby'] = 'date';
                        $args['order'] = 'DESC';
                        break;
                }
            }
            $query = new WP_Query($args);
            ob_start();
            if ($query->have_posts()) {
                echo '<div class="zl-testimonial-slider">';

                // Separate loops for each design section
                $design1_posts = array();
                $design2_posts = array();
                $design3_posts = array();

                while ($query->have_posts()) {
                    $query->the_post();
                    $person_name = get_post_meta(get_the_ID(), 'testimonial_person_name', true);
                    $designation = get_post_meta(get_the_ID(), 'testimonial_positions', true);
                    $countss = floatval(get_post_meta(get_the_ID(), 'star_rating', true)); // Retrieve the fractional rating     
                    $post_data = array(
                        'post_thumbnail' => get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class' => 'zl-testimonial-post-feature', 'style' => "border-radius: $selected_feature_imagestyle"."%;")),
                        // 'title' => get_the_title(),
                        'title' => $person_name,
                        'excerpt' => get_the_excerpt(),
                        'designation' => $designation,
                        'rating' => $countss
                    );

                    // Initialize arrays if not already set
                    if (!isset($design1_posts)) $design1_posts = array();
                    if (!isset($design2_posts)) $design2_posts = array();
                    if (!isset($design3_posts)) $design3_posts = array();
                    
                    switch ($selected_design) {
                        case 'design1':
                            $design1_posts[] = $post_data;
                            break;

                        case 'design2':
                            $design2_posts[] = $post_data;
                            break;

                        case 'design3':
                            $design3_posts[] = $post_data;
                            break;
                    }
                } 
                ?>
                <style>
                    .fa-star:before{
                        content: '★' !important;
                    }
                    .zl-testimonial-star{
                        display:contents !important;
                    }
                    .slick-slide{
                        background-color: <?php echo esc_attr($selected_bg_color); ?>; 
                    }
                </style>
                <?php
                 // Output Slick sliders for each design section
                if($selected_display_name=='true'){ echo '<style>.zl-testimonial-post-title { display: none; }</style>'; }
                if($selected_display_picture=='true'){ echo '<style>img.zl-testimonial-post-feature.wp-post-image, .zl_grid_image , .zl-testimonial-post-thumbnail ,.zl-testimonial-content img, .zl-testimonial-post-thumbnail img{ display: none; }</style>'; }
                if($selected_display_designation=='true'){ echo '<style>.zl-testimonial-designation { display: none !important; }</style>'; }
                if($selected_display_rating=='true'){ echo '<style>.zl-testimonial-rating { display: none !important; }</style>'; }
                if($selected_display_content=='true'){ echo '<style>.zl-testimonial-post-excerpt  { display: none !important; }</style>'; }
                if($selected_text_align){
                     echo '<style>.zl-testimonial-post-content, .zl-testimonial-text { text-align:'.esc_attr($selected_text_align).' !important;}</style>';
                }else{
                     echo '<style>.zl-testimonial-post-content, zl-testimonial-text { text-align:center !important; }</style>';
                }
                switch ($selected_layout) {
                    case 'grid':
                        echo '<div class="zl-testimonial-grid" style="display: grid; grid-template-columns: repeat(' . esc_attr($selected_columns) . ', 1fr); gap: '. esc_attr($select_column_gap) . 'px;">';
                        while ($query->have_posts()) {
                            $query->the_post();
                            $person_name = get_post_meta(get_the_ID(), 'testimonial_person_name', true);
                            $designation = get_post_meta(get_the_ID(), 'testimonial_positions', true);
                            $countss = floatval(get_post_meta(get_the_ID(), 'star_rating', true));

                            echo '<div class="zl-testimonial-grid-item" style="margin: '.esc_attr($select_griditem_margin).'px !important; padding:'.esc_attr($select_griditem_padding).'px; text-align:center; background:'.esc_attr($selected_bg_color).';">';
                            echo '<div class="zl-testimonial-content-wrapper">';
                            if ($selected_display_content) {
                                $post_excert = get_the_excerpt();
                                echo '<p class="zl_testimo_desc zl-testimonial-post-excerpt zl-testimonial-text" style="color:'. esc_attr($selected_content_color).'; font-family:'.esc_attr($select_contentfont_family).'; font-style:'.esc_attr($select_contentfont_style).'; font-size:'. esc_attr($select_content_titlefont_size).'px;">' . esc_html($post_excert) . '</p>';
                            }
                            if ($selected_display_picture) {
                                if (has_post_thumbnail()) {
                                    echo get_the_post_thumbnail(get_the_ID(), 'thumbnail', array('class' => 'zl_grid_image', 'style' => "border-radius:50px; height:90px;width:90px;"));
                                } else {
                                    $static_image_url = plugin_dir_url(__FILE__) . 'assets/Images/thumbnail.png';
                                    echo '<div>';
                                    echo '<img class="zl_grid_image" src="' . esc_url($static_image_url) . '" alt="Static Image" width="90" height="90">';
                                    echo '</div>';
                                }
                            }
                            if ($selected_display_rating) {
                                echo '<div class="zl-star-rating zl-testimonial-rating" data-rating="' . esc_attr($countss) . '">';
                                for ($i = 0; $i < floor($countss); $i++) {
                                    echo '<span class="dashicons dashicons-star-filled" style="color:'.esc_attr($selected_ratingicon_color).';"></span>';
                                }
                                echo '</div>';
                            }
                            echo '<div class="zl-testimonial-text">';
                                if ($selected_display_name) {
                                    echo '<h5 class="zl_title_grid zl-testimonial-post-title" style="color:'.esc_attr($selected_title_color).'; font-family:'.esc_attr($select_titlefont_family).'; font-style:'.esc_attr($select_titlefont_style).'; font-size:'. esc_attr($select_titlefont_size).'px;">' . esc_html($person_name) . '</h5>';
                                }
                                if ($selected_display_designation) {
                                    echo '<p class="zl_designation_grid zl-testimonial-designation" style="color:'. esc_attr($selected_designation_color) .'; font-family:'.esc_attr($select_designationfont_family).'; font-style:'.esc_attr($select_designationfont_style).'; font-size:'. esc_attr($select_designation_titlefont_size).'px;">' . esc_html($designation) . '</p>';
                                }
                            echo '</div>';
                            echo '</div>'; 
                            echo '</div>';
                        }
                        echo '</div>';
                        break;
                    case 'slider':
                        echo '<div class="zl-testimonial-slider">';
                        if ($selected_design === 'design1' && !empty($design1_posts)) {
                    
                            echo '<div class="zl-slick-design-1" data-id="' . esc_attr(get_the_ID()) . '" data-a="'.esc_attr($data_a_attribute).'" data-p="'.esc_attr($slickslidesarrow).'" data-m="'.esc_attr($slickslidesdots).'" data-e="'.esc_attr($slidedelayvalue).'" data-h="'.esc_attr($slickslidesloop).'" data-k="'.esc_attr($slickslideshover).'" data-swipe1="'.esc_attr($slickslidesdrag_mt).'" data-resd1="'.esc_attr($selected_res_desktop_col).'" data-resl1="'.esc_attr($selected_res_laptop_col).'" data-rest1="'.esc_attr($selected_res_tablet_col).'" data-lazyload1="'.esc_attr($slickslideslazy_load).'" data-arrowstyle1="' . esc_attr($selected_arrowstyle) . '">';
                            foreach ($design1_posts as $index =>  $post_data) {
                                // Output each post data with the appropriate structure for Design 1
                                ?>
                                <div class="zl-testimonial-design-1 zl-slider-post" style="background-color:<?php echo esc_attr($selected_bg_color); ?>">
                                    <div class="zl-testimonial-post-thumbnail">
                                        <?php   $post_thumbnail = $post_data['post_thumbnail'];
                                            if (!empty($post_thumbnail)) {
                                                echo wp_kses_post($post_thumbnail);
                                            } else {
                                                $static_image_url = plugin_dir_url(__FILE__) . 'assets/Images/thumbnail.png';
                                                echo '<img src="' . esc_url($static_image_url) . '" alt="Static Image" width="150" height="150" style="margin-left: 26%;">';
                                            } ?>
                                    </div>
                                    <div class="zl-testimonial-post-content">
                                        <h2 class="zl-testimonial-post-title zl-t-person-name" style="color:<?php echo esc_attr($selected_title_color); ?>; font-family:<?php echo esc_attr($select_titlefont_family); ?>; font-style:<?php echo esc_attr($select_titlefont_style); ?>; font-size:<?php echo esc_attr($select_titlefont_size); ?>px ;"><?php echo esc_attr($post_data['title']); ?></h2>
                                        <?php if (!empty($post_data['designation'])) { ?>
                                            <div class="zl-testimonial-designation" style="color:<?php echo esc_attr($selected_designation_color); ?>; font-style:<?php echo esc_attr($select_designationfont_style); ?>; font-family:<?php echo esc_attr($select_designationfont_family); ?>; font-size:<?php echo esc_attr($select_designation_titlefont_size); ?>px ;"">Designation: <?php echo esc_attr($post_data['designation']); ?></div>
                                        <?php } ?>
                                        <?php if (!empty($post_data['rating'])) { ?>
                                            <div class="zl-testimonial-rating" style="color:<?php echo esc_attr($selected_ratingicon_color); ?>">
                                                <?php for ($i = 0; $i < $post_data['rating']; $i++) { ?>
                                                    <div class="zl-testimonial-star">
                                                        <i class="fa fa-star" style="color:<?php echo esc_attr($selected_ratingicon_color); ?>"></i>
                                                    </div>
                                                <?php } ?>
                                            </div>
                                        <?php } ?>
                                        <p class="zl-testimonial-post-excerpt zl-excerpt" style="color:<?php echo esc_attr($selected_content_color); ?>; font-family:<?php echo esc_attr($select_contentfont_family); ?>; font-style:<?php echo esc_attr($select_contentfont_style); ?>; font-size:<?php echo esc_attr($select_content_titlefont_size); ?>px ;"><?php echo wp_kses_post(wp_trim_words($post_data['excerpt'], 60)); ?></p>
                                    </div>
                                </div>
                                <?php
                            }
                            echo '</div>'; // Close Design 1 slider
                        } elseif ($selected_design === 'design2' && !empty($design2_posts)) {
                            // Output Slick slider for Design 2
                            echo '<div class="zl-slick-design-2" data-id="' . esc_attr(get_the_ID()) . '" data-b="'.esc_attr($data_a_attribute).'" data-q="'.esc_attr($slickslidesarrow).'" data-n="'.esc_attr($slickslidesdots).'" data-f="'.esc_attr($slidedelayvalue).'" data-i="'.esc_attr($slickslidesloop).'" data-w="'.esc_attr($slickslideshover).'" data-swipe2="'.esc_attr($slickslidesdrag_mt).'" data-resd2="'.esc_attr($selected_res_desktop_col).'" data-resl2="'.esc_attr($selected_res_laptop_col).'" data-rest2="'.esc_attr($selected_res_tablet_col).'" data-lazyload2="'.esc_attr($slickslideslazy_load).'" data-arrowstyle2="' . esc_attr($selected_arrowstyle) . '">';
                            foreach ($design2_posts as $index => $post_data) {
                                // Output each post data with the appropriate structure for Design 2
                                ?>
                                <div class="zl-testimonial-design-2 zl-slider-post">
                                    <div class="zl-testimonial-post-thumbnail" style="background-color:<?php echo esc_attr($selected_bg_color); ?>">
                                        <?php $post_thumbnail = $post_data['post_thumbnail'];
                                            if (!empty($post_thumbnail)) {
                                                echo wp_kses_post($post_thumbnail);
                                            } else {
                                                $static_image_url = plugin_dir_url(__FILE__) . 'assets/Images/thumbnail.png';
                                                echo '<img src="' . esc_url($static_image_url) . '" alt="Static Image" width="150" height="150">';
                                            } ?>
                                        <div class="zl-testimonial-post-content">
                                            <h2 class="zl-testimonial-post-title zl-t-person-name" style="color:<?php echo esc_attr($selected_title_color); ?>; font-family:<?php echo esc_attr($select_titlefont_family); ?>; font-style:<?php echo esc_attr($select_titlefont_style); ?>; font-size:<?php echo esc_attr($select_titlefont_size); ?>px ;"><?php echo esc_attr($post_data['title']); ?></h2>
        
                                            <?php if (!empty($post_data['designation'])) { ?>
                                                <div class="zl-testimonial-designation" style="color:<?php echo esc_attr($selected_designation_color); ?>; font-style:<?php echo esc_attr($select_designationfont_style); ?>; font-family:<?php echo esc_attr($select_designationfont_family); ?>; font-size:<?php echo esc_attr($select_designation_titlefont_size); ?>px ;">Designation: <?php echo esc_attr($post_data['designation']); ?></div>
                                            <?php } ?>
        
                                            <?php if (!empty($post_data['rating'])) { ?>
                                                <div class="zl-testimonial-rating" style="color:<?php echo esc_attr($selected_ratingicon_color); ?>">
                                                    <?php for ($i = 0; $i < $post_data['rating']; $i++) { ?>
                                                        <div class="zl-testimonial-star">
                                                            <i class="fa fa-star" style="color:<?php echo esc_attr($selected_ratingicon_color); ?>"></i>
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            <?php } ?>
                                            <div class="zl-testimonial-double-qoute">
                                                <p class="zl-testimonial-content zl-testimonial-post-excerpt" style="color:<?php echo esc_attr($selected_content_color); ?>; font-family:<?php echo esc_attr($select_contentfont_family); ?>; font-style:<?php echo esc_attr($select_contentfont_style); ?>; font-size:<?php echo esc_attr($select_content_titlefont_size); ?>px ;"><?php echo wp_kses_post(wp_trim_words($post_data['excerpt'], 60)); ?></p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?php
                            }
                            echo '</div>'; // Close Design 2 slider
                        } elseif ($selected_design === 'design3' && !empty($design3_posts)) {
                            // Output Slick slider for Design 3
                            echo '<div class="zl-slick-design-3" data-id="' . esc_attr(get_the_ID()) . '" data-c="'.esc_attr($data_a_attribute).'" data-r="'.esc_attr($slickslidesarrow).'" data-o="'.esc_attr($slickslidesdots).'" data-g="'.esc_attr($slidedelayvalue).'" data-j="'.esc_attr($slickslidesloop).'" data-s="'.esc_attr($slickslideshover).'" data-swipe3="'.esc_attr($slickslidesdrag_mt).'" data-resd3="'.esc_attr($selected_res_desktop_col).'" data-resl3="'.esc_attr($selected_res_laptop_col).'" data-rest3="'.esc_attr($selected_res_tablet_col).'" data-lazyload3="'.esc_attr($slickslideslazy_load).'" data-arrowstyle3="' . esc_attr($selected_arrowstyle) . '">';
                            foreach ($design3_posts as $index => $post_data) {
                                // Output each post data with the appropriate structure for Design 3 ?>
                                <div class="zl-testimonial-design-3 zl-slider-post">
                                    <div class="zl-testimonial-post-content" style="background-color:<?php echo esc_attr($selected_bg_color); ?>">
                                        <p class="zl-testimonial-content zl-testimonial-post-excerpt" style="color:<?php echo esc_attr($selected_content_color); ?>; font-family:<?php echo esc_attr($select_contentfont_family); ?>; font-style:<?php echo esc_attr($select_contentfont_style); ?>; font-size:<?php echo esc_attr($select_content_titlefont_size); ?>px ;"><?php 
                                            echo wp_kses_post(wp_trim_words($post_data['excerpt'], 60)); ?>
                                        </p>
                                        <div class="zl-testimonial-content">
                                            <?php $post_thumbnail = $post_data['post_thumbnail'];
                                            if (!empty($post_thumbnail)) {
                                                echo wp_kses_post($post_thumbnail);
                                            } else {
                                                $static_image_url = plugin_dir_url(__FILE__) . 'assets/Images/thumbnail.png';
                                                echo '<img src="' . esc_url($static_image_url) . '" alt="Static Image" width="150" height="150">';
                                            } ?>
                                        </div>
                                        <div class="zl-testimonial-text">
                                            <h2 class="zl-testimonial-post-title zl-t-person-name" style="color:<?php echo esc_attr($selected_title_color); ?>; font-family:<?php echo esc_attr($select_titlefont_family); ?>; font-style:<?php echo esc_attr($select_titlefont_style); ?>; font-size:<?php echo esc_attr($select_titlefont_size); ?>px ;"><?php echo esc_attr($post_data['title']);
                                            ?></h2>
        
                                            <?php if (!empty($post_data['designation'])) { ?>
                                                <div class="zl-testimonial-designation" style="color:<?php echo esc_attr($selected_designation_color); ?>; font-style:<?php echo esc_attr($select_designationfont_style); ?>; font-family:<?php echo esc_attr($select_designationfont_family); ?>; font-size:<?php echo esc_attr($select_designation_titlefont_size); ?>px ;">Designation: <?php echo esc_attr($post_data['designation']); ?>
                                                </div>
                                            <?php } ?>
        
                                            <?php if (!empty($post_data['rating'])) { ?>
                                                <div class="zl-testimonial-rating" style="color:<?php echo esc_attr($selected_ratingicon_color); ?>">
                                                    <?php for ($i = 0; $i < $post_data['rating']; $i++) { ?>
                                                        <div class="zl-testimonial-star">
                                                            <i class="fa fa-star" style="color:<?php echo esc_attr($selected_ratingicon_color); ?>"></i>
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            <?php } ?>        
                                        </div>
                                    </div>
                                </div>
                                <?php
                            }
                            echo '</div>'; // Close Design 3 slider
                        }
                        echo '</div>';
                        break;
                    default:
                        echo 'no testimonial Found';
                        break;
                }
                // echo '</div>'; // Close slider container
                echo '</div>'; // Close testimonial-slider div
            }
            wp_reset_postdata();
            return ob_get_clean();
        } else {
            // If the post with the provided ID is not found, return an error message
            return '<p>Error: Testimonial not found.</p>';
        }
    } else {
        // If the 'id' parameter is not provided or is empty, return empty content
        return '';
    }
}
add_shortcode('easy_testimonials', 'zl_testimonial_design_shortcode');

// Add a meta box to the post slider edit screen
function zl_add_shortcode_meta_box() {
    add_meta_box(
        'zl_shortcode_meta_box',           // Meta box ID
        'Generated Shortcode',             // Meta box Title
        'zl_display_shortcode_meta_box',   // Callback function
        'zl_shortc_setting',               // Post type where the box appears
        'advanced',                            // Context where the box appears (side, normal, advanced)
        'high'                             // Priority of the box
    );
}
add_action('add_meta_boxes', 'zl_add_shortcode_meta_box');

// Display the shortcode meta box content(pages and Template)
function zl_display_shortcode_meta_box($post) {
    $shortcode = "[easy_testimonials id=\"{$post->ID}\"]";
    echo '<div class="zl_shortcode_inline">';
    echo '<div class="zl_inline_meta" style="border-right: black 1px solid;"><h3>Shortcode</h3><p>Copy the shortcode below and use it in your posts or pages:</p>';
    echo '<input type="text" class="zl_shortcode_input" readonly="readonly" value="' . esc_attr($shortcode) . '" style="width: 70%;" /></div>';

    echo '<div class="zl_inline_meta"><h3>Template Include</h3><p>Copy and paste the PHP code into your template file:</p>';
    echo '<input type="text" class="zl_shortcode_input" readonly="readonly" value="<?php echo do_shortcode(\'' . esc_attr($shortcode) . '\'); ?>" style="width: 70%;" /></div>';
    echo '</div>';
}

// Submission Form
function zl_add_testimonials_submenu_submission_form() {
    add_submenu_page(
        'edit.php?post_type=zl_testimonials', // parent slug
        'Testimonials Submission Form', // page title
        'Frontend Submission Form', // menu title
        'manage_options', // capability
        'testimonials-submission-form', // menu slug
        'zl_testimonials_settings_submission_form_callback' // callback function
    );

    // Custom css
    add_submenu_page(
        'edit.php?post_type=zl_testimonials', 
        'Testimonials Custom CSS', 
        'Custom CSS', 
        'manage_options', 
        'testimonials-custom-css', 
        'zl_testimonials_settings_custom_css_callback' // callback function
    );

    // Custom Export/ Import Functionalities
    add_submenu_page(
        'edit.php?post_type=zl_testimonials', 
        'Testimonials Import Export', 
        'Import / Export Testimonials', 
        'manage_options', 
        'testimonials-import-export-testimonials', 
        'zl_testimonials_settings_import_export_testimonials_callback' // callback function
    );
}
add_action('admin_menu', 'zl_add_testimonials_submenu_submission_form');

// Ajax for Top rated featch testimonial
function zl_fetch_testimonials_callback() {
    // Check if IDs are passed
    if (isset($_POST['ids']) && !empty($_POST['ids'])) {
        $ids = array_map('intval', $_POST['ids']);
        $args = array(
            'post_type' => 'zl_testimonials',
            'post__in' => $ids,
            'posts_per_page' => -1,
            'post_status' => 'publish'
        );
        
        $query = new WP_Query($args);        
        if ($query->have_posts()) {
            ob_start(); // Start output buffering
            while ($query->have_posts()) {
                $query->the_post();?>
                <div class="zl-testimonial-item">
                    <h2><?php echo esc_html(get_the_title()); ?></h2>
                    <p><?php echo esc_html(get_the_excerpt()); ?></p>
                </div>
                <?php
            }
            wp_reset_postdata();
            // Get the buffered content and escape it
            $output = ob_get_clean();
            echo wp_kses_post($output);
        }
    }
    wp_die(); // End the AJAX request
}
add_action('wp_ajax_zl_fetch_testimonials', 'zl_fetch_testimonials_callback');
add_action('wp_ajax_nopriv_zl_fetch_testimonials', 'zl_fetch_testimonials_callback');

function zl_testimonials_settings_submission_form_callback(){ 
    $saved_options = zl_get_testimonials_checkbox_selection();
    $saved_values = get_option('testimonials_saved_form_labels', array());
    $template_code = '<?php echo do_shortcode(\'[frontend_submission_form]\'); ?>';?>
    
    <h3><?php esc_html_e('Easy Testimonial Frontend Form Shortcode','easy-testimonials-carousel'); ?></h3>
    <h4><?php esc_html_e('To display a form with fields selected here, just copy and paste this','easy-testimonials-carousel'); ?></h4>
    <div class="zl_shortcode_inline">
        <div class="zl_inline_meta" style="border-right: black 1px solid;">
            <h3><?php esc_html_e('Shortcode','easy-testimonials-carousel'); ?></h3>
            <p><?php esc_html_e('Copy the shortcode below and use it in your posts or pages:','easy-testimonials-carousel'); ?></p>
            <input type="text" class="zl_shortcode_input" readonly="readonly" value="[frontend_submission_form]" style="width: 70%; background-color: #fff;" />
        </div>
        <div class="zl_inline_meta">
            <h3><?php esc_html_e('Template Include','easy-testimonials-carousel'); ?></h3>
            <p><?php esc_html_e('Copy and paste the PHP code into your template file:','easy-testimonials-carousel'); ?></p>
            <input type="text" class="zl_shortcode_input" readonly="readonly" value="<?php echo esc_html($template_code); ?>" style="width: 70%; background-color: #fff;" />
        </div>
    </div><hr>

    <h4><?php esc_html_e('From the list below select and give the name of the field you want to show as input fields to the front end users to submit testimonials:', 'easy-testimonials-carousel'); ?></h4>
    <form id="zl_fieldSelectionForm" method="post" action="">
        <table class="zl-form-table">
            <tbody>
                <tr>
                    <td><h4><input type="checkbox" id="zl_easy_testimo_title" name="options[]" value="Title" <?php echo in_array('Title', $saved_options) ? 'checked' : ''; ?>>
                    <label for="zltestimo_title"><?php esc_html_e('Title','easy-testimonials-carousel'); ?> </label></h4></td>
                    <td><input id="t_title" class="zl_easy_testimo_field" type="text" name="title" value="<?php echo isset($saved_values['title']) ? esc_attr($saved_values['title']) : 'Title'; ?>" /></td>
                </tr>
                <tr>
                    <td><h4><input type="checkbox" id="zl_easy_testimo_name" name="options[]" value="Name" <?php echo in_array('Name', $saved_options) ? 'checked' : ''; ?>>
                    <label for="zltestimo_name"><?php esc_html_e('Name','easy-testimonials-carousel'); ?></label></h4></td>
                    <td><input id="t_name" class="zl_easy_testimo_field" type="text" name="name" value="<?php echo isset($saved_values['name']) ? esc_attr($saved_values['name']) : 'Name'; ?>" /></td>
                </tr>
                <tr>
                    <td><h4><input type="checkbox" id="zl_easy_testimo_designation" name="options[]" value="Designation" <?php echo in_array('Designation', $saved_options) ? 'checked' : ''; ?>>
                    <label for="zltestimo_designation"><?php esc_html_e('Designation','easy-testimonials-carousel'); ?> </label></h4></td>
                    <td><input type="text" class="zl_easy_testimo_field" name="designation" id="t_designation" value="<?php echo isset($saved_values['designation']) ? esc_attr($saved_values['designation']) : 'Designation'; ?>"></td>
                </tr>
                <tr>
                    <td><h4><input type="checkbox" id="zl_easy_testimo_categories" name="options[]" value="Categories" <?php echo in_array('Categories', $saved_options) ? 'checked' : ''; ?>>
                    <label for="zltestimo_categories"><?php esc_html_e('Categories','easy-testimonials-carousel'); ?></label></h4></td>
                    <td><input type="text" class="zl_easy_testimo_field" name="categories" id="t_categories" value="<?php echo isset($saved_values['categories']) ? esc_attr($saved_values['categories']) : 'Categories'; ?>" /></td>
                </tr>
                <tr>
                    <td><h4><input type="checkbox" id="zl_easy_testimo_upload_img" name="options[]" value="Upload Image" <?php echo in_array('Upload Image', $saved_options) ? 'checked' : ''; ?>>
                    <label for="zltestimo_upload_image"><?php esc_html_e('Upload Image','easy-testimonials-carousel'); ?> </label></h4></td>
                    <td><input type="text" class="zl_easy_testimo_field" name="upload_image" id="t_upload_image" value="<?php echo isset($saved_values['upload_image']) ? esc_attr($saved_values['upload_image']) : 'Upload Image'; ?>" /></td>
                </tr>
                <tr>
                    <td><h4><input type="checkbox" id="zl_easy_testimo_rating" name="options[]" value="Rating" <?php echo in_array('Rating', $saved_options) ? 'checked' : ''; ?>>
                    <label for="zltestimo_rating"> <?php esc_html_e('Rating','easy-testimonials-carousel'); ?></label></h4></td>
                    <td><input type="text" class="zl_easy_testimo_field" name="rating" id="t_rating" value="<?php echo isset($saved_values['rating']) ? esc_attr($saved_values['rating']) : 'Rating'; ?>"></td>
                </tr>
                <tr>
                    <td><h4><input type="checkbox" id="zl_easy_testimo_message" name="options[]" value="Testimonial Message" <?php echo in_array('Testimonial Message', $saved_options) ? 'checked' : ''; ?>>
                    <label for="zltestimo_message"><?php esc_html_e('Testimonial Message','easy-testimonials-carousel'); ?></label></h4></td>
                    <td><input type="text" class="zl_easy_testimo_field" name="testimonial_message" id="t_message" value="<?php echo isset($saved_values['testimonial_message']) ? esc_attr($saved_values['testimonial_message']) : 'Testimonial Message'; ?>"></td>
                </tr>
                <tr>
                    <td><label><h4><?php esc_html_e('Submit Button Text','easy-testimonials-carousel'); ?> </h4></label></td>
                    <td><input type="text" name="submit_btn_text" id="t_submit_btn_text" class="zl_easy_testimo_field" value="<?php echo isset($saved_values['submit_btn_text']) ? esc_attr($saved_values['submit_btn_text']) : 'Submit'; ?>" /></td>
                </tr>
            </tbody>
        </table>
        <?php submit_button(); ?>
    </form> <?php 
}
function zl_testimonials_settings_custom_css_callback(){ ?>
    <div class="wrap">
        <form method="post" action="options.php">
            <?php
            settings_fields('custom_css_options');
            do_settings_sections('custom-css-options');
            submit_button();
            ?>
        </form>
    </div><?php
}
function zl_custom_css_init() {
    // Custom CSS
    register_setting('custom_css_options', 'custom_css', 'sanitize_textarea_field');
    add_settings_section('custom_css_section', 'Custom CSS', 'zl_custom_css_section_callback', 'custom-css-options');
    add_settings_field('custom_css_field', 'Enter your custom CSS', 'zl_custom_css_field_callback', 'custom-css-options', 'custom_css_section');
}
add_action('admin_init', 'zl_custom_css_init');
function zl_custom_css_section_callback() {
    echo '<p>Enter your custom CSS below:</p>';
}

function zl_custom_css_field_callback() {
    $css = get_option('custom_css'); ?>
    <textarea id="zl-custom-css-editor" name="custom_css" rows="20" cols="90"><?php echo esc_textarea($css); ?></textarea>
    <?php
}

function zl_testimonials_settings_import_export_testimonials_callback(){ ?>
    <div class="wrap">
        <h1><?php esc_html_e('Export Custom Post Type','easy-testimonials-carousel'); ?> </h1><br>
        <a href="<?php echo esc_url(add_query_arg('export_custom_post_type', '1')); ?>" class="button button-primary"><?php esc_html_e('Export Custom Post Type(Zl-testimonial) to CSV','easy-testimonials-carousel'); ?></a> <br><br> <br><br>
        <hr> <br><br>
        <h1><?php esc_html_e('Import Testimonials', 'easy-testimonials-carousel'); ?></h1>
        <form method="post" enctype="multipart/form-data">
            <label for="csv_type">
                <input type="radio" name="import_type" value="csv" checked> <?php esc_html_e('CSV', 'easy-testimonials-carousel'); ?>
            </label> &nbsp;&nbsp;
            <label for="xml_wp_type">
                <input type="radio" name="import_type" value="wxr"> <?php esc_html_e('WordPress XML', 'easy-testimonials-carousel'); ?>
            </label>
            <br><br>
            <input type="file" name="zl_testimonials_import_file" />
            <input type="submit" name="zl_testimonials_import" value="Import" class="button button-primary" />
        </form>
    </div>
    <?php
    // Handle the file upload and import process
    if (isset($_POST['zl_testimonials_import'])) {
        zl_handle_import();
    }
}

function zl_handle_import() {
    if (!empty($_FILES['zl_testimonials_import_file']['tmp_name'])) {
        $file = $_FILES['zl_testimonials_import_file']['tmp_name'];
        $import_type = $_POST['import_type'];
        // echo "<pre>";
        // print_r($_FILES['zl_testimonials_import_file']['name']);
        // echo "</pre>";
        // die;
        if ($import_type === 'csv') {
            zl_testimonials_import_from_csv($file);
        }elseif ($import_type === 'wxr') {
            zl_testimonials_import_from_wxr($file);
        }else{
            echo '<div style="font-size: 15px; color: red;">Please select the correct file type.</div>';
        }
    } else {
        // echo 'No file uploaded.';
        echo '<div style="font-size: 15px; color: red;">No file uploaded.</div>';
    }
}

// Function to import CSV data into zl_testimonials
function zl_testimonials_import_from_csv($file_path) {
    
    $file_name =$_FILES['zl_testimonials_import_file']['name'];
    $file_extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
    if ($file_extension !== 'csv') {
        echo '<div style="font-size: 15px; color: red;">Error: The selected file is not a CSV file. Please upload a valid CSV file.</div>';
        return; // Exit the function if the file is not a CSV
    }
    if (($handle = fopen($file_path, 'r')) !== FALSE) {
        $headers = fgetcsv($handle, 1000, ',');
        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
            // Prepare post data
            $record = array_combine($headers, $data);
            $title_slug = sanitize_title($record['Title']);
            $existing_post_id = zl_get_post_id_by_slug($title_slug);
            if ($existing_post_id) {
                $post_data = array(
                    'ID' => $existing_post_id,
                    'post_title' => $record['Title'],
                    'post_content' => $record['Content'],
                    'post_type' => 'zl_testimonials',
                    'post_status' => 'publish'
                );
                // Update post meta
                wp_update_post($post_data);
                update_post_meta($existing_post_id, 'testimonial_person_name', $record['PersonName']);
                update_post_meta($existing_post_id, 'star_rating', $record['StarRating']);
                update_post_meta($existing_post_id, 'testimonial_positions', $record['Designation']);

                // Update taxonomy
                // zl_update_testimonial_categories($existing_post_id, $record['Categories']);
            }else {
                $post_data = array(
                    'post_title'   => sanitize_text_field($record['Title']), // Title
                    'post_content' => sanitize_textarea_field($record['Content']), // Content
                    'post_status'  => 'publish',
                    'post_type'    => 'zl_testimonials',
                );
                // Insert the post into the database
                $post_id = wp_insert_post($post_data);
                if (!is_wp_error($post_id)) {
                    // Add custom fields / insert post meta (Fields)
                    update_post_meta($post_id, 'testimonial_person_name', sanitize_text_field($record['PersonName']));
                    update_post_meta($post_id, 'testimonial_positions', sanitize_text_field($record['Designation']));
                    $star_rating_numeric = $record['StarRating'];
                    $star_rating = sanitize_text_field($star_rating_numeric) . ':' . sanitize_text_field($star_rating_numeric);
                    update_post_meta($post_id, 'star_rating', $star_rating);

                    // Handle featured image
                    $image_url = esc_url_raw($record['FeaturedImageURL']);
                    if (!empty($image_url)) {
                        if (!function_exists('media_sideload_image')) {
                            require_once(ABSPATH . 'wp-admin/includes/media.php');
                            require_once(ABSPATH . 'wp-admin/includes/file.php');
                            require_once(ABSPATH . 'wp-admin/includes/image.php');
                        }

                        // Sideload image and set as featured image
                        $media = media_sideload_image($image_url, $post_id, null, 'id');
                        if (!is_wp_error($media)) {
                            set_post_thumbnail($post_id, $media);
                        }
                    }
                    // Handle categories
                    zl_update_testimonial_categories($post_id, $record['Categories']);
                }
            }
        }
        fclose($handle);
        echo '<br><br><div class="zl_success_import" style="font-size: 20px; color: green;">Testimonials imported successfully from CSV file.</div>';
    } else {
        echo '<div style="style="font-size: 15px; color: red;"">Error opening the file or File Not Found.</div>';
    }
}
// Handles for importing data of Wordpress XML
function zl_testimonials_import_from_wxr($file_path) {
    if (!file_exists($file_path)) {
        echo 'File not found.';
        return;
    }

    libxml_use_internal_errors(true);
    $xml_data = simplexml_load_file($file_path);

    if ($xml_data === false) {
        echo 'Invalid XML file.';
        foreach (libxml_get_errors() as $error) {
            echo $error->message;
        }
        return;
    }

    // Register namespaces
    $namespaces = $xml_data->getNamespaces(true);
    $thumbnail_urls = [];

    $rating_map = [
        'zero_star' => 0,
        'one_star' => 1,
        'two_star' => 2,
        'three_star' => 3,
        'four_star' => 4,
        'five_star' => 5,
    ];

    // Second pass to process each post
    foreach ($xml_data->channel->item as $item) {
        $namespaces = $item->getNamespaces(true);
        $wp = $item->children($namespaces['wp']);

        $post_title = (string) $item->title;
        $post_date = (string) $wp->post_date;
        $post_id = (string) $wp->post_id;
        $post_name = (string) $wp->post_name;
        $post_type = (string) $wp->post_type;

        if ($post_type === 'attachment') {
            continue;
        }

        $content = $item->children($namespaces['content']);
        $content_desc = (string) $content->encoded;

        $thumbnail_id = 0;
        $tpro_name = 'N/A';
        $tpro_designation = 'N/A';
        $tpro_rating = 0;

        // Check for _thumbnail_id and sp_tpro_meta_options
        foreach ($wp->postmeta as $postmeta) {
            $meta_key = (string) $postmeta->children($namespaces['wp'])->meta_key;
            $meta_value = (string) $postmeta->children($namespaces['wp'])->meta_value;

            if ($meta_key === '_thumbnail_id') {
                $thumbnail_id = (int) $meta_value;
                // break;
            } elseif ($meta_key === 'sp_tpro_meta_options') {
                $unserialized_data = unserialize(trim($meta_value));
                $tpro_name = $unserialized_data['tpro_name'] ?? 'N/A';
                $tpro_designation = $unserialized_data['tpro_designation'] ?? 'N/A';
                // $tpro_rating = $unserialized_data['tpro_rating'] ?? 0;
                 // Convert rating from descriptor to integer
                 $rating_descriptor = $unserialized_data['tpro_rating'] ?? '';
                 $tpro_rating = $rating_map[$rating_descriptor] ?? 0;
            }
        }
        
        $thumbnail_url = wp_get_attachment_url($thumbnail_id);

        // Prepare post data
        $post_data = array(
            'post_title'   => $post_title,
            'post_content' => $content_desc,
            'post_status'  => 'publish',
            'post_type'    => 'zl_testimonials',
        );

        // Insert or update the post
        $existing_post_id = zl_get_post_id_by_slug($post_name);
        if ($existing_post_id) {
            $post_data['ID'] = $existing_post_id;
            wp_update_post($post_data);
            $post_id = $existing_post_id;
        } else {
            $post_id = wp_insert_post($post_data);
        }

        // Update or set the featured image
        if ($thumbnail_url) {
            if (!function_exists('media_sideload_image')) {
                require_once(ABSPATH . 'wp-admin/includes/media.php');
                require_once(ABSPATH . 'wp-admin/includes/file.php');
                require_once(ABSPATH . 'wp-admin/includes/image.php');
            }

            // Check if the image already exists
            $current_thumbnail_id = get_post_thumbnail_id($post_id);
            if ($current_thumbnail_id) {
                // Remove the current featured image if it exists
                delete_post_thumbnail($post_id);
            }

            // Sideload image and set as featured image
            $media_id = media_sideload_image($thumbnail_url, $post_id, null, 'id');
            if (!is_wp_error($media_id)) {
                set_post_thumbnail($post_id, $media_id);
            } else {
                echo 'Error setting thumbnail: ' . $media_id->get_error_message();
            }
        }

        // Update post meta
        update_post_meta($post_id, 'testimonial_person_name', sanitize_text_field($tpro_name));
        update_post_meta($post_id, 'testimonial_positions', sanitize_text_field($tpro_designation));
        $star_rating_numeric = $tpro_rating;
        $star_rating = sanitize_text_field($star_rating_numeric) . ':' . sanitize_text_field($star_rating_numeric);
        update_post_meta($post_id, 'star_rating', $star_rating);

        // Debugging: Output each post's details
        // echo "<pre>";
        // echo "Post Title: $post_title\n";
        // echo "Post Name: $post_name\n";
        // echo "TPro Name: $tpro_name\n";
        // echo "TPro Designation: $tpro_designation\n";
        // echo "TPro Rating: $tpro_rating\n";
        // echo "Thumbnail URL: $thumbnail_url\n";       
        // echo "</pre>";
    }

    echo '<br><br><div class="zl_success_import" style="font-size: 20px; color: green;">Testimonials imported successfully from WordPress XML.</div>';
}


function zl_update_testimonial_categories($post_id, $categories) {
    // Initialize categories array
    $categories_array = array();
    
    if (is_object($categories)) {
        foreach ($categories as $category) {
            $categories_array[] = (string)$category;
        }
    } elseif (is_string($categories)) {
        $categories_array = explode(',', $categories);
    }

    $term_ids = array();
    foreach ($categories_array as $category_name) {
        $category_name = trim($category_name);
        if (!empty($category_name)) {
            $term = term_exists($category_name, 'testimonial-category');
            if (!$term) {
                $term = wp_insert_term($category_name, 'testimonial-category');
            }
            if (!is_wp_error($term)) {
                $term_id = is_array($term) ? $term['term_id'] : $term;
                $term_ids[] = $term_id;
            }
        }
    }
    if (!empty($term_ids)) {
        wp_set_post_terms($post_id, $term_ids, 'testimonial-category');
    }
}
// Function to get post ID by title slug
function zl_get_post_id_by_slug($slug) {
    $post = get_page_by_path($slug, OBJECT, 'zl_testimonials');
    return $post ? $post->ID : false;
}
function zl_export_custom_post_type_to_csv() {
    // Check for the export action
    if (!isset($_GET['export_custom_post_type']) || !current_user_can('manage_options')) {
        return;
    }
    // Set the headers to force download
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=custom_post_type_data.csv');

    // // Create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');

    // // Output the column headings
    fputcsv($output, array('ID', 'Title', 'Content', 'PersonName','FeaturedImageURL','StarRating','Designation','Categories'));
    
    // Initialize testimonials array
    $testimonials = array();
    
    $args = array(
        'post_type' => 'zl_testimonials',
        'posts_per_page' => -1,
    );
    $query = new WP_Query($args);

    // Loop through the posts and output the data
    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();
        $title = get_the_title();
        $content = get_the_content();
        $person_name_data = get_post_meta($post_id, 'testimonial_person_name', true);
        $designation = get_post_meta($post_id, 'testimonial_positions', true);
        $featured_image_url = get_the_post_thumbnail_url($post_id, 'full');
        $star_rating = get_post_meta($post_id, 'star_rating', true);
        $categories = wp_get_post_terms($post_id, 'testimonial-category', array('fields' => 'names'));
        $categories_string = implode(',', $categories);
        $testimonial = array(
            'post_id' => $post_id,
            'title' => $title,
            'content' => $content,
            'Person_name' => $person_name_data,
            'FeaturedImageURL'=> $featured_image_url,
            'StarRating' => $star_rating,
            'Designation' => $designation,
            'Categories' => $categories_string
        );    
        // Push the associative array into the main array
        $testimonials[] = $testimonial;
        fputcsv($output, array($post_id, $title, $content, $person_name_data,$featured_image_url,$star_rating,$designation,$categories_string));
    }
    // Close the file pointer
    fclose($output);
    exit();
}
add_action('init', 'zl_export_custom_post_type_to_csv');

function zl_save_testimonials_checkbox_selection($options, $values) {
    update_option('testimonials_checkbox_options', $options);
    update_option('testimonials_saved_form_labels', $values);
}
// Function to retrieve checkbox selection from the WordPress options
function zl_get_testimonials_checkbox_selection() {
    return get_option('testimonials_checkbox_options', array());
}

// Add action to save checkbox selection when form is submitted
add_action('admin_init', function() {
    if (isset($_POST['options']) || isset($_POST['title']) || isset($_POST['name']) || isset($_POST['designation']) || isset($_POST['categories']) || isset($_POST['upload_image']) || isset($_POST['rating']) || isset($_POST['testimonial_message']) || isset($_POST['submit_btn_text'])) {
        $options = isset($_POST['options']) ? $_POST['options'] : array();
        $values = array(
            'title' => sanitize_text_field($_POST['title']),
            'name' => sanitize_text_field($_POST['name']),
            'designation' => sanitize_text_field($_POST['designation']),
            'categories' => sanitize_text_field($_POST['categories']),
            'upload_image' => sanitize_text_field($_POST['upload_image']),
            'rating' => sanitize_text_field($_POST['rating']),
            'testimonial_message' => sanitize_text_field($_POST['testimonial_message']),
            'submit_btn_text' => sanitize_text_field($_POST['submit_btn_text'])
        );
        zl_save_testimonials_checkbox_selection($options, $values);
    }
});
function zl_custom_submission_form() {
    $checkbox_options = zl_get_testimonials_checkbox_selection();
    $saved_values = get_option('testimonials_saved_form_labels', array());
    ob_start(); // Start output buffering
    // if (session_status() === PHP_SESSION_NONE) {
    //     session_start();
    // }
    // Check for success message
    $success_message = get_transient('zl_testimonial_success_message');
    if ($success_message) {
        echo '<div class="zl-success-message" style="font-size: 22px !important; color: green;"><p>' . esc_html($success_message) . '</p></div>';
        delete_transient('zl_testimonial_success_message');
    }
    
    // Check for error message
    $error_message = get_transient('zl_testimonial_error_message');
    if ($error_message) {
        echo '<div class="zl-error-message" style="font-size: 20px; color: red;"><p>' . esc_html($error_message) . '</p></div>';
        delete_transient('zl_testimonial_error_message');
    } ?>
    <div class="zl_frontend_submission">
        <form id="zl_custom_form" action="" method="post" enctype="multipart/form-data">
            <div class="zl_fieldset">
                <?php if (in_array('Title', $checkbox_options)) :
                    $title_label = isset($saved_values['title']) ? $saved_values['title'] : 'Title';?>
                    <div class="zl_testimo_fieldgroup">
                        <label for="title_t"><?php echo esc_html($title_label); ?>:</label>
                        <input type="text" id="zl_title_t" name="title" >
                        <div class="zl-err-message"></div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('Name', $checkbox_options)) :
                    $name_label = isset($saved_values['name']) ? $saved_values['name'] : 'Name'; ?>
                    <div class="zl_testimo_fieldgroup">
                        <label for="name"><?php echo esc_html($name_label); ?>:</label>
                        <input type="text" id="zl_name_t" name="testimonial_person_name">
                        <div class="zl-err-message"></div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('Designation', $checkbox_options)) : 
                    $designation_label = isset($saved_values['designation']) ? $saved_values['designation'] : 'Designation';?>
                    <div class="zl_testimo_fieldgroup">
                        <label for="designation_t"><?php echo esc_html($designation_label); ?>:</label>
                        <input type="text" id="zl_designation_t" name="testimonial_positions" >
                        <div class="zl-err-message"></div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('Categories', $checkbox_options)) : 
                    $categories_label = isset($saved_values['categories']) ? $saved_values['categories'] : 'Categories';?>
                    <div class="zl_testimo_fieldgroup">
                        <label for="categories_t"><?php echo esc_html($categories_label); ?>:</label>
                        <?php
                        $testimonial_categories = get_terms(array(
                            'taxonomy' => 'testimonial-category',
                            'hide_empty' => false, // Set to true if you want to hide empty categories
                        ));
                        // Display checkboxes
                        foreach ($testimonial_categories as $category) {
                            echo '<div class="zl-category-checkbox">';
                            echo '<input type="checkbox" id="category_' . esc_attr($category->term_id) . '" name="testimonial-category[]" value="' . esc_attr($category->term_id) . '">';
                            echo '<label for="category_' . esc_attr($category->term_id) . '">' . esc_html($category->name) . '</label>';
                            echo '</div>';
                        } ?>
                    </div>
                <?php endif; ?>
                <?php if (in_array('Upload Image', $checkbox_options)) : 
                    $upload_image_label = isset($saved_values['upload_image']) ? $saved_values['upload_image'] : 'Upload Image';?>
                    <div class="zl_testimo_fieldgroup">
                        <label for="user_profile_t"><?php echo esc_html($upload_image_label); ?>: <span class="zl_form_msg"><?php esc_html_e('(image size must be exactly 250x250 pixels)', 'easy-testimonials-carousel'); ?> </span></label>
                        <input type="file" name="testimonial_image" id="zl_user_profile_t" accept="image/*">
                        <div class="zl-err-message"></div>
                    </div>
                <?php endif; ?>
                <?php if (in_array('Rating', $checkbox_options)) : 
                    $rating_label = isset($saved_values['rating']) ? $saved_values['rating'] : 'Rating';?>
                    <div class="zl_testimo_fieldgroup">
                        <label for="rating"><?php echo esc_html($rating_label); ?>:</label>
                        <select class="zl_rating_t" name="star_rating">
                            <option value="1:1"><?php esc_html_e('1 star', 'easy-testimonials-carousel'); ?></option>
                            <option value="2:2"><?php esc_html_e('2 star', 'easy-testimonials-carousel'); ?></option>
                            <option value="3:3"><?php esc_html_e('3 star', 'easy-testimonials-carousel'); ?></option>
                            <option value="4:4"><?php esc_html_e('4 star', 'easy-testimonials-carousel'); ?></option>
                            <option value="5:5"><?php esc_html_e('5 star', 'easy-testimonials-carousel'); ?></option>
                        </select>
                        <div class="zl-err-message"></div> 
                    </div>
                <?php endif; ?>
                <?php if (in_array('Testimonial Message', $checkbox_options)) :
                    $testimonial_message_label = isset($saved_values['testimonial_message']) ? $saved_values['testimonial_message'] : 'Testimonial Message'; ?>
                    <div class="zl_testimo_fieldgroup">
                        <label for="message"><?php echo esc_html($testimonial_message_label); ?>:</label>
                        <textarea id="zl_message" rows="8" cols="5" name="testimo_message"></textarea>
                        <div class="zl-err-message"></div>
                    </div>
                <?php endif; ?>
                <?php $submit_btn_text = isset($saved_values['submit_btn_text']) ? $saved_values['submit_btn_text'] : 'Submit'; ?>
                <div class="zl_testimo_fieldgroup">
                    <input type="submit" name="custom_submit_form" value="<?php echo esc_html($submit_btn_text); ?>">
                </div>
            </div>
        </form>
    </div>
<?php return ob_get_clean(); // Return the buffered content
}

function zl_handle_custom_form_submission() {
    if (isset($_POST['custom_submit_form'])) {
        // Sanitize and process the form data
        $testimonial_person_name = sanitize_text_field($_POST['testimonial_person_name']);
        $title = sanitize_text_field($_POST['title']);
        $testimonial_positions = sanitize_text_field($_POST['testimonial_positions']);
        $testimo_message = sanitize_textarea_field($_POST['testimo_message']);
        $star_rating = sanitize_textarea_field($_POST['star_rating']);
        $categories = isset($_POST['testimonial-category']) ? array_map('intval', $_POST['testimonial-category']) : [];
        $post_status = is_user_logged_in() ? 'publish' : 'pending';
        $testimonial_post = array(
            'post_title'    => wp_strip_all_tags($title),
            'post_content'  => $testimo_message,
            'post_status'   => 'publish',
            'post_type'     => 'zl_testimonials',
            'tax_input'     => array('testimonial-category' => $categories)
        );

        // Insert the post into the database save post testimonial
        $post_id = wp_insert_post($testimonial_post);
        if ($post_id) {
            wp_set_object_terms($post_id, $categories, 'testimonial-category');
            add_post_meta($post_id, 'testimonial_person_name', $testimonial_person_name, true);
            add_post_meta($post_id, 'testimonial_positions', $testimonial_positions, true);
            add_post_meta($post_id, 'star_rating', $star_rating, true);
               // Check if an image was uploaded
            if (!empty($_FILES['testimonial_image']['name'])) {
                $uploaded_file = $_FILES['testimonial_image'];
               
                if ($uploaded_file['error'] == 0) {
                    if (!function_exists('wp_handle_upload')) {
                        require_once(ABSPATH . 'wp-admin/includes/file.php');
                    }
                    $upload = wp_handle_upload($uploaded_file, array('test_form' => false));
                    if (isset($upload['file'])) {
                        if (!function_exists('wp_generate_attachment_metadata')) {
                            require_once(ABSPATH . 'wp-admin/includes/image.php');
                        }
                        if (!function_exists('media_handle_upload')) {
                            require_once(ABSPATH . 'wp-admin/includes/media.php');
                        }
                        // Attach the uploaded image to the post and set it as the featured image
                        $attachment = array(
                            'guid'           => $upload['url'],
                            'post_mime_type' => $upload['type'],
                            'post_title'     => sanitize_file_name($uploaded_file['name']),
                            'post_content'   => '',
                            'post_status'    => 'inherit'
                        );
                        // Insert the attachment
                        $attach_id = wp_insert_attachment($attachment, $upload['file'], $post_id);
                        // Generate attachment metadata and update the database record
                        $attach_data = wp_generate_attachment_metadata($attach_id, $upload['file']);
                        wp_update_attachment_metadata($attach_id, $attach_data);
                        // Set the attachment as the featured image for the post
                        set_post_thumbnail($post_id, $attach_id);
                    }else{
                        set_transient('zl_testimonial_error_message', 'There was an error uploading your image. Your testimonial has been submitted without an image.', 30);
                    }
                }else {
                    // Handle upload error
                    set_transient('zl_testimonial_error_message', 'There was an error with the file upload. Your testimonial has been submitted without an image.', 30);
                }
            }
            // Output a thank you message
            set_transient('zl_testimonial_success_message', 'Thank you, ' . esc_html($testimonial_person_name) . '. Your testimonial has been saved.', 30);
        } else {
            set_transient('zl_testimonial_error_message', 'There was an error submitting your testimonial. Please try again later.', 30);
            // echo '<p>There was an error submitting your testimonial. Please try again later.</p>';
        }
        // Redirect to the same page to avoid resubmission on refresh
        wp_redirect(esc_url_raw($_SERVER['REQUEST_URI']));
        exit;
    }
}
add_action('wp', 'zl_handle_custom_form_submission');

// Submission form custom shortcode 
function zl_custom_form_shortcode() {
    return zl_custom_submission_form();
}
add_shortcode('frontend_submission_form', 'zl_custom_form_shortcode');
