<?php

/**
 * Functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package car-dealership-carkit
 * @since 1.0.0
 */

/**
 * The theme version.
 *
 * @since 1.0.0
 */
define('CAR_DEALERSHIP_CARKIT', wp_get_theme()->get('Version'));


/**
 * Add theme support for block styles and editor style.
 *
 * @since 1.0.0
 *
 * @return void
 */
function car_dealership_carkit_setup()
{
    add_editor_style('./assets/css/style-shared.css');

    /*
	 * Load additional block styles.
	 * See details on how to add more styles in the readme.txt.
	 */
    $styled_blocks = ['button', 'quote', 'navigation', 'search'];
    foreach ($styled_blocks as $block_name) {
        $args = array(
            'handle' => "car_dealership_carkit-$block_name",
            'src'    => get_theme_file_uri("assets/css/blocks/$block_name.min.css"),
            'path'   => get_theme_file_path("assets/css/blocks/$block_name.min.css"),
        );
        // Replace the "core" prefix if you are styling blocks from plugins.
        wp_enqueue_block_style("core/$block_name", $args);
    }

    load_theme_textdomain('car-dealership-carkit', get_template_directory() . '/languages');

    // Add support for WooCommerce
    add_theme_support('woocommerce');

    // custom logo
    add_theme_support('custom-logo');

    // Add theme support for selective refresh for widgets.
    add_theme_support('customize-selective-refresh-widgets');
    add_theme_support("wp-block-styles");
    add_theme_support("responsive-embeds");
    add_theme_support('custom-header');
    add_theme_support("custom-background");
    add_theme_support("align-wide");

    /*
		* Switch default core markup for search form, comment form, and comments
		* to output valid HTML5.
		*/
    add_theme_support('html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
    ));

    // Add default posts and comments RSS feed links to head.
    add_theme_support('automatic-feed-links');

    /*
		 * Let WordPress manage the document title.
		 * By adding theme support, we declare that this theme does not use a
		 * hard-coded <title> tag in the document head, and expect WordPress to
		 * provide it for us.
		 */
    add_theme_support('title-tag');


    /*
		 * Enable support for Post Thumbnails on posts and pages.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		 */
    add_theme_support('post-thumbnails');

    // This theme uses wp_nav_menu() in one location.
    register_nav_menus(array(
        'main_menu' => esc_html__('Primary Menu', 'car-dealership-carkit'),
        'footer_links' => esc_html__('Footer Links', 'car-dealership-carkit'),
        'mobile_menu' => esc_html__('Mobile Menu', 'car-dealership-carkit'),
    ));
}

add_action('after_setup_theme', 'car_dealership_carkit_setup');


/**
 * Enqueue the CSS files.
 *
 * @since 1.0.0
 *
 * @return void
 */
function car_dealership_carkit_styles()
{
    wp_enqueue_style(
        'car_dealership_carkit-style',
        get_stylesheet_uri(),
        [],
        CAR_DEALERSHIP_CARKIT
    );
    wp_enqueue_style(
        'car_dealership_carkit-shared-styles',
        get_theme_file_uri('assets/css/style-shared.css'),
        [],
        CAR_DEALERSHIP_CARKIT
    );

    $remote_url = 'https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&family=Space+Grotesk:wght@300..700&family=Quicksand:wght@300;400;500;600;700&display=swap';
    if (function_exists('wptt_get_webfont_url')) {
        wp_enqueue_style('google-fonts',  wptt_get_webfont_url($remote_url), false);
    }

    wp_enqueue_style(
        'bootstrap-grid-5',
        get_theme_file_uri('assets/libs/bootstrap-5.3.3/css/bootstrap-grid.min.css'),
        [],
        '5.3.3'
    );
    wp_enqueue_script('bootstrap-5', get_theme_file_uri('assets/libs/bootstrap-5.3.3/js/bootstrap.min.js'), ['jquery'], '5.3.3', true);

    wp_enqueue_style(
        'fontawesome-6.5',
        get_theme_file_uri('assets/libs/fontawesome-6.7/css/all.min.css'),
        [],
        '6.7'
    );
    wp_enqueue_style(
        'car_dealership_carkit',
        get_theme_file_uri('assets/css/theme.css'),
        [],
        CAR_DEALERSHIP_CARKIT
    );
    wp_enqueue_style(
        'car_dealership_carkit-responsive',
        get_theme_file_uri('assets/css/theme-responsive.css'),
        [],
        CAR_DEALERSHIP_CARKIT
    );

    wp_enqueue_script('car_dealership_carkit', get_theme_file_uri('assets/js/theme.js'), ['jquery'], CAR_DEALERSHIP_CARKIT, true);
}
add_action('wp_enqueue_scripts', 'car_dealership_carkit_styles');

