<?php
/**
 * Page.
 *
 * @package MadeByAura\Prime\Pages
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Pages;

use MadeByAura\Prime\Base;
use MadeByAura\Prime\Helpers\SingletonTrait;

defined( 'ABSPATH' ) || die();

/**
 * Page.
 */
class Page {
	use SingletonTrait;

	/**
	 * Constructor.
	 */
	protected function __construct() {
		// Add hooks after WordPress environment has been set up.
		add_action( 'wp', [ __CLASS__, 'add_hooks' ] );
	}

	/**
	 * Add hooks after WordPress environment has been set up.
	 */
	public static function add_hooks() {
		// Do not proceed if the current page is not page.
		if ( ! is_singular( 'page' ) ) {
			return;
		}

		// Disable page title conditionally.
		add_filter( 'aura_theme|view|disable|views/preface', [ __CLASS__, 'disable_page_title_conditionally' ] );

		// Change sidebar position.
		add_filter( 'aura_theme_sidebar_position', [ __CLASS__, 'change_position_sidebar' ] );
	}

	/**
	 * Disable page title conditionally.
	 */
	public static function disable_page_title_conditionally() {
		return boolval( Base::get_acf( 'page', 'preface_disabled' ) );
	}

	/**
	 * Change sidebar position.
	 *
	 * @param bool|string $position  Sidebar position.
	 * @return string
	 */
	public static function change_position_sidebar( $position ) {
		// Get meta.
		$post_meta = Base::get_acf( 'page', 'position_sidebar', null, 'default' );

		return 'default' === $post_meta ? $position : $post_meta;
	}
}
