Skip to content

Commit

Permalink
Remove compatibility with Timber 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Jan 23, 2024
1 parent 1dd3189 commit 5d66b92
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 56 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"composer/installers": "^1.0 || ^2.0",
"php": ">=7.4.0",
"timber/timber": "^1.19 || ^2.0"
"timber/timber": "^2.0"
},
"require-dev": {
"wpackagist-plugin/advanced-custom-fields": "5.*",
Expand Down
43 changes: 4 additions & 39 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,12 +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.
if ( version_compare( \Timber\Timber::$version, '2.0.0', '>=' ) ) {
add_filter('timber/twig/filters', [ $self, 'add_filters' ]);
add_filter('timber/twig/functions', [ $self, 'add_functions' ]);
} else {
add_filter( 'timber/twig', [ $self, 'filter_twig_legacy' ] );
}
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 @@ -212,35 +206,6 @@ public function add_functions( array $functions ): array {
return $functions;
}

/**
* Set filters to use Timmy filters and functions in Twig.
*
* @param \Twig\Environment $twig The Twig Environment instance.
*
* @return \Twig\Environment $twig
*/
public function filter_twig_legacy( $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' ) );

$twig->addFunction( new Twig_Function( 'get_timmy_image', [ '\Timmy\Timmy', 'get_image' ] ) );

$twig->addFunction( new Twig_Function( 'get_timber_image_responsive_acf', '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' ) );

return $twig;
}

/**
* Define global $_wp_additional_image_sizes with Timmy sizes
*
Expand Down Expand Up @@ -709,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
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

0 comments on commit 5d66b92

Please sign in to comment.