// Filters.
require_once get_theme_file_path('inc/filters.php');

// Block variation example.
require_once get_theme_file_path('inc/register-block-variations.php');

// Block style examples.
require_once get_theme_file_path('inc/register-block-styles.php');

// Block pattern and block category examples.
require_once get_theme_file_path('inc/register-block-patterns.php');

require_once get_theme_file_path('includes/welcome.php');
require_once get_theme_file_path('includes/init.php');

// Font upload
require_once get_theme_file_path('includes/wptt-webfont-loader.php');



/**
 * Admin styles.
 *
 */

function car_dealership_carkit_admin_head_styles() {
    $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
    if ( ! $screen ) {
        return;
    }

    // These styles are only intended for demo import / plugin install related pages.
    $allowed_screen_ids = array(
        'appearance_page_one-click-demo-import',
        'appearance_page_tgmpa-install-plugins',
        'appearance_page_car-dealership-carkit-dashboard',
    );
    if ( ! in_array( $screen->id, $allowed_screen_ids, true ) ) {
        return;
    }

    echo '<style>
      .appearance_page-dashboard #setting-error-tgmpa {
        margin-left: 0;
      }
      
	  .ocdi__content-container .ocdi-content-notice--warning,
      .ocdi__content-container #setting-error-tgmpa,
      .ocdi-install-plugins-content-content .ocdi-content-notice.ocdi-content-notice--warning,
      .ocdi__file-upload-container .ocdi__file-upload-container-items .ocdi__file-upload:not(:first-child),
      .ocdi__file-upload-container .ocdi__file-upload-container-items--second-row,
      .ocdi__content-container .plugin-item.plugin-item-wpforms-lite,
      .ocdi__content-container .plugin-item.plugin-item-all-in-one-seo-pack,
      .ocdi__content-container .plugin-item.plugin-item-google-analytics-for-wordpress {
        display: none !important;
      }
      
      .button.button-primary.js-ocdi-install-plugins-before-import.ocdi-button-disabled::after {
        content: "\f113";
        font-family: dashicons;
        display: inline-block;
        line-height: 1;
        font-weight: 400;
        font-style: normal;
        speak: never;
        text-decoration: inherit;
        text-transform: none;
        text-rendering: auto;
        -webkit-animation: car-dealership-carkit-spin 2s infinite linear;
        animation: car-dealership-carkit-spin 2s infinite linear;
        margin-left: 13px;
        display: inline-block;
    }
        
    @keyframes car-dealership-carkit-spin {
        0% {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg);
        }
        100% {
        -webkit-transform: rotate(360deg);
        transform: rotate(360deg);
        }
    }
    </style>';
}
add_action( 'admin_head', 'car_dealership_carkit_admin_head_styles' );

if (get_option('wdk_theme_carkit_installed') == 1) {

    add_filter('template_include', function ($template) {
        // Check if the template is in the /templates/ directory and is an HTML file
        $html_templates_dir = get_template_directory() . '/templates/';

        if (strpos($template, 'template-canvas.php')) {
            // No need to escape paths when including, only escape when outputting
            return get_template_directory() . '/html-template-wrapper.php';
        }

        if (strpos($template, $html_templates_dir) === 0 && pathinfo($template, PATHINFO_EXTENSION) === 'html') {
            // No need to escape paths when including, only escape when outputting
            return get_template_directory() . '/html-template-wrapper.php';
        }
        return $template;
    });

    add_action('wp_head', function () {
        echo '<style>
            /* Inline CSS for wdk_theme_carkit_installed */
            .hidde-on-wdk_theme_carkit_installed {
                display: none;
            }
          
        </style>';
    });
}


/**
 * Add Sub Menu Page to the Layers Menu Item
 */
if (! function_exists('carkit_register_submenu_page')) {
    function carkit_register_submenu_page()
    {
        add_theme_page(
            esc_html__('Car Dealership Carkit Help', 'car-dealership-carkit'),
            esc_html__('Car Dealership Carkit Help', 'car-dealership-carkit'),
            'edit_theme_options',
            'car-dealership-carkit-dashboard',
            'carkit_get_child_onboarding'
        );
    }
}
function carkit_get_child_onboarding()
{
    require_once get_stylesheet_directory() . '/includes/theme-help.php';
}
add_action('admin_menu', 'carkit_register_submenu_page', 60);
