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