<?php
/**
 * Name: Show IDs
 * Description: Display post, page, category, tag, and user ID numbers in admin tables for developers needing IDs for custom queries, shortcodes, or theme development
 * Category: Admin
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Development and Debugging",
 *     "description": "Quickly copy post, page, and category IDs when writing custom queries or troubleshooting template issues without inspecting source code."
 *   },
 *   {
 *     "title": "Shortcode Development",
 *     "description": "Easily reference post/page IDs when building custom shortcodes that require specific content IDs as parameters."
 *   },
 *   {
 *     "title": "API Integration Work",
 *     "description": "Grab user, term, and post IDs for REST API calls or third-party integrations without running database queries."
 *   }
 * ]
 * WhereToFind: {
 *   "location": "Posts/Pages/Users Lists",
 *   "url": "edit.php",
 *   "instructions": "ID column appears in posts, pages, users, and taxonomy lists for easy reference",
 *   "newTab": false
 * }
 * Added: 2025-06-12
 * Tags: id, identifier, admin, columns, posts, pages, users, taxonomy
 */

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

class SwitchboardShowIds {
    
    public function __construct() {
        add_action('admin_init', [$this, 'add_id_columns']);
        add_action('admin_head', [$this, 'add_column_styles']);
    }
    
    public function add_column_styles() {
        echo '<style>
            .column-switchboard_id { width: 60px !important; }
            .column-switchboard_id strong { font-family: monospace; }
        </style>';
    }
    
    public function add_id_columns() {
        // Posts and Pages
        add_filter('manage_posts_columns', [$this, 'add_id_column']);
        add_filter('manage_pages_columns', [$this, 'add_id_column']);
        add_action('manage_posts_custom_column', [$this, 'show_id_column'], 10, 2);
        add_action('manage_pages_custom_column', [$this, 'show_id_column'], 10, 2);
        
        // Categories and Tags
        add_filter('manage_edit-category_columns', [$this, 'add_id_column']);
        add_filter('manage_edit-post_tag_columns', [$this, 'add_id_column']);
        add_action('manage_category_custom_column', [$this, 'show_taxonomy_id_column'], 10, 3);
        add_action('manage_post_tag_custom_column', [$this, 'show_taxonomy_id_column'], 10, 3);
        
        // Users
        add_filter('manage_users_columns', [$this, 'add_id_column']);
        add_action('manage_users_custom_column', [$this, 'show_user_id_column'], 10, 3);
        
        // Media
        add_filter('manage_media_columns', [$this, 'add_id_column']);
        add_action('manage_media_custom_column', [$this, 'show_id_column'], 10, 2);
        
        // Custom Post Types
        $post_types = get_post_types(['public' => true, '_builtin' => false]);
        if (is_array($post_types) && !empty($post_types)) {
            foreach ($post_types as $post_type) {
                add_filter("manage_{$post_type}_posts_columns", [$this, 'add_id_column']);
                add_action("manage_{$post_type}_posts_custom_column", [$this, 'show_id_column'], 10, 2);
            }
        }
    }
    
    public function add_id_column($columns) {
        $new_columns = [];
        foreach ($columns as $key => $value) {
            if ($key === 'title' || $key === 'name' || $key === 'username') {
                $new_columns['switchboard_id'] = 'ID';
            }
            $new_columns[$key] = $value;
        }
        return $new_columns;
    }
    
    public function show_id_column($column, $post_id) {
        if ($column === 'switchboard_id') {
            echo '<strong>' . esc_html($post_id) . '</strong>';
        }
    }
    
    public function show_taxonomy_id_column($content, $column, $term_id) {
        if ($column === 'switchboard_id') {
            return '<strong>' . esc_html($term_id) . '</strong>';
        }
        return $content;
    }
    
    public function show_user_id_column($content, $column, $user_id) {
        if ($column === 'switchboard_id') {
            return '<strong>' . esc_html($user_id) . '</strong>';
        }
        return $content;
    }

    /**
     * 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 SwitchboardShowIds();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);