Skip to content

Commit 476bc10

Browse files
Merge remote-tracking branch 'upstream/master' into integration-tests-playwright
2 parents b42ab7b + cb5faa9 commit 476bc10

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/class-tiny-helpers.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/*
3+
* Tiny Compress Images - WordPress plugin.
4+
* Copyright (C) 2015-2018 Tinify B.V.
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the Free
8+
* Software Foundation; either version 2 of the License, or (at your option)
9+
* any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14+
* more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program; if not, write to the Free Software Foundation, Inc., 51
18+
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*/
20+
21+
class Tiny_Helpers {
22+
23+
/**
24+
* truncate_text will truncate a string to a given length.
25+
* When text is longer than the given length, the string will be truncated and
26+
* the last characters will be replaced with an ellipsis.
27+
*
28+
* We can use mb_strlen & mb_substr as WordPress provides a compat function for
29+
* it if mbstring php module is not installed.
30+
*
31+
* @param string $text the text
32+
* @param integer $length the maximum length of the string
33+
* @return string the truncated string
34+
*/
35+
public static function truncate_text( string $text, int $length ) {
36+
if ( mb_strlen( $text ) > $length ) {
37+
return mb_substr( $text, 0, $length - 3 ) . '...';
38+
}
39+
return $text;
40+
}
41+
}

src/class-tiny-image.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function get_wp_metadata() {
164164
}
165165

166166
public function file_type_allowed() {
167-
return in_array( $this->get_mime_type(), array( 'image/jpeg', 'image/png', 'image/webp') );
167+
return in_array( $this->get_mime_type(), array( 'image/jpeg', 'image/png', 'image/webp' ) );
168168
}
169169

170170
public function get_mime_type() {
@@ -385,7 +385,7 @@ public function get_latest_error() {
385385
if ( isset( $size->meta['error'] ) && isset( $size->meta['message'] ) ) {
386386
if ( null === $last_timestamp || $last_timestamp < $size->meta['timestamp'] ) {
387387
$last_timestamp = $size->meta['timestamp'];
388-
$error_message = mb_strimwidth( $size->meta['message'], 0 , 140, '...' );
388+
$error_message = Tiny_Helpers::truncate_text( $size->meta['message'], 140 );
389389
}
390390
}
391391
}

test/unit/TinyHelpersTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
require_once dirname(__FILE__) . '/TinyTestCase.php';
4+
5+
class Tiny_Helpers_Test extends Tiny_TestCase
6+
{
7+
public function set_up()
8+
{
9+
parent::set_up();
10+
}
11+
12+
public function test_truncates_text_to_length()
13+
{
14+
$input_text = "Text to be truncated because it is long";
15+
$expected_output = "Text to be trunca...";
16+
17+
$this->assertEquals($expected_output, Tiny_Helpers::truncate_text($input_text, 20));
18+
}
19+
20+
public function test_will_not_truncate_if_text_is_shorter_than_length()
21+
{
22+
$input_text = "Text will not be truncated";
23+
24+
$this->assertEquals($input_text, Tiny_Helpers::truncate_text($input_text, 26));
25+
}
26+
}

0 commit comments

Comments
 (0)