<?php
// Custom Post Type :: SLIDING PHOTOS AND VIDEOS
function custom_post_type_slides() {
    $labels = array(
        'name'                => __('Slides', 'kipya'),
        'add_new_item'        => __('Add New Slide', 'kipya'),
        'add_new'             => __('Add New Slide', 'kipya'),
        'edit_item'           => __('Edit Slide', 'kipya'),
        'update_item'         => __('Update Slide', 'kipya'),
        'all_items'           => __('All Slides', 'kipya'),
        'search_items'        => __('Search Slide', 'kipya'),
        'singular_name'       => __('Slide'),
    );
    $supports = array(
        'title',        // Post title
        'excerpt',      // Allows short description
        'thumbnail',    // Allows featured images
    );
    $args = array(
        'labels'              => $labels,
        'description'         => 'Post type for sliding photos and videos',
        'supports'            => $supports,
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 6,
        'menu_icon'           => 'dashicons-format-gallery',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'capability_type'     => 'post',
    );
    register_post_type('slides', $args);
}
add_action('init', 'custom_post_type_slides');

// Add a Meta Box for Video URL
function add_video_url_meta_box() {
    add_meta_box(
        'video_url_meta_box',
        __('Slide Video URL', 'kipya'),
        'render_video_url_meta_box',
        'slides',
        'normal',
        'high'
    );
}
add_action('add_meta_boxes', 'add_video_url_meta_box');

// Render the Meta Box
function render_video_url_meta_box($post) {
    // Retrieve the existing value from the database
    $video_url = get_post_meta($post->ID, '_slide_video_url', true);
    ?>
    <label for="slide_video_url"><?php _e('Video URL (e.g., YouTube, Vimeo):', 'kipya'); ?></label>
    <input type="url" id="slide_video_url" name="slide_video_url" value="<?php echo esc_attr($video_url); ?>" style="width: 100%; margin-top: 8px;">
    <p class="description"><?php _e('Add a video URL if this slide should display a video.', 'kipya'); ?></p>
    <?php
}

// Save the Video URL Meta Box Data
function save_video_url_meta_box($post_id) {
    if (isset($_POST['slide_video_url'])) {
        $video_url = sanitize_text_field($_POST['slide_video_url']);
        update_post_meta($post_id, '_slide_video_url', $video_url);
    }
}
add_action('save_post', 'save_video_url_meta_box');

// Modify the Admin Dashboard Columns
function add_custom_columns_to_slides($columns) {
    $new_columns = array(
        'slide_media'       => 'Media',
        'title'             => 'Title',
        'slide_description' => 'Description',
    );

    unset($columns['date']); // Remove default date column
    $columns = array_merge($new_columns, $columns);
    return $columns;
}
add_filter('manage_edit-slides_columns', 'add_custom_columns_to_slides');

// Populate the Custom Columns
function populate_custom_columns_slides($column, $post_id) {
    switch ($column) {
        case 'slide_media':
            // Check if there's a video URL
            $video_url = get_post_meta($post_id, '_slide_video_url', true);
            if ($video_url) {
                echo '<a href="' . esc_url($video_url) . '" target="_blank">Video</a>';
            } else {
                // Display the featured image if no video
                $photo_url = get_the_post_thumbnail_url($post_id, 'thumbnail');
                if ($photo_url) {
                    echo '<img src="' . esc_url($photo_url) . '" alt="Slide Media" style="max-height: 80px; width: auto;" />';
                } else {
                    echo 'No media available';
                }
            }
            break;

        case 'slide_description':
            echo esc_html(get_the_excerpt($post_id));
            break;
    }
}
add_action('manage_slides_posts_custom_column', 'populate_custom_columns_slides', 10, 2);
