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

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


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

	return $updated_defaults;
}
add_filter( 'visualblogger_customizer_defaults', 'visualblogger_customizer_add_defaults' );

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

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

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

		// Getting data from Theme Options
		$length	= visualblogger_gtm( 'visualblogger_excerpt_length' );

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

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

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

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

endif;

/**
 * Customize archive title.
 */
add_filter('get_the_archive_title', function ($title) {
    if (is_category()) {
        $title = single_cat_title('', false);
    } elseif (is_tag()) {
        $title = single_tag_title('', false);
    } elseif (is_author()) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } elseif (is_tax()) { //for custom post types
        $title = single_term_title('', false);
    } elseif (is_post_type_archive()) {
        $title = post_type_archive_title('', false);
    }
    return $title;
});

/**
 * Search Filter 
 */
if ( ! function_exists( 'visualblogger_search_filter' ) && ( ! is_admin() ) ) :

function visualblogger_search_filter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}

add_filter('pre_get_posts','visualblogger_search_filter');

endif;

/**
 * Set the default image if none exists.
 *
 * @param string $html              The post thumbnail HTML.
 * @param int    $post_id           The post ID.
 * @param int    $post_thumbnail_id The post thumbnail ID.
 * @return html
 */
function visualblogger_fallback_post_thumbnail_html( $html, $post_id, $post_thumbnail_id ) {
    if ( empty( $html ) ) {
        $html = '<img class="default-thumbnail" src="' . get_template_directory_uri() . '/assets/img/featured_default.png' . '" alt="' . get_the_title( $post_id ) . '" />';
    }

    return $html;
}
add_filter( 'post_thumbnail_html', 'visualblogger_fallback_post_thumbnail_html', 5, 3 );

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

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

            <div class="visualblogger-pro-header">
                <span class="dashicons dashicons-star-filled"></span>
                <strong class="visualblogger-pro-title">
                    <?php esc_html_e( 'Upgrade to VisualBlog Pro', 'visualblogger' ); ?>
                </strong>
            </div>
            <p class="visualblogger-pro-desc">
                <?php esc_html_e( 'You are currently using the free version of the VisualBlogger theme. Upgrade to the Pro version to unlock additional features:', 'visualblogger' ); ?>
            </p>
            <ul class="visualblogger-pro-features">
                <li><?php esc_html_e( 'Theme Options Panel', 'visualblogger' ); ?></li>
                <li><?php esc_html_e( 'One Click Demo Import', 'visualblogger' ); ?></li>
                <li><?php esc_html_e( '1000+ Google Fonts', 'visualblogger' ); ?></li>
                <li><?php esc_html_e( '11+ Homepage Styles', 'visualblogger' ); ?></li>
                <li><?php esc_html_e( '19+ Block Patterns', 'visualblogger' ); ?></li>
                <li><?php esc_html_e( 'Priority Theme Support and More', 'visualblogger' ); ?></li>                
            </ul>

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

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

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

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

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

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

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

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

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

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

        .visualblogger-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 visualbloggerBorderPulse {
            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); }
        }
        '
    );
});