<?php
/*
Plugin Name: CodeWP Copyright Year Shortcode
Plugin URI: https://codewp.ai
Description: A flexible shortcode to display copyright years in WordPress posts and pages.
Version: 1.0
Author: CodeWP
Author URI: https://codewp.ai
License: GPLv2 or later
Text Domain: codewp-copyright-year
*/

// Register a new shortcode: [cwpai_cr_year]
add_shortcode( 'cwpai_cr_year', 'cwpai_copyright_year_func' );

function cwpai_copyright_year_func( $atts ) {
    // Attributes with more options
    $atts = shortcode_atts(
        array(
            'start_year' => '',
            'separator' => '-',
            'format' => 'Y', // PHP date format
            'before_text' => '',
            'after_text' => '',
            'display_condition' => 'always' // Options: always, past, future
        ),
        $atts,
        'cwpai_cr_year'
    );

    $current_year = date($atts['format']);
    $output = '';

    // Conditional display
    switch ($atts['display_condition']) {
        case 'past':
            if ( !empty($atts['start_year']) && $atts['start_year'] < $current_year ) {
                $output = "{$atts['start_year']}{$atts['separator']}{$current_year}";
            }
            break;
        case 'future':
            if ( !empty($atts['start_year']) && $atts['start_year'] > $current_year ) {
                $output = "{$atts['start_year']}{$atts['separator']}{$current_year}";
            }
            break;
        default: // 'always'
            $output = !empty($atts['start_year']) && $atts['start_year'] < $current_year
                ? "{$atts['start_year']}{$atts['separator']}{$current_year}"
                : $current_year;
            break;
    }

    // Adding before and after text
    return $atts['before_text'] . $output . $atts['after_text'];
}
?>