PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.1
WebberZone Top 10 — Popular Posts v4.3.1
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.1, at includes/util/class-helpers.php

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