<?php 
// Custom Post Type :: SLIDING PHOTOS (with Custom Link)

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',
        'excerpt',
        'thumbnail',
    );
    
    $args = array(
        'labels'              => $labels,
        'description'         => 'Post type for sliding photos',
        '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_meta_box_cb' => 'add_slide_meta_boxes' // Add meta boxes
    );
    
    register_post_type('slides', $args);
}
add_action('init', 'custom_post_type_slides');

// Add meta boxes for slide link
function add_slide_meta_boxes() {
    add_meta_box(
        'slide_link_meta_box',
        'Slide Link',
        'render_slide_link_meta_box',
        'slides',
        'normal',
        'high'
    );
}

// Render the slide link meta box
function render_slide_link_meta_box($post) {
    // Get existing value
    $slide_link = get_post_meta($post->ID, '_slide_link', true);
    
    // Nonce for security
    wp_nonce_field('slide_link_nonce', 'slide_link_nonce');
    ?>
    <p>
        <label for="slide_link">Slide Link URL:</label><br>
        <input type="url" id="slide_link" name="slide_link" value="<?php echo esc_url($slide_link); ?>" style="width: 100%;" placeholder="https://example.com">
        <small>Leave empty if the slide shouldn't link anywhere</small>
    </p>
    <?php
}

// Save slide link data
function save_slide_link_data($post_id) {
    // Check nonce
    if (!isset($_POST['slide_link_nonce']) || !wp_verify_nonce($_POST['slide_link_nonce'], 'slide_link_nonce')) {
        return;
    }
    
    // Check user permissions
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    
    // Save/update the link
    if (isset($_POST['slide_link'])) {
        update_post_meta($post_id, '_slide_link', esc_url_raw($_POST['slide_link']));
    } else {
        delete_post_meta($post_id, '_slide_link');
    }
}
add_action('save_post_slides', 'save_slide_link_data');

// Add custom columns to the SLIDING PHOTOS - DASHBOARD
function add_custom_columns_to_slides($columns) {
    $new_columns = array(
        'slide_photo'       => 'Photo',
        'title'             => 'Title',
        'slide_link'        => 'Link',
        'slide_description' => 'Description',
    );

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

// Populate custom columns
function populate_custom_columns_slides($column, $post_id) {
    switch ($column) {
        case 'slide_photo':
            $photo_url = get_the_post_thumbnail_url($post_id, 'thumbnail');
            echo $photo_url ? '<img src="' . esc_url($photo_url) . '" alt="Slide Photo" style="max-height: 80px; width: auto;" />' : 'No photo';
            break;
            
        case 'slide_link':
            $link = get_post_meta($post_id, '_slide_link', true);
            if ($link) {
                echo '<a href="' . esc_url($link) . '" target="_blank">' . esc_html($link) . '</a>';
            } else {
                echo 'No link';
            }
            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);

// Make link column sortable
function make_slide_link_column_sortable($columns) {
    $columns['slide_link'] = 'slide_link';
    return $columns;
}
add_filter('manage_edit-slides_sortable_columns', 'make_slide_link_column_sortable');

// Handle sorting by link
function handle_slide_link_sorting($query) {
    if (!is_admin() || !$query->is_main_query()) return;
    
    if ($query->get('orderby') === 'slide_link') {
        $query->set('meta_key', '_slide_link');
        $query->set('orderby', 'meta_value');
    }
}
add_action('pre_get_posts', 'handle_slide_link_sorting');