Skip to content
6 changes: 5 additions & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
php: [ '8.1', '8.2', '8.3', '8.4']
runs-on: ${{ matrix.operating-system }}
services:
# Setup MYSQL
Expand All @@ -34,8 +34,12 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ matrix.php != '8.1' && 'mbstring' || '' }}
tools: composer

- name: Show PHP version
run: php -v

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

Expand Down
57 changes: 57 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,63 @@

// region

/**
* Polyfill for mb_strimwidth.
*/
if ( ! function_exists( 'mb_strimwidth' ) ) {
/**
* Polyfill for mb_strimwidth.
*
* @param string $source The string to trim.
* @param integer $start The starting position.
* @param integer $width The width to trim to.
* @param string $trim_marker The marker to append if the string is trimmed.
* @param string $encoding The character encoding.
*
* @return string
*/
function mb_strimwidth( $source, $start, $width, $trim_marker = '', $encoding = 'UTF-8' ) {
// Fallback using mb_substr if available
if ( function_exists( 'mb_substr' ) ) {
$substr = mb_substr( $source, $start, $width, $encoding );
if ( mb_strlen( $source, $encoding ) > $width ) {
return $substr . $trim_marker;
}
return $substr;
} else {
// Rough fallback using substr (not multibyte-safe!)
$substr = substr( $source, $start, $width );
if ( strlen( $source ) > $width ) {
return $substr . $trim_marker;
}
return $substr;
}
}
}

/**
* Polyfill for mb_strlen.
*/
if ( ! function_exists( 'mb_strlen' ) ) {
/**
* Polyfill for mb_strlen.
*
* @param string $source The string to measure.
* @param string $encoding The character encoding.
*
* @return integer
*/
function mb_strlen( $source, $encoding = 'UTF-8' ) {
// Use preg_match_all to count UTF-8 characters
if ( 'UTF-8' === $encoding ) {
return preg_match_all( '/./u', $source, $matches );
}

// Fallback: use strlen (not multibyte-safe!)
return strlen( $source );
}
}

/**
* Returns the plugin's main class instance.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Settings/Test_Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function test_can_set_drop_tables_on_uninstall(): void {
public function test_can_set_and_get_migrations(): void {
$migration_1 = $this->createMock( Abstract_Migration::class );
$migration_2 = $this->createMock( Abstract_Migration::class );
$migrations = array( $migration_1::class, $migration_2::class );
$migrations = [get_class($migration_1), get_class($migration_2)];
Settings::update_migrations( $migrations );
$this->assertEquals( $migrations, Settings::migrations() );
}
Expand Down
1 change: 1 addition & 0 deletions tests/Test_Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ public static function normalize_url_provider(): array {
public function test_can_normalize_url( string $url, string $expected ): void {
$this->assertSame( $expected, \wpcomsp_wayback_link_fixer_normalize_url( $url ) );
}

}