Actions
2 days ago
AnalyticsCleanup.php
1 week ago
AnalyticsConsent.php
1 week ago
AnalyticsEventDto.php
1 week ago
AnalyticsEventWithTimeDto.php
1 week ago
AnalyticsGenericEventHandler.php
1 week ago
AnalyticsSender.php
2 days ago
ErrorCode.php
1 week ago
WithAnalyticsAPI.php
1 week ago
WithAnalyticsSiteInfo.php
2 days ago
WithAnalyticsSiteInfo.php
251 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Analytics; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Adapter\WpAdapter; |
| 7 | use WPStaging\Framework\SiteInfo; |
| 8 | |
| 9 | trait WithAnalyticsSiteInfo |
| 10 | { |
| 11 | public function getAnalyticsSiteInfo() |
| 12 | { |
| 13 | global $wpdb, $wp_version, $wp_db_version; |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | include ABSPATH . WPINC . '/version.php'; |
| 20 | |
| 21 | |
| 22 | $mysqlInfo = $wpdb->get_var('SELECT VERSION();'); |
| 23 | |
| 24 | $mySqlVersionNumber = $this->getDatabaseVersionNumber((string)$mysqlInfo); |
| 25 | $engine = $this->getNormalizedDatabaseEngine((string)$mysqlInfo, $mySqlVersionNumber); |
| 26 | |
| 27 | $wpstgSettings = get_option('wpstg_settings', []); |
| 28 | |
| 29 | if (!is_array($wpstgSettings)) { |
| 30 | $wpstgSettings = []; |
| 31 | } |
| 32 | |
| 33 | $plugins = $this->getActivePlugins(); |
| 34 | |
| 35 | $siteInfo = WPStaging::make(SiteInfo::class); |
| 36 | |
| 37 | $systemInfo = [ |
| 38 | 'is_staging_site' => (int)$siteInfo->isStagingSite(), |
| 39 | |
| 40 | 'db_copy_query_limit' => !empty($wpstgSettings['queryLimit']) ? $wpstgSettings['queryLimit'] : null, |
| 41 | 'db_sr_limit' => !empty($wpstgSettings['querySRLimit']) ? $wpstgSettings['querySRLimit'] : null, |
| 42 | 'file_copy_limit' => !empty($wpstgSettings['fileLimit']) ? $wpstgSettings['fileLimit'] : null, |
| 43 | 'cpu_priority' => !empty($wpstgSettings['cpuLoad']) ? $wpstgSettings['cpuLoad'] : null, |
| 44 | 'file_copy_batch_size' => !empty($wpstgSettings['batchSize']) ? $wpstgSettings['batchSize'] : null, |
| 45 | 'max_file_size' => !empty($wpstgSettings['maxFileSize']) ? $wpstgSettings['maxFileSize'] : null, |
| 46 | 'optimizer' => !empty($wpstgSettings['optimizer']) ? $wpstgSettings['optimizer'] : null, |
| 47 | |
| 48 | |
| 49 | 'keep_permalinks' => !empty($wpstgSettings['keepPermalinks']) ? $wpstgSettings['keepPermalinks'] : false, |
| 50 | 'disable_admin_login' => !empty($wpstgSettings['disableAdminLogin']) ? $wpstgSettings['disableAdminLogin'] : false, |
| 51 | 'delay_between_requests' => 0, |
| 52 | 'debug_mode' => !empty($wpstgSettings['debugMode']) ? $wpstgSettings['debugMode'] : false, |
| 53 | 'remove_data_on_uninstall' => !empty($wpstgSettings['unInstallOnDelete']) ? $wpstgSettings['unInstallOnDelete'] : false, |
| 54 | 'access_permission' => !empty($wpstgSettings['userRoles']) ? $wpstgSettings['userRoles'] : [], |
| 55 | 'users_with_staging_access' => !empty($wpstgSettings['usersWithStagingAccess']) ? $wpstgSettings['usersWithStagingAccess'] : '', |
| 56 | 'enable_compression' => !empty($wpstgSettings['enableCompression']) ? $wpstgSettings['enableCompression'] : false, |
| 57 | |
| 58 | 'php_version' => phpversion(), |
| 59 | 'php_architecture' => $siteInfo->getPhpArchitecture(), |
| 60 | 'blog_id' => get_current_blog_id(), |
| 61 | 'network_id' => WPStaging::make(WpAdapter::class)->getCurrentNetworkId(), |
| 62 | 'single_or_multi' => is_multisite() ? 'multi' : 'single', |
| 63 | 'wpstaging_free_or_pro' => WPStaging::isPro() ? 'pro' : 'free', |
| 64 | 'wpstaging_version' => WPStaging::getVersion(), |
| 65 | 'operating_system_family' => stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : 'UNIX', |
| 66 | 'operating_system_family_raw' => PHP_OS, |
| 67 | 'operating_system_architecture' => $siteInfo->getOsArchitecture(), |
| 68 | 'active_theme' => get_option('stylesheet') ?: 'UNDEFINED', |
| 69 | 'wordpress_version' => $wp_version, |
| 70 | 'wpdb_version' => $wp_db_version, |
| 71 | 'db_collate' => $wpdb->collate, |
| 72 | 'db_charset' => $wpdb->charset, |
| 73 | 'sql_server_version_number' => $mySqlVersionNumber, |
| 74 | 'sql_server_version_engine' => $engine, |
| 75 | 'sql_server_version_engine_raw' => $mysqlInfo, |
| 76 | 'site_active_plugins' => isset($plugins['siteActive']) ? $plugins['siteActive'] : '', |
| 77 | 'mu_plugins' => isset($plugins['muPlugins']) ? $plugins['muPlugins'] : '', |
| 78 | 'network_active_plugins' => isset($plugins['networkActive']) ? $plugins['networkActive'] : '', |
| 79 | 'php_extensions' => $this->getPhpExtensions(), |
| 80 | ]; |
| 81 | |
| 82 | return $systemInfo; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | protected function getDatabaseVersionNumber(string $mysqlInfo): string |
| 92 | { |
| 93 | preg_match('/^[0-9.]+/', $mysqlInfo, $matches); |
| 94 | |
| 95 | return empty($matches) ? 'UNDEFINED' : array_shift($matches); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | protected function getNormalizedDatabaseEngine(string $mysqlInfo, string $mySqlVersionNumber): string |
| 112 | { |
| 113 | $namedEngines = [ |
| 114 | 'mariadb' => 'MARIADB', |
| 115 | 'azure' => 'AZURE', |
| 116 | 'postgre' => 'POSTGRE', |
| 117 | 'percona' => 'PERCONA', |
| 118 | 'oracle' => 'ORACLE', |
| 119 | 'tidb' => 'TIDB', |
| 120 | 'vitess' => 'VITESS', |
| 121 | 'singlestore' => 'SINGLESTORE', |
| 122 | 'starrocks' => 'STARROCKS', |
| 123 | 'doris' => 'DORIS', |
| 124 | 'mysql' => 'MYSQL', |
| 125 | ]; |
| 126 | |
| 127 | foreach ($namedEngines as $marker => $engine) { |
| 128 | if (stripos($mysqlInfo, $marker) !== false) { |
| 129 | return $engine; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (stripos(preg_replace('/[^\w]/', '', $mysqlInfo), 'microsoftsqlserver') !== false) { |
| 134 | return 'MICROSOFTSQLSERVER'; |
| 135 | } |
| 136 | |
| 137 | return preg_match('/^\d+\.\d+/', $mySqlVersionNumber) === 1 ? 'MYSQL' : 'UNDEFINED'; |
| 138 | } |
| 139 | |
| 140 | protected function getActivePlugins() |
| 141 | { |
| 142 | if (!function_exists('get_plugin_data')) { |
| 143 | include_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 144 | } |
| 145 | |
| 146 | $plugins = [ |
| 147 | 'siteActive' => [], |
| 148 | 'muPlugins' => [], |
| 149 | 'networkActive' => [], |
| 150 | ]; |
| 151 | |
| 152 | $callback = function () { |
| 153 | return []; |
| 154 | }; |
| 155 | |
| 156 | $undefinedString = 'UNDEFINED'; |
| 157 | |
| 158 | $wpPluginDir = wp_normalize_path(WP_PLUGIN_DIR); |
| 159 | $wpmuPluginDir = wp_normalize_path(WPMU_PLUGIN_DIR); |
| 160 | |
| 161 | |
| 162 | add_filter('pre_site_option_active_sitewide_plugins', $callback); |
| 163 | |
| 164 | $plugins = $this->getPlugins(); |
| 165 | foreach ($plugins as $activePlugin) { |
| 166 | $pluginFile = WP_PLUGIN_DIR . "/" . $activePlugin; |
| 167 | $name = str_replace($wpPluginDir, '', wp_normalize_path($activePlugin)); |
| 168 | $name = trim($name, '/\\'); |
| 169 | |
| 170 | if (!file_exists($pluginFile)) { |
| 171 | $plugins['siteActive'][$name] = $undefinedString; |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | $pluginData = get_plugin_data($pluginFile); |
| 176 | $version = array_key_exists('Version', $pluginData) ? $pluginData['Version'] : $undefinedString; |
| 177 | $plugins['siteActive'][$name] = $version; |
| 178 | } |
| 179 | |
| 180 | remove_filter('pre_site_option_active_sitewide_plugins', $callback); |
| 181 | |
| 182 | |
| 183 | foreach (get_mu_plugins() ?: [] as $activeMuPlugin => $pluginData) { |
| 184 | $version = array_key_exists('Version', $pluginData) ? $pluginData['Version'] : $undefinedString; |
| 185 | $name = str_replace($wpmuPluginDir, '', wp_normalize_path($activeMuPlugin)); |
| 186 | $name = trim($name, '/\\'); |
| 187 | |
| 188 | $plugins['muPlugins'][$name] = $version; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | if (function_exists('wp_get_active_network_plugins')) { |
| 193 | foreach (wp_get_active_network_plugins() ?: [] as $activePlugin) { |
| 194 | $name = str_replace($wpPluginDir, '', wp_normalize_path($activePlugin)); |
| 195 | $name = trim($name, '/\\'); |
| 196 | |
| 197 | $pluginFile = WP_PLUGIN_DIR . "/" . $activePlugin; |
| 198 | if (!file_exists($pluginFile)) { |
| 199 | $plugins['networkActive'][$name] = $undefinedString; |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | $pluginData = get_plugin_data($activePlugin); |
| 204 | $version = array_key_exists('Version', $pluginData) ? $pluginData['Version'] : $undefinedString; |
| 205 | |
| 206 | $plugins['networkActive'][$name] = $version; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return $plugins; |
| 211 | } |
| 212 | |
| 213 | protected function getPhpExtensions() |
| 214 | { |
| 215 | |
| 216 | if (!is_callable('get_loaded_extensions')) { |
| 217 | return []; |
| 218 | } |
| 219 | |
| 220 | $phpExtensions = @get_loaded_extensions(); |
| 221 | |
| 222 | |
| 223 | if (!is_array($phpExtensions)) { |
| 224 | return []; |
| 225 | } |
| 226 | |
| 227 | return $phpExtensions; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | protected function getPlugins() |
| 236 | { |
| 237 | global $wpdb; |
| 238 | |
| 239 | $optionTable = esc_sql($wpdb->prefix) . "options"; |
| 240 | $result = $wpdb->get_var("SHOW TABLES LIKE '$optionTable'"); |
| 241 | if ($result != $optionTable) { |
| 242 | return []; |
| 243 | } |
| 244 | |
| 245 | $sql = "SELECT option_value FROM " . esc_sql($wpdb->prefix) . "options WHERE option_name = 'active_plugins'"; |
| 246 | $result = $wpdb->get_results($sql, ARRAY_A); |
| 247 | $result = isset($result[0]['option_value']) ? unserialize($result[0]['option_value']) : []; |
| 248 | return (array)$result; |
| 249 | } |
| 250 | } |
| 251 |