File: /var/www/delta/wp-content/themes/delta/functions/setup-timber.php
<?php
/**
* Theme root directory. Web path/url.
*/
define("THEMEROOT", get_stylesheet_directory_uri());
/**
* Theme root directory. Path on disk.
*/
define("THEMEDIR", __DIR__ . "/..");
$composer_autoload = THEMEDIR . "/vendor/autoload.php";
/**
* Initialize Timber. It is installed using Composer, so we can initialize it only if Composer autoload file is present.
*/
if ( file_exists( $composer_autoload ) ) {
require_once $composer_autoload;
$timber = new Timber\Timber();
}
/**
* This ensures that Timber is loaded and available as a PHP class.
* If not, it gives an error message to help direct developers on where to activate
*/
if ( ! class_exists( "Timber" ) ) {
add_action(
"admin_notices",
function() {
echo '<div class="error"><p>Timber not activated. Make sure that you have Timber installed in this theme using Composer. Refresh website if needed after activating Timber.</p></div>';
}
);
add_filter(
"template_include",
function() {
echo '<h1>Timber not activated. Make sure that you have Timber installed in this theme using Composer. Refresh website if needed after activating Timber.</h1>';
}
);
return;
}
/**
* Sets the directories (inside your theme) to find .twig files
*/
Timber::$dirname = array( THEMEROOT, "templates", "views" );
/**
* By default, Timber does NOT autoescape values. Want to enable Twig"s autoescape?
* No prob! Just set this value to true
*/
Timber::$autoescape = false;
/**
* We"re going to configure our theme inside of a subclass of Timber\Site
* You can move this to its own file and include here via php"s include("MySite.php")
*/
class DeltaSite extends Timber\Site {
/** Add timber support. */
public function __construct() {
add_action( "after_setup_theme", array( $this, "theme_supports" ) );
add_filter( "timber/context", array( $this, "add_to_context" ) );
add_filter( "timber/twig", array( $this, "add_to_twig" ) );
add_action( "init", array( $this, "register_post_types" ) );
add_action( "init", array( $this, "register_taxonomies" ) );
add_action( "init", array( $this, "setup_menus" ) );
parent::__construct();
}
/** This is where you add some context
*
* @param string $context context["this"] Being the Twig"s {{ this }}.
*/
public function add_to_context( $context ) {
$context["menu"]["main"] = new Timber\Menu("main-menu");
$context["menu"]["primary"] = new Timber\Menu("primary");
$context["menu"]["footer"] = new Timber\Menu("footer");
$context["site"] = $this;
$context["ajaxurl"] = get_home_url() . '/wp-admin/admin-ajax.php';
$context["cookies_accepted"] = $_COOKIE['cookiesAccepted'];
$context["site_logo_icon"] = get_field('logo_icon', 'options');
$context["site_logo_text"] = get_field('logo_text', 'options');
$context["facebook_link"] = get_field('facebook_link', 'options');
$context["twitter_link"] = get_field('twitter_link', 'options');
$context["linkedin_link"] = get_field('linkedin_link', 'options');
$context["instagram_link"] = get_field('instagram_link', 'options');
$context["footer_content"] = get_field('footer_content', 'options');
$context["newsletter_title"] = get_field('newsletter_title', 'options');
$context["newsletter_text"] = get_field('newsletter_text', 'options');
$context["newsletter_success_icon"] = get_field('newsletter_success_icon', 'options');
$context["newsletter_success_title"] = get_field('newsletter_success_title', 'options');
$context["newsletter_success_text"] = get_field('newsletter_success_text', 'options');
$context["social_media_icons"] = get_field('social_media_icons', 'options');
$footer_form_id = get_field('footer_form', 'options');
$context['footer_form'] = do_shortcode('[contact-form-7 id="' . $footer_form_id . '"]');
$context["contact_page"] = get_field('contact_page', 'options');
$context["our_work_page"] = get_field('our_work_page', 'options');
$context["privacy_policy_page"] = get_field('privacy_policy_page', 'options');
$context["contact_page_form"] = get_field('contact_page_form', 'options');
$context["blog_page"] = get_field('blog_page', 'options');
$context["delta_experience_link"] = get_field('delta_experience_link', 'options');
$context["words_per_minute"] = get_field("words_per_minute", "option");
$context["languages"] = apply_filters('wpml_active_languages', null, array(
'skip_missing' => 0,
'orderby' => 'id',
'order' => 'asc'
)
);
$context["current_language"] = apply_filters( 'wpml_current_language', null );
$context["show_language_switcher"] = get_field('show_language_switcher', 'option');
return $context;
}
public function setup_menus(){
register_nav_menus( array (
"primary" => __( "Primary menu", "delta-lang" ),
"footer" => __( "Footer menu", "delta-lang" )
) );
}
public function setup_image_sizes(){
// add_image_size( "19x6_large", "1920", "600", true );
}
public function theme_supports() {
// Make theme available for translation. Translations can be added to the /languages/ directory.
load_theme_textdomain( "delta-lang", THEMEDIR . "/languages" );
// Add default posts and comments RSS feed links to head.
add_theme_support( "automatic-feed-links" );
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( "title-tag" );
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support( "post-thumbnails" );
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
"html5",
array(
"comment-form",
"comment-list",
"gallery",
"caption",
)
);
/*
* Enable support for Post Formats.
*
* See: https://codex.wordpress.org/Post_Formats
*/
add_theme_support(
"post-formats",
array(
"aside",
"image",
"video",
"quote",
"link",
"gallery",
"audio",
)
);
add_theme_support( "menus" );
}
/** This is where you can add your own functions to twig.
*
* @param string $twig get extension.
*/
public function add_to_twig( $twig ) {
$twig->addExtension( new Twig\Extension\StringLoaderExtension() );
$twig->addFilter( new Timber\Twig_Filter( "format_session", "format_session" ) );
$twig->addFunction(new \Twig\TwigFunction('t', function ($text, $domain = 'delta-lang') {
return __($text, $domain);
}));
return $twig;
}
}
class DeltaFlexible extends Timber\Post {
public function blocks() {
$blocks = array();
foreach ( $this->meta( "sections" ) as $section ) {
$elements = array();
foreach ( $section["elements"] as $element ) {
include "templates/page-elements/" . $element["acf_fc_layout"] . ".php";
$elements[] = $element;
}
$section["elements"] = $elements;
$blocks[] = $section;
}
return $blocks;
}
}
new DeltaSite();