| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utils Helper Class |
| 4 |
* |
| 5 |
* Provides utility functions for gathering system information, |
| 6 |
* sanitizing data, and other helper operations. |
| 7 |
* |
| 8 |
* @package LinnoSDK\Telemetry |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace LinnoSDK\Telemetry\Helpers; |
| 13 |
|
| 14 |
/** |
| 15 |
* Utils class |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class Utils { |
| 20 |
/** |
| 21 |
* Get PHP version |
| 22 |
* |
| 23 |
* @return string PHP version string |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
public static function getPhpVersion(): string { |
| 27 |
return PHP_VERSION; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get WordPress version |
| 32 |
* |
| 33 |
* @return string WordPress version string |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
public static function getWordPressVersion(): string { |
| 37 |
global $wp_version; |
| 38 |
return $wp_version ?? ''; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get MySQL version |
| 43 |
* |
| 44 |
* @return string MySQL/MariaDB version string |
| 45 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
public static function getMySqlVersion(): string { |
| 48 |
global $wpdb; |
| 49 |
|
| 50 |
if ( ! isset( $wpdb ) ) { |
| 51 |
return ''; |
| 52 |
} |
| 53 |
|
| 54 |
return $wpdb->db_version(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get server software information |
| 59 |
* |
| 60 |
* @return string Server software string |
| 61 |
* @since 1.0.0 |
| 62 |
*/ |
| 63 |
public static function getServerSoftware(): string { |
| 64 |
return $_SERVER['SERVER_SOFTWARE'] ?? ''; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get site URL |
| 69 |
* |
| 70 |
* @return string Site URL |
| 71 |
* @since 1.0.0 |
| 72 |
*/ |
| 73 |
public static function getSiteUrl(): string { |
| 74 |
return get_site_url(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get current timestamp in ISO 8601 format |
| 79 |
* |
| 80 |
* @return string ISO 8601 formatted timestamp |
| 81 |
* @since 1.0.0 |
| 82 |
*/ |
| 83 |
public static function getCurrentTimestamp(): string { |
| 84 |
return gmdate( 'c' ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Sanitize event name to allow only alphanumeric characters and underscores |
| 89 |
* |
| 90 |
* @param string $event Event name to sanitize |
| 91 |
* |
| 92 |
* @return string Sanitized event name |
| 93 |
* @since 1.0.0 |
| 94 |
*/ |
| 95 |
public static function sanitizeEventName( string $event ): string { |
| 96 |
return preg_replace( '/[^a-zA-Z0-9_\/]/', '', $event ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sanitize properties array recursively |
| 101 |
* |
| 102 |
* Preserves special OpenPanel properties like __identify that start with double underscore. |
| 103 |
* Also preserves the case of keys inside the __identify block as OpenPanel is case-sensitive |
| 104 |
* for those specific identification keys (profileId, firstName, etc). |
| 105 |
* |
| 106 |
* @param array $properties Properties array to sanitize |
| 107 |
* @param bool $is_identify Whether we are currently sanitizing the __identify block |
| 108 |
* |
| 109 |
* @return array Sanitized properties array |
| 110 |
* @since 1.0.0 |
| 111 |
*/ |
| 112 |
public static function sanitizeProperties( array $properties, bool $is_identify = false ): array { |
| 113 |
$sanitized = array(); |
| 114 |
|
| 115 |
foreach ( $properties as $key => $value ) { |
| 116 |
// Preserve special OpenPanel properties (starting with __) |
| 117 |
// or if we are already inside the __identify block (to maintain camelCase) |
| 118 |
if ( $is_identify || strpos( $key, '__' ) === 0 ) { |
| 119 |
$sanitized_key = $key; |
| 120 |
} else { |
| 121 |
$sanitized_key = sanitize_key( $key ); |
| 122 |
} |
| 123 |
|
| 124 |
if ( is_array( $value ) ) { |
| 125 |
$sanitized[ $sanitized_key ] = self::sanitizeProperties( $value, $is_identify || $key === '__identify' ); |
| 126 |
} elseif ( is_string( $value ) ) { |
| 127 |
$sanitized[ $sanitized_key ] = sanitize_text_field( $value ); |
| 128 |
} elseif ( is_numeric( $value ) ) { |
| 129 |
$sanitized[ $sanitized_key ] = $value; |
| 130 |
} elseif ( is_bool( $value ) ) { |
| 131 |
$sanitized[ $sanitized_key ] = $value; |
| 132 |
} else { |
| 133 |
$sanitized[ $sanitized_key ] = sanitize_text_field( (string) $value ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return $sanitized; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Generate a UUID v4 for profile identification |
| 142 |
* |
| 143 |
* Uses WordPress wp_generate_uuid4() if available, otherwise generates manually. |
| 144 |
* |
| 145 |
* @return string UUID v4 string |
| 146 |
* @since 1.0.0 |
| 147 |
*/ |
| 148 |
public static function generateProfileId(): string { |
| 149 |
// Use WordPress function if available |
| 150 |
if ( function_exists( 'wp_generate_uuid4' ) ) { |
| 151 |
return wp_generate_uuid4(); |
| 152 |
} |
| 153 |
|
| 154 |
// Fallback: Generate UUID v4 manually |
| 155 |
$data = random_bytes( 16 ); |
| 156 |
|
| 157 |
// Set version to 0100 |
| 158 |
$data[6] = chr( ord( $data[6] ) & 0x0f | 0x40 ); |
| 159 |
// Set bits 6-7 to 10 |
| 160 |
$data[8] = chr( ord( $data[8] ) & 0x3f | 0x80 ); |
| 161 |
|
| 162 |
return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( bin2hex( $data ), 4 ) ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get or create a site-level profile ID |
| 167 |
* |
| 168 |
* This creates a consistent profile ID for the WordPress installation, |
| 169 |
* stored in the options table. This allows tracking the site as a profile |
| 170 |
* without requiring user-specific identification. |
| 171 |
* |
| 172 |
* @return string Site profile ID (UUID v4) |
| 173 |
* @since 1.0.0 |
| 174 |
*/ |
| 175 |
public static function getSiteProfileId(): string { |
| 176 |
$option_name = 'linno_telemetry_site_profile_id'; |
| 177 |
$profile_id = get_option( $option_name ); |
| 178 |
|
| 179 |
if ( empty( $profile_id ) ) { |
| 180 |
$profile_id = self::generateProfileId(); |
| 181 |
update_option( $option_name, $profile_id, false ); |
| 182 |
} |
| 183 |
|
| 184 |
return $profile_id; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Extract plugin version from plugin file headers |
| 189 |
* |
| 190 |
* @param string $plugin_file Path to the plugin file |
| 191 |
* |
| 192 |
* @return string Plugin version or '0.0.0' if not found |
| 193 |
* @since 1.0.0 |
| 194 |
*/ |
| 195 |
public static function getPluginVersion( string $plugin_file ): string { |
| 196 |
if ( ! file_exists( $plugin_file ) ) { |
| 197 |
return '0.0.0'; |
| 198 |
} |
| 199 |
|
| 200 |
// Use WordPress function if available |
| 201 |
if ( function_exists( 'get_plugin_data' ) ) { |
| 202 |
$plugin_data = get_plugin_data( $plugin_file, false, false ); |
| 203 |
return $plugin_data['Version'] ?? '0.0.0'; |
| 204 |
} |
| 205 |
|
| 206 |
// Fallback: Read file and extract version manually |
| 207 |
$file_content = file_get_contents( $plugin_file ); |
| 208 |
|
| 209 |
if ( $file_content === false ) { |
| 210 |
return '0.0.0'; |
| 211 |
} |
| 212 |
|
| 213 |
// Look for Version: x.x.x in the plugin header |
| 214 |
if ( preg_match( '/Version:\s*([0-9.]+)/i', $file_content, $matches ) ) { |
| 215 |
return $matches[1]; |
| 216 |
} |
| 217 |
|
| 218 |
return '0.0.0'; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Get the current user's identification data for telemetry. |
| 223 |
* |
| 224 |
* @return array Identification data with profileId and user info. |
| 225 |
* @since 1.0.1 |
| 226 |
*/ |
| 227 |
public static function get_current_user_identify(): array { |
| 228 |
$identify = [ |
| 229 |
'profileId' => self::getSiteProfileId(), |
| 230 |
]; |
| 231 |
|
| 232 |
if ( function_exists( 'wp_get_current_user' ) ) { |
| 233 |
$current_user = wp_get_current_user(); |
| 234 |
if ( $current_user && $current_user->ID > 0 ) { |
| 235 |
$identify['email'] = $current_user->user_email; |
| 236 |
$identify['firstName'] = $current_user->first_name ?: 'User'; |
| 237 |
$identify['lastName'] = $current_user->last_name ?: ''; |
| 238 |
if ( function_exists( 'get_avatar_url' ) ) { |
| 239 |
$identify['avatar'] = get_avatar_url( $current_user->ID ); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return $identify; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Get unique site ID |
| 249 |
* |
| 250 |
* @return string Unique site ID |
| 251 |
* @since 1.0.0 |
| 252 |
*/ |
| 253 |
public static function getUniqueSiteId(): string |
| 254 |
{ |
| 255 |
return md5( home_url() ); |
| 256 |
} |
| 257 |
} |
| 258 |
|