From c44ea0cc829278fcdc816ed2ce259b24aaa3e2fb Mon Sep 17 00:00:00 2001 From: Lukas Gaechter Date: Fri, 18 Aug 2023 17:42:09 +0200 Subject: [PATCH 1/7] Add compatibility with Timber 2.x --- composer.json | 2 +- lib/Timmy.php | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index bec3e41..22b5c9d 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "require": { "composer/installers": "^1.0 || ^2.0", "php": ">=7.4.0", - "timber/timber": "^1.19" + "timber/timber": "^1.19 || ^2.0.0-rc.1" }, "require-dev": { "wpackagist-plugin/advanced-custom-fields": "5.*", diff --git a/lib/Timmy.php b/lib/Timmy.php index 23f3e85..73bb03c 100644 --- a/lib/Timmy.php +++ b/lib/Timmy.php @@ -46,7 +46,11 @@ 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' ] ); + if ( version_compare( \Timber\Timber::$version, '2.0.0', '>=' ) ) { + add_filter('timber/twig/filters', [ $self, 'filter_twig' ]); + } else { + add_filter( 'timber/twig', [ $self, 'filter_twig_legacy' ] ); + } add_filter( 'timmy/resize/ignore', array( __CLASS__, 'ignore_unallowed_files' ), 10, 2 ); } @@ -167,6 +171,34 @@ public static function get_image( $attachment, $size ) { return $image; } + /** + * Adds Twig filters. + * + * @param array $filters + * + * @return array + */ + public function filter_twig( array $filters ) { + $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' ]; + $filters['get_timmy_image'] = [ 'callable' => [ '\Timmy\Timmy', 'get_image' ] ]; + + // ACF. + $filters['get_timber_image_responsive_acf'] = [ 'callable' => 'get_timber_image_responsive_acf' ]; + + // Image texts. + $filters['get_timber_image_alt'] = [ 'callable' => 'get_timber_image_alt' ]; + $filters['get_timber_image_caption'] = [ 'callable' => 'get_timber_image_caption' ]; + $filters['get_timber_image_description'] = [ 'callable' => 'get_timber_image_description' ]; + + return $filters; + } + /** * Set filters to use Timmy filters and functions in Twig. * @@ -174,7 +206,7 @@ public static function get_image( $attachment, $size ) { * * @return \Twig\Environment $twig */ - public function filter_twig( $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' ) ); From 262447de3afb49f9605a84f17e65656b6ed96f70 Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Tue, 19 Sep 2023 17:30:57 +0200 Subject: [PATCH 2/7] feat: Add compatibility for Timber 2.0 - Use timber/twig/functions' filter --- lib/Timmy.php | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/Timmy.php b/lib/Timmy.php index 73bb03c..46ad1ff 100644 --- a/lib/Timmy.php +++ b/lib/Timmy.php @@ -47,7 +47,8 @@ public static function init() { // 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, 'filter_twig' ]); + 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' ] ); } @@ -178,7 +179,7 @@ public static function get_image( $attachment, $size ) { * * @return array */ - public function filter_twig( array $filters ) { + 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' ]; @@ -186,17 +187,29 @@ public function filter_twig( array $filters ) { $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' ]; - $filters['get_timmy_image'] = [ 'callable' => [ '\Timmy\Timmy', 'get_image' ] ]; + + return $filters; + } + + /** + * Adds Twig functions. + * + * @param array $functions + * + * @return array + */ + public function add_functions( array $functions ): array { + $functions['get_timmy_image'] = [ 'callable' => [ '\Timmy\Timmy', 'get_image' ] ]; // ACF. - $filters['get_timber_image_responsive_acf'] = [ 'callable' => 'get_timber_image_responsive_acf' ]; + $functions['get_timber_image_responsive_acf'] = [ 'callable' => 'get_timber_image_responsive_acf' ]; // Image texts. - $filters['get_timber_image_alt'] = [ 'callable' => 'get_timber_image_alt' ]; - $filters['get_timber_image_caption'] = [ 'callable' => 'get_timber_image_caption' ]; - $filters['get_timber_image_description'] = [ 'callable' => '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 $filters; + return $functions; } /** From c7784b0da552147cde28b971c3120d94087b9ab5 Mon Sep 17 00:00:00 2001 From: Lukas Gaechter Date: Fri, 17 Nov 2023 10:51:54 +0100 Subject: [PATCH 3/7] Update Timber dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 22b5c9d..629e4ec 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "require": { "composer/installers": "^1.0 || ^2.0", "php": ">=7.4.0", - "timber/timber": "^1.19 || ^2.0.0-rc.1" + "timber/timber": "^1.19 || ^2.0" }, "require-dev": { "wpackagist-plugin/advanced-custom-fields": "5.*", From 1dd3189f374a921cc7cb1443a0df4bb36eed5205 Mon Sep 17 00:00:00 2001 From: Lukas Gaechter Date: Wed, 13 Dec 2023 10:01:30 +0100 Subject: [PATCH 4/7] fix: Fix tests with Timber 2.0 --- tests/bootstrap.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 31de165..a972bf0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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'; From 5d66b927941646c023f3b720e9453c855be5e36e Mon Sep 17 00:00:00 2001 From: Lukas Gaechter Date: Tue, 23 Jan 2024 22:11:26 +0100 Subject: [PATCH 5/7] Remove compatibility with Timber 1.x --- composer.json | 2 +- lib/Timmy.php | 43 ++++--------------------------------- tests/TimmyUnitTestCase.php | 6 +++--- tests/test-image-texts.php | 2 +- tests/test-timmy.php | 21 ++++++++---------- 5 files changed, 18 insertions(+), 56 deletions(-) diff --git a/composer.json b/composer.json index 629e4ec..a17fda7 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/lib/Timmy.php b/lib/Timmy.php index 46ad1ff..f780b65 100644 --- a/lib/Timmy.php +++ b/lib/Timmy.php @@ -3,8 +3,6 @@ namespace Timmy; use Timber; -use Timber\Twig_Filter; -use Timber\Twig_Function; use WP_Post; /** @@ -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 ); } @@ -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 * @@ -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. diff --git a/tests/TimmyUnitTestCase.php b/tests/TimmyUnitTestCase.php index a689ee8..a0fd71b 100644 --- a/tests/TimmyUnitTestCase.php +++ b/tests/TimmyUnitTestCase.php @@ -1,7 +1,7 @@ set_description( $attachment_id, $args['description'] ); } - return new Image( $attachment_id ); + return Timber::get_image( $attachment_id ); } /** @@ -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; } diff --git a/tests/test-image-texts.php b/tests/test-image-texts.php index cfd0bf6..8747e52 100644 --- a/tests/test-image-texts.php +++ b/tests/test-image-texts.php @@ -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' ); diff --git a/tests/test-timmy.php b/tests/test-timmy.php index 0f542cd..6df2790 100644 --- a/tests/test-timmy.php +++ b/tests/test-timmy.php @@ -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() { @@ -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() { @@ -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 ); } From d9504b0370baf78261e3324cc570e30df8e52fee Mon Sep 17 00:00:00 2001 From: Lukas Gaechter Date: Fri, 1 Mar 2024 16:27:22 +0100 Subject: [PATCH 6/7] Update dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a17fda7..15bbc87 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "require": { "composer/installers": "^1.0 || ^2.0", - "php": ">=7.4.0", + "php": "^7.4 || ^8.0", "timber/timber": "^2.0" }, "require-dev": { From 09ffd7e749348d8e8162c59db367ad6c22c7dcb3 Mon Sep 17 00:00:00 2001 From: Lukas Gaechter Date: Mon, 4 Mar 2024 08:43:25 +0100 Subject: [PATCH 7/7] Run release please on 2.x --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 94a2c48..e7c5add 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,7 +4,7 @@ on: push: branches: - 1.x - - 1.x-release-workflow + - 2.x permissions: contents: write