Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with Timber 2.x #71

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"license": "MIT",
"require": {
"composer/installers": "^1.0 || ^2.0",
"php": ">=7.4.0",
"timber/timber": "^1.19"
"php": "^7.4 || ^8.0",
"timber/timber": "^2.0"
},
"require-dev": {
"wpackagist-plugin/advanced-custom-fields": "5.*",
Expand Down
56 changes: 33 additions & 23 deletions lib/Timmy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Timmy;

use Timber;
use Timber\Twig_Filter;
use Timber\Twig_Function;
use WP_Post;

/**
Expand Down Expand Up @@ -46,7 +44,8 @@ public static function init() {
add_action( 'after_setup_theme', [ $self, 'after_setup_theme' ] );

// Add filters and functions to integrate Timmy into Timber and Twig.
add_filter( 'timber/twig', [ $self, 'filter_twig' ] );
add_filter('timber/twig/filters', [ $self, 'add_filters' ]);
add_filter('timber/twig/functions', [ $self, 'add_functions' ]);

add_filter( 'timmy/resize/ignore', array( __CLASS__, 'ignore_unallowed_files' ), 10, 2 );
}
Expand Down Expand Up @@ -168,32 +167,43 @@ public static function get_image( $attachment, $size ) {
}

/**
* Set filters to use Timmy filters and functions in Twig.
* Adds Twig filters.
*
* @param \Twig\Environment $twig The Twig Environment instance.
* @param array $filters
*
* @return \Twig\Environment $twig
* @return array
*/
public function filter_twig( $twig ) {
$twig->addFilter( new Twig_Filter( 'get_timber_image', 'get_timber_image' ) );
$twig->addFilter( new Twig_Filter( 'get_timber_image_src', 'get_timber_image_src' ) );
$twig->addFilter( new Twig_Filter( 'get_timber_image_srcset', 'get_timber_image_srcset' ) );
$twig->addFilter( new Twig_Filter( 'get_timber_image_responsive', 'get_timber_image_responsive' ) );
$twig->addFilter( new Twig_Filter( 'get_timber_image_responsive_src', 'get_timber_image_responsive_src' ) );
$twig->addFilter( new Twig_Filter( 'get_timber_picture_responsive', 'get_timber_picture_responsive' ) );

$twig->addFilter( new Twig_Filter( 'lazy', 'make_timber_image_lazy' ) );
public function add_filters( array $filters ): array {
$filters['get_timber_image'] = [ 'callable' => 'get_timber_image' ];
$filters['get_timber_image_src'] = [ 'callable' => 'get_timber_image_src' ];
$filters['get_timber_image_srcset'] = [ 'callable' => 'get_timber_image_srcset' ];
$filters['get_timber_image_responsive'] = [ 'callable' => 'get_timber_image_responsive' ];
$filters['get_timber_image_responsive_src'] = [ 'callable' => 'get_timber_image_responsive_src' ];
$filters['get_timber_picture_responsive'] = [ 'callable' => 'get_timber_picture_responsive' ];
$filters['lazy'] = [ 'callable' => 'make_timber_image_lazy' ];

return $filters;
}

$twig->addFunction( new Twig_Function( 'get_timmy_image', [ '\Timmy\Timmy', 'get_image' ] ) );
/**
* Adds Twig functions.
*
* @param array $functions
*
* @return array
*/
public function add_functions( array $functions ): array {
$functions['get_timmy_image'] = [ 'callable' => [ '\Timmy\Timmy', 'get_image' ] ];

$twig->addFunction( new Twig_Function( 'get_timber_image_responsive_acf', 'get_timber_image_responsive_acf' ) );
// ACF.
$functions['get_timber_image_responsive_acf'] = [ 'callable' => 'get_timber_image_responsive_acf' ];

// Image texts.
$twig->addFunction( new Twig_Function( 'get_timber_image_alt', 'get_timber_image_alt' ) );
$twig->addFunction( new Twig_Function( 'get_timber_image_caption', 'get_timber_image_caption' ) );
$twig->addFunction( new Twig_Function( 'get_timber_image_description', 'get_timber_image_description' ) );
$functions['get_timber_image_alt'] = [ 'callable' => 'get_timber_image_alt' ];
$functions['get_timber_image_caption'] = [ 'callable' => 'get_timber_image_caption' ];
$functions['get_timber_image_description'] = [ 'callable' => 'get_timber_image_description' ];

return $twig;
return $functions;
}

/**
Expand Down Expand Up @@ -664,10 +674,10 @@ public function filter_wp_prepare_attachment_for_js( $response, $attachment, $me
*/
public static function get_timber_image( $timber_image ) {
if ( is_numeric( $timber_image ) ) {
$timber_image = new Timber\Image( $timber_image );
$timber_image = Timber::get_image( $timber_image );
} elseif ( is_array( $timber_image ) && isset( $timber_image['ID'] ) ) {
// Convert an ACF image array into a Timber image.
$timber_image = new Timber\Image( $timber_image['ID'] );
$timber_image = Timber::get_image( $timber_image['ID'] );
}

// Check if non-empty TimberImage was found before returning it.
Expand Down
6 changes: 3 additions & 3 deletions tests/TimmyUnitTestCase.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Timber\Image;
use Timber\Post;
use Timber\Timber;
use Yoast\WPTestUtils\WPIntegration\TestCase;

/**
Expand Down Expand Up @@ -148,7 +148,7 @@ public function create_image( $args = [] ) {
$this->set_description( $attachment_id, $args['description'] );
}

return new Image( $attachment_id );
return Timber::get_image( $attachment_id );
}

/**
Expand All @@ -160,7 +160,7 @@ public function create_post_with_image() {

set_post_thumbnail( $post_id, $attachment_id );

$post = new Post( $post_id );
$post = Timber::get_post( $post_id );

return $post;
}
Expand Down
7 changes: 6 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
function _manually_load_plugin() {
require dirname( __FILE__ ) . '/../vendor/autoload.php';

new Timber\Timber();
if (version_compare(Timber\Timber::$version, '2.0.0', '>=')) {
Timber\Timber::init();
} else {
new Timber\Timber();
}

Timmy::init();

require dirname( __FILE__ ) . '/../wp-content/plugins/advanced-custom-fields/acf.php';
Expand Down
2 changes: 1 addition & 1 deletion tests/test-image-texts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_get_timber_image_texts() {
$this->set_description( $attachment->ID, $description );

// Reload attachment to get updated values.
$attachment = new Image( $attachment->ID );
$attachment = Timber::get_image( $attachment->ID );

$result = get_timber_image_texts( $attachment, 'large' );

Expand Down
21 changes: 9 additions & 12 deletions tests/test-timmy.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ function test_attached_image_with_missing_post() {
}

public function test_timmy_ignores_pdf() {
$attachment = $this->create_image( [ 'file' => 'example.pdf' ] );
$result = image_downsize( $attachment->ID );
$attachment_id = $this->create_image_attachment( 0, 'example.pdf' );
$result = image_downsize( $attachment_id );

$image = false;
$this->assertEquals( $image, $result );
$this->assertFalse( $result );
}

public function test_timmy_ignores_svg() {
Expand All @@ -121,12 +120,10 @@ public function test_timmy_ignores_svg() {
}

public function test_timmy_ignores_video() {
$attachment = $this->create_image( [ 'file' => 'video.mp4' ] );
$result = image_downsize( $attachment->ID );
$attachment_id = $this->create_image_attachment( 0, 'video.mp4' );
$result = image_downsize( $attachment_id );

$image = false;

$this->assertEquals( $image, $result );
$this->assertFalse( $result );
}

public function test_timmy_ignores_svg_when_generating_metadata() {
Expand All @@ -146,9 +143,9 @@ public function test_timmy_ignores_gif_when_generating_metadata() {
}

public function test_timmy_ignores_video_when_generating_metadata() {
$attachment = $this->create_image( [ 'file' => 'video.mp4' ] );
$file_src = get_attached_file( $attachment->ID );
$meta = wp_generate_attachment_metadata( $attachment->ID, $file_src );
$attachment_id = $this->create_image_attachment( 0, 'video.mp4' );
$file_src = get_attached_file( $attachment_id );
$meta = wp_generate_attachment_metadata( $attachment_id, $file_src );

$this->assertArrayNotHasKey( 'sizes', $meta );
}
Expand Down
Loading