Activation
4 years ago
Admin
4 years ago
Block
4 years ago
Container
4 years ago
Front
4 years ago
Rest
4 years ago
Widget
4 years ago
Bootstrap.php
4 years ago
Cache.php
4 years ago
Helper.php
4 years ago
I18N.php
4 years ago
Image.php
4 years ago
Output.php
4 years ago
Query.php
4 years ago
Settings.php
4 years ago
Themer.php
4 years ago
Translate.php
4 years ago
WordPressPopularPosts.php
4 years ago
deprecated.php
4 years ago
template-tags.php
4 years ago
Helper.php
375 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPressPopularPosts; |
| 4 | |
| 5 | class Helper { |
| 6 | |
| 7 | /** |
| 8 | * Checks for valid number. |
| 9 | * |
| 10 | * @since 2.1.6 |
| 11 | * @param int number |
| 12 | * @return bool |
| 13 | */ |
| 14 | public static function is_number($number) |
| 15 | { |
| 16 | return !empty($number) && is_numeric($number) && (intval($number) == floatval($number)); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Converts a number into a short version, eg: 1000 -> 1k |
| 21 | * |
| 22 | * @see https://gist.github.com/RadGH/84edff0cc81e6326029c |
| 23 | * @since 5.2.0 |
| 24 | * @param int |
| 25 | * @param int |
| 26 | * @return mixed string|bool |
| 27 | */ |
| 28 | public static function prettify_number($number, $precision = 1) |
| 29 | { |
| 30 | if ( ! is_numeric($number) ) |
| 31 | return false; |
| 32 | |
| 33 | if ( $number < 900 ) { |
| 34 | // 0 - 900 |
| 35 | $n_format = number_format($number, $precision); |
| 36 | $suffix = ''; |
| 37 | } elseif ( $number < 900000 ) { |
| 38 | // 0.9k-850k |
| 39 | $n_format = number_format($number / 1000, $precision); |
| 40 | $suffix = 'k'; |
| 41 | } elseif ( $number < 900000000 ) { |
| 42 | // 0.9m-850m |
| 43 | $n_format = number_format($number / 1000000, $precision); |
| 44 | $suffix = 'm'; |
| 45 | } elseif ( $number < 900000000000 ) { |
| 46 | // 0.9b-850b |
| 47 | $n_format = number_format($number / 1000000000, $precision); |
| 48 | $suffix = 'b'; |
| 49 | } else { |
| 50 | // 0.9t+ |
| 51 | $n_format = number_format($number / 1000000000000, $precision); |
| 52 | $suffix = 't'; |
| 53 | } |
| 54 | |
| 55 | // Remove unnecessary zeroes after decimal. "1.0" -> "1"; "1.00" -> "1" |
| 56 | // Intentionally does not affect partials, eg "1.50" -> "1.50" |
| 57 | if ( $precision > 0 ) { |
| 58 | $dotzero = '.' . str_repeat('0', $precision); |
| 59 | $n_format = str_replace($dotzero, '', $n_format); |
| 60 | } |
| 61 | |
| 62 | return $n_format . $suffix; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Checks for valid date. |
| 67 | * |
| 68 | * @since 4.0.0 |
| 69 | * @param string $date |
| 70 | * @param string $format |
| 71 | * @return bool |
| 72 | */ |
| 73 | public static function is_valid_date($date = null, $format = 'Y-m-d') |
| 74 | { |
| 75 | $d = \DateTime::createFromFormat($format, $date); |
| 76 | return $d && $d->format($format) === $date; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns an array of dates between two dates. |
| 81 | * |
| 82 | * @since 4.0.0 |
| 83 | * @param string $start_date |
| 84 | * @param string $end_date |
| 85 | * @param string $format |
| 86 | * @return array|bool |
| 87 | */ |
| 88 | public static function get_date_range($start_date = null, $end_date = null, $format = 'Y-m-d') |
| 89 | { |
| 90 | if ( |
| 91 | self::is_valid_date($start_date, $format) |
| 92 | && self::is_valid_date($end_date, $format) |
| 93 | ) { |
| 94 | $dates = []; |
| 95 | |
| 96 | $begin = new \DateTime($start_date, new \DateTimeZone(Helper::get_timezone())); |
| 97 | $end = new \DateTime($end_date, new \DateTimeZone(Helper::get_timezone())); |
| 98 | |
| 99 | if ( $begin < $end ) { |
| 100 | while( $begin <= $end ) { |
| 101 | $dates[] = $begin->format($format); |
| 102 | $begin->modify('+1 day'); |
| 103 | } |
| 104 | } |
| 105 | else { |
| 106 | while( $begin >= $end ) { |
| 107 | $dates[] = $begin->format($format); |
| 108 | $begin->modify('-1 day'); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $dates; |
| 113 | } |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns server date. |
| 120 | * |
| 121 | * @since 2.1.6 |
| 122 | * @access private |
| 123 | * @return string |
| 124 | */ |
| 125 | public static function curdate() |
| 126 | { |
| 127 | return current_time('Y-m-d', false); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns mysql datetime. |
| 132 | * |
| 133 | * @since 2.1.6 |
| 134 | * @access private |
| 135 | * @return string |
| 136 | */ |
| 137 | public static function now() |
| 138 | { |
| 139 | return current_time('mysql'); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns current timestamp. |
| 144 | * |
| 145 | * @since 5.0.2 |
| 146 | * @return string |
| 147 | */ |
| 148 | public static function timestamp() |
| 149 | { |
| 150 | // current_datetime() is WP 5.3+ |
| 151 | return ( function_exists('current_datetime') ) ? current_datetime()->getTimestamp() : current_time('timestamp'); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Checks whether a string is a valid timestamp. |
| 156 | * |
| 157 | * @since 5.2.0 |
| 158 | * @param string $string |
| 159 | * @return bool |
| 160 | */ |
| 161 | public static function is_timestamp($string) |
| 162 | { |
| 163 | if ( |
| 164 | ( is_int($string) || ctype_digit($string) ) |
| 165 | && strtotime(date('Y-m-d H:i:s', $string)) === (int) $string |
| 166 | ) { |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns site's timezone. |
| 175 | * |
| 176 | * Code borrowed from Rarst's awesome WpDateTime class: https://github.com/Rarst/wpdatetime |
| 177 | * |
| 178 | * @since 5.0.0 |
| 179 | * @return string |
| 180 | */ |
| 181 | public static function get_timezone() |
| 182 | { |
| 183 | $timezone_string = get_option('timezone_string'); |
| 184 | |
| 185 | if ( ! empty($timezone_string) ) { |
| 186 | return $timezone_string; |
| 187 | } |
| 188 | |
| 189 | $offset = get_option('gmt_offset'); |
| 190 | $sign = $offset < 0 ? '-' : '+'; |
| 191 | $hours = (int) $offset; |
| 192 | $minutes = abs(($offset - (int) $offset) * 60); |
| 193 | $offset = sprintf('%s%02d:%02d', $sign, abs($hours), $minutes); |
| 194 | |
| 195 | return $offset; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Returns time. |
| 200 | * |
| 201 | * @since 2.3.0 |
| 202 | * @return string |
| 203 | */ |
| 204 | public static function microtime_float() |
| 205 | { |
| 206 | list($msec, $sec) = explode(' ', microtime()); |
| 207 | return (float) $msec + (float) $sec; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Merges two associative arrays recursively. |
| 212 | * |
| 213 | * @since 2.3.4 |
| 214 | * @link http://www.php.net/manual/en/function.array-merge-recursive.php#92195 |
| 215 | * @param array array1 |
| 216 | * @param array array2 |
| 217 | * @return array |
| 218 | */ |
| 219 | public static function merge_array_r(array $array1, array $array2) |
| 220 | { |
| 221 | $merged = $array1; |
| 222 | |
| 223 | foreach( $array2 as $key => &$value ) { |
| 224 | if ( is_array($value) && isset($merged[$key]) && is_array($merged[$key]) ) { |
| 225 | $merged[$key] = self::merge_array_r($merged[$key], $value); |
| 226 | } else { |
| 227 | $merged[$key] = $value; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return $merged; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Debug function. |
| 236 | * |
| 237 | * @since 3.0.0 |
| 238 | * @param mixed $v variable to display with var_dump() |
| 239 | * @param mixed $v,... unlimited optional number of variables to display with var_dump() |
| 240 | */ |
| 241 | public static function debug($v) |
| 242 | { |
| 243 | if ( !defined('WPP_DEBUG') || !WPP_DEBUG ) |
| 244 | return; |
| 245 | |
| 246 | foreach( func_get_args() as $arg ) { |
| 247 | print "<pre>"; |
| 248 | var_dump($arg); |
| 249 | print "</pre>"; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Truncates text. |
| 255 | * |
| 256 | * @since 4.0.0 |
| 257 | * @param string $text |
| 258 | * @param int $length |
| 259 | * @param bool $truncate_by_words |
| 260 | * @return string |
| 261 | */ |
| 262 | public static function truncate($text = '', $length = 25, $truncate_by_words = false, $more = '...') |
| 263 | { |
| 264 | if ( '' !== $text ) { |
| 265 | // Truncate by words |
| 266 | if ( $truncate_by_words ) { |
| 267 | $words = explode(" ", $text, $length + 1); |
| 268 | |
| 269 | if ( count($words) > $length ) { |
| 270 | array_pop($words); |
| 271 | $text = rtrim(implode(" ", $words), ",.") . $more; |
| 272 | } |
| 273 | } |
| 274 | // Truncate by characters |
| 275 | elseif ( strlen($text) > $length ) { |
| 276 | $text = rtrim(mb_substr($text, 0, $length , get_bloginfo('charset')), " ,.") . $more; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return $text; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Gets post/page ID if current page is singular |
| 285 | * |
| 286 | * @since 3.1.2 |
| 287 | */ |
| 288 | public static function is_single() |
| 289 | { |
| 290 | $trackable = []; |
| 291 | $registered_post_types = get_post_types(['public' => true], 'names'); |
| 292 | |
| 293 | foreach( $registered_post_types as $post_type ) { |
| 294 | $trackable[] = $post_type; |
| 295 | } |
| 296 | |
| 297 | $trackable = apply_filters('wpp_trackable_post_types', $trackable); |
| 298 | |
| 299 | if ( |
| 300 | is_singular($trackable) |
| 301 | && !is_front_page() |
| 302 | && !is_preview() |
| 303 | && !is_trackback() |
| 304 | && !is_feed() |
| 305 | && !is_robots() |
| 306 | && !is_customize_preview() |
| 307 | ) { |
| 308 | return get_queried_object_id(); |
| 309 | } |
| 310 | |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Adds scheme to a given URL. |
| 316 | * |
| 317 | * @since 5.0.0 |
| 318 | * @param string $url |
| 319 | * @param string $scheme |
| 320 | * @return string|bool |
| 321 | */ |
| 322 | static function add_scheme($url = null, $scheme = 'https://') |
| 323 | { |
| 324 | $url_args = parse_url($url); |
| 325 | |
| 326 | if ( $url_args ) { |
| 327 | // No need to do anything, URL is fine |
| 328 | if ( isset($url_args['scheme']) ) |
| 329 | return $url; |
| 330 | // Return URL with scheme |
| 331 | return $scheme . $url_args['host'] . $url_args['path']; |
| 332 | } |
| 333 | |
| 334 | // Invalid/malformed URL |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Checks whether an URL points to an actual image. |
| 340 | * |
| 341 | * This function used to live in src/Image, moved it here |
| 342 | * on version 5.4.0 to use it where needed. |
| 343 | * |
| 344 | * @since 5.0.0 |
| 345 | * @access private |
| 346 | * @param string |
| 347 | * @return array|bool |
| 348 | */ |
| 349 | static function is_image_url($url) |
| 350 | { |
| 351 | $path = parse_url($url, PHP_URL_PATH); |
| 352 | $encoded_path = array_map('urlencode', explode('/', $path)); |
| 353 | $parse_url = str_replace($path, implode('/', $encoded_path), $url); |
| 354 | |
| 355 | if ( ! filter_var($parse_url, FILTER_VALIDATE_URL) ) |
| 356 | return false; |
| 357 | |
| 358 | // Check extension |
| 359 | $file_name = basename($path); |
| 360 | $file_name = sanitize_file_name($file_name); |
| 361 | $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); |
| 362 | $allowed_ext = ['jpg', 'jpeg', 'png', 'gif']; |
| 363 | |
| 364 | if ( ! in_array($ext, $allowed_ext) ) |
| 365 | return false; |
| 366 | |
| 367 | // sanitize URL, just in case |
| 368 | $image_url = esc_url($url); |
| 369 | // remove querystring |
| 370 | preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $image_url, $matches); |
| 371 | |
| 372 | return ( is_array($matches) && ! empty($matches) ) ? $matches : false; |
| 373 | } |
| 374 | } |
| 375 |