<?php
/**
 * Theme setup and core WordPress integrations.
 *
 * @package Gustavo_Portfolio
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Register theme supports, menus, and editor features.
 *
 * @return void
 */
function gustavoo_portfolio_setup() {
	load_theme_textdomain( 'gustavoo-portfolio', get_template_directory() . '/languages' );

	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'title-tag' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'responsive-embeds' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'editor-styles' );
	add_editor_style( 'assets/css/editor.css' );

	add_theme_support(
		'html5',
		array(
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'script',
			'search-form',
			'style',
		)
	);

	add_theme_support(
		'custom-logo',
		array(
			'height'      => 160,
			'width'       => 160,
			'flex-height' => true,
			'flex-width'  => true,
		)
	);

	register_nav_menus(
		array(
			'primary'         => __( 'Menu principal', 'gustavoo-portfolio' ),
			'news_categories' => __( 'Menu de categorias de notícias', 'gustavoo-portfolio' ),
			'footer'          => __( 'Menu do rodapé', 'gustavoo-portfolio' ),
		)
	);

	add_image_size( 'gustavoo-project-card', 720, 480, true );
	add_image_size( 'gustavoo-post-card', 720, 460, true );
}
add_action( 'after_setup_theme', 'gustavoo_portfolio_setup' );

/**
 * Define a comfortable content width for embeds and media.
 *
 * @return void
 */
function gustavoo_portfolio_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'gustavoo_portfolio_content_width', 780 );
}
add_action( 'after_setup_theme', 'gustavoo_portfolio_content_width', 0 );

/**
 * Add useful state classes without coupling templates to presentation logic.
 *
 * @param string[] $classes Existing body classes.
 * @return string[]
 */
function gustavoo_portfolio_body_classes( $classes ) {
	if ( is_front_page() ) {
		$classes[] = 'has-overlay-header';
	}

	if ( ! is_front_page() && is_active_sidebar( 'sidebar-blog' ) ) {
		$classes[] = 'has-blog-sidebar';
	}

	if ( ! is_singular() ) {
		$classes[] = 'is-list-view';
	}

	return array_unique( $classes );
}
add_filter( 'body_class', 'gustavoo_portfolio_body_classes' );

/** Set the posts page browser title to the editorial section name. */
function gustavoo_portfolio_blog_document_title( $title ) {
	if ( is_home() ) {
		$title['title'] = __( 'Últimas notícias', 'gustavoo-portfolio' );
	}

	return $title;
}
add_filter( 'document_title_parts', 'gustavoo_portfolio_blog_document_title' );

/** Keep the editorial top grid focused on the five newest posts. */
function gustavoo_portfolio_news_posts_per_page( $query ) {
	if ( is_admin() || ! $query->is_main_query() || ! ( $query->is_home() || $query->is_category() ) ) {
		return;
	}

	$query->set( 'posts_per_page', 5 );
}
add_action( 'pre_get_posts', 'gustavoo_portfolio_news_posts_per_page' );

/** Refresh category rewrite rules once after the editorial templates are installed. */
function gustavoo_portfolio_refresh_editorial_rewrites() {
	$version = '1.0.0';

	if ( $version === get_option( 'gustavoo_portfolio_editorial_rewrite_version' ) ) {
		return;
	}

	flush_rewrite_rules( false );
	update_option( 'gustavoo_portfolio_editorial_rewrite_version', $version, false );
}
add_action( 'init', 'gustavoo_portfolio_refresh_editorial_rewrites', 99 );

/**
 * Print a fallback icon only when WordPress has no configured Site Icon.
 *
 * @return void
 */
function gustavoo_portfolio_fallback_site_icon() {
	if ( has_site_icon() || ! function_exists( 'gustavoo_portfolio_get_image_source' ) ) {
		return;
	}

	$icon_url = gustavoo_portfolio_get_image_source( 'logo-mark', 'png' );

	if ( $icon_url ) {
		printf( '<link rel="icon" href="%s" sizes="192x192">' . "\n", esc_url( $icon_url ) );
	}
}
add_action( 'wp_head', 'gustavoo_portfolio_fallback_site_icon', 2 );
