<?php
/**
 * Name: Featured Image Column
 * Description: Show featured image thumbnail column in posts and pages list for quick visual identification of content with images and finding posts without featured images
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Visual Product Catalog",
 *     "description": "See product images directly in WooCommerce products list to quickly verify correct images are assigned without opening each product."
 *   },
 *   {
 *     "title": "Blog Post Thumbnails",
 *     "description": "Visual confirmation that every blog post has a featured image before publishing, catching missing images at a glance."
 *   },
 *   {
 *     "title": "Portfolio Management",
 *     "description": "Preview project thumbnails in custom post type list when managing portfolio items or case studies with visual components."
 *   }
 * ]
 * Added: 2025-08-10
 * Tags: featured-image, thumbnail, column, admin, list
 * WhereToFind: {
 *   "location": "Posts/Pages List Tables",
 *   "url": "edit.php",
 *   "instructions": "Featured image thumbnails will appear in a new column",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardFeaturedImageColumn {
    
    public function __construct() {
        // Add column to posts
        add_filter('manage_posts_columns', [$this, 'add_image_column']);
        add_action('manage_posts_custom_column', [$this, 'display_image_column'], 10, 2);
        
        // Add column to pages
        add_filter('manage_pages_columns', [$this, 'add_image_column']);
        add_action('manage_pages_custom_column', [$this, 'display_image_column'], 10, 2);
        
        // Add CSS for proper sizing
        add_action('admin_head', [$this, 'add_column_css']);
    }
    
    public function add_image_column($columns) {
        // Insert featured image column after checkbox
        $new_columns = [];
        foreach ($columns as $key => $value) {
            $new_columns[$key] = $value;
            if ($key === 'cb') {
                $new_columns['featured_image'] = 'Image';
            }
        }
        return $new_columns;
    }
    
    public function display_image_column($column_name, $post_id) {
        if ($column_name === 'featured_image') {
            if (has_post_thumbnail($post_id)) {
                echo get_the_post_thumbnail($post_id, [50, 50]);
            } else {
                echo '<span style="color: #999;">—</span>';
            }
        }
    }
    
    public function add_column_css() {
        ?>
        <style>
        .column-featured_image {
            width: 60px;
            text-align: center;
        }
        .column-featured_image img {
            width: 50px;
            height: 50px;
            object-fit: cover;
            border-radius: 4px;
            display: block;
            margin: 0 auto;
        }
        </style>
        <?php
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }
}

$switchboard_instance = new SwitchboardFeaturedImageColumn();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
