File: /var/www/linde-ai/wp-content/themes/linde/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 LindeSite 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" ) );
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["site"] = $this;
// $context["sample"] = get_field("sample_acf_field", "option");
return $context;
}
public function setup_menus(){
register_nav_menus( array (
"primary" => __( "Primary menu", "linde-lang" ),
"footer" => __( "Footer menu", "linde-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( "linde-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" ) );
return $twig;
}
}
class LindeFlexible extends Timber\Post {
public function blocks() {
$blocks = array();
foreach ( $this->meta( "sections" ) as $section ) {
$elements = array();
// Check if elements is an array to surpress warnings
if (is_array($section["elements"])) {
foreach ( $section["elements"] as $element ) {
include dirname(__DIR__) . '/templates/page-elements/' . $element["acf_fc_layout"] . ".php";
$elements[] = $element;
}
}
$section["elements"] = $elements;
$blocks[] = $section;
}
return $blocks;
}
}
new LindeSite();