PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 7.3.5
WP Popular Posts v7.3.5
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 8 months ago Admin 8 months ago Block 8 months ago Compatibility 8 months ago Container 8 months ago Front 8 months ago Rest 8 months ago Shortcode 8 months ago Traits 8 months ago Widget 8 months ago Bootstrap.php 8 months ago Cache.php 8 months ago Helper.php 8 months ago Image.php 8 months ago Output.php 8 months ago Query.php 8 months ago Settings.php 8 months ago Themer.php 8 months ago Translate.php 8 months ago Upgrader.php 8 months ago WordPressPopularPosts.php 8 months ago deprecated.php 8 months ago template-tags.php 8 months ago
Helper.php
394 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_datetime()->format('Y-m-d');
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_datetime()->format('Y-m-d H:i:s');
141 }
142
143 /**
144 * Returns current timestamp.
145 *
146 * @since 5.0.2
147 * @return string
148 */
149 public static function timestamp()
150 {
151 return current_datetime()->getTimestamp();
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 * Truncates text.
211 *
212 * @since 4.0.0
213 * @param string $text
214 * @param int $length
215 * @param bool $truncate_by_words
216 * @return string
217 */
218 public static function truncate(string $text = '', int $length = 25, bool $truncate_by_words = false, string $more = '...')
219 {
220 if ( '' !== $text ) {
221 $charset = get_bloginfo('charset');
222
223 // Truncate by words
224 if ( $truncate_by_words ) {
225 $words = explode(' ', $text, $length + 1);
226
227 if ( count($words) > $length ) {
228 array_pop($words);
229 $text = rtrim(implode(' ', $words), ',.') . $more;
230 }
231 }
232 // Truncate by characters
233 elseif ( mb_strlen($text, $charset) > $length ) {
234 $text = rtrim(mb_substr($text, 0, $length, $charset), ' ,.') . $more;
235 }
236 }
237
238 return $text;
239 }
240
241 /**
242 * Gets post/page ID if current page is singular
243 *
244 * @since 3.1.2
245 */
246 public static function is_single()
247 {
248 $trackable = [];
249 $registered_post_types = get_post_types(['public' => true], 'names');
250
251 foreach( $registered_post_types as $post_type ) {
252 $trackable[] = $post_type;
253 }
254
255 $trackable = apply_filters('wpp_trackable_post_types', $trackable);
256
257 if (
258 is_singular($trackable)
259 && ! is_front_page()
260 && ! is_preview()
261 && ! is_trackback()
262 && ! is_feed()
263 && ! is_robots()
264 && ! is_customize_preview()
265 ) {
266 return get_queried_object_id();
267 }
268
269 return false;
270 }
271
272 /**
273 * Adds scheme to a given URL.
274 *
275 * @since 5.0.0
276 * @param string $url
277 * @param string $scheme
278 * @return string|bool
279 */
280 public static function add_scheme(?string $url, string $scheme = 'https://')
281 {
282 $url_args = parse_url($url);
283
284 if ( $url_args ) {
285 // No need to do anything, URL is fine
286 if ( isset($url_args['scheme']) ) {
287 return $url;
288 }
289 // Return URL with scheme
290 return $scheme . $url_args['host'] . $url_args['path'];
291 }
292
293 // Invalid/malformed URL
294 return false;
295 }
296
297 /**
298 * Checks whether an URL points to an actual image.
299 *
300 * This function used to live in src/Image, moved it here
301 * on version 5.4.0 to use it where needed.
302 *
303 * @since 5.0.0
304 * @access private
305 * @param string
306 * @return array|bool
307 */
308 public static function is_image_url(string $url)
309 {
310 $path = parse_url($url, PHP_URL_PATH);
311 $encoded_path = array_map('urlencode', explode('/', $path));
312 $parse_url = str_replace($path, implode('/', $encoded_path), $url);
313
314 if ( ! filter_var($parse_url, FILTER_VALIDATE_URL) ) {
315 return false;
316 }
317
318 // Check extension
319 $file_name = basename($path);
320 $file_name = sanitize_file_name($file_name);
321 $ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
322 $allowed_ext = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif'];
323
324 if ( ! in_array($ext, $allowed_ext) ) {
325 return false;
326 }
327
328 // sanitize URL, just in case
329 $image_url = esc_url($url);
330 // remove querystring
331 preg_match('/[^\?]+\.(jpg|jpeg|gif|png|webp|avif)/i', $image_url, $matches);
332
333 return ( is_array($matches) && ! empty($matches) ) ? $matches : false;
334 }
335
336 /**
337 * Sanitizes HTML output.
338 *
339 * @since 6.3.3
340 * @param string $html
341 * @param array $options Public options
342 * @return string $html The (sanitized) HTML code
343 */
344 public static function sanitize_html(string $html, array $options)
345 {
346 $allowed_tags = wp_kses_allowed_html('post');
347
348 if ( isset($allowed_tags['form']) ) {
349 unset($allowed_tags['form']);
350 }
351
352 if (
353 isset($options['theme']['name'])
354 && $options['theme']['name']
355 ) {
356 $allowed_tags['style'] = [
357 'id' => 1,
358 'nonce' => 1
359 ];
360 }
361
362 $allowed_tags['img']['decoding'] = true;
363 $allowed_tags['img']['srcset'] = true;
364
365 return wp_kses($html, $allowed_tags);
366 }
367
368 /**
369 * Retrieves post/page content from the database.
370 *
371 * @param int $id
372 * @return mixed
373 */
374 public static function get_post_content(int $id)
375 {
376 /** @var wpdb $wpdb */
377 global $wpdb;
378
379 $posts_table = "{$wpdb->posts}";
380
381 //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
382 $content = $wpdb->get_var(
383 $wpdb->prepare(
384 "SELECT post_content FROM %i WHERE ID = %d;",
385 $posts_table,
386 $id
387 )
388 );
389 //phpcs:enable
390
391 return ( $content ) ? sanitize_post_field('post_content', $content, $id, 'display') : '';
392 }
393 }
394