<?php
/**
 * Tech360 functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Tech360
 */

if ( ! defined( '_S_VERSION' ) ) {
	// Replace the version number of the theme on each release.
	define( '_S_VERSION', '1.0.0' );
}

/**
 * Remove editor from Homepage template
 */

function t360_remove_editor() {

	$id = isset( $_GET[ 'post' ] ) ? $_GET[ 'post' ] : null;
	$homepage_id = get_option( 'page_on_front' );

	// $template_arr = get_post_meta( $id, '_wp_page_template' );
	// $template = empty( $template_arr ) ? false : $template_arr[ 0 ];

	// if( 'tpl_homepage.php' == $template ) {
	if( $id == $homepage_id ) {
		remove_post_type_support( 'page', 'editor' );
	}

}
add_action( 'init', 't360_remove_editor', 99 );

/**
 * Define constants
 */

if ( ! function_exists( 'tech360_setup' ) ) :
	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 *
	 * Note that this function is hooked into the after_setup_theme hook, which
	 * runs before the init hook. The init hook is too late for some features, such
	 * as indicating support for post thumbnails.
	 */
	function tech360_setup() {
		/*
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 * If you're building a theme based on Tech360, use a find and replace
		 * to change 'tech360' to the name of your theme in all the template files.
		 */
		load_theme_textdomain( 'tech360', get_template_directory() . '/languages' );

		// 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(
			[
				'header_menu' => esc_html__( 'Header Menu', 'tech360' ),
				'footer_menu_quicklinks' => esc_html__( 'Quicklinks Footer Menu', 'tech360' ),
				'footer_menu_services' => esc_html__( 'Services Footer Menu', 'tech360' )
			]
		);

		/*
		 * 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',
				'style',
				'script',
			)
		);

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		add_theme_support(
			'custom-logo',
			array(
				'height'      => 250,
				'width'       => 250,
				'flex-width'  => true,
				'flex-height' => true,
			)
		);
	}
endif;
add_action( 'after_setup_theme', 'tech360_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function tech360_content_width() {
	// This variable is intended to be overruled from themes.
	// Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$GLOBALS['content_width'] = apply_filters( 'tech360_content_width', 640 );
}
add_action( 'after_setup_theme', 'tech360_content_width', 0 );

/**
 * Add shortcode support to widgets
 */
add_filter( 'widget_text', 'do_shortcode' );

/**
 * Enqueue scripts and styles.
 */
function tech360_scripts() {

	wp_enqueue_style( 'tech360-style', get_stylesheet_uri(), [], _S_VERSION );
	wp_style_add_data( 'tech360-style', 'rtl', 'replace' );
	wp_enqueue_style( 'tech360', CODE_URI . 'css/t360.min.css', [ 'tech360-style' ], _S_VERSION );
	wp_enqueue_style( 'fontawesome', CODE_URI . 'css/vendor/fontawesome/all.min.css', [], '5.14.0' );

	// Load GSAP & plugins
	wp_enqueue_script( 'gsap', CODE_URI . 'js/vendor/gsap/gsap.min.js', [], '3.5.0', true );
	wp_enqueue_script( 'gsap-scrolltrigger', CODE_URI . 'js/vendor/gsap/ScrollTrigger.min.js', [ 'gsap' ], '3.5.0', true );
	wp_enqueue_script( 'gsap-scrollto', CODE_URI . 'js/vendor/gsap/ScrollToPlugin.min.js', [ 'gsap' ], '3.5.0', true );

	// Load Slick
	wp_enqueue_style( 'slick', CODE_URI . 'js/vendor/slick/slick.css', [], '1.8.1' );
	wp_enqueue_script( 'slick', CODE_URI . 'js/vendor/slick/slick.min.js', [ 'jquery' ], '1.8.1', false );

	// Load Lightbox
	wp_enqueue_style( 'lightbox', CODE_URI . 'js/vendor/lightbox/css/lightbox.min.css', [], '2.11.3' );
	wp_enqueue_script( 'lightbox', CODE_URI . 'js/vendor/lightbox/js/lightbox.min.js', [ 'jquery' ], '2.11.3', true );

	wp_enqueue_script( 'header-menu', CODE_URI . 'js/header_menu.js', [ 'jquery', 'gsap' ], _S_VERSION, true );
	wp_enqueue_script( 'lazy-images', CODE_URI . 'js/lazy_images.js', [ 'jquery', 'gsap' ], _S_VERSION, true );
	wp_enqueue_script( 'inview', CODE_URI . 'js/inview.js', [ 'jquery', 'gsap', 'gsap-scrolltrigger' ], _S_VERSION, true );
	wp_enqueue_script( 'back-to-top', CODE_URI . 'js/back_to_top.js', [ 'jquery', 'gsap', 'gsap-scrollto' ], _S_VERSION, true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'tech360_scripts' );

/**
 * Implement the Custom Header feature.
 */
// require get_template_directory() . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
	require get_template_directory() . '/inc/jetpack.php';
}

/**
 * Load WooCommerce compatibility file.
 */
if ( class_exists( 'WooCommerce' ) ) {
	require get_template_directory() . '/inc/woocommerce.php';
}

require get_template_directory() . '/theme-code/theme-functions.php';
