From 3dfff4f6cf482d9fc2bb48e3e3383a3aa31dd510 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Thu, 2 May 2024 17:27:51 +0200 Subject: [PATCH 1/6] feat: rework of PHP files. --- 404.php | 15 ++++++------ archive.php | 51 ++++++++++++++++++++------------------ author.php | 27 ++++++++++++-------- functions.php | 16 ++++++++---- index.php | 31 +++++++++++++---------- page.php | 29 ++++++++++------------ search.php | 21 ++++++++-------- single.php | 27 +++++++++++--------- src/StarterSite.php | 60 ++++++++++++++++++++++++++------------------- 9 files changed, 153 insertions(+), 124 deletions(-) diff --git a/404.php b/404.php index e56dea257..583d57851 100644 --- a/404.php +++ b/404.php @@ -1,13 +1,12 @@ $title, + 'posts' => $posts, +]); diff --git a/author.php b/author.php index 22935423a..e7003576c 100644 --- a/author.php +++ b/author.php @@ -1,21 +1,28 @@ query_vars['author'] ) ) { - $author = Timber::get_user( $wp_query->query_vars['author'] ); - $context['author'] = $author; - $context['title'] = 'Author Archives: ' . $author->name(); +$title = false; +if (isset($wp_query->query_vars['author'])) { + $author = Timber::get_user($wp_query->query_vars['author']); + $title = 'Author Archives: ' . $author->name(); } -Timber::render( array( 'author.twig', 'archive.twig' ), $context ); + +$context = Timber::context([ + 'title' => $title, + 'posts' => Timber::get_posts(), + 'author' => $author, +]); + +Timber::render(array('author.twig', 'archive.twig'), $context); diff --git a/functions.php b/functions.php index 43cddcbec..0711c22ab 100644 --- a/functions.php +++ b/functions.php @@ -1,17 +1,23 @@ $posts, + 'foo' => 'bar', +]); + +Timber::render($templates, $context); diff --git a/page.php b/page.php index 256fb9d11..5787b313c 100644 --- a/page.php +++ b/page.php @@ -1,4 +1,5 @@ $post, + ] +); -$timber_post = Timber::get_post(); -$context['post'] = $timber_post; -Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context ); +Timber::render(array('page-' . $timber_post->post_name . '.twig', 'page.twig'), $context); diff --git a/search.php b/search.php index 069b3aae7..d274f2979 100644 --- a/search.php +++ b/search.php @@ -1,18 +1,17 @@ 'Search results for ' . get_search_query(), + 'posts' => Timber::get_posts(), +]); -Timber::render( $templates, $context ); +Timber::render($templates, $context); diff --git a/single.php b/single.php index e47be9045..658f3293b 100644 --- a/single.php +++ b/single.php @@ -1,20 +1,23 @@ $post, + ] +); -if ( post_password_required( $timber_post->ID ) ) { - Timber::render( 'single-password.twig', $context ); +if (post_password_required($post->ID)) { + Timber::render('single-password.twig', $context); } else { - Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', 'single.twig' ), $context ); + Timber::render(array('single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single-' . $post->slug . '.twig', 'single.twig'), $context); } diff --git a/src/StarterSite.php b/src/StarterSite.php index 7f6c3ac86..3f3d84575 100644 --- a/src/StarterSite.php +++ b/src/StarterSite.php @@ -1,19 +1,24 @@ tag in the document head, and expect WordPress to * provide it for us. */ - add_theme_support( 'title-tag' ); + 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' ); + add_theme_support('post-thumbnails'); /* * Switch default core markup for search form, comment form, and comments @@ -98,7 +105,7 @@ public function theme_supports() { ) ); - add_theme_support( 'menus' ); + add_theme_support('menus'); } /** @@ -106,7 +113,8 @@ public function theme_supports() { * * @param string $text being 'foo', then returned 'foo bar!'. */ - public function myfoo( $text ) { + public function myfoo($text) + { $text .= ' bar!'; return $text; } @@ -116,14 +124,15 @@ public function myfoo( $text ) { * * @param Twig\Environment $twig get extension. */ - public function add_to_twig( $twig ) { + public function add_to_twig($twig) + { /** * Required when you want to use Twig’s template_from_string. * @link https://twig.symfony.com/doc/3.x/functions/template_from_string.html */ // $twig->addExtension( new Twig\Extension\StringLoaderExtension() ); - $twig->addFilter( new Twig\TwigFilter( 'myfoo', [ $this, 'myfoo' ] ) ); + $twig->addFilter(new Twig\TwigFilter('myfoo', [$this, 'myfoo'])); return $twig; } @@ -137,9 +146,10 @@ public function add_to_twig( $twig ) { * * @return array */ - function update_twig_environment_options( $options ) { - // $options['autoescape'] = true; + function update_twig_environment_options($options) + { + // $options['autoescape'] = true; - return $options; + return $options; } } From c56b1b95bcecdec56e12902195ba957a7fca5a06 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Wed, 15 May 2024 06:16:50 +0200 Subject: [PATCH 2/6] chore: update namespace --- 404.php | 2 +- archive.php | 2 +- author.php | 2 +- functions.php | 2 +- page.php | 2 +- single.php | 2 +- src/StarterSite.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/404.php b/404.php index 583d57851..869be1374 100644 --- a/404.php +++ b/404.php @@ -4,7 +4,7 @@ * The template for the 404 page */ -namespace Timber\StarterTheme; +namespace App; use Timber\Timber; diff --git a/archive.php b/archive.php index 0b0079af6..91846b616 100644 --- a/archive.php +++ b/archive.php @@ -10,7 +10,7 @@ * */ -namespace Timber\StarterTheme; +namespace App; use Timber\Timber; diff --git a/author.php b/author.php index e7003576c..c3031a1ab 100644 --- a/author.php +++ b/author.php @@ -7,7 +7,7 @@ * */ -namespace Timber\StarterTheme; +namespace App; use Timber\Timber; diff --git a/functions.php b/functions.php index 0711c22ab..8331926af 100644 --- a/functions.php +++ b/functions.php @@ -7,7 +7,7 @@ * @link https://github.com/timber/starter-theme */ -namespace Timber\StarterTheme; +namespace App; use Timber\Timber; diff --git a/page.php b/page.php index 5787b313c..5207a9bad 100644 --- a/page.php +++ b/page.php @@ -10,7 +10,7 @@ * */ -namespace Timber\StarterTheme; +namespace App; use Timber\Timber; diff --git a/single.php b/single.php index 658f3293b..0129ebcec 100644 --- a/single.php +++ b/single.php @@ -5,7 +5,7 @@ * @link https://developer.wordpress.org/themes/basics/template-hierarchy/ */ -namespace Timber\StarterTheme; +namespace App; use Timber\Timber; diff --git a/src/StarterSite.php b/src/StarterSite.php index 3f3d84575..bf269aa7c 100644 --- a/src/StarterSite.php +++ b/src/StarterSite.php @@ -1,6 +1,6 @@ Date: Wed, 15 May 2024 06:18:19 +0200 Subject: [PATCH 3/6] fix: dont provide post again --- single.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/single.php b/single.php index 0129ebcec..531fb1c3a 100644 --- a/single.php +++ b/single.php @@ -9,12 +9,8 @@ use Timber\Timber; -$post = Timber::get_post(); -$context = Timber::context( - [ - 'post' => $post, - ] -); +$context = Timber::context(); +$post = $context['post']; if (post_password_required($post->ID)) { Timber::render('single-password.twig', $context); From 1455f3abc1bd2b097edf50f251b948b890ad4ef9 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Wed, 15 May 2024 06:19:51 +0200 Subject: [PATCH 4/6] Remove $dirname since we only use the default location now. --- functions.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/functions.php b/functions.php index 8331926af..1cae0e0a9 100644 --- a/functions.php +++ b/functions.php @@ -17,7 +17,4 @@ Timber::init(); -// Sets the directories (inside your theme) to find .twig files. -Timber::$dirname = ['templates', 'views']; - new StarterSite(); From b83b1f751ff14f075b7802e36780eb7a8abd4fde Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Wed, 15 May 2024 06:27:33 +0200 Subject: [PATCH 5/6] fix: re-provide context --- archive.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/archive.php b/archive.php index 91846b616..1305089e4 100644 --- a/archive.php +++ b/archive.php @@ -16,8 +16,6 @@ $templates = array('archive.twig', 'index.twig'); -$context = Timber::context(); - $title = 'Archive'; if (is_day()) { $title = 'Archive: ' . get_the_date('D M Y'); @@ -35,9 +33,8 @@ array_unshift($templates, 'archive-' . get_post_type() . '.twig'); } -$posts = Timber::get_posts(); - -Timber::render($templates, [ +$context = Timber::context([ 'title' => $title, - 'posts' => $posts, ]); + +Timber::render($templates, $context); From f645fb090246fd0e565c6b67f4321944f6369758 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Mon, 20 May 2024 13:44:43 +0200 Subject: [PATCH 6/6] chore: remove unneeded get_post(s) calls --- author.php | 1 - index.php | 2 -- page.php | 11 +++-------- search.php | 1 - src/StarterSite.php | 2 +- 5 files changed, 4 insertions(+), 13 deletions(-) diff --git a/author.php b/author.php index c3031a1ab..2d63a05a5 100644 --- a/author.php +++ b/author.php @@ -21,7 +21,6 @@ $context = Timber::context([ 'title' => $title, - 'posts' => Timber::get_posts(), 'author' => $author, ]); diff --git a/index.php b/index.php index ffa5d2446..ed3d2c254 100644 --- a/index.php +++ b/index.php @@ -13,7 +13,6 @@ use Timber\Timber; -$posts = Timber::get_posts(); $templates = array('index.twig'); if (is_home()) { @@ -21,7 +20,6 @@ } $context = Timber::context([ - 'posts' => $posts, 'foo' => 'bar', ]); diff --git a/page.php b/page.php index 5207a9bad..e0612797a 100644 --- a/page.php +++ b/page.php @@ -14,12 +14,7 @@ use Timber\Timber; -$post = Timber::get_post(); +$context = Timber::context(); +$post = $context['post']; -$context = Timber::context( - [ - 'post' => $post, - ] -); - -Timber::render(array('page-' . $timber_post->post_name . '.twig', 'page.twig'), $context); +Timber::render(array('page-' . $post->post_name . '.twig', 'page.twig'), $context); diff --git a/search.php b/search.php index d274f2979..61f28f7e1 100644 --- a/search.php +++ b/search.php @@ -11,7 +11,6 @@ $context = Timber::context([ 'title' => 'Search results for ' . get_search_query(), - 'posts' => Timber::get_posts(), ]); Timber::render($templates, $context); diff --git a/src/StarterSite.php b/src/StarterSite.php index bf269aa7c..8d3d4f625 100644 --- a/src/StarterSite.php +++ b/src/StarterSite.php @@ -132,7 +132,7 @@ public function add_to_twig($twig) */ // $twig->addExtension( new Twig\Extension\StringLoaderExtension() ); - $twig->addFilter(new Twig\TwigFilter('myfoo', [$this, 'myfoo'])); + $twig->addFilter(new \Twig\TwigFilter('myfoo', [$this, 'myfoo'])); return $twig; }