Skip to content

Commit

Permalink
Merge 78f4955 into 8545e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Nov 10, 2022
2 parents 8545e2e + 78f4955 commit fec5303
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 33 deletions.
12 changes: 10 additions & 2 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,16 @@ public function render($file, $data = null, $expires = false, $cache_mode = self
$output = false;
if (false !== $expires) {
ksort($data);
$key = md5($file . json_encode($data));
$output = $this->get_cache($key, self::CACHEGROUP, $cache_mode);
$encoded = json_encode($data);

/**
* The encoding might fail, e.g. when an object has a recursion. In that case, we’d rather not cache the
* data instead of possibly returning the wrong data.
*/
if (false !== $encoded) {
$key = md5($file . $encoded);
$output = $this->get_cache($key, self::CACHEGROUP, $cache_mode);
}
}

if (false === $output || null === $output) {
Expand Down
30 changes: 29 additions & 1 deletion src/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* <script src="http://example.org/wp-content/themes/my-theme/static/js/all.js"></script>
* ```
*/
class Theme extends Core
class Theme extends Core implements \JsonSerializable
{
/**
* The human-friendly name of the theme (ex: `My Timber Starter Theme`)
Expand Down Expand Up @@ -209,4 +209,32 @@ public function display($header)
{
return $this->theme->display($header);
}

/**
* Returns serialized theme data.
*
* This data will e.g. be used when a `Timber\Theme` object is used to generate a key. We need to serialize the data
* because the $parent property is a reference to itself. This recursion would cause json_encode() to fail.
*
* @internal
* @return array
*/
public function jsonSerialize(): array
{
return [
'name' => $this->name,
'parent' => [
'name' => $this->parent->name,
'parent' => null,
'parent_slug' => null,
'slug' => $this->parent->slug,
'uri' => $this->parent->uri,
'version' => $this->parent->version,
],
'parent_slug' => $this->parent_slug,
'slug' => $this->slug,
'uri' => $this->uri,
'version' => $this->version,
];
}
}
23 changes: 23 additions & 0 deletions tests/Timber_UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,27 @@ protected function callMethod($obj, $name, array $args = [])
$method->setAccessible(true);
return $method->invokeArgs($obj, $args);
}

public static function _setupChildTheme()
{
$dest_dir = WP_CONTENT_DIR . '/themes/fake-child-theme';
if (!file_exists($dest_dir)) {
mkdir($dest_dir, 0777, true);
}
if (!file_exists($dest_dir . '/views')) {
mkdir($dest_dir . '/views', 0777, true);
}
copy(__DIR__ . '/assets/style.css', $dest_dir . '/style.css');
copy(__DIR__ . '/assets/single.twig', $dest_dir . '/views/single.twig');
}

public static function _setupParentTheme()
{
$dest_dir = WP_CONTENT_DIR . '/themes/twentyfifteen';
if (!file_exists($dest_dir . '/views')) {
mkdir($dest_dir . '/views', 0777, true);
}
copy(__DIR__ . '/assets/single-parent.twig', $dest_dir . '/views/single.twig');
copy(__DIR__ . '/assets/single-parent.twig', $dest_dir . '/views/single-parent.twig');
}
}
1 change: 1 addition & 0 deletions tests/assets/single-post-cached.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ post.id }}
31 changes: 31 additions & 0 deletions tests/test-timber-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,37 @@ public function testTimberLoaderCacheTransientsAdminLoggedIn()
self::_unswapFiles();
}

public function testTimberTransientCacheWithMultiplePosts()
{
$post_ids = $this->factory->post->create_many(3);

// Cache the first post.
$this->go_to(get_permalink($post_ids[0]));
$context = Timber::context();

ob_start();
Timber::render('assets/single-post-cached.twig', $context, 60);
$result = trim(ob_get_clean());

$this->assertEquals($post_ids[0], $result);

// Get second post.
$this->go_to(get_permalink($post_ids[1]));
$context = Timber::context();

ob_start();
Timber::render('assets/single-post-cached.twig', $context, 60);
$result = trim(ob_get_clean());

$this->assertEquals($post_ids[1], $result);

// Check if two transients exists.
global $wpdb;
$query = "SELECT * FROM {$wpdb->options} WHERE option_name LIKE '_transient_timberloader_%'";
$wpdb->get_results($query);
$this->assertSame(2, $wpdb->num_rows);
}

public function _swapFiles()
{
rename(__DIR__ . '/assets/single-post-rand.twig', __DIR__ . '/assets/single-post-rand.twig.tmp');
Expand Down
31 changes: 4 additions & 27 deletions tests/test-timber-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public function testTimberLocationsFilterAdded()

public function testTwigLoadsFromChildTheme()
{
$this->_setupParentTheme();
$this->_setupChildTheme();
parent::_setupParentTheme();
parent::_setupChildTheme();
$this->assertFileExists(WP_CONTENT_DIR . '/themes/fake-child-theme/style.css');
switch_theme('fake-child-theme');
$child_theme = get_stylesheet_directory_uri();
Expand All @@ -108,33 +108,10 @@ public function testTwigLoadsFromChildTheme()
$this->assertEquals('I am single.twig', trim($str));
}

public static function _setupChildTheme()
{
$dest_dir = WP_CONTENT_DIR . '/themes/fake-child-theme';
if (!file_exists($dest_dir)) {
mkdir($dest_dir, 0777, true);
}
if (!file_exists($dest_dir . '/views')) {
mkdir($dest_dir . '/views', 0777, true);
}
copy(__DIR__ . '/assets/style.css', $dest_dir . '/style.css');
copy(__DIR__ . '/assets/single.twig', $dest_dir . '/views/single.twig');
}

public static function _setupParentTheme()
{
$dest_dir = WP_CONTENT_DIR . '/themes/twentyfifteen';
if (!file_exists($dest_dir . '/views')) {
mkdir($dest_dir . '/views', 0777, true);
}
copy(__DIR__ . '/assets/single-parent.twig', $dest_dir . '/views/single.twig');
copy(__DIR__ . '/assets/single-parent.twig', $dest_dir . '/views/single-parent.twig');
}

public function testTwigLoadsFromParentTheme()
{
$this->_setupParentTheme();
$this->_setupChildTheme();
parent::_setupParentTheme();
parent::_setupChildTheme();
switch_theme('fake-child-theme');
$templates = ['single-parent.twig'];
$str = Timber::compile($templates, []);
Expand Down
4 changes: 2 additions & 2 deletions tests/test-timber-parent-child.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class TestTimberParentChild extends Timber_UnitTestCase
{
public function testParentChildGeneral()
{
TestTimberLoader::_setupParentTheme();
TestTimberLoader::_setupChildTheme();
self::_setupParentTheme();
self::_setupChildTheme();
switch_theme('fake-child-theme');
register_post_type('course');
//copy a specific file to the PARENT directory
Expand Down
35 changes: 34 additions & 1 deletion tests/test-timber-theme.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

use Timber\Theme;

class TestTimberTheme extends Timber_UnitTestCase
{
protected $backup_wp_theme_directories;

public $theme_slug = 'twentythirty';
public $theme_slug = 'twentynineteen';

public function testThemeVersion()
{
Expand Down Expand Up @@ -107,6 +109,37 @@ public function testThemeDisplay()
switch_theme('default');
}

public function testTimberThemeJsonSerialize()
{
self::_setupParentTheme();
self::_setupChildTheme();
switch_theme('fake-child-theme');

$theme = new Theme('fake-child-theme');

$encoded = json_encode($theme);

$this->assertNotFalse($encoded);

$decoded = json_decode($encoded, true);

$this->assertEquals([
'name' => 'Twenty Thirteen Child',
'parent' => [
'name' => 'Twenty Fifteen',
'parent' => null,
'parent_slug' => null,
'slug' => 'twentyfifteen',
'uri' => 'http://example.org/wp-content/themes/twentyfifteen',
'version' => '3.3',
],
'parent_slug' => 'twentyfifteen',
'slug' => 'fake-child-theme',
'uri' => 'http://example.org/wp-content/themes/twentyfifteen',
'version' => '1.0.0',
], $decoded);
}

public function set_up()
{
global $wp_theme_directories;
Expand Down

0 comments on commit fec5303

Please sign in to comment.