PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.0.7
Admin Columns v3.0.7
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 / String.php
codepress-admin-columns / classes / Helper Last commit date
Array.php 8 years ago Date.php 8 years ago File.php 8 years ago Html.php 8 years ago Icon.php 8 years ago Image.php 8 years ago Media.php 8 years ago Network.php 8 years ago Post.php 8 years ago String.php 8 years ago Taxonomy.php 8 years ago User.php 8 years ago
String.php
302 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class AC_Helper_String {
8
9 /**
10 * @since 1.3.1
11 */
12 public function shorten_url( $url ) {
13 if ( ! $url ) {
14 return false;
15 }
16
17 return ac_helper()->html->link( $url, url_shorten( $url ), array( 'title' => $url ) );
18 }
19
20 /**
21 * @since 1.3
22 */
23 public function strip_trim( $string ) {
24 return trim( strip_tags( $string ) );
25 }
26
27 /**
28 * Count the number of words in a string (multibyte-compatible)
29 *
30 * @since 3.0
31 *
32 * @param $string
33 *
34 * @return int Number of words
35 */
36 public function word_count( $string ) {
37 if ( empty( $string ) ) {
38 return false;
39 }
40
41 $string = $this->strip_trim( $string );
42
43 if ( empty( $string ) ) {
44 return false;
45 }
46
47 $patterns = array(
48 'strip' => '/<[a-zA-Z\/][^<>]*>/',
49 'clean' => '/[0-9.(),;:!?%#$¿\'"_+=\\/-]+/',
50 'w' => '/\S\s+/',
51 'c' => '/\S/',
52 );
53
54 $string = preg_replace( $patterns['strip'], ' ', $string );
55 $string = preg_replace( '/&nbsp;|&#160;/i', ' ', $string );
56 $string = preg_replace( $patterns['clean'], '', $string );
57
58 if ( ! strlen( preg_replace( '/\s/', '', $string ) ) ) {
59 return 0;
60 }
61
62 return preg_match_all( $patterns['w'], $string, $matches ) + 1;
63 }
64
65 /**
66 * @see wp_trim_words();
67 *
68 * @since 3.0
69 *
70 * @return string
71 */
72 public function trim_words( $string = '', $num_words = 30, $more = null ) {
73 if ( ! $string ) {
74 return false;
75 }
76
77 if ( ! $num_words ) {
78 return $string;
79 }
80
81 return wp_trim_words( $string, $num_words, $more );
82 }
83
84 /**
85 * Trims a string and strips tags if there is any HTML
86 *
87 * @param string $string
88 * @param int $limit
89 *
90 * @return string
91 */
92 public function trim_characters( $string, $limit = 10, $trail = null ) {
93 $limit = absint( $limit );
94
95 if ( 1 > $limit ) {
96 return $string;
97 }
98
99 $string = wp_strip_all_tags( $string );
100
101 if ( mb_strlen( $string ) <= $limit ) {
102 return $string;
103 }
104
105 if ( null === $trail ) {
106 $trail = __( '&hellip;' );
107 }
108
109 return mb_substr( $string, 0, $limit ) . $trail;
110 }
111
112 /**
113 * Formats a valid hex color to a 6 digit string, optionally prefixed with a #
114 *
115 * Example: #FF0 will be fff000 based on the $prefix parameter
116 *
117 * @param string $hex Valid hex color
118 * @param bool $prefix Prefix with a # or not
119 *
120 * @return string
121 */
122 protected function hex_format( $hex, $prefix = false ) {
123 $hex = ltrim( $hex, '#' );
124
125 if ( strlen( $hex ) == 3 ) {
126 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
127 }
128
129 if ( $prefix ) {
130 $hex = '#' . $hex;
131 }
132
133 return strtolower( $hex );
134 }
135
136 /**
137 * Get RGB values from a hex color string
138 *
139 * @since 3.0
140 *
141 * @param string $hex Valid hex color
142 *
143 * @return array
144 */
145 public function hex_to_rgb( $hex ) {
146 $hex = $this->hex_format( $hex );
147
148 return sscanf( $hex, '%2x%2x%2x' );
149 }
150
151 /**
152 * Get contrasting hex color based on given hex color
153 *
154 * @since 3.0
155 *
156 * @param string $hex Valid hex color
157 *
158 * @return string
159 */
160 public function hex_get_contrast( $hex ) {
161 $rgb = $this->hex_to_rgb( $hex );
162 $contrast = ( $rgb[0] * 0.299 + $rgb[1] * 0.587 + $rgb[2] * 0.114 ) < 186 ? 'fff' : '333';
163
164 return $this->hex_format( $contrast, true );
165 }
166
167 /**
168 * @since 1.2.0
169 *
170 * @param string $url
171 *
172 * @return bool
173 */
174 public function is_image( $url ) {
175 return $url && is_string( $url ) && in_array( strrchr( $url, '.' ), array( '.jpg', '.jpeg', '.gif', '.png', '.bmp' ) );
176 }
177
178 /**
179 * @since 3.0
180 *
181 * @param string $string
182 *
183 * @return array
184 */
185 public function comma_separated_to_array( $string ) {
186 $array = array();
187 if ( is_scalar( $string ) ) {
188 if ( strpos( $string, ',' ) !== false ) {
189 $array = array_filter( explode( ',', ac_helper()->string->strip_trim( str_replace( ' ', '', $string ) ) ) );
190 } else {
191 $array = array( $string );
192 }
193 } else if ( is_array( $string ) ) {
194 $array = $string;
195 }
196
197 return $array;
198 }
199
200 /**
201 * @since 3.0
202 *
203 * @param string $string
204 *
205 * @return array
206 */
207 public function string_to_array_integers( $string ) {
208 $integers = array();
209
210 foreach ( $this->comma_separated_to_array( $string ) as $k => $value ) {
211 if ( is_numeric( trim( $value ) ) ) {
212 $integers[] = $value;
213 }
214 }
215
216 return $integers;
217 }
218
219 /**
220 * @since 3.0
221 *
222 * @param string $hex Color Hex Code
223 */
224 public function get_color_block( $hex ) {
225 if ( ! $hex ) {
226 return false;
227 }
228
229 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>';
230 }
231
232 /**
233 * @return bool
234 */
235 public function is_valid_url( $url ) {
236 return filter_var( $url, FILTER_VALIDATE_URL ) || preg_match( '/[^\w.-]/', $url );
237 }
238
239 /**
240 * @return string Display empty value
241 */
242 public function get_empty_char() {
243 _deprecated_function( __METHOD__, '3.0', 'AC_Column::get_empty_char' );
244
245 return '&ndash;';
246 }
247
248 /**
249 * @param string $string
250 *
251 * @return bool
252 */
253 public function contains_html_only( $string ) {
254 return strlen( $string ) !== strlen( strip_tags( $string ) );
255 }
256
257 /**
258 * @param string $value
259 *
260 * @return bool
261 */
262 public function is_empty( $value ) {
263 return ! $this->is_not_empty( $value );
264 }
265
266 /**
267 * @param string $value
268 *
269 * @return bool
270 */
271 public function is_not_empty( $value ) {
272 return $value || 0 === $value;
273 }
274
275 /**
276 * Return an array into a comma separated sentence. For example [minute, hours, days] becomes: "minute, hours or days".
277 *
278 * @param array $words
279 *
280 * @return string
281 */
282 public function enumeration_list( $words, $compound = 'or' ) {
283 if ( empty( $words ) || ! is_array( $words ) ) {
284 return false;
285 }
286
287 if ( 'and' === $compound ) {
288 return wp_sprintf( '%l', $words );
289 }
290
291 if ( 'or' === $compound ) {
292 $compound = __( ' or ', 'codepress-admin-columns' );
293 }
294
295 $last = end( $words );
296 $delimiter = ', ';
297
298 return str_replace( $delimiter . $last, $compound . $last, implode( $delimiter, $words ) );
299 }
300
301 }
302