<?php
/**
 * Setup Hooks Class
 *
 * @package errigal
 * @since 1.1.0
 */

namespace Errigal\Hook;

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Content_Hook as Content_Hook,
	Errigal_Setup_Hook as Setup_Hook,
	Errigal_WP_Global as WP_Global,
	Errigal_WP_Hook as WP_Hook,
};

/**
 * This class is in charge of adding setup hooks.
 *
 * @since 1.1.0
 */
class Errigal_Setup_Hooks extends Errigal_Hooks_Base {
	/**
	 * Sets content width.
	 *
	 * Priority 0 to make it available to lower priority callbacks.
	 *
	 * @since 1.1.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/after_setup_theme/
	 *
	 * @global int $content_width
	 *
	 * @return void
	 */
	public function set_content_width() {
		/**
		 * Filters content width.
		 *
		 * Sets content width in pixels, based on theme design and stylesheet.
		 * This variable is intended to be overruled from themes.
		 * Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
		 *
		 * @since 1.0.0
		 *
		 * @param int $content_width Content width.
		 */
		$GLOBALS[ WP_Global::CONTENT_WIDTH ] = apply_filters(
			Content_Hook::CONTENT_WIDTH,
			640
		);
	}

	/**
	 * Adds theme supports.
	 *
	 * Note that this function is hooked into after_setup_theme hook, which
	 * runs before init hook. The init hook is too late for some features, such
	 * as indicating support for post thumbnails.
	 *
	 * @since 1.0.0
	 *
	 * @link https://developer.wordpress.org/reference/hooks/after_setup_theme/
	 *
	 * @return void
	 */
	public function add_theme_supports() {
		// Makes theme available for translation.
		load_theme_textdomain( 'errigal', get_template_directory() . '/languages' );

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

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

		/*
		 * Adds post-formats support.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/post-formats/
		 */
		add_theme_support(
			'post-formats',
			array(
				'aside',
				'gallery',
				'link',
				'image',
				'quote',
				'status',
				'video',
				'audio',
				'chat',
			)
		);

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

		/*
		 * Switches default core markup for search form, comment form, and comments
		 * to output valid HTML5.
		 *
		 * @link https://developer.wordpress.org/reference/functions/add_theme_support/#html5
		 * @link https://make.wordpress.org/themes/handbook/review/accessibility/required/
		 */
		add_theme_support(
			'html5',
			array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'style',
				'script',
				'navigation-widgets',
			)
		);

		// Sets up the WordPress core custom background feature.
		add_theme_support(
			'custom-background',
			/**
			* Filters custom background arguments.
			*
			* @since 1.2.0
			*
			* @link https://developer.wordpress.org/reference/functions/add_theme_support/
			*
			* @param array $custom_background_args Associative array of arguments for custom background.
			*/
			apply_filters(
				Setup_Hook::CUSTOM_BACKGROUND_ARGS,
				array(
					'default-color' => 'ffffff',
					'default-image' => '',
				)
			)
		);

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

		/**
		 * Adds support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		$custom_logo_height = 140;
		$custom_logo_width  = 250;

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

		// Adds theme support for editor styles.
		add_theme_support( 'editor-styles' );

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

		// Adds theme support for Block Styles.
		add_theme_support( 'wp-block-styles' );

		// Adds theme support for full and wide align images.
		add_theme_support( 'align-wide' );

		// Adds theme support for responsive embedded content.
		add_theme_support( 'responsive-embeds' );
	}

	/**
	 * Adds hooks.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	protected function add_hooks() {
		add_action( WP_Hook::AFTER_SETUP_THEME, array( $this, 'set_content_width' ), WP_Hook::PRIORITY_LOWEST );
		add_action( WP_Hook::AFTER_SETUP_THEME, array( $this, 'add_theme_supports' ) );
	}
}
