Skip to content

Commit 6f3f670

Browse files
committed
added mb_ pollyfils and added a test suite to ensure it still works
1 parent 0da2d71 commit 6f3f670

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

.github/workflows/unit-tests.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@ jobs:
1111
strategy:
1212
matrix:
1313
operating-system: [ubuntu-latest]
14-
php-versions: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
14+
include:
15+
- php: '7.4'
16+
mbstring: true
17+
- php: '8.0'
18+
mbstring: true
19+
- php: '8.1'
20+
mbstring: true
21+
- php: '8.2'
22+
mbstring: true
23+
- php: '8.3'
24+
mbstring: true
25+
- php: '8.3'
26+
mbstring: false # This is the special case
27+
- php: '8.4'
28+
mbstring: true
1529
runs-on: ${{ matrix.operating-system }}
1630
services:
1731
# Setup MYSQL
@@ -30,11 +44,12 @@ jobs:
3044
3145
steps:
3246
- uses: actions/checkout@v4
33-
- name: Setup proper PHP version
47+
- name: Setup PHP ${{ matrix.php }} (mbstring = ${{ matrix.mbstring }})
3448
uses: shivammathur/setup-php@v2
3549
with:
3650
php-version: ${{ matrix.php }}
3751
tools: composer
52+
extensions: ${{ matrix.mbstring && 'mbstring' || '' }}
3853

3954
- name: Validate composer.json and composer.lock
4055
run: composer validate --strict

functions.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,63 @@
2121

2222
// region
2323

24+
/**
25+
* Polyfill for mb_strimwidth.
26+
*/
27+
if ( ! function_exists( 'mb_strimwidth' ) ) {
28+
/**
29+
* Polyfill for mb_strimwidth.
30+
*
31+
* @param string $source The string to trim.
32+
* @param integer $start The starting position.
33+
* @param integer $width The width to trim to.
34+
* @param string $trim_marker The marker to append if the string is trimmed.
35+
* @param string $encoding The character encoding.
36+
*
37+
* @return string
38+
*/
39+
function mb_strimwidth( $source, $start, $width, $trim_marker = '', $encoding = 'UTF-8' ) {
40+
// Fallback using mb_substr if available
41+
if ( function_exists( 'mb_substr' ) ) {
42+
$substr = mb_substr( $source, $start, $width, $encoding );
43+
if ( mb_strlen( $source, $encoding ) > $width ) {
44+
return $substr . $trim_marker;
45+
}
46+
return $substr;
47+
} else {
48+
// Rough fallback using substr (not multibyte-safe!)
49+
$substr = substr( $source, $start, $width );
50+
if ( strlen( $source ) > $width ) {
51+
return $substr . $trim_marker;
52+
}
53+
return $substr;
54+
}
55+
}
56+
}
57+
58+
/**
59+
* Polyfill for mb_strlen.
60+
*/
61+
if ( ! function_exists( 'mb_strlen' ) ) {
62+
/**
63+
* Polyfill for mb_strlen.
64+
*
65+
* @param string $source The string to measure.
66+
* @param string $encoding The character encoding.
67+
*
68+
* @return integer
69+
*/
70+
function mb_strlen( $source, $encoding = 'UTF-8' ) {
71+
// Use preg_match_all to count UTF-8 characters
72+
if ( 'UTF-8' === $encoding ) {
73+
return preg_match_all( '/./u', $source, $matches );
74+
}
75+
76+
// Fallback: use strlen (not multibyte-safe!)
77+
return strlen( $source );
78+
}
79+
}
80+
2481
/**
2582
* Returns the plugin's main class instance.
2683
*

0 commit comments

Comments
 (0)