Strings.php
7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
namespace AC\Helper;
class Strings {
/**
* @param $url
*
* @return bool|false|string
* @since 1.3.1
*/
public function shorten_url( $url ) {
if ( ! $url ) {
return false;
}
return ac_helper()->html->link( $url, url_shorten( $url ), [ 'title' => $url ] );
}
public function contains( string $haystack, string $needle ): bool {
return '' === $needle || false !== strpos( $haystack, $needle );
}
public function starts_with( string $haystack, string $needle ): bool {
return '' === $needle || 0 === strpos( $haystack, $needle );
}
public function ends_with( string $haystack, string $needle ): bool {
if ( '' === $haystack && '' !== $needle ) {
return false;
}
$len = strlen( $needle );
return 0 === substr_compare( $haystack, $needle, -$len, $len );
}
/**
* @param $string
*
* @return string
* @since 1.3
*/
public function strip_trim( $string ) {
return trim( strip_tags( $string ) );
}
/**
* @param string $content
*
* @return array
*/
public function get_shortcodes( $content ) {
global $shortcode_tags;
if ( ! $shortcode_tags || ! $content ) {
return [];
}
$shortcodes = [];
$_shortcodes = array_keys( $shortcode_tags );
asort( $_shortcodes );
foreach ( $_shortcodes as $shortcode ) {
$count = substr_count( $content, '[' . $shortcode . ']' );
$count += substr_count( $content, '[' . $shortcode . ' ' );
if ( $count ) {
$shortcodes[ $shortcode ] = $count;
}
}
return $shortcodes;
}
/**
* Count the number of words in a string (multibyte-compatible)
*
* @param $string
*
* @return int Number of words
* @since 3.0
*/
public function word_count( $string ) {
if ( empty( $string ) ) {
return 0;
}
$string = $this->strip_trim( $string );
if ( empty( $string ) ) {
return 0;
}
$patterns = [
'strip' => '/<[a-zA-Z\/][^<>]*>/',
'clean' => '/[0-9.(),;:!?%#$¿\'"_+=\\/-]+/',
'w' => '/\S\s+/',
'c' => '/\S/',
];
$string = preg_replace( $patterns['strip'], ' ', $string );
$string = preg_replace( '/ | /i', ' ', $string );
$string = preg_replace( $patterns['clean'], '', $string );
if ( ! strlen( preg_replace( '/\s/', '', $string ) ) ) {
return 0;
}
return preg_match_all( $patterns['w'], $string, $matches ) + 1;
}
/**
* @param string $string
* @param int $num_words
* @param null $more
*
* @return false|string
* @see wp_trim_words();
* @since 3.0
*/
public function trim_words( $string = '', $num_words = 30, $more = null ) {
if ( $this->is_empty( $string ) ) {
return false;
}
if ( ! $num_words ) {
return $string;
}
return wp_trim_words( $string, $num_words, $more );
}
/**
* Trims a string and strips tags if there is any HTML
*
* @param string $string
* @param int $limit
* @param null $trail
*
* @return string
*/
public function trim_characters( $string, $limit = 10, $trail = null ) {
$limit = absint( $limit );
if ( 1 > $limit ) {
return $string;
}
$string = wp_strip_all_tags( $string );
if ( mb_strlen( $string ) <= $limit ) {
return $string;
}
if ( null === $trail ) {
$trail = __( '…' );
}
return trim( mb_substr( $string, 0, $limit ) ) . $trail;
}
/**
* Formats a valid hex color to a 6 digit string, optionally prefixed with a #
* Example: #FF0 will be fff000 based on the $prefix parameter
*
* @param string $hex Valid hex color
* @param bool $prefix Prefix with a # or not
*
* @return string
*/
protected function hex_format( $hex, $prefix = false ) {
$hex = ltrim( $hex, '#' );
if ( strlen( $hex ) == 3 ) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
if ( $prefix ) {
$hex = '#' . $hex;
}
return strtolower( $hex );
}
/**
* Get RGB values from a hex color string
*
* @param string $hex Valid hex color
*
* @return array
* @since 3.0
*/
public function hex_to_rgb( $hex ) {
$hex = $this->hex_format( $hex );
return sscanf( $hex, '%2x%2x%2x' );
}
/**
* Get contrasting hex color based on given hex color
*
* @param string $hex Valid hex color
*
* @return string
* @since 3.0
*/
public function hex_get_contrast( $hex ) {
$rgb = $this->hex_to_rgb( $hex );
$contrast = ( $rgb[0] * 0.299 + $rgb[1] * 0.587 + $rgb[2] * 0.114 ) < 186 ? 'fff' : '333';
return $this->hex_format( $contrast, true );
}
/**
* @param string $url
*
* @return bool
* @since 1.2.0
*/
public function is_image( $url ) {
if ( ! $url || ! is_string( $url ) ) {
return false;
}
$ext = strtolower( pathinfo( strtok( $url, '?' ), PATHINFO_EXTENSION ) );
return in_array( $ext, [ 'jpg', 'jpeg', 'gif', 'png', 'bmp' ] );
}
/**
* @param string $string
*
* @return array
* @since 3.0
*/
public function comma_separated_to_array( $string ) {
$array = [];
if ( is_scalar( $string ) ) {
if ( strpos( $string, ',' ) !== false ) {
$array = array_filter( explode( ',', ac_helper()->string->strip_trim( str_replace( ' ', '', $string ) ) ) );
} else {
$array = [ $string ];
}
} else if ( is_array( $string ) ) {
$array = $string;
}
return $array;
}
/**
* @param string $string
*
* @return array
* @since 3.0
*/
public function string_to_array_integers( $string ) {
$integers = [];
foreach ( $this->comma_separated_to_array( $string ) as $k => $value ) {
if ( is_numeric( trim( $value ) ) ) {
$integers[] = $value;
}
}
return $integers;
}
/**
* @param string $hex Color Hex Code
*
* @return string
* @since 3.0
*/
public function get_color_block( $hex ) {
if ( ! $hex ) {
return false;
}
return '<div class="cpac-color"><span style="background-color:' . esc_attr( $hex ) . ';color:' . esc_attr( $this->hex_get_contrast( $hex ) ) . '">' . esc_html( $hex ) . '</span></div>';
}
/**
* @param $url
*
* @return bool
*/
public function is_valid_url( $url ) {
return filter_var( $url, FILTER_VALIDATE_URL ) || preg_match( '/[^\w.-]/', $url );
}
/**
* @return string Display empty value
*/
public function get_empty_char() {
_deprecated_function( __METHOD__, '3.0', 'AC\Column::get_empty_char' );
return '–';
}
/**
* @param string $string
*
* @return bool
*/
public function contains_html_only( $string ) {
return strlen( $string ) !== strlen( strip_tags( $string ) );
}
/**
* @param string $value
*
* @return bool
*/
public function is_empty( $value ) {
return ! $this->is_not_empty( $value );
}
/**
* @param string $value
*
* @return bool
*/
public function is_not_empty( $value ) {
return $value || 0 === $value || '0' === $value;
}
/**
* Return an array into a comma separated sentence. For example [minute, hours, days] becomes: "minute, hours or days".
*
* @param array $words
* @param string $compound
*
* @return string
*/
public function enumeration_list( $words, $compound = 'or' ) {
if ( empty( $words ) || ! is_array( $words ) ) {
return false;
}
if ( 'and' === $compound ) {
return wp_sprintf( '%l', $words );
}
if ( 'or' === $compound ) {
$compound = __( ' or ', 'codepress-admin-columns' );
}
$compound = sprintf( ' %s ', trim( $compound ) );
$last = end( $words );
$delimiter = ', ';
return str_replace( $delimiter . $last, $compound . $last, implode( $delimiter, $words ) );
}
}