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

Extimg-patch #2825

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ public function src(): string
* </figure>
* ```
*
* @return string
* @return string|null
*/
public function caption(): string
public function caption(): ?string
{
/**
* Filters the attachment caption.
Expand Down
13 changes: 4 additions & 9 deletions src/ExternalImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,11 @@ public static function build($url, array $args = []): ?ExternalImage
return null;
}

$args = \wp_parse_args($args, [
'alt' => '',
]);

$external_image = new static();

if (!empty($args['alt'])) {
$external_image->alt_text = (string) $args['alt'];
}

if (!empty($args['caption'])) {
$external_image->caption = (string) $args['caption'];
}
Expand Down Expand Up @@ -471,13 +466,13 @@ protected function init_with_url($url)
*
* @return string Alt text stored in WordPress.
*/
public function alt(): string
public function alt(): ?string
{
return $this->alt_text;
return $this->alt_text ?? null;
}

public function caption(): string
public function caption(): ?string
{
return $this->caption;
return $this->caption ?? null;
}
}
4 changes: 2 additions & 2 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ public function aspect()
* alt="You should always add alt texts to your images for better accessibility" />
* ```
*
* @return string Alt text stored in WordPress.
* @return string|null Alt text stored in WordPress.
*/
public function alt(): string
public function alt(): ?string
{
$alt = $this->meta('_wp_attachment_image_alt');
return \trim(\wp_strip_all_tags($alt));
Expand Down
8 changes: 4 additions & 4 deletions src/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public function path(): string;
*
* @api
* @since 2.0
* @return string
* @return string|null
*/
public function caption(): string;
public function caption(): ?string;

/**
* Gets filesize in bytes.
Expand Down Expand Up @@ -95,7 +95,7 @@ public function aspect();
* empty.
*
* @api
* @return string Alt text stored in WordPress.
* @return string|null Alt text stored in WordPress.
*/
public function alt(): string;
public function alt(): ?string;
}
6 changes: 6 additions & 0 deletions tests/test-external-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,25 @@

public function testExternalImageWithInvalidUrl()
{
$image = Timber::get_external_image(78);

Check failure on line 62 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to static method get_external_image() on an unknown class Timber.

$this->assertNull($image);

Check failure on line 64 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestExternalImage::assertNull().
}

public function testExternalImageWithAbsolutePath()
{
$dest = self::copy_image_to_stylesheet('assets/images');
$this->addFile($dest);
$this->assertFileExists($dest);

Check failure on line 71 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestExternalImage::assertFileExists().

$image = Timber::get_external_image($dest);

Check failure on line 73 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to static method get_external_image() on an unknown class Timber.

$this->assertSame(

Check failure on line 75 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestExternalImage::assertSame().
'http://example.org/wp-content/themes/timber-test-theme/assets/images/cardinals.jpg',
$image->src()
);

$this->assertSame(

Check failure on line 80 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestExternalImage::assertSame().
'http://example.org/wp-content/themes/timber-test-theme/assets/images/cardinals.jpg',
$image->src('medium')
);
Expand All @@ -87,12 +87,12 @@
{
$dest = self::copy_image_to_stylesheet('assets/images');
$this->addFile($dest);
$this->assertFileExists($dest);

Check failure on line 90 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestExternalImage::assertFileExists().

$image = Timber::get_external_image('/wp-content/themes/timber-test-theme/assets/images/cardinals.jpg');

Check failure on line 92 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to static method get_external_image() on an unknown class Timber.
$image2 = Timber::get_external_image('wp-content/themes/timber-test-theme/assets/images/cardinals.jpg');

Check failure on line 93 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to static method get_external_image() on an unknown class Timber.

$this->assertSame(

Check failure on line 95 in tests/test-external-image.php

View workflow job for this annotation

GitHub Actions / PHP static analysis

Call to an undefined method TestExternalImage::assertSame().
'http://example.org/wp-content/themes/timber-test-theme/assets/images/cardinals.jpg',
$image->src()
);
Expand Down Expand Up @@ -243,6 +243,9 @@
$this->addFile($dest);
$this->assertFileExists($dest);

$image = Timber::get_external_image($dest);
$this->assertSame(null, $image->alt());

$image = Timber::get_external_image($dest, [
'alt' => 'Cardinals logo',
]);
Expand All @@ -258,6 +261,9 @@
$this->addFile($dest);
$this->assertFileExists($dest);

$image = Timber::get_external_image($dest);
$this->assertSame(null, $image->caption());

$image = Timber::get_external_image($dest, [
'caption' => 'Cardinals logo',
]);
Expand Down