Skip to content

Commit

Permalink
Merge pull request #2719 from timber/2.x-remove-string-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
nlemoine committed Mar 17, 2023
2 parents 316bf64 + 0d65559 commit 86f9d10
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 35 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"require": {
"php": "^7.4 || ^8.0",
"composer/installers": "^1.0 || ^2.0",
"symfony/polyfill-php80": "^1.27",
"twig/twig": "^2.15.3 || ^3.0"
},
"require-dev": {
Expand Down
5 changes: 5 additions & 0 deletions docs/v2/upgrade-guides/2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,11 @@ The following functions and properties were **removed from the codebase**, eithe
- `get_current_url()` – use `Timber\URLHelper::get_current_url()` instead
- `filter_array()` – use `array_filter()` or `Timber\Helper::wp_list_filter()` instead.

### Timber\TextHelper

- `starts_with()` - use `str_starts_with()` instead.
- `ends_with()` - use `str_ends_with()` instead.

### Timber\Integration\Command

The whole `Timber\Integration\Command` class was removed. Its methods were moved over to the `Timber\Cache\Cleaner` class.
Expand Down
8 changes: 4 additions & 4 deletions src/ImageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public static function is_svg($file_path)
return false;
}

if (TextHelper::ends_with(strtolower($file_path), '.svg')) {
if (str_ends_with(strtolower($file_path), '.svg')) {
return true;
}

Expand Down Expand Up @@ -537,14 +537,14 @@ public static function analyze_url($url)
];
$upload_dir = wp_upload_dir();
$tmp = $url;
if (TextHelper::starts_with($tmp, ABSPATH) || TextHelper::starts_with($tmp, '/srv/www/')) {
if (str_starts_with($tmp, ABSPATH) || str_starts_with($tmp, '/srv/www/')) {
// we've been given a dir, not an url
$result['absolute'] = true;
if (TextHelper::starts_with($tmp, $upload_dir['basedir'])) {
if (str_starts_with($tmp, $upload_dir['basedir'])) {
$result['base'] = self::BASE_UPLOADS; // upload based
$tmp = URLHelper::remove_url_component($tmp, $upload_dir['basedir']);
}
if (TextHelper::starts_with($tmp, WP_CONTENT_DIR)) {
if (str_starts_with($tmp, WP_CONTENT_DIR)) {
$result['base'] = self::BASE_CONTENT; // content based
$tmp = URLHelper::remove_url_component($tmp, WP_CONTENT_DIR);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ public function audio()
$audio = false;

// Only get audio from the content if a playlist isn’t present.
if (false === strpos($this->content(), 'wp-playlist-script')) {
if (!str_contains($this->content(), 'wp-playlist-script')) {
$audio = get_media_embedded_in_content($this->content(), ['audio']);
}

Expand All @@ -1929,7 +1929,7 @@ public function video()
$video = false;

// Only get video from the content if a playlist isn't present.
if (false === strpos($this->content(), 'wp-playlist-script')) {
if (!str_contains($this->content(), 'wp-playlist-script')) {
$video = get_media_embedded_in_content($this->content(), ['video', 'object', 'embed', 'iframe']);
}

Expand Down
27 changes: 0 additions & 27 deletions src/TextHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,33 +106,6 @@ public static function remove_tags($string, $tags = [])
return preg_replace('#<(' . implode('|', $tags) . ')(?:[^>]+)?>.*?</\1>#s', '', $string);
}

/**
* @api
* @param string $haystack
* @param string $needle
* @return boolean
*/
public static function starts_with($haystack, $needle)
{
if (0 === strpos($haystack, $needle)) {
return true;
}
return false;
}

/**
* @api
* Does the string in question (haystack)
* end with the substring in question (needle)?
* @param string $haystack
* @param string $needle
* @return boolean
*/
public static function ends_with($haystack, $needle)
{
return (substr($haystack, strlen($haystack) - strlen($needle)) == $needle);
}

/**
*
*
Expand Down
4 changes: 2 additions & 2 deletions tests/test-timber-text-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class TestTimberTextHelper extends Timber_UnitTestCase

public function testStartsWith()
{
$maybe_starts_with = Timber\TextHelper::starts_with($this->gettysburg, 'Four score');
$maybe_starts_with = str_starts_with($this->gettysburg, 'Four score');
$this->assertTrue($maybe_starts_with);
}

public function testDontStartWith()
{
$maybe_starts_with = Timber\TextHelper::starts_with($this->gettysburg, "Can't get enough of that SugarCrisp");
$maybe_starts_with = str_starts_with($this->gettysburg, "Can't get enough of that SugarCrisp");
$this->assertFalse($maybe_starts_with);
}

Expand Down

0 comments on commit 86f9d10

Please sign in to comment.