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

$current_theme = wp_get_theme( 'glaze-blog-lite' );

define( 'GLAZE_BLOG_LITE_VERSION', $current_theme->get( 'Version' ) );

if ( ! function_exists( 'glaze_blog_lite_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 glaze_blog_lite_setup() {
		/*
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 * If you're building a theme based on Glaze Blog, use a find and replace
		 * to change 'glaze-blog-lite' to the name of your theme in all the template files.
		 */
		load_theme_textdomain( 'glaze-blog-lite', 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' );
		// Thumbnail size for blog, archive and search page.
		add_image_size( 'glaze-blog-lite-thumbnail-one', 800, 450, true );
		// Thumbnail size for related posts.
		add_image_size( 'glaze-blog-lite-thumbnail-two', 450, 450, true );

		// This theme uses wp_nav_menu() in one location.
		register_nav_menus(
			array(
				'menu-1' => esc_html__( 'Main Menu', 'glaze-blog-lite' ),
				'menu-2' => esc_html__( 'Top Header Menu', 'glaze-blog-lite' ),
			)
		);

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

		// Set up the WordPress core custom background feature.
		add_theme_support(
			'custom-background',
			apply_filters(
				'glaze_blog_lite_custom_background_args',
				array(
					'default-color' => 'ffffff',
					'default-image' => '',
				)
			)
		);

		// 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,
			)
		);

		// Add editor style.
		add_theme_support( 'editor-styles' );
		add_editor_style( 'admin/css/editor-style.css' );

		// Add support for gutenberg.
		add_theme_support( 'align-wide' );
		add_theme_support( 'wp-block-styles' );
		add_theme_support( 'responsive-embeds' );

		/**
		 * Remove block widget support in WordPress version 5.8 & later.
		 *
		 * @link https://make.wordpress.org/core/2021/06/29/block-based-widgets-editor-in-wordpress-5-8/
		 */
		remove_theme_support( 'widgets-block-editor' );
	}
}
add_action( 'after_setup_theme', 'glaze_blog_lite_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 glaze_blog_lite_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( 'glaze_blog_lite_content_width', 640 );
}
add_action( 'after_setup_theme', 'glaze_blog_lite_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function glaze_blog_lite_widgets_init() {

	register_sidebar(
		array(
			'name'          => esc_html__( 'Sidebar', 'glaze-blog-lite' ),
			'id'            => 'glaze-blog-lite-sidebar',
			'description'   => esc_html__( 'Add widgets here.', 'glaze-blog-lite' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<div class="widget_title"><h3>',
			'after_title'   => '</h3></div>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer Left', 'glaze-blog-lite' ),
			'id'            => 'glaze-blog-lite-footer-left',
			'description'   => esc_html__( 'Add widgets here.', 'glaze-blog-lite' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<div class="widget_title"><h3>',
			'after_title'   => '</h3></div>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer Middle', 'glaze-blog-lite' ),
			'id'            => 'glaze-blog-lite-footer-middle',
			'description'   => esc_html__( 'Add widgets here.', 'glaze-blog-lite' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<div class="widget_title"><h3>',
			'after_title'   => '</h3></div>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer Right', 'glaze-blog-lite' ),
			'id'            => 'glaze-blog-lite-footer-right',
			'description'   => esc_html__( 'Add widgets here.', 'glaze-blog-lite' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<div class="widget_title"><h3>',
			'after_title'   => '</h3></div>',
		)
	);

	register_widget( 'Glaze_Blog_Lite_Author_Widget' );

	register_widget( 'Glaze_Blog_Lite_Post_Widget' );

	register_widget( 'Glaze_Blog_Lite_Social_Widget' );
}
add_action( 'widgets_init', 'glaze_blog_lite_widgets_init' );

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

	wp_enqueue_style(
		'glaze-blog-lite-style',
		get_stylesheet_uri(),
		array(),
		GLAZE_BLOG_LITE_VERSION,
		'all'
	);

	wp_enqueue_style( // phpcs:ignore
		'glaze-blog-lite-google-fonts',
		glaze_blog_lite_fonts_url(),
		array(),
		null,
		'all'
	);

	wp_enqueue_style(
		'glaze-blog-lite-main',
		get_template_directory_uri() . '/assets/dist/css/main.css',
		array(),
		GLAZE_BLOG_LITE_VERSION,
		'all'
	);

	wp_enqueue_script(
		'glaze-blog-lite-bundle',
		get_template_directory_uri() . '/assets/dist/js/bundle.min.js',
		array( 'jquery' ),
		GLAZE_BLOG_LITE_VERSION,
		true
	);

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


/**
 * Enqueue scripts and styles for admin.
 */
function glaze_blog_lite_admin_enqueue() {

	wp_enqueue_style(
		'glaze-blog-lite-admin',
		get_template_directory_uri() . '/admin/css/admin.css',
		array(),
		GLAZE_BLOG_LITE_VERSION,
		true
	);
}
add_action( 'admin_enqueue_scripts', 'glaze_blog_lite_admin_enqueue' );


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

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

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

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

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

/**
 * Funtion to load plugin dependencies.
 */
require get_template_directory() . '/includes/plugin-functions.php';

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


/**
 * Load breadcrumbs.
 */
require get_template_directory() . '/third-party/breadcrumbs.php';

/**
 * Load TGM plugin activation.
 */
require get_template_directory() . '/third-party/class-tgm-plugin-activation.php';

/**
 * Load Post Widget.
 */
require get_template_directory() . '/widgets/class-glaze-blog-lite-post-widget.php';

/**
 * Load Author Widget.
 */
require get_template_directory() . '/widgets/class-glaze-blog-lite-author-widget.php';

/**
 * Load Social Links Widget.
 */
require get_template_directory() . '/widgets/class-glaze-blog-lite-social-widget.php';
