PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 3.0
Admin Columns v3.0
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 9 years ago Date.php 9 years ago File.php 9 years ago Html.php 9 years ago Icon.php 9 years ago Image.php 9 years ago Network.php 9 years ago Post.php 9 years ago String.php 9 years ago Taxonomy.php 9 years ago User.php 9 years ago
String.php
280 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 return $string ? wp_trim_words( $string, $num_words, $more ) : false;
74 }
75
76 /**
77 * @param string $string
78 * @param int $limit
79 *
80 * @return string
81 */
82 public function trim_characters( $string, $limit = 10, $trail = '&hellip;' ) {
83 $limit = absint( $limit );
84
85 if ( 1 > $limit || strlen( $string ) <= $limit ) {
86 return $string;
87 }
88
89 return substr( $string, 0, $limit ) . $trail;
90 }
91
92 /**
93 * Formats a valid hex color to a 6 digit string, optionally prefixed with a #
94 *
95 * Example: #FF0 will be fff000 based on the $prefix parameter
96 *
97 * @param string $hex Valid hex color
98 * @param bool $prefix Prefix with a # or not
99 *
100 * @return string
101 */
102 protected function hex_format( $hex, $prefix = false ) {
103 $hex = ltrim( $hex, '#' );
104
105 if ( strlen( $hex ) == 3 ) {
106 $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
107 }
108
109 if ( $prefix ) {
110 $hex = '#' . $hex;
111 }
112
113 return strtolower( $hex );
114 }
115
116 /**
117 * Get RGB values from a hex color string
118 *
119 * @since 3.0
120 *
121 * @param string $hex Valid hex color
122 *
123 * @return array
124 */
125 public function hex_to_rgb( $hex ) {
126 $hex = $this->hex_format( $hex );
127
128 return sscanf( $hex, '%2x%2x%2x' );
129 }
130
131 /**
132 * Get contrasting hex color based on given hex color
133 *
134 * @since 3.0
135 *
136 * @param string $hex Valid hex color
137 *
138 * @return string
139 */
140 public function hex_get_contrast( $hex ) {
141 $rgb = $this->hex_to_rgb( $hex );
142 $contrast = ( $rgb[0] * 0.299 + $rgb[1] * 0.587 + $rgb[2] * 0.114 ) < 186 ? 'fff' : '333';
143
144 return $this->hex_format( $contrast, true );
145 }
146
147 /**
148 * @since 1.2.0
149 *
150 * @param string $url
151 *
152 * @return bool
153 */
154 public function is_image( $url ) {
155 return $url && is_string( $url ) ? in_array( strrchr( $url, '.' ), array( '.jpg', '.jpeg', '.gif', '.png', '.bmp' ) ) : false;
156 }
157
158 /**
159 * @since 3.0
160 *
161 * @param string $string
162 *
163 * @return array
164 */
165 public function comma_separated_to_array( $string ) {
166 $array = array();
167 if ( is_scalar( $string ) ) {
168 if ( strpos( $string, ',' ) !== false ) {
169 $array = array_filter( explode( ',', ac_helper()->string->strip_trim( str_replace( ' ', '', $string ) ) ) );
170 } else {
171 $array = array( $string );
172 }
173 } else if ( is_array( $string ) ) {
174 $array = $string;
175 }
176
177 return $array;
178 }
179
180 /**
181 * @since 3.0
182 *
183 * @param string $string
184 *
185 * @return array
186 */
187 public function string_to_array_integers( $string ) {
188 $values = $this->comma_separated_to_array( $string );
189
190 foreach ( $values as $k => $value ) {
191 if ( ! is_numeric( trim( $value ) ) ) {
192 unset( $values[ $k ] );
193 }
194 }
195
196 return $values;
197 }
198
199 /**
200 * @since 3.0
201 *
202 * @param string $hex Color Hex Code
203 */
204 public function get_color_block( $hex ) {
205 if ( ! $hex ) {
206 return false;
207 }
208
209 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>';
210 }
211
212 /**
213 * @return bool
214 */
215 public function is_valid_url( $url ) {
216 return filter_var( $url, FILTER_VALIDATE_URL ) || preg_match( '/[^\w.-]/', $url );
217 }
218
219 /**
220 * @return string Display empty value
221 */
222 public function get_empty_char() {
223 _deprecated_function( __METHOD__, '3.0' );
224
225 return '&ndash;';
226 }
227
228 /**
229 * @param string $string
230 *
231 * @return bool
232 */
233 public function contains_html_only( $string ) {
234 return strlen( $string ) !== strlen( strip_tags( $string ) );
235 }
236
237 /**
238 * @param string $value
239 *
240 * @return bool
241 */
242 public function is_empty( $value ) {
243 return ! $this->is_not_empty( $value );
244 }
245
246 /**
247 * @param string $value
248 *
249 * @return bool
250 */
251 public function is_not_empty( $value ) {
252 return $value || 0 === $value;
253 }
254
255 /**
256 * Return an array into a comma separated sentence. For example [minute, hours, days] becomes: "minute, hours or days".
257 *
258 * @param array $words
259 *
260 * @return string
261 */
262 public function enumeration_list( $words, $compound = 'or' ) {
263 if ( empty( $words ) || ! is_array( $words ) ) {
264 return false;
265 }
266
267 if ( 'or' === $compound ) {
268 $compound = __( ' or ', 'codepress-admin-columns' );
269 } else {
270 $compound = __( ' and ', 'codepress-admin-columns' );
271 }
272
273 $last = end( $words );
274 $delimiter = ', ';
275
276 return str_replace( $delimiter . $last, $compound . $last, implode( $delimiter, $words ) );
277 }
278
279 }
280