PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Helper / Strings.php
codepress-admin-columns / classes / Helper Last commit date
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('/&nbsp;|&#160;/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 = __('&hellip;');
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