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