PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / 9.0.2
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) v9.0.2
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / inc / utils.php

utils.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) 9.0.2, at inc/utils.php

210 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName -- File naming is acceptable
2 /**
3 * Utils Component for WP Analytify
4 *
5 * This file contains all utility functions and helpers
6 * that were previously in the main plugin file.
7 *
8 * @package WP_Analytify
9 * @since 8.0.0
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Utils Component Class
18 *
19 * Static utility functions for the plugin
20 */
21 class Analytify_Utils {
22
23 /**
24 * Format number for display
25 *
26 * @param int $number Number to format.
27 * @return string Formatted number
28 */
29 public static function format_number( $number ) {
30 if ( $number >= 1000000 ) {
31 return round( $number / 1000000, 1 ) . 'M';
32 } elseif ( $number >= 1000 ) {
33 return round( $number / 1000, 1 ) . 'K';
34 }
35 return number_format( $number );
36 }
37
38 /**
39 * Format time for display
40 *
41 * @param int $seconds Seconds to format.
42 * @return string Formatted time
43 */
44 public static function format_time( $seconds ) {
45 $hours = floor( $seconds / 3600 );
46 $minutes = floor( ( $seconds % 3600 ) / 60 );
47 $secs = $seconds % 60;
48
49 if ( $hours > 0 ) {
50 return sprintf( '%02d:%02d:%02d', $hours, $minutes, $secs );
51 } else {
52 return sprintf( '%02d:%02d', $minutes, $secs );
53 }
54 }
55
56 /**
57 * Check if current user can view analytics
58 *
59 * @return bool Whether user can view analytics
60 */
61 public static function can_view_analytics() {
62 $allowed_roles = get_option( 'wp-analytify-admin' );
63 if ( ! $allowed_roles || ! isset( $allowed_roles['show_analytics_roles_back_end'] ) ) {
64 return current_user_can( 'manage_options' );
65 }
66
67 $user = wp_get_current_user();
68 $user_roles = $user->roles;
69
70 foreach ( $user_roles as $role ) {
71 if ( in_array( $role, $allowed_roles['show_analytics_roles_back_end'], true ) ) {
72 return true;
73 }
74 }
75
76 return false;
77 }
78
79 /**
80 * Get current page URL
81 *
82 * @return string Current page URL
83 */
84 public static function get_current_page_url() {
85 global $wp;
86 return home_url( add_query_arg( array(), $wp->request ) );
87 }
88
89 /**
90 * Sanitize and validate date
91 *
92 * @param string $date Date string.
93 * @return string|false Sanitized date or false if invalid
94 */
95 public static function sanitize_date( $date ) {
96 $timestamp = strtotime( $date );
97 if ( false === $timestamp ) {
98 return false;
99 }
100 return gmdate( 'Y-m-d', $timestamp );
101 }
102
103 /**
104 * Pretty numbers (in thousands) - Legacy function from main file
105 *
106 * @param int $num number.
107 * @return string
108 */
109 public static function wpa_pretty_numbers( $num ) {
110 return round( ( $num / 1000 ), 2 ) . 'k';
111 }
112
113 /**
114 * Format numbers - Legacy function from main file
115 *
116 * @param int $num number.
117 * @return string
118 */
119 public static function wpa_number_format( $num ) {
120 return number_format( $num );
121 }
122
123 /**
124 * Pretty time to display - Legacy function from main file
125 *
126 * @param int $time time.
127 * @return string
128 */
129 public static function pa_pretty_time( $time ) {
130 // Check if numeric.
131 if ( is_numeric( $time ) ) {
132 $value = array(
133 'years' => '00',
134 'days' => '00',
135 'hours' => '',
136 'minutes' => '',
137 'seconds' => '',
138 );
139
140 $attach_hours = '';
141 $attach_min = '';
142 $attach_sec = '';
143
144 $time = floor( $time );
145
146 if ( $time >= 31556926 ) {
147 $value['years'] = floor( $time / 31556926 );
148 $time = ( $time % 31556926 );
149 } //$time >= 31556926
150
151 if ( $time >= 86400 ) {
152 $value['days'] = floor( $time / 86400 );
153 $time = ( $time % 86400 );
154 } //$time >= 86400
155 if ( $time >= 3600 ) {
156 $value['hours'] = str_pad( (string) floor( $time / 3600 ), 1, '0', STR_PAD_LEFT );
157 $time = ( $time % 3600 );
158 } //$time >= 3600
159 if ( $time >= 60 ) {
160 $value['minutes'] = str_pad( (string) floor( $time / 60 ), 1, '0', STR_PAD_LEFT );
161 $time = ( $time % 60 );
162 } //$time >= 60
163 $value['seconds'] = str_pad( (string) floor( $time ), 1, '0', STR_PAD_LEFT );
164 // Get the hour:minute:second version.
165 if ( '' !== $value['hours'] ) {
166 $attach_hours = '<sub>' . _x( 'h', 'Hour Time', 'wp-analytify' ) . '</sub> ';
167 }
168 if ( '' !== $value['minutes'] ) {
169 $attach_min = '<sub>' . _x( 'm', 'Minute Time', 'wp-analytify' ) . '</sub> ';
170 }
171 if ( '' !== $value['seconds'] ) {
172 $attach_sec = '<sub>' . _x( 's', 'Second Time', 'wp-analytify' ) . '</sub>';
173 }
174
175 return $value['hours'] . $attach_hours . $value['minutes'] . $attach_min . $value['seconds'] . $attach_sec;
176 } else {
177 return '';
178 }
179 }
180
181 /**
182 * Check current user role - Legacy function from main file
183 *
184 * @since 1.2.2
185 * @param array<string, mixed> $access_level selected access level.
186 * @return boolean
187 */
188 public static function pa_check_roles( $access_level ) {
189
190 // Convert string to array if none of the role selected.
191 $access_level = (array) $access_level;
192
193 if ( is_user_logged_in() && ! empty( $access_level ) ) {
194
195 global $current_user;
196 $roles = $current_user->roles;
197
198 if ( array_intersect( $roles, $access_level ) ) {
199 return true;
200 } elseif ( is_super_admin( $current_user->ID ) && is_multisite() ) {
201 return true;
202 } else {
203 return false;
204 }
205 }
206
207 return false;
208 }
209 }
210