wickywills
12/8/2017 - 4:00 PM

Site.php

Site.php

<?php

namespace Lumberjack\Core;

use Timber\Site as TimberSite;
use Timber\Helper as TimberHelper;
use Lumberjack\Core\Menu;

class Site extends TimberSite {

	public function __construct() {
		add_filter('get_twig', [$this, 'addToTwig']);
		add_filter('timber_context', [$this, 'addToContext']);

		parent::__construct();
	}

	public function addToContext($data) {
		$data['template_directory'] = get_template_directory_uri();

		$data['is_home'] = is_home();
		$data['is_front_page'] = is_front_page();
		$data['is_logged_in'] = is_user_logged_in();

		// Get the page title, and prefix it with ' | ' if it exists (for use in html title)
		$data['wp_title'] = TimberHelper::function_wrapper('wp_title', ['|', false, 'right']);

		$data['menu'] = new Menu('primary');
		$data['menuSecondary'] = new Menu('secondary');


		/**
		 * Header [.c-header]
		 * 
		 * Allow the logo to be overridden on landing pages. If no other logo
		 * is supplied, then use the default VW logo
		 */
		$logo = get_field('logo', 'visitwinchester_general');
		if (get_field('logo_override')) {
			$logo = get_field('logo_override');
		}
		$data['header'] = [
			'element' => 'a',
			'href' => get_home_url(),
			'classes' => '',
			'label' => $data['wp_title'],
			'logo' => $logo
		];


		/**
		 * "Stay social"
		 */
		$context['social'] = [];
		$iconsArr = [];
		if (have_rows('icons', 'visitwinchester_general')):
			while (have_rows('icons', 'visitwinchester_general')) : the_row();
				$iconsArr[] = [
					"link" => get_sub_field('link')['url'],
					"linkTarget" => get_sub_field('link')['target'],
					"icon" => get_sub_field('icon'),
					"image" => get_sub_field('image')
				];
			endwhile;
		endif;

		$data['icons'] = $iconsArr;


		/**
		 * Filters
		 */
		$currTermId = (is_tax() ? get_queried_object()->term_id : "");
		$filters = [
			0 => [
				"title" => "Filter by what's on",
				"facet" => facetwp_display('facet', 'whats_on')
			],
			1 => [
				"title" => "Filter by things to do",
				"facet" => facetwp_display('facet', 'things_to_do')
			],
			2 => [
				"title" => "Filter by food and drink",
				"facet" => facetwp_display('facet', 'food_drink')
			],
			3 => [
				"title" => "Filter by shopping",
				"facet" => facetwp_display('facet', 'shopping')
			],
			4 => [
				"title" => "Filter by where to stay",
				"facet" => facetwp_display('facet', 'where_to_stay')
			],
			5 => [
				"title" => "Filter by who is going",
				"facet" => facetwp_display('facet', 'by_who_is_going')
			],
			6 => [
				"title" => "Filter by location",
				"facet" => facetwp_display('facet', 'by_location')
			],
			7 => [
				"title" => "Seasonal",
				"facet" => facetwp_display('facet', 'seasonal')
			],
			8 => [
				"title" => "Accessibility statement",
				"facet" => facetwp_display('facet', 'accessibility_statement')
			]
		];
		

		// If where-to-stay, then show additional certain filters
		if ($currTermId == 80) {
			array_push($filters, array('title' => 'Budget - Per room per night prices', 'facet' => facetwp_display('facet', 'budget_per_room_per_night_prices')));
			array_push($filters, array('title' => 'Budget - Unit per week prices', 'facet' => facetwp_display('facet', 'budget_unit_per_week_prices')));
			array_push($filters, array('title' => 'Accommodation facilities', 'facet' => facetwp_display('facet', 'budget_per_room_per_night_prices')));
			array_push($filters, array('title' => 'Accommodation gradings/inspections', 'facet' => facetwp_display('facet', 'accommodation_gradingsinspections')));
		}

		// If what's on, Things to do, Food & Drink, show additional filters
		if ($currTermId == 12 || $currTermId == 79 || $currTermId == 82) {
			array_push($filters, array('title' => 'Budget', 'facet' => facetwp_display('facet', 'budget')));
		}
		
		// If it's not a taxonomy page, then just add all the filters
		if ( !is_tax() ){
			array_push($filters, array('title' => 'Budget - Per room per night prices', 'facet' => facetwp_display('facet', 'budget_per_room_per_night_prices')));
			array_push($filters, array('title' => 'Budget - Unit per week prices', 'facet' => facetwp_display('facet', 'budget_unit_per_week_prices')));
			array_push($filters, array('title' => 'Accommodation facilities', 'facet' => facetwp_display('facet', 'budget_per_room_per_night_prices')));
			array_push($filters, array('title' => 'Accommodation gradings/inspections', 'facet' => facetwp_display('facet', 'accommodation_gradingsinspections')));
			array_push($filters, array('title' => 'Budget', 'facet' => facetwp_display('facet', 'budget')));
		}

		// Remove all the filters that aren't needed
		foreach ($filters as $key => $filter) {
			if (!$filter['facet']) {
				unset($filters[$key]);
			}
		}

		$data['filters'] = $filters;


		return $data;
	}

	public function addToTwig($twig) {
		return $twig;
	}

}