<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package VisualNews
 */

/**
 * Add customizer default values.
 *
 * @param array $default_options
 * @return array
 */
function visualnews_customizer_add_defaults( $default_options) {
	$defaults = array(
		// Excerpt Options
		'visualnews_excerpt_length'    => 30,
	);


	$updated_defaults = wp_parse_args( $defaults, $default_options );

	return $updated_defaults;
}
add_filter( 'visualnews_customizer_defaults', 'visualnews_customizer_add_defaults' );

/**
 * Returns theme mod value saved for option merging with default option if available.
 * @since 1.0
 */
function visualnews_gtm( $option ) {
	// Get our Customizer defaults
	$defaults = apply_filters( 'visualnews_customizer_defaults', true );

	return isset( $defaults[ $option ] ) ? get_theme_mod( $option, $defaults[ $option ] ) : get_theme_mod( $option );
}

if ( ! function_exists( 'visualnews_excerpt_length' ) ) :
	/**
	 * Sets the post excerpt length to n words.
	 *
	 * function tied to the excerpt_length filter hook.
	 * @uses filter excerpt_length
	 */
	function visualnews_excerpt_length( $length ) {
		if ( is_admin() ) {
			return $length;
		}

		// Getting data from Theme Options
		$length	= visualnews_gtm( 'visualnews_excerpt_length' );

		return absint( $length );
	} // visualnews_excerpt_length.
endif;
add_filter( 'excerpt_length', 'visualnews_excerpt_length', 999 );

/*
 * Remove parentheses from categories widget
 */
function visualnewscategories_postcount_filter ($variable) {
   $variable = str_replace('(', '<span class="post-count"> ', $variable);
   $variable = str_replace(')', ' </span>', $variable);
   return $variable;
}
add_filter('wp_list_categories','visualnewscategories_postcount_filter');

/**
 * Customize excerpt more.
 */
if ( ! function_exists( 'visualnews_excerpt_more' ) ) :

function visualnews_excerpt_more( $more ) {
    if ( is_admin() ) {
        return $more;
    } else {
        return '... ';
    }
}
add_filter( 'excerpt_more', 'visualnews_excerpt_more' );

endif;

/**
 * Pro upgrade notice
 */
function visualnews_pro_upgrade_admin_notice() {

    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }
    ?>
    <div class="notice notice-info is-dismissible visualnews-pro-notice visualnews-pro-animate">
        <div class="">

            <div class="visualnews-pro-header">
                <span class="dashicons dashicons-star-filled"></span>
                <strong class="visualnews-pro-title">
                    <?php esc_html_e( 'Upgrade to VisualNews Pro', 'visualnews' ); ?>
                </strong>
            </div>
            <p class="visualnews-pro-desc">
                <?php esc_html_e( 'You are currently using the free version of the VisualNews theme. Upgrade to the Pro version to unlock additional features:', 'visualnews' ); ?>
            </p>
            <ul class="visualnews-pro-features">
                <li><?php esc_html_e( 'Theme Options Panel', 'visualnews' ); ?></li>
                <li><?php esc_html_e( 'One Click Demo Import', 'visualnews' ); ?></li>
                <li><?php esc_html_e( '1000+ Google Fonts', 'visualnews' ); ?></li>
                <li><?php esc_html_e( 'Sticky Header/Navigation', 'visualnews' ); ?></li>
                <li><?php esc_html_e( 'Header Search', 'visualnews' ); ?></li>
                <li><?php esc_html_e( 'Single Post Options', 'visualnews' ); ?></li>
                <li><?php esc_html_e( 'Priority Theme Support and More', 'visualnews' ); ?></li>                
            </ul>

            <div class="cta-button">
                <?php 
                    $theme = wp_get_theme();
                ?>
                <a href="<?php echo esc_url( $theme->get( 'AuthorURI' ) . '/themes/visualnews-pro' ); ?>"
                   class="button button-primary visualnews-pro-btn"
                   target="_blank"
                   rel="noopener noreferrer">
                    <?php esc_html_e( 'Upgrade to VisualNews Pro', 'visualnews' ); ?>
                </a>
            </div>

        </div>
    </div>
    <?php
}
add_action( 'admin_notices', 'visualnews_pro_upgrade_admin_notice' );

add_action( 'admin_enqueue_scripts', function () {
    wp_add_inline_style(
        'wp-admin',
        '
        .visualnews-pro-notice {
            padding: 0;
            border-left: none;
            background: transparent;
        }

        .visualnews-pro-animate {
            position: relative;
            padding: 14px 18px 16px;
            background: #fffaf3;
            border-radius: 4px;
        }

        .visualnews-pro-animate::before {
            content: "";
            position: absolute;
            inset: 0;
            border-left: 6px solid #ff9800;
            animation: visualnewsBorderPulse 2.2s ease-in-out infinite;
        }

        .visualnews-pro-header {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-bottom: 6px;
            position: relative;
            z-index: 1;
        }

        .visualnews-pro-header .dashicons {
            color: #ff9800;
            font-size: 22px;
        }

        .visualnews-pro-title {
            font-size: 14px;
        }

        /* Clean list styling */
        .visualnews-pro-features {
            list-style: none;
            margin: 0 0 10px;
            padding: 0;
            position: relative;
            z-index: 1;
        }

        .visualnews-pro-features li {
            font-size: 13px;
            color: #555;
            padding-left: 20px;
            margin: 2px 0;
            position: relative;
        }

        .visualnews-pro-features li::before {
            content: "\f147"; /* dashicons-yes */
            font-family: dashicons;
            position: absolute;
            left: 0;
            top: 0;
            color: #46b450;
            font-size: 16px;
        }

        .cta-button {
            position: relative;
            z-index: 1;
        }

        @keyframes visualnewsBorderPulse {
            0%   { box-shadow: 0 0 0 0 rgba(255,152,0,.35); }
            70%  { box-shadow: 0 0 0 10px rgba(255,152,0,0); }
            100% { box-shadow: 0 0 0 0 rgba(255,152,0,0); }
        }
        '
    );
});