<?php
/**
 * upTown functions and definitions
 *
 * @package upTown
 */

/**
 * Set the content width based on the theme's design and stylesheet
 *
 * @since 1.0
 */
if ( ! isset( $content_width ) )
	$content_width = 640; /* pixels */

define( 'UPTOWN_THEME_DIR', trailingslashit( get_template_directory() ) );
define( 'UPTOWN_THEME_URI', trailingslashit( get_template_directory_uri() ) );

$uptown_theme = wp_get_theme( 'uptown' );
if ( $uptown_theme->exists() ) {

	$theme = wp_get_theme();
	$version = $theme->Version; // get theme version
	unset( $theme );

	// force css load on local web server
	if ( in_array( wp_get_environment_type(), array( 'development', 'local' ) ) ) {
		$version = time();
	}

	define( 'UPTOWN_THEME_VERSION', $version );

}
else {
	global $wp_version;
    define( 'UPTOWN_THEME_VERSION', esc_html( $wp_version ) );

}

/**
 * Get the asset suffix depending on the current environment.
 *
 * @return string
 */
function uptown_get_asset_suffix() {
	if ( in_array( wp_get_environment_type(), array( 'development', 'local' ), true ) ) {
		return '';
	}

	return '.min';
}

/**
 * Load CSS
 */
if ( ! function_exists( 'uptown_load_styles' ) ) {
	function uptown_load_styles(): void {
		$style_suffix = uptown_get_asset_suffix();

		wp_enqueue_style( 'uptown-style', get_stylesheet_uri() );

		wp_enqueue_style( 'oswald-lora', UPTOWN_THEME_URI . 'uptown-fonts' . $style_suffix . '.css', array(), UPTOWN_THEME_VERSION, 'all' );

		// Accessible menu following https://github.com/argenteum/accessible-nav-wp
		wp_enqueue_style( 'uptown-menu', UPTOWN_THEME_URI . 'css/menu' . $style_suffix . '.css', array(), UPTOWN_THEME_VERSION, 'all' );

		wp_enqueue_style( 'font-awesome', UPTOWN_THEME_URI . 'css/all' . $style_suffix . '.css', array(), UPTOWN_THEME_VERSION, 'all' );

		wp_enqueue_style( 'uptown',
		                  get_template_directory_uri() . '/css/main' . $style_suffix . '.css',
		                  array(),
		                  UPTOWN_THEME_VERSION,
		                  'all'
		);

	}
	add_action( 'wp_enqueue_scripts', 'uptown_load_styles', 15 );
}

/**
 * Sets up theme defaults and register 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 post thumbnails.
 *
 * @since 1.0
 */
if ( ! function_exists( 'uptown_setup' ) ) {

	function uptown_setup() {
	
		/**
		 * Add default posts and comment RSS feed links to head
		 */
		add_theme_support( 'automatic-feed-links' );
		
		add_theme_support( 'title-tag' );

		add_theme_support( 'custom-logo', array(
			'height'        => 35,
			'width'         => 130,
			'flex-height'   => true,
			'flex-width'    => true,
			'header-text'   => array( 'site-title', 'site-description' ),
		) );

		/**
		 * Enable support for Post Thumbnails on posts and pages
		 *
		 * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
		 */
		add_theme_support( 'post-thumbnails' );
	    
	    // Set post-thumbnail size
	    set_post_thumbnail_size( 300, 167, true );
	    
	    // Set custom image size 'big'
	    add_image_size( 'big', 640, 360, true );
	    
	    // Set custom image size 'full'
	    add_image_size( 'full', 980, 450, true );    
	
		/**
		 * This theme uses wp_nav_menu() in one location.
		 */
		register_nav_menus( array(
			'primary' => __( 'Primary Menu', 'uptown' ),
		) );
	
		/**
		 * Set up the WordPress core custom background feature.
		 */
		add_theme_support( 'custom-background', apply_filters( 'uptown_custom_background_args', array(
			'default-color' => 'e2e2e2',
			'default-image' => '',
		) ) );
		
		/**
		 * Add post editor style.
		 */
		add_editor_style( 'css/style-editor' . uptown_get_asset_suffix() . '.css' );

		/**
		 * Enable support for responsive-embeds feature
		 *
		 * @link https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#responsive-embedded-content
		 */
		add_theme_support( 'responsive-embeds' );

		/**
		 * Enable support for wp-block-styles feature
		 *
		 * @link https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#opinionated-block-styles
		 */
		add_theme_support( 'wp-block-styles' );

		/**
		 * Enable support for wide alignment feature
		 *
		 * @link https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/#wide-alignment
		 */
		add_theme_support( 'align-wide' );

		/**
		 * Enable HTML5 feature to allow the use of HTML5 markup
		 * 
		 * @link https://developer.wordpress.org/reference/functions/add_theme_support/#parameters
		 * @link https://codex.wordpress.org/Theme_Markup
		 */
		$args = array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		
			'meta',
			'style',
			'script',
		);
		add_theme_support( 'html5', $args );

		/**
		 * Enable to add custom CSS support
		 * 
		 * @link https://github.com/WordPress/gutenberg/pull/49396
		 */
		// add_theme_support( 'register_block_style' );

		/**
		 * Enable to use block patterns
		 */
		// add_theme_support( 'register_block_pattern' );
	}

} // endif uptown_setup
add_action( 'after_setup_theme', 'uptown_setup' );



/**
 * Register widgetized area and update sidebar with default widgets
 *
 * @since 1.0
 */

add_action( 'widgets_init', 'uptown_widgets_init' );
 
function uptown_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar', 'uptown' ),
		'id'            => 'sidebar',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<div class="title"><h3 class="widget-title">',
		'after_title'   => '</h3></div>',
	) );
}


/**
 * Enqueue scripts and styles
 *
 * @since 1.0
 */
 
add_action( 'wp_enqueue_scripts', 'uptown_scripts' );

function uptown_scripts() {
	$asset_suffix = uptown_get_asset_suffix();

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );

	if ( is_singular() && wp_attachment_is_image() )
		wp_enqueue_script( 'uptown-keyboard-image-navigation', UPTOWN_THEME_URI . 'js/keyboard-image-navigation' . $asset_suffix . '.js', array( 'jquery' ), UPTOWN_THEME_VERSION, true );

	wp_enqueue_script( 'uptown-menu', UPTOWN_THEME_URI . 'js/menu' . $asset_suffix . '.js', array( 'jquery' ), UPTOWN_THEME_VERSION, array( 'in_footer' => true ) );

	wp_localize_script(
		'uptown-menu',
		'uptownMenuL10n',
		array(
			'expand'   => __( 'Expand child menu', 'uptown' ),
			'collapse' => __( 'Collapse child menu', 'uptown' ),
		)
	);

}


/**
 * Implement helpful helper functions
 *
 * @since 1.0
 */
require UPTOWN_THEME_DIR . 'inc/helpers.php';


/**
 * Implement the Custom Header feature
 *
 * @since 1.0
 */
require UPTOWN_THEME_DIR . 'inc/custom-header.php';


/**
 * Custom template tags for this theme
 *
 * @since 1.0
 */
require UPTOWN_THEME_DIR . 'inc/template-tags.php';


/**
 * Custom functions that act independently of the theme templates
 *
 * @since 1.0
 */
require UPTOWN_THEME_DIR . 'inc/extras.php';


/**
 * Customizer additions
 *
 * @since 1.0
 */
require UPTOWN_THEME_DIR . 'inc/customizer.php';
