Account
2 years ago
Cache
2 years ago
Contracts
2 years ago
Core
2 years ago
Http
2 years ago
Notices
2 years ago
Analyst.php
2 years ago
ApiRequestor.php
2 years ago
ApiResponse.php
2 years ago
Collector.php
2 years ago
Mutator.php
2 years ago
helpers.php
2 years ago
Collector.php
222 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Analyst; |
| 4 | |
| 5 | use Analyst\Contracts\AnalystContract; |
| 6 | |
| 7 | /** |
| 8 | * Class Collector is a set of getters |
| 9 | * to retrieve some data from wp site |
| 10 | */ |
| 11 | class Collector |
| 12 | { |
| 13 | /** |
| 14 | * @var AnalystContract |
| 15 | */ |
| 16 | protected $sdk; |
| 17 | |
| 18 | /** |
| 19 | * @var \WP_User |
| 20 | */ |
| 21 | protected $user; |
| 22 | |
| 23 | public function __construct(AnalystContract $sdk) |
| 24 | { |
| 25 | $this->sdk = $sdk; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Load current user into memory |
| 30 | */ |
| 31 | public function loadCurrentUser() |
| 32 | { |
| 33 | $this->user = wp_get_current_user(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get site url |
| 38 | * |
| 39 | * @return string |
| 40 | */ |
| 41 | public function getSiteUrl() |
| 42 | { |
| 43 | return get_option('siteurl'); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get current user email |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | public function getCurrentUserEmail() |
| 52 | { |
| 53 | return $this->user->user_email; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get's email from general settings |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | public function getGeneralEmailAddress() |
| 62 | { |
| 63 | return get_option('admin_email'); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Is this user administrator |
| 68 | * |
| 69 | * @return bool |
| 70 | */ |
| 71 | public function isUserAdministrator() |
| 72 | { |
| 73 | return in_array('administrator', $this->user->roles); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * User name |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | public function getCurrentUserName() |
| 82 | { |
| 83 | return $this->user ? $this->user->user_nicename : 'unknown'; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * WP version |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
| 91 | public function getWordPressVersion() |
| 92 | { |
| 93 | global $wp_version; |
| 94 | |
| 95 | return $wp_version; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * PHP version |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public function getPHPVersion() |
| 104 | { |
| 105 | return phpversion(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Resolves plugin information |
| 110 | * |
| 111 | * @param string $path Absolute path to plugin |
| 112 | * @return array |
| 113 | */ |
| 114 | public function resolvePluginData($path) |
| 115 | { |
| 116 | if( !function_exists('get_plugin_data') ){ |
| 117 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 118 | } |
| 119 | |
| 120 | return get_plugin_data($path); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get plugin name by path |
| 125 | * |
| 126 | * @param $path |
| 127 | * @return string |
| 128 | */ |
| 129 | public function getPluginName($path) |
| 130 | { |
| 131 | $data = $this->resolvePluginData($path); |
| 132 | |
| 133 | return $data['Name']; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get plugin version |
| 138 | * |
| 139 | * @param $path |
| 140 | * @return string |
| 141 | */ |
| 142 | public function getPluginVersion($path) |
| 143 | { |
| 144 | $data = $this->resolvePluginData($path); |
| 145 | |
| 146 | return $data['Version'] ? $data['Version'] : null; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get server ip |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | public function getServerIp() |
| 155 | { |
| 156 | return sanitize_text_field($_SERVER['SERVER_ADDR']); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return string |
| 161 | */ |
| 162 | public function getSDKVersion() |
| 163 | { |
| 164 | return $this->sdk->version(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @return string |
| 169 | */ |
| 170 | public function getMysqlVersion() |
| 171 | { |
| 172 | $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); |
| 173 | |
| 174 | if ($conn) { |
| 175 | $version = mysqli_get_server_info($conn); |
| 176 | } else { |
| 177 | return 'unknown'; |
| 178 | } |
| 179 | |
| 180 | return $version ? $version : 'unknown'; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return string |
| 185 | */ |
| 186 | public function getSiteLanguage() |
| 187 | { |
| 188 | return get_locale(); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | /** |
| 193 | * Current WP theme |
| 194 | * |
| 195 | * @return false|string |
| 196 | */ |
| 197 | public function getCurrentThemeName() |
| 198 | { |
| 199 | return wp_get_theme()->get('Name'); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Get active plugins list |
| 204 | * |
| 205 | * @return array |
| 206 | */ |
| 207 | public function getActivePluginsList() |
| 208 | { |
| 209 | if (!function_exists('get_plugins')) { |
| 210 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 211 | } |
| 212 | |
| 213 | $allPlugins = get_plugins(); |
| 214 | |
| 215 | $activePluginsNames = array_map(function ($path) use ($allPlugins) { |
| 216 | return $allPlugins[$path]['Name']; |
| 217 | }, get_option('active_plugins')); |
| 218 | |
| 219 | return $activePluginsNames; |
| 220 | } |
| 221 | } |
| 222 |