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 diff --git a/composer.json b/composer.json index bec3e41..15bbc87 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/lib/Timmy.php b/lib/Timmy.php index 8672a17..08f98ab 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,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 ); } @@ -169,32 +168,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; } /** @@ -686,10 +696,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/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'; 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 ); }