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