Image
2 days ago
Select
2 days ago
Arrays.php
2 days ago
AvailableTranslations.php
2 days ago
Creatable.php
2 days ago
Dashicon.php
2 days ago
Date.php
2 days ago
Html.php
2 days ago
Icon.php
2 days ago
Image.php
2 days ago
LocalFile.php
2 days ago
Mbstring.php
2 days ago
MediaIdResolver.php
2 days ago
Menu.php
2 days ago
Network.php
2 days ago
Post.php
2 days ago
Strings.php
2 days ago
Taxonomy.php
2 days ago
User.php
2 days ago
UserRoles.php
2 days ago
WpDateFormat.php
2 days ago
Strings.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Helper; |
| 6 | |
| 7 | use WP_HTML_Tag_Processor; |
| 8 | |
| 9 | class Strings extends Creatable |
| 10 | { |
| 11 | public function starts_with(string $haystack, string $needle): bool |
| 12 | { |
| 13 | return str_starts_with($haystack, $needle); |
| 14 | } |
| 15 | |
| 16 | public function remove_prefix(string $string, string $prefix): string |
| 17 | { |
| 18 | return $this->starts_with($string, $prefix) |
| 19 | ? substr($string, strlen($prefix)) |
| 20 | : $string; |
| 21 | } |
| 22 | |
| 23 | public function remove_suffix(string $string, string $suffix): string |
| 24 | { |
| 25 | return $this->ends_with($string, $suffix) |
| 26 | ? substr($string, 0, -strlen($suffix)) |
| 27 | : $string; |
| 28 | } |
| 29 | |
| 30 | public function ends_with(string $haystack, string $needle): bool |
| 31 | { |
| 32 | return str_ends_with($haystack, $needle); |
| 33 | } |
| 34 | |
| 35 | public function get_shortcodes(string $content): array |
| 36 | { |
| 37 | global $shortcode_tags; |
| 38 | |
| 39 | if (! $shortcode_tags || ! $content) { |
| 40 | return []; |
| 41 | } |
| 42 | |
| 43 | $shortcodes = []; |
| 44 | |
| 45 | $_shortcodes = array_keys($shortcode_tags); |
| 46 | asort($_shortcodes); |
| 47 | |
| 48 | foreach ($_shortcodes as $shortcode) { |
| 49 | $count = substr_count($content, '[' . $shortcode . ']'); |
| 50 | $count += substr_count($content, '[' . $shortcode . ' '); |
| 51 | |
| 52 | if ($count) { |
| 53 | $shortcodes[$shortcode] = $count; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return $shortcodes; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Count the number of words in a string (multibyte-compatible) |
| 62 | */ |
| 63 | public function word_count(string $string): int |
| 64 | { |
| 65 | if (empty($string)) { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | $string = trim(strip_tags($string)); |
| 70 | |
| 71 | if (empty($string)) { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | $patterns = [ |
| 76 | 'strip' => '/<[a-zA-Z\/][^<>]*>/', |
| 77 | 'clean' => '/[0-9.(),;:!?%#$¿\'"_+=\\/-]+/', |
| 78 | 'w' => '/\S\s+/', |
| 79 | 'c' => '/\S/', |
| 80 | ]; |
| 81 | |
| 82 | $string = (string) preg_replace($patterns['strip'], ' ', $string); |
| 83 | $string = (string) preg_replace('/ | /i', ' ', $string); |
| 84 | $string = (string) preg_replace($patterns['clean'], '', $string); |
| 85 | |
| 86 | if (! strlen((string) preg_replace('/\s/', '', $string))) { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | return preg_match_all($patterns['w'], $string, $matches) + 1; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Trims a string and strips tags if there is any HTML |
| 95 | */ |
| 96 | public function trim_characters(string $string, int $limit = 10, ?string $trail = null): string |
| 97 | { |
| 98 | if (1 > $limit) { |
| 99 | return $string; |
| 100 | } |
| 101 | |
| 102 | $string = wp_strip_all_tags($string); |
| 103 | |
| 104 | if (Mbstring::strlen($string) <= $limit) { |
| 105 | return $string; |
| 106 | } |
| 107 | |
| 108 | if (null === $trail) { |
| 109 | $trail = __('…'); |
| 110 | } |
| 111 | |
| 112 | return trim(Mbstring::substr($string, 0, $limit)) . $trail; |
| 113 | } |
| 114 | |
| 115 | public function is_image(string $url): bool |
| 116 | { |
| 117 | if (! $url) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | $path = (string) strtok($url, '?#'); |
| 122 | $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
| 123 | |
| 124 | return in_array($ext, ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp', 'svg', 'avif'], true); |
| 125 | } |
| 126 | |
| 127 | public function is_valid_url(string $url): bool |
| 128 | { |
| 129 | return false !== filter_var($url, FILTER_VALIDATE_URL); |
| 130 | } |
| 131 | |
| 132 | public function is_empty($value): bool |
| 133 | { |
| 134 | return ! $this->is_not_empty($value); |
| 135 | } |
| 136 | |
| 137 | public function is_not_empty($value): bool |
| 138 | { |
| 139 | return $value || 0 === $value || '0' === $value; |
| 140 | } |
| 141 | |
| 142 | public function get_image_urls_from_string(string $string): array |
| 143 | { |
| 144 | if (! $string) { |
| 145 | return []; |
| 146 | } |
| 147 | |
| 148 | if (false === strpos($string, '<img')) { |
| 149 | return []; |
| 150 | } |
| 151 | |
| 152 | $urls = []; |
| 153 | |
| 154 | $processor = new WP_HTML_Tag_Processor($string); |
| 155 | |
| 156 | while ($processor->next_tag(['tag_name' => 'img'])) { |
| 157 | $src = $processor->get_attribute('src'); |
| 158 | |
| 159 | if (! is_string($src) || '' === $src) { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | $urls[] = $src; |
| 164 | } |
| 165 | |
| 166 | return $urls; |
| 167 | } |
| 168 | |
| 169 | public function enumeration_list(array $items, string $compound = 'or'): string |
| 170 | { |
| 171 | if ('and' === $compound) { |
| 172 | return wp_sprintf('%l', $items); |
| 173 | } |
| 174 | |
| 175 | if ('or' === $compound) { |
| 176 | $compound = __(' or ', 'codepress-admin-columns'); |
| 177 | } |
| 178 | |
| 179 | $compound = sprintf(' %s ', trim($compound)); |
| 180 | |
| 181 | $last = array_pop($items); |
| 182 | |
| 183 | if (! $items) { |
| 184 | return (string)$last; |
| 185 | } |
| 186 | |
| 187 | return implode(', ', $items) . $compound . $last; |
| 188 | } |
| 189 | |
| 190 | } |
| 191 |