<?php
/**
 * Name: Estimated Publish Date
 * Description: Show how long ago a post was published in human-readable relative format like five days ago or two months ago for content freshness
 * Category: Content
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Content Calendar Planning",
 *     "description": "Track estimated publish dates for 30+ draft articles in editorial calendar without committing to scheduled publish."
 *   },
 *   {
 *     "title": "Freelancer Deadlines",
 *     "description": "Show contributors when their drafts are expected to be published for planning their content pipeline."
 *   },
 *   {
 *     "title": "Client Approval Workflow",
 *     "description": "Display target publish dates while content awaits client review before scheduling actual publication."
 *   }
 * ]
 * Added: 2025-08-06
 * Tags: date, time, published, human-readable, ux
 * WhereToFind: {
 *   "location": "Frontend (Posts with Shortcode)",
 *   "url": "/",
 *   "instructions": "Add [human_date] shortcode to see '5 days ago' style dates",
 *   "newTab": true
 * }
 */

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

class SwitchboardEstimatedPublishDate {

    public function __construct() {
        add_filter('the_date', [$this, 'human_readable_date'], 10, 4);
        add_filter('get_the_date', [$this, 'human_readable_date'], 10, 3);
        add_shortcode('human_date', [$this, 'human_date_shortcode']);
    }

    /**
     * Register data locations for cleanup system
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }
    
    public function human_readable_date($the_date, $format = '', $post = null) {
        if (!is_singular() || is_admin()) {
            return $the_date;
        }
        
        $post = get_post($post);
        if (!$post) {
            return $the_date;
        }
        
        $post_time = strtotime($post->post_date);
        $current_time = current_time('timestamp');
        $time_diff = $current_time - $post_time;
        
        // If less than 30 days, show relative time
        if ($time_diff < 30 * DAY_IN_SECONDS) {
            // Use mysql2date to avoid triggering the filter recursively
            $original_date = mysql2date(get_option('date_format'), $post->post_date);
            return sprintf(
                '<span title="%s">%s</span>',
                esc_attr($original_date),
                /* translators: %s: human-readable time difference */
                sprintf(esc_html__('%s ago', 'wpswitchboard'), human_time_diff($post_time, $current_time))
            );
        }
        
        return $the_date;
    }
    
    public function human_date_shortcode($atts) {
        global $post;

        if (!$post || !is_object($post) || !isset($post->post_date)) {
            return '';
        }

        $post_time = strtotime($post->post_date);
        $current_time = current_time('timestamp');

        /* translators: %s: human-readable time difference */
        return sprintf(esc_html__('%s ago', 'wpswitchboard'), human_time_diff($post_time, $current_time));
    }
}

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