<?php
/**
 * Name: Custom Excerpt Length
 * Description: Control excerpt length in words and customize read more link text for blog archives, category pages, and search results to match design requirements
 * Category: Utilities
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Perfect Blog Previews",
 *     "description": "Set excerpt to exactly 25 words for blog archives instead of WordPress default 55 to match your design layout."
 *   },
 *   {
 *     "title": "Custom Read More Text",
 *     "description": "Change '[...]' to 'Continue Reading' or clickable link to improve call-to-action on post previews."
 *   },
 *   {
 *     "title": "Mobile-Optimized Excerpts",
 *     "description": "Reduce excerpt length to 15 words for cleaner mobile blog listings without editing theme code."
 *   }
 * ]
 * Added: 2025-08-18
 * Tags: excerpt, length, read-more, content, customization
 * WhereToFind: {
 *   "location": "Configure in module settings",
 *   "url": "",
 *   "instructions": "Set excerpt length and read more text - applies to archives",
 *   "newTab": false
 * }
 */

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

class SwitchboardCustomExcerptLength {
    
    public function __construct() {
        add_filter('excerpt_length', [$this, 'custom_excerpt_length'], 999);
        add_filter('excerpt_more', [$this, 'custom_excerpt_more']);
    }
    
    public function custom_excerpt_length($length) {
        $settings = get_option('switchboard_custom-excerpt-length_settings', []);
        return isset($settings['excerpt_length']) ? intval($settings['excerpt_length']) : 55;
    }
    
    public function custom_excerpt_more($more) {
        global $post;

        // Validate $post object exists
        if (!$post || !is_object($post)) {
            return $more;
        }

        $settings = get_option('switchboard_custom-excerpt-length_settings', []);
        $custom_more = isset($settings['excerpt_more']) ? $settings['excerpt_more'] : '[...]';

        // Replace %link% with actual post link
        if (strpos($custom_more, '%link%') !== false) {
            $link = '<a href="' . esc_url(get_permalink($post->ID)) . '">' . esc_html__('Read More', 'wpswitchboard') . '</a>';
            $custom_more = str_replace('%link%', $link, $custom_more);
        }

        return ' ' . $custom_more;
    }

    /**
     * Render settings panel
     */
    public function render_settings($settings = []) {
        $excerpt_length = isset($settings['excerpt_length']) ? intval($settings['excerpt_length']) : 55;
        $excerpt_more = isset($settings['excerpt_more']) ? $settings['excerpt_more'] : '[...]';
        ?>
        <input type="hidden" name="module" value="custom-excerpt-length">

        <div class="sb-sp-group">
            <label for="setting_excerpt_length" class="sb-sp-label"><?php esc_html_e('Excerpt Length (Words)', 'wpswitchboard'); ?></label>
            <input type="number" name="setting_excerpt_length" id="setting_excerpt_length" value="<?php echo esc_attr($excerpt_length); ?>" min="10" max="200" class="sb-sp-select" style="width: 150px;">
            <p class="sb-sp-description"><?php esc_html_e('Number of words to display in excerpts. Default: 55', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-group">
            <label for="setting_excerpt_more" class="sb-sp-label"><?php esc_html_e('Read More Text', 'wpswitchboard'); ?></label>
            <input type="text" name="setting_excerpt_more" id="setting_excerpt_more" value="<?php echo esc_attr($excerpt_more); ?>" class="sb-sp-select" style="width: 100%; max-width: 400px;" placeholder="[...]">
            <p class="sb-sp-description"><?php esc_html_e('Text shown after excerpt. Use %link% to create clickable "Read More" link.', 'wpswitchboard'); ?></p>
        </div>

        <div class="sb-sp-info-box sb-sp-info-box-info">
            <h4 class="sb-sp-heading-sm"><?php esc_html_e('Usage Examples', 'wpswitchboard'); ?></h4>
            <ul class="sb-sp-list">
                <li class="sb-sp-list-item">
                    <strong><?php esc_html_e('Simple ellipsis:', 'wpswitchboard'); ?></strong>
                    <code style="margin-left: 8px; padding: 2px 8px; background: #f3f4f6; border-radius: 4px; font-family: monospace; font-size: 12px;">...</code>
                    <span style="margin-left: 8px; color: #64748b;">→ <?php esc_html_e('Displays: "... and more content..."', 'wpswitchboard'); ?></span>
                </li>
                <li class="sb-sp-list-item">
                    <strong><?php esc_html_e('Custom text:', 'wpswitchboard'); ?></strong>
                    <code style="margin-left: 8px; padding: 2px 8px; background: #f3f4f6; border-radius: 4px; font-family: monospace; font-size: 12px;">[Continue Reading]</code>
                    <span style="margin-left: 8px; color: #64748b;">→ <?php esc_html_e('Displays: "... [Continue Reading]"', 'wpswitchboard'); ?></span>
                </li>
                <li class="sb-sp-list-item">
                    <strong><?php esc_html_e('Clickable link:', 'wpswitchboard'); ?></strong>
                    <code style="margin-left: 8px; padding: 2px 8px; background: #f3f4f6; border-radius: 4px; font-family: monospace; font-size: 12px;">%link%</code>
                    <span style="margin-left: 8px; color: #64748b;">→ <?php esc_html_e('Creates clickable "Read More" link to full post', 'wpswitchboard'); ?></span>
                </li>
                <li class="sb-sp-list-item">
                    <strong><?php esc_html_e('Combined:', 'wpswitchboard'); ?></strong>
                    <code style="margin-left: 8px; padding: 2px 8px; background: #f3f4f6; border-radius: 4px; font-family: monospace; font-size: 12px;">... %link%</code>
                    <span style="margin-left: 8px; color: #64748b;">→ <?php esc_html_e('Displays: "... <Read More link>"', 'wpswitchboard'); ?></span>
                </li>
            </ul>
        </div>

        <div class="sb-sp-info-box sb-sp-info-box-tip">
            <p class="sb-sp-description" style="margin: 0;">
                <strong><?php esc_html_e('Tip:', 'wpswitchboard'); ?></strong>
                <?php esc_html_e('This applies to automatically generated excerpts. Manually written excerpts (in post editor) are not affected by word count but will use your custom "Read More" text.', 'wpswitchboard'); ?>
            </p>
        </div>

        <div class="sb-sp-info-box sb-sp-info-box-tip">
            <p class="sb-sp-description" style="margin: 0;">
                <strong><?php esc_html_e('Where It Appears:', 'wpswitchboard'); ?></strong>
                <?php esc_html_e('Blog archives, category pages, search results, and anywhere WordPress displays post excerpts (excluding custom post types unless theme supports it).', 'wpswitchboard'); ?>
            </p>
        </div>
        <?php
    }

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

// Initialize and register with data system
$switchboard_instance = new SwitchboardCustomExcerptLength();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
