PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 6.1.1
WP Popular Posts v6.1.1
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Helper.php
wordpress-popular-posts / src Last commit date
Activation 3 years ago Admin 3 years ago Block 3 years ago Container 3 years ago Front 3 years ago Rest 3 years ago Traits 3 years ago Widget 3 years ago Bootstrap.php 3 years ago Cache.php 3 years ago Helper.php 3 years ago I18N.php 3 years ago Image.php 3 years ago Output.php 3 years ago Query.php 3 years ago Settings.php 3 years ago Themer.php 3 years ago Translate.php 3 years ago WordPressPopularPosts.php 3 years ago deprecated.php 3 years ago template-tags.php 3 years ago
Helper.php
352 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 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(?string $date, $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(string $start_date, string $end_date, string $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, wp_timezone());
97 $end = new \DateTime($end_date, wp_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) /** @TODO: starting PHP 8.0 $string can be declared as mixed $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 time.
175 *
176 * @since 2.3.0
177 * @return string
178 */
179 public static function microtime_float()
180 {
181 list($msec, $sec) = explode(' ', microtime());
182 return (float) $msec + (float) $sec;
183 }
184
185 /**
186 * Merges two associative arrays recursively.
187 *
188 * @since 2.3.4
189 * @link http://www.php.net/manual/en/function.array-merge-recursive.php#92195
190 * @param array array1
191 * @param array array2
192 * @return array
193 */
194 public static function merge_array_r(array $array1, array $array2)
195 {
196 $merged = $array1;
197
198 foreach( $array2 as $key => &$value ) {
199 if ( is_array($value) && isset($merged[$key]) && is_array($merged[$key]) ) {
200 $merged[$key] = self::merge_array_r($merged[$key], $value);
201 } else {
202 $merged[$key] = $value;
203 }
204 }
205
206 return $merged;
207 }
208
209 /**
210 * Debug function.
211 *
212 * @since 3.0.0
213 * @param mixed $v variable to display with var_dump()
214 * @param mixed $v,... unlimited optional number of variables to display with var_dump()
215 */
216 public static function debug($v) /** @TODO: remove this function, we don't use it at all */
217 {
218 if ( !defined('WPP_DEBUG') || !WPP_DEBUG )
219 return;
220
221 foreach( func_get_args() as $arg ) {
222 print "<pre>";
223 var_dump($arg);
224 print "</pre>";
225 }
226 }
227
228 /**
229 * Truncates text.
230 *
231 * @since 4.0.0
232 * @param string $text
233 * @param int $length
234 * @param bool $truncate_by_words
235 * @return string
236 */
237 public static function truncate(string $text = '', int $length = 25, bool $truncate_by_words = false, string $more = '...')
238 {
239 if ( '' !== $text ) {
240 $charset = get_bloginfo('charset');
241
242 // Truncate by words
243 if ( $truncate_by_words ) {
244 $words = explode(" ", $text, $length + 1);
245
246 if ( count($words) > $length ) {
247 array_pop($words);
248 $text = rtrim(implode(" ", $words), ",.") . $more;
249 }
250 }
251 // Truncate by characters
252 elseif ( mb_strlen($text, $charset) > $length ) {
253 $text = rtrim(mb_substr($text, 0, $length , $charset), " ,.") . $more;
254 }
255 }
256
257 return $text;
258 }
259
260 /**
261 * Gets post/page ID if current page is singular
262 *
263 * @since 3.1.2
264 */
265 public static function is_single()
266 {
267 $trackable = [];
268 $registered_post_types = get_post_types(['public' => true], 'names');
269
270 foreach( $registered_post_types as $post_type ) {
271 $trackable[] = $post_type;
272 }
273
274 $trackable = apply_filters('wpp_trackable_post_types', $trackable);
275
276 if (
277 is_singular($trackable)
278 && !is_front_page()
279 && !is_preview()
280 && !is_trackback()
281 && !is_feed()
282 && !is_robots()
283 && !is_customize_preview()
284 ) {
285 return get_queried_object_id();
286 }
287
288 return false;
289 }
290
291 /**
292 * Adds scheme to a given URL.
293 *
294 * @since 5.0.0
295 * @param string $url
296 * @param string $scheme
297 * @return string|bool
298 */
299 static function add_scheme(?string $url, string $scheme = 'https://')
300 {
301 $url_args = parse_url($url);
302
303 if ( $url_args ) {
304 // No need to do anything, URL is fine
305 if ( isset($url_args['scheme']) )
306 return $url;
307 // Return URL with scheme
308 return $scheme . $url_args['host'] . $url_args['path'];
309 }
310
311 // Invalid/malformed URL
312 return false;
313 }
314
315 /**
316 * Checks whether an URL points to an actual image.
317 *
318 * This function used to live in src/Image, moved it here
319 * on version 5.4.0 to use it where needed.
320 *
321 * @since 5.0.0
322 * @access private
323 * @param string
324 * @return array|bool
325 */
326 static function is_image_url(string $url)
327 {
328 $path = parse_url($url, PHP_URL_PATH);
329 $encoded_path = array_map('urlencode', explode('/', $path));
330 $parse_url = str_replace($path, implode('/', $encoded_path), $url);
331
332 if ( ! filter_var($parse_url, FILTER_VALIDATE_URL) )
333 return false;
334
335 // Check extension
336 $file_name = basename($path);
337 $file_name = sanitize_file_name($file_name);
338 $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
339 $allowed_ext = ['jpg', 'jpeg', 'png', 'gif'];
340
341 if ( ! in_array($ext, $allowed_ext) )
342 return false;
343
344 // sanitize URL, just in case
345 $image_url = esc_url($url);
346 // remove querystring
347 preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $image_url, $matches);
348
349 return ( is_array($matches) && ! empty($matches) ) ? $matches : false;
350 }
351 }
352