PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.2.3
WP Popular Posts v5.2.3
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 5 years ago Admin 5 years ago Container 5 years ago Front 5 years ago Moment 5 years ago Rest 5 years ago Widget 5 years ago Bootstrap.php 5 years ago Cache.php 5 years ago Helper.php 5 years ago I18N.php 5 years ago Image.php 5 years ago Output.php 5 years ago Query.php 5 years ago Settings.php 5 years ago Themer.php 5 years ago Translate.php 5 years ago WordPressPopularPosts.php 5 years ago deprecated.php 5 years ago template-tags.php 5 years ago
Helper.php
336 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 try {
164 new \DateTime('@' . $string);
165 } catch(\Exception $e) {
166 return false;
167 }
168 return true;
169 }
170
171 /**
172 * Returns site's timezone.
173 *
174 * Code borrowed from Rarst's awesome WpDateTime class: https://github.com/Rarst/wpdatetime
175 *
176 * @since 5.0.0
177 * @return string
178 */
179 public static function get_timezone()
180 {
181 $timezone_string = get_option('timezone_string');
182
183 if ( ! empty($timezone_string) ) {
184 return $timezone_string;
185 }
186
187 $offset = get_option('gmt_offset');
188 $sign = $offset < 0 ? '-' : '+';
189 $hours = (int) $offset;
190 $minutes = abs(($offset - (int) $offset) * 60);
191 $offset = sprintf('%s%02d:%02d', $sign, abs($hours), $minutes);
192
193 return $offset;
194 }
195
196 /**
197 * Returns time.
198 *
199 * @since 2.3.0
200 * @return string
201 */
202 public static function microtime_float()
203 {
204 list($msec, $sec) = explode(' ', microtime());
205 return (float) $msec + (float) $sec;
206 }
207
208 /**
209 * Merges two associative arrays recursively.
210 *
211 * @since 2.3.4
212 * @link http://www.php.net/manual/en/function.array-merge-recursive.php#92195
213 * @param array array1
214 * @param array array2
215 * @return array
216 */
217 public static function merge_array_r(array $array1, array $array2)
218 {
219 $merged = $array1;
220
221 foreach( $array2 as $key => &$value ) {
222 if ( is_array($value) && isset($merged[$key]) && is_array($merged[$key]) ) {
223 $merged[$key] = self::merge_array_r($merged[$key], $value);
224 } else {
225 $merged[$key] = $value;
226 }
227 }
228
229 return $merged;
230 }
231
232 /**
233 * Debug function.
234 *
235 * @since 3.0.0
236 * @param mixed $v variable to display with var_dump()
237 * @param mixed $v,... unlimited optional number of variables to display with var_dump()
238 */
239 public static function debug($v)
240 {
241 if ( !defined('WPP_DEBUG') || !WPP_DEBUG )
242 return;
243
244 foreach( func_get_args() as $arg ) {
245 print "<pre>";
246 var_dump($arg);
247 print "</pre>";
248 }
249 }
250
251 /**
252 * Truncates text.
253 *
254 * @since 4.0.0
255 * @param string $text
256 * @param int $length
257 * @param bool $truncate_by_words
258 * @return string
259 */
260 public static function truncate($text = '', $length = 25, $truncate_by_words = false, $more = '...')
261 {
262 if ( '' !== $text ) {
263 // Truncate by words
264 if ( $truncate_by_words ) {
265 $words = explode(" ", $text, $length + 1);
266
267 if ( count($words) > $length ) {
268 array_pop($words);
269 $text = rtrim(implode(" ", $words), ",.") . " {$more}";
270 }
271 }
272 // Truncate by characters
273 elseif ( strlen($text) > $length ) {
274 $text = rtrim(mb_substr($text, 0, $length , get_bloginfo('charset')), " ,.") . $more;
275 }
276 }
277
278 return $text;
279 }
280
281 /**
282 * Gets post/page ID if current page is singular
283 *
284 * @since 3.1.2
285 */
286 public static function is_single()
287 {
288 $trackable = [];
289 $registered_post_types = get_post_types(['public' => true], 'names');
290
291 foreach( $registered_post_types as $post_type ) {
292 $trackable[] = $post_type;
293 }
294
295 $trackable = apply_filters('wpp_trackable_post_types', $trackable);
296
297 if (
298 is_singular($trackable)
299 && !is_front_page()
300 && !is_preview()
301 && !is_trackback()
302 && !is_feed()
303 && !is_robots()
304 && !is_customize_preview()
305 ) {
306 return get_queried_object_id();
307 }
308
309 return false;
310 }
311
312 /**
313 * Adds scheme to a given URL.
314 *
315 * @since 5.0.0
316 * @param string $url
317 * @param string $scheme
318 * @return string|bool
319 */
320 static function add_scheme($url = null, $scheme = 'https://')
321 {
322 $url_args = parse_url($url);
323
324 if ( $url_args ) {
325 // No need to do anything, URL is fine
326 if ( isset($url_args['scheme']) )
327 return $url;
328 // Return URL with scheme
329 return $scheme . $url_args['host'] . $url_args['path'];
330 }
331
332 // Invalid/malformed URL
333 return false;
334 }
335 }
336