PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.4
WebberZone Top 10 — Popular Posts v4.3.4
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / util / class-helpers.php

class-helpers.php in WebberZone Top 10 — Popular Posts 4.3.4, at includes/util/class-helpers.php

601 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helpers class.
4 *
5 * @package WebberZone\Top_Ten\Util
6 */
7
8 namespace WebberZone\Top_Ten\Util;
9
10 if ( ! defined( 'WPINC' ) ) {
11 die;
12 }
13
14 /**
15 * Helpers class.
16 *
17 * @since 3.3.0
18 */
19 class Helpers {
20
21 /**
22 * Constructor class.
23 *
24 * @since 3.3.0
25 */
26 public function __construct() {
27 }
28
29 /**
30 * Retrieve the from date for the query
31 *
32 * @since 2.6.0
33 *
34 * @param string $time A date/time string.
35 * @param int $daily_range Daily range.
36 * @param int $hour_range Hour range.
37 * @return string From date
38 */
39 public static function get_from_date( $time = null, $daily_range = null, $hour_range = null ) {
40
41 $current_time = isset( $time ) ? strtotime( $time ) : strtotime( current_time( 'mysql' ) );
42 $daily_range = isset( $daily_range ) ? absint( $daily_range ) : (int) \tptn_get_option( 'daily_range' );
43 $hour_range = isset( $hour_range ) ? absint( $hour_range ) : (int) \tptn_get_option( 'hour_range' );
44
45 if ( \tptn_get_option( 'daily_midnight' ) ) {
46 $from_date = $current_time - ( max( 0, ( $daily_range - 1 ) ) * DAY_IN_SECONDS );
47 $from_date = gmdate( 'Y-m-d 0:0:0', $from_date );
48 } else {
49 $from_date = $current_time - ( $daily_range * DAY_IN_SECONDS + $hour_range * HOUR_IN_SECONDS );
50 $from_date = gmdate( 'Y-m-d H:0:0', $from_date );
51 }
52
53 /**
54 * Retrieve the from date for the query
55 *
56 * @since 2.6.0
57 *
58 * @param string $from_date From date.
59 * @param string $time A date/time string.
60 * @param int $daily_range Daily range.
61 * @param int $hour_range Hour range.
62 */
63 return apply_filters( 'tptn_get_from_date', $from_date, $time, $daily_range, $hour_range );
64 }
65
66 /**
67 * Get a human-readable label for a custom period range.
68 *
69 * @since 4.3.2
70 *
71 * @param int|null $daily_range Number of days. Defaults to the 'daily_range' option.
72 * @return string 'Daily' when the range is 1 day, else 'Custom (N days)'.
73 */
74 public static function get_daily_range_label( $daily_range = null ) {
75 $daily_range = isset( $daily_range ) ? absint( $daily_range ) : (int) \tptn_get_option( 'daily_range', 1 );
76
77 if ( 1 === $daily_range ) {
78 return __( 'Daily', 'top-10' );
79 }
80
81 return sprintf(
82 /* translators: %d: Number of days. */
83 __( 'Custom (%d days)', 'top-10' ),
84 $daily_range
85 );
86 }
87
88
89 /**
90 * Convert float number to format based on the locale if number_format_count is true.
91 *
92 * @since 2.6.0
93 *
94 * @param float $number The number to convert based on locale.
95 * @param int $decimals Optional. Precision of the number of decimal places. Default 0.
96 * @return string Converted number in string format.
97 */
98 public static function number_format_i18n( $number, $decimals = 0 ) {
99
100 $formatted = (float) $number;
101
102 if ( \tptn_get_option( 'number_format_count' ) ) {
103 $formatted = number_format_i18n( (float) $formatted );
104 }
105
106 /**
107 * Filters the number formatted based on the locale.
108 *
109 * @since 2.6.0
110 *
111 * @param string $formatted Converted number in string format.
112 * @param float $number The number to convert based on locale.
113 * @param int $decimals Precision of the number of decimal places.
114 */
115 return apply_filters( 'number_format_i18n', $formatted, $number, $decimals );
116 }
117
118 /**
119 * Convert a string to CSV.
120 *
121 * @since 2.9.0
122 *
123 * @param array $input Input string.
124 * @param string $delimiter Delimiter.
125 * @param string $enclosure Enclosure.
126 * @param string $terminator Terminating string.
127 * @return string CSV string.
128 */
129 public static function str_putcsv( $input, $delimiter = ',', $enclosure = '"', $terminator = "\n" ) {
130 // First convert associative array to numeric indexed array.
131 $work_array = array();
132 foreach ( $input as $key => $value ) {
133 $work_array[] = $value;
134 }
135
136 $string = '';
137 $input_size = count( $work_array );
138
139 for ( $i = 0; $i < $input_size; $i++ ) {
140 // Nested array, process nest item.
141 if ( is_array( $work_array[ $i ] ) ) {
142 $string .= self::str_putcsv( $work_array[ $i ], $delimiter, $enclosure, $terminator );
143 } else {
144 switch ( gettype( $work_array[ $i ] ) ) {
145 case 'NULL':
146 $formatted = '';
147 break;
148 case 'boolean':
149 $formatted = ( true === $work_array[ $i ] ) ? 'true' : 'false';
150 break;
151 case 'integer':
152 $formatted = (string) (int) $work_array[ $i ];
153 break;
154 case 'double':
155 $formatted = number_format( (float) $work_array[ $i ], 2, '.', '' );
156 break;
157 case 'string':
158 $formatted = str_replace( $enclosure, $enclosure . $enclosure, (string) $work_array[ $i ] );
159 break;
160 default:
161 $formatted = '';
162 break;
163 }
164 $string .= $enclosure . $formatted . $enclosure;
165 $string .= ( $i < ( $input_size - 1 ) ) ? $delimiter : $terminator;
166 }
167 }
168
169 return $string;
170 }
171
172 /**
173 * Truncate a string to a certain length.
174 *
175 * @since 2.5.4
176 *
177 * @param string $input String to truncate.
178 * @param int $count Maximum number of characters to take.
179 * @param string $more What to append if $input needs to be trimmed.
180 * @param bool $break_words Optionally choose to break words.
181 * @return string Truncated string.
182 */
183 public static function trim_char( $input, $count = 60, $more = '&hellip;', $break_words = false ) {
184 $input = wp_strip_all_tags( $input, true );
185 if ( 0 === $count ) {
186 return '';
187 }
188 if ( mb_strlen( $input ) > $count && $count > 0 ) {
189 $count -= min( $count, mb_strlen( $more ) );
190 if ( ! $break_words ) {
191 $input = preg_replace( '/\s+?(\S+)?$/u', '', mb_substr( $input, 0, $count + 1 ) );
192 }
193 $input = mb_substr( $input, 0, $count ) . $more;
194 }
195 /**
196 * Filters truncated string.
197 *
198 * @since 2.4.0
199 *
200 * @param string $input String to truncate.
201 * @param int $count Maximum number of characters to take.
202 * @param string $more What to append if $input needs to be trimmed.
203 * @param bool $break_words Optionally choose to break words.
204 */
205 return apply_filters( 'tptn_trim_char', $input, $count, $more, $break_words );
206 }
207
208 /**
209 * Get the WP_Query arguments.
210 *
211 * @return array WP_Query arguments.
212 */
213 public static function get_wp_query_arguments() {
214 $arguments = array(
215 // Author Parameters.
216 'author' => '',
217 'author_name' => '',
218 'author__in' => array(),
219 'author__not_in' => array(),
220
221 // Category Parameters.
222 'cat' => '',
223 'category_name' => '',
224 'category__and' => array(),
225 'category__in' => array(),
226 'category__not_in' => array(),
227
228 // Tag Parameters.
229 'tag' => '',
230 'tag_id' => '',
231 'tag__and' => array(),
232 'tag__in' => array(),
233 'tag__not_in' => array(),
234 'tag_slug__and' => array(),
235 'tag_slug__in' => array(),
236
237 // Search Parameters.
238 'search_columns' => array(),
239 'exact' => false,
240 'sentence' => false,
241
242 // Post & Page Parameters.
243 'p' => '',
244 'name' => '',
245 'page_id' => '',
246 'pagename' => '',
247 'post__in' => array(),
248 'post__not_in' => array(),
249 'post_parent' => '',
250 'post_parent__in' => array(),
251 'post_parent__not_in' => array(),
252 'post_name__in' => array(),
253
254 // Password Parameters.
255 'has_password' => false,
256 'post_password' => null,
257
258 // Post Type Parameters.
259 'post_type' => '',
260
261 // Status Parameters.
262 'post_status' => '',
263
264 // Comment Parameters.
265 'comment_count' => '',
266
267 // Pagination Parameters.
268 'posts_per_page' => '',
269
270 // Order & Orderby Parameters.
271 'orderby' => '',
272 'order' => '',
273
274 // Date Parameters.
275 'year' => '',
276 'monthnum' => '',
277 'day' => '',
278 'hour' => '',
279 'minute' => '',
280 'second' => '',
281
282 // Custom Field (post meta) Parameters.
283 'meta_key' => '', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
284 'meta_value' => '', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
285 'meta_value_num' => '',
286 'meta_compare' => '',
287 );
288
289 return $arguments;
290 }
291
292 /**
293 * Parse WP_Query variables to parse comma separated list of IDs and convert them to arrays as needed by WP_Query.
294 *
295 * @param array $query_vars Defined query variables.
296 * @return array Complete query variables with undefined ones filled in empty.
297 */
298 public static function parse_wp_query_arguments( $query_vars ) {
299
300 $array_keys = array(
301 'category__in',
302 'category__not_in',
303 'category__and',
304 'post__in',
305 'post__not_in',
306 'post_name__in',
307 'tag__in',
308 'tag__not_in',
309 'tag__and',
310 'tag_slug__in',
311 'tag_slug__and',
312 'post_parent__in',
313 'post_parent__not_in',
314 'author__in',
315 'author__not_in',
316 );
317
318 foreach ( $array_keys as $key ) {
319 if ( isset( $query_vars[ $key ] ) ) {
320 $query_vars[ $key ] = wp_parse_list( $query_vars[ $key ] );
321 }
322 }
323
324 return $query_vars;
325 }
326
327 /**
328 * Check if the user agent matches known bots.
329 *
330 * @since 3.3.0
331 * @link https://github.com/janusman/robot-user-agents
332 *
333 * @return bool True if the user agent matches a known bot pattern, false otherwise.
334 */
335 public static function is_bot() {
336 if ( ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
337 return false;
338 }
339
340 $user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
341
342 $bot_user_agents = array(
343 '11A465',
344 'AddThis.com',
345 'AdsBot-Google',
346 'Ahrefs',
347 'alexa site audit',
348 'AlipesNewsBot',
349 'Amazonbot',
350 'Amazon-Route53-Health-Check-Service',
351 'ApacheBench',
352 'AppDynamics',
353 'Applebot',
354 'ArchiveBot',
355 'Archive-It',
356 'AspiegelBot',
357 'Assetnote',
358 'axios',
359 'azure-logic-apps',
360 'Baiduspider',
361 'Barkrowler',
362 'bingbot',
363 'BLEXBot',
364 'BLP_bbot',
365 'BluechipBacklinks',
366 'Buck',
367 'Bytespider',
368 'CCBot',
369 'check_http',
370 'CloudFlare-Prefetch',
371 'cludo.com bot',
372 'colly',
373 'contentkingapp',
374 'Cookiebot',
375 'CopperEgg',
376 'crawler4j',
377 'Csnibot',
378 'Curebot',
379 'curl',
380 'CyotekWebCopy',
381 'Daum',
382 'Datadog Agent',
383 'DataForSeoBot',
384 'Detectify',
385 'DotBot',
386 'Dow Jones Searchbot',
387 'DuckDuckBot',
388 'facebookexternalhit',
389 'Faraday',
390 'FeedBurner',
391 'FeedFetcher-Google',
392 'feedonomics',
393 'Fess',
394 'Funnelback',
395 'Fuzz Faster U Fool',
396 'GAChecker',
397 'Ghost Inspector',
398 'Grapeshot',
399 'gobuster',
400 'gocolly',
401 'Googlebot',
402 'GoogleStackdriverMonitoring',
403 'Go-http-client',
404 'go-resty',
405 'GuzzleHttp',
406 'HeadlessChrome',
407 'heritrix',
408 'hokifyBot',
409 'Honolulu-bot',
410 'HTTrack',
411 'HubSpot Crawler',
412 'ICC-Crawler',
413 'Imperva',
414 'IonCrawl',
415 'jooble',
416 'KauaiBot',
417 'Kinza',
418 'LieBaoFast',
419 'linabot',
420 'Linespider',
421 'Linguee',
422 'LinkChecker',
423 'LinkedInBot',
424 'LinkUpBot',
425 'LinuxGetUrl',
426 'LMY47V',
427 'MacOutlook',
428 'Magnet.me',
429 'Magus Bot',
430 'Mail.RU_Bot',
431 'MauiBot',
432 'Mb2345Browser',
433 'MegaIndex',
434 'Microsoft Office',
435 'Microsoft Outlook',
436 'Microsoft Word',
437 'MicroMessenger',
438 'mindbreeze-crawler',
439 'mirrorweb.com',
440 'MJ12bot',
441 'monitoring-plugins',
442 'Monsidobot',
443 'MQQBrowser',
444 'msnbot',
445 'MSOffice',
446 'MTRobot',
447 'nagios-plugins',
448 'nettle',
449 'Neevabot',
450 'NewsCred',
451 'newspaper',
452 'Nuclei',
453 'NukeScan',
454 'OnCrawl',
455 'Orbbot',
456 'PageFreezer',
457 'panscient.com',
458 'PetalBot',
459 'Pingdom.com',
460 'Pinterestbot',
461 'PiplBot',
462 'python-requests',
463 'Qwantify',
464 'Re-re Studio',
465 'Riddler',
466 'RocketValidator',
467 'rogerbot',
468 'RustBot',
469 'Safeassign',
470 'Scrapy',
471 'Screaming Frog',
472 'SeobilityBot',
473 'Search365bot',
474 'SearchBlox',
475 'searchunify',
476 'Seekport',
477 'SemanticScholarBot',
478 'SemrushBot',
479 'SEOkicks',
480 'seoscanners',
481 'serpstatbot',
482 'SessionCam',
483 'SeznamBot',
484 'Site24x7',
485 'SiteAuditBot',
486 'siteimprove',
487 'SiteLockSpider',
488 'SiteSucker',
489 'SkypeRoom',
490 'Slackbot',
491 'Slurp',
492 'Sogou web spider',
493 'special_archiver',
494 'SpiderLing',
495 'StatusCake',
496 'Swiftbot',
497 'Synack',
498 'Turnitin',
499 'trendictionbot',
500 'trendkite-akashic-crawler',
501 'UCBrowser',
502 'Uptime',
503 'UptimeRobot',
504 'UT-Dorkbot',
505 'weborama-fetcher',
506 'WhiteHat Security',
507 'Wget',
508 'WTWBot',
509 'www.loc.gov',
510 'Xenu Link Sleuth',
511 'Vagabondo',
512 'VelenPublicWebCrawler',
513 'Yeti',
514 'Veracode Security Scan',
515 'YandexBot',
516 'YandexImages',
517 'YisouSpider',
518 'Zabbix',
519 'ZoominfoBot',
520 'ZoomSpider',
521 );
522
523 /**
524 * Filter the list of known bots.
525 *
526 * @since 3.3.0
527 *
528 * @param array $bot_user_agents List of known bots.
529 */
530 $bot_user_agent = apply_filters( 'tptn_bots', $bot_user_agents );
531
532 foreach ( $bot_user_agents as $bot_user_agent ) {
533 if ( preg_match( '/' . preg_quote( strtolower( $bot_user_agent ), '/' ) . '/i', strtolower( $user_agent ) ) ) {
534 return true;
535 }
536 }
537
538 return false;
539 }
540
541 /**
542 * Get all terms of a post.
543 *
544 * @since 4.0.0
545 *
546 * @param int|\WP_Post $post Post ID or WP_Post object.
547 * @return array Array of taxonomies.
548 */
549 public static function get_all_terms( $post ) {
550 $taxonomies = array();
551
552 if ( ! empty( $post ) ) {
553 $post = get_post( $post );
554 }
555
556 if ( ! empty( $post ) ) {
557 $taxonomies = get_object_taxonomies( $post );
558 }
559
560 $all_terms = array();
561
562 // Loop through the taxonomies and get the terms for the post for each taxonomy.
563 foreach ( $taxonomies as $taxonomy ) {
564 $terms = get_the_terms( $post, $taxonomy );
565 if ( $terms && ! is_wp_error( $terms ) ) {
566 $all_terms = array_merge( $all_terms, $terms );
567 }
568 }
569
570 return $all_terms;
571 }
572
573 /**
574 * Sanitize args.
575 *
576 * @since 4.1.1
577 *
578 * @param array $args Array of arguments.
579 * @return array Sanitized array of arguments.
580 */
581 public static function sanitize_args( $args ): array {
582 foreach ( $args as $key => $value ) {
583 if ( is_string( $value ) ) {
584 switch ( $key ) {
585 case 'class':
586 case 'className':
587 case 'extra_class':
588 $classes = explode( ' ', $value );
589 $sanitized_classes = array_map( 'sanitize_html_class', $classes );
590 $args[ $key ] = implode( ' ', $sanitized_classes );
591 break;
592 default:
593 $args[ $key ] = wp_kses_post( $value );
594 break;
595 }
596 }
597 }
598 return $args;
599 }
600 }
601