Skip to content

Commit

Permalink
feat: rework of PHP files. (#152)
Browse files Browse the repository at this point in the history
* feat: rework of PHP files.
* chore: update namespace
* fix: don't provide post again
* Remove $dirname since we only use the default location now.
* chore: remove unneeded get_post(s) calls
  • Loading branch information
Levdbas authored May 21, 2024
1 parent 91d07cc commit 0a6cb4d
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 126 deletions.
15 changes: 7 additions & 8 deletions 404.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

/**
* The template for displaying 404 pages (Not Found)
*
* Methods for TimberHelper can be found in the /functions sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
* The template for the 404 page
*/

namespace App;

use Timber\Timber;

$context = Timber::context();
Timber::render( '404.twig', $context );
Timber::render('404.twig', $context);
50 changes: 25 additions & 25 deletions archive.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
<?php

/**
* The template for displaying Archive pages.
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* Methods for TimberHelper can be found in the /lib sub-directory
* Learn more: https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.2
*/

$templates = array( 'archive.twig', 'index.twig' );
namespace App;

use Timber\Timber;

$context = Timber::context();
$templates = array('archive.twig', 'index.twig');

$context['title'] = 'Archive';
if ( is_day() ) {
$context['title'] = 'Archive: ' . get_the_date( 'D M Y' );
} elseif ( is_month() ) {
$context['title'] = 'Archive: ' . get_the_date( 'M Y' );
} elseif ( is_year() ) {
$context['title'] = 'Archive: ' . get_the_date( 'Y' );
} elseif ( is_tag() ) {
$context['title'] = single_tag_title( '', false );
} elseif ( is_category() ) {
$context['title'] = single_cat_title( '', false );
array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
} elseif ( is_post_type_archive() ) {
$context['title'] = post_type_archive_title( '', false );
array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
$title = 'Archive';
if (is_day()) {
$title = 'Archive: ' . get_the_date('D M Y');
} elseif (is_month()) {
$title = 'Archive: ' . get_the_date('M Y');
} elseif (is_year()) {
$title = 'Archive: ' . get_the_date('Y');
} elseif (is_tag()) {
$title = single_tag_title('', false);
} elseif (is_category()) {
$title = single_cat_title('', false);
array_unshift($templates, 'archive-' . get_query_var('cat') . '.twig');
} elseif (is_post_type_archive()) {
$title = post_type_archive_title('', false);
array_unshift($templates, 'archive-' . get_post_type() . '.twig');
}

$context['posts'] = Timber::get_posts();
$context = Timber::context([
'title' => $title,
]);

Timber::render( $templates, $context );
Timber::render($templates, $context);
26 changes: 16 additions & 10 deletions author.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<?php

/**
* The template for displaying Author Archive pages
*
* Methods for TimberHelper can be found in the /lib sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
*/

namespace App;

use Timber\Timber;

global $wp_query;

$context = Timber::context();
$context['posts'] = Timber::get_posts();
if ( isset( $wp_query->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,
'author' => $author,
]);

Timber::render(array('author.twig', 'archive.twig'), $context);
17 changes: 10 additions & 7 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

/**
* Timber starter-theme
* https://github.com/timber/starter-theme
* Functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
* @link https://github.com/timber/starter-theme
*/

namespace App;

use Timber\Timber;

// Load Composer dependencies.
require_once __DIR__ . '/vendor/autoload.php';

require_once __DIR__ . '/src/StarterSite.php';

Timber\Timber::init();

// Sets the directories (inside your theme) to find .twig files.
Timber::$dirname = [ 'templates', 'views' ];
Timber::init();

new StarterSite();
29 changes: 16 additions & 13 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<?php

/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists
* E.g., it puts together the home page when no home.php file exists.
*
* Methods for TimberHelper can be found in the /lib sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*/

$context = Timber::context();
$context['posts'] = Timber::get_posts();
$context['foo'] = 'bar';
$templates = array( 'index.twig' );
if ( is_home() ) {
array_unshift( $templates, 'front-page.twig', 'home.twig' );
use Timber\Timber;

$templates = array('index.twig');

if (is_home()) {
array_unshift($templates, 'front-page.twig', 'home.twig');
}
Timber::render( $templates, $context );

$context = Timber::context([
'foo' => 'bar',
]);

Timber::render($templates, $context);
22 changes: 7 additions & 15 deletions page.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* The template for displaying all pages.
*
Expand All @@ -7,22 +8,13 @@
* and that other 'pages' on your WordPress site will use a
* different template.
*
* To generate specific templates for your pages you can use:
* /mytheme/templates/page-mypage.twig
* (which will still route through this PHP file)
* OR
* /mytheme/page-mypage.php
* (in which case you'll want to duplicate this file and save to the above path)
*
* Methods for TimberHelper can be found in the /lib sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
*/

namespace App;

use Timber\Timber;

$context = Timber::context();
$post = $context['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-' . $post->post_name . '.twig', 'page.twig'), $context);
20 changes: 9 additions & 11 deletions search.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php

/**
* Search results page
*
* Methods for TimberHelper can be found in the /lib sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*/

$templates = array( 'search.twig', 'archive.twig', 'index.twig' );
use Timber\Timber;

$templates = array('search.twig', 'archive.twig', 'index.twig');

$context = Timber::context();
$context['title'] = 'Search results for ' . get_search_query();
$context['posts'] = Timber::get_posts();
$context = Timber::context([
'title' => 'Search results for ' . get_search_query(),
]);

Timber::render( $templates, $context );
Timber::render($templates, $context);
23 changes: 11 additions & 12 deletions single.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?php

/**
* The Template for displaying all single posts
*
* Methods for TimberHelper can be found in the /lib sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*/

$context = Timber::context();
$timber_post = Timber::get_post();
$context['post'] = $timber_post;
namespace App;

use Timber\Timber;

$context = Timber::context();
$post = $context['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);
}
Loading

0 comments on commit 0a6cb4d

Please sign in to comment.