Skip to content

Commit 78d928d

Browse files
Levdbasgchtr
andauthored
fix: Fix deprecation notice in trim_words method when null is passed. (#3131)
* fix: fix deprecation notice in trim_words method when null is passed. --------- Co-authored-by: Lukas Gächter <[email protected]>
1 parent c6e4502 commit 78d928d

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/TextHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public static function trim_characters($text, $num_chars = 60, $more = '&hellip;
4343
*/
4444
public static function trim_words($text, $num_words = 55, $more = null, $allowed_tags = 'p a span b i br blockquote')
4545
{
46+
if (empty($text)) {
47+
return '';
48+
}
49+
4650
if (null === $more) {
4751
$more = \__('&hellip;');
4852
}
@@ -62,7 +66,7 @@ public static function trim_words($text, $num_words = 55, $more = null, $allowed
6266
* Default `p a span b i br blockquote`.
6367
*/
6468
$allowed_tags_array = \explode(' ', (string) \apply_filters('timber/trim_words/allowed_tags', $allowed_tags));
65-
$allowed_tags_array = \array_filter($allowed_tags_array, fn ($value) => $value !== '');
69+
$allowed_tags_array = \array_filter($allowed_tags_array, fn($value) => $value !== '');
6670
$allowed_tag_string = '<' . \implode('><', $allowed_tags_array) . '>';
6771

6872
$text = \strip_tags($text, $allowed_tag_string);

tests/test-timber-text-helper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ public function testTruncaseEnglish()
3131
$result = Timber::compile_string($str);
3232
$this->assertEquals(wp_trim_words($this->gettysburg, 5), $result);
3333
}
34+
35+
public function testNullTruncate()
36+
{
37+
$chars = null;
38+
$str = '{{ null|truncate( 5, true ) }}';
39+
$result = Timber::compile_string($str);
40+
$this->assertEquals('', $result);
41+
}
3442
}

0 commit comments

Comments
 (0)