<?php

/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Car Expert
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function car_expert_body_classes($classes)
{
	// Adds a class of hfeed to non-singular pages.
	if (!is_singular()) {
		$classes[] = 'hfeed';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if (!is_active_sidebar('sidebar-1')) {
		$classes[] = 'no-sidebar';
	}

	return $classes;
}
add_filter('body_class', 'car_expert_body_classes');

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function car_expert_pingback_header()
{
	if (is_singular() && pings_open()) {
		printf('<link rel="pingback" href="%s">', esc_url(get_bloginfo('pingback_url')));
	}
}
add_action('wp_head', 'car_expert_pingback_header');


function cat_expert_new_user()
{
	$install_date = get_option('car_expert_theme_install_date');
	$current_date = current_time('mysql');
	$days_to_show_intro = 2;

	$install_timestamp = strtotime($install_date);
	$current_timestamp = strtotime($current_date);

	if (($current_timestamp - $install_timestamp) <= ($days_to_show_intro * DAY_IN_SECONDS)) {
		$show_intro = true;
	} else {
		$show_intro = false;
	}
	return $show_intro;
}


/**
 * Add admin bar link for Car Expert Pro upgrade
 */
function car_expert_admin_bar_upgrade_link($wp_admin_bar) {
	// Only show to users who can install plugins (admins)
	if (!current_user_can('install_plugins')) {
		return;
	}

	// SVG icon for upgrade link
	$svg_icon = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
	<path d="M12 2L15.09 8.26L22 9.27L17 14.14L18.18 21.02L12 17.77L5.82 21.02L7 14.14L2 9.27L8.91 8.26L12 2Z" fill="#00b9eb"/>
	</svg>';

	// Add the menu item
	$wp_admin_bar->add_node(array(
		'id'    => 'car-expert-upgrade-pro',
		'title' => $svg_icon . '<span class="ab-label">' . esc_html__('Upgrade to Car Expert Pro', 'car-expert') . '</span>',
		'href'  => esc_url('https://wpthemespace.com/product/car-expert-pro/?add-to-cart=8966'),
		'meta'  => array(
			'title' => esc_attr__('Upgrade to Car Expert Pro for more features', 'car-expert'),
			'class' => 'car-expert-upgrade-pro',
		),
	));
}
add_action('admin_bar_menu', 'car_expert_admin_bar_upgrade_link', 999);