Skip to content

Commit 4b0dab5

Browse files
committed
Add PHP polyfills
1 parent 915df6f commit 4b0dab5

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

load.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
define( 'SQLITE_MAIN_FILE', __FILE__ );
1616

17+
require_once __DIR__ . '/php-polyfills.php';
1718
require_once __DIR__ . '/admin-page.php';
1819
require_once __DIR__ . '/activate.php';
1920
require_once __DIR__ . '/deactivate.php';

php-polyfills.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Polyfills for php 7 & 8 functions
4+
*
5+
* @package wp-sqlite-integration
6+
*/
7+
8+
if ( ! function_exists( 'str_starts_with' ) ) {
9+
/**
10+
* Check if a string starts with a specific substring.
11+
*
12+
* @param string $haystack The string to search in.
13+
* @param string $needle The string to search for.
14+
*
15+
* @see https://www.php.net/manual/en/function.str-starts-with
16+
*
17+
* @return bool
18+
*/
19+
function str_starts_with( string $haystack, string $needle ) {
20+
return empty( $needle ) || 0 === strpos( $haystack, $needle );
21+
}
22+
}
23+
24+
if ( ! function_exists( 'str_contains' ) ) {
25+
/**
26+
* Check if a string contains a specific substring.
27+
*
28+
* @param string $haystack The string to search in.
29+
* @param string $needle The string to search for.
30+
*
31+
* @see https://www.php.net/manual/en/function.str-contains
32+
*
33+
* @return bool
34+
*/
35+
function str_contains( string $haystack, string $needle ) {
36+
return empty( $needle ) || false !== strpos( $haystack, $needle );
37+
}
38+
}
39+
40+
if ( ! function_exists( 'str_ends_with' ) ) {
41+
/**
42+
* Check if a string ends with a specific substring.
43+
*
44+
* @param string $haystack The string to search in.
45+
* @param string $needle The string to search for.
46+
*
47+
* @see https://www.php.net/manual/en/function.str-ends-with
48+
*
49+
* @return bool
50+
*/
51+
function str_ends_with( string $haystack, string $needle ) {
52+
return empty( $needle ) || substr( $haystack, -strlen( $needle ) === $needle );
53+
}
54+
}

tests/bootstrap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function apply_filters( $tag, $value, ...$args ) {
3333
}
3434

3535
/**
36-
* Polyfills for php 8 functions
36+
* Polyfills for php 7 & 8 functions
3737
*/
3838

3939
if ( ! function_exists( 'str_starts_with' ) ) {

0 commit comments

Comments
 (0)