| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Action_GetState extends MWP_Action_Abstract |
| 12 |
{ |
| 13 |
const USERS = 'users'; |
| 14 |
|
| 15 |
const POSTS = 'posts'; |
| 16 |
|
| 17 |
const COMMENTS = 'comments'; |
| 18 |
|
| 19 |
const SQL_RESULT = 'sqlResult'; |
| 20 |
|
| 21 |
const SINGLE_SQL_RESULT = 'singleSqlResult'; |
| 22 |
|
| 23 |
const PLUGINS = 'plugins'; |
| 24 |
|
| 25 |
const THEMES = 'themes'; |
| 26 |
|
| 27 |
const PLUGIN_UPDATES = 'pluginUpdates'; |
| 28 |
|
| 29 |
const THEME_UPDATES = 'themeUpdates'; |
| 30 |
|
| 31 |
const CORE_UPDATES = 'coreUpdates'; |
| 32 |
|
| 33 |
const ROLES = 'roles'; |
| 34 |
|
| 35 |
const SITE_INFO = 'siteInfo'; |
| 36 |
|
| 37 |
const SERVER_INFO = 'serverInfo'; |
| 38 |
|
| 39 |
public function execute(array $params = array()) |
| 40 |
{ |
| 41 |
$result = array(); |
| 42 |
|
| 43 |
foreach ($params as $fieldName => $queryInfo) { |
| 44 |
$start = microtime(true); |
| 45 |
mwp_logger()->debug('Started getting field '.$queryInfo['type']); |
| 46 |
$queryResult = $this->getField($queryInfo['type'], $queryInfo['options']); |
| 47 |
mwp_logger()->debug('Finished getting field '.$queryInfo['type']); |
| 48 |
$end = sprintf("%.6f", microtime(true) - $start); |
| 49 |
$result[$fieldName] = array( |
| 50 |
'type' => $queryInfo['type'], |
| 51 |
'benchmark' => $end, |
| 52 |
'result' => $queryResult, |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
return $result; |
| 57 |
} |
| 58 |
|
| 59 |
private function getField($type, $options) |
| 60 |
{ |
| 61 |
switch ($type) { |
| 62 |
case self::USERS: |
| 63 |
return $this->getUsers($options); |
| 64 |
case self::POSTS: |
| 65 |
return $this->getPosts($options); |
| 66 |
case self::COMMENTS: |
| 67 |
return $this->getComments($options); |
| 68 |
case self::SQL_RESULT: |
| 69 |
return $this->getSqlResult($options); |
| 70 |
case self::SINGLE_SQL_RESULT: |
| 71 |
return $this->getSingleSqlResult($options); |
| 72 |
case self::PLUGINS: |
| 73 |
return $this->getPlugins($options); |
| 74 |
case self::THEMES: |
| 75 |
return $this->getThemes($options); |
| 76 |
case self::PLUGIN_UPDATES: |
| 77 |
return $this->getPluginUpdates($options); |
| 78 |
case self::THEME_UPDATES: |
| 79 |
return $this->getThemeUpdates($options); |
| 80 |
case self::CORE_UPDATES: |
| 81 |
return $this->getCoreUpdates($options); |
| 82 |
case self::ROLES: |
| 83 |
return $this->getRoles($options); |
| 84 |
case self::SITE_INFO: |
| 85 |
return $this->getSiteInfo($options); |
| 86 |
case self::SERVER_INFO: |
| 87 |
return $this->getServerInfo($options); |
| 88 |
default: |
| 89 |
throw new RuntimeException(sprintf('Undefined field type provided: "%s".', $type)); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
protected function getUsers(array $options = array()) |
| 94 |
{ |
| 95 |
$userQuery = $this->container->getUserQuery(); |
| 96 |
$users = $userQuery->query($options); |
| 97 |
|
| 98 |
if (function_exists('is_main_site') && is_main_site()) { |
| 99 |
$this->includeAllSuperAdmins($users); |
| 100 |
} |
| 101 |
|
| 102 |
return $users; |
| 103 |
} |
| 104 |
|
| 105 |
private function includeAllSuperAdmins(array &$users) |
| 106 |
{ |
| 107 |
foreach (get_super_admins() as $superAdminUsername) { |
| 108 |
foreach ($users as &$user) { |
| 109 |
if ($user['username'] !== $superAdminUsername || $user['roles'] !== false) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
$user['roles'] = array('administrator' => true); |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
protected function getPosts(array $options = array()) |
| 119 |
{ |
| 120 |
$postQuery = $this->container->getPostQuery(); |
| 121 |
$posts = $postQuery->query($options); |
| 122 |
|
| 123 |
return $posts; |
| 124 |
} |
| 125 |
|
| 126 |
protected function getComments(array $options = array()) |
| 127 |
{ |
| 128 |
$commentQuery = $this->container->getCommentQuery(); |
| 129 |
$comments = $commentQuery->query($options); |
| 130 |
|
| 131 |
return $comments; |
| 132 |
} |
| 133 |
|
| 134 |
protected function getSingleSqlResult(array $options = array()) |
| 135 |
{ |
| 136 |
$options += array( |
| 137 |
'query' => null, |
| 138 |
); |
| 139 |
|
| 140 |
return $this->container->getWordPressContext()->getDb()->get_var($options['query']); |
| 141 |
} |
| 142 |
|
| 143 |
protected function getSqlResult(array $options = array()) |
| 144 |
{ |
| 145 |
$options += array( |
| 146 |
'query' => null, |
| 147 |
); |
| 148 |
|
| 149 |
return $this->container->getWordPressContext()->getDb()->get_results($options['query']); |
| 150 |
} |
| 151 |
|
| 152 |
protected function getPlugins(array $options = array()) |
| 153 |
{ |
| 154 |
$options += array( |
| 155 |
'fetchDescription' => false, |
| 156 |
'fetchAutoUpdate' => true, |
| 157 |
'fetchAvailableUpdate' => false, |
| 158 |
'fetchActivatedAt' => true, |
| 159 |
); |
| 160 |
|
| 161 |
$pluginProvider = $this->container->getPluginProvider(); |
| 162 |
$plugins = $pluginProvider->fetch($options); |
| 163 |
$autoUpdateManager = $this->container->getAutoUpdateManager(); |
| 164 |
|
| 165 |
if ($options['fetchAutoUpdate']) { |
| 166 |
foreach ($plugins as &$plugin) { |
| 167 |
$plugin['autoUpdate'] = isset($plugin['slug']) ? $autoUpdateManager->isEnabledForPlugin($plugin['slug']) : false; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if ($options['fetchAvailableUpdate']) { |
| 172 |
$um = $this->container->getUpdateManager(); |
| 173 |
foreach ($plugins as &$plugin) { |
| 174 |
if (!isset($plugin['basename'])) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
$update = $um->getPluginUpdate($plugin['basename']); |
| 179 |
if ($update !== null) { |
| 180 |
$plugin['updateVersion'] = $update->version; |
| 181 |
$plugin['updatePackage'] = $update->package; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if ($options['fetchActivatedAt']) { |
| 187 |
$recentlyActivated = $this->container->getWordPressContext()->optionGet('recently_activated'); |
| 188 |
foreach ($plugins as &$plugin) { |
| 189 |
if (isset($recentlyActivated[$plugin['basename']])) { |
| 190 |
$plugin['activatedAt'] = date('Y-m-d\TH:i:sO', $recentlyActivated[$plugin['basename']]); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $plugins; |
| 196 |
} |
| 197 |
|
| 198 |
protected function getThemes(array $options = array()) |
| 199 |
{ |
| 200 |
$options += array( |
| 201 |
'fetchDescription' => false, |
| 202 |
'fetchAutoUpdate' => true, |
| 203 |
'fetchAvailableUpdate' => false, |
| 204 |
); |
| 205 |
|
| 206 |
$themeProvider = $this->container->getThemeProvider(); |
| 207 |
$themes = $themeProvider->fetch($options); |
| 208 |
$autoUpdateManager = $this->container->getAutoUpdateManager(); |
| 209 |
|
| 210 |
if ($options['fetchAutoUpdate']) { |
| 211 |
foreach ($themes as &$theme) { |
| 212 |
$theme['autoUpdate'] = $autoUpdateManager->isEnabledForTheme($theme['name']); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if ($options['fetchAvailableUpdate']) { |
| 217 |
$um = $this->container->getUpdateManager(); |
| 218 |
foreach ($themes as &$theme) { |
| 219 |
if (!isset($theme['slug'])) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
|
| 223 |
$update = $um->getThemeUpdate($theme['slug']); |
| 224 |
if ($update !== null) { |
| 225 |
$theme['updateVersion'] = $update->version; |
| 226 |
$theme['updatePackage'] = $update->package; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return $themes; |
| 232 |
} |
| 233 |
|
| 234 |
public function getPluginUpdates(array $options = array()) |
| 235 |
{ |
| 236 |
$um = $this->container->getUpdateManager(); |
| 237 |
|
| 238 |
return $um->getPluginUpdates(); |
| 239 |
} |
| 240 |
|
| 241 |
public function getThemeUpdates(array $options = array()) |
| 242 |
{ |
| 243 |
$um = $this->container->getUpdateManager(); |
| 244 |
|
| 245 |
return $um->getThemeUpdates(); |
| 246 |
} |
| 247 |
|
| 248 |
public function getCoreUpdates(array $options = array()) |
| 249 |
{ |
| 250 |
$um = $this->container->getUpdateManager(); |
| 251 |
|
| 252 |
return $um->getCoreUpdates(); |
| 253 |
} |
| 254 |
|
| 255 |
public function getRoles(array $options = array()) |
| 256 |
{ |
| 257 |
$options += array( |
| 258 |
'capabilities' => false, |
| 259 |
); |
| 260 |
|
| 261 |
$roles = $this->container->getWordPressContext()->getUserRoles(); |
| 262 |
|
| 263 |
$result = array(); |
| 264 |
|
| 265 |
foreach ($roles as $roleSlug => $roleInfo) { |
| 266 |
$role = array( |
| 267 |
'slug' => $roleSlug, |
| 268 |
'name' => $roleInfo['name'], |
| 269 |
); |
| 270 |
if ($options['capabilities']) { |
| 271 |
$role['capabilities'] = $roleInfo['capabilities']; |
| 272 |
} |
| 273 |
$result[] = $role; |
| 274 |
} |
| 275 |
|
| 276 |
return $result; |
| 277 |
} |
| 278 |
|
| 279 |
public function getSiteInfo(array $options = array()) |
| 280 |
{ |
| 281 |
$context = $this->container->getWordPressContext(); |
| 282 |
|
| 283 |
return array( |
| 284 |
'title' => $context->getSiteTitle(), |
| 285 |
'description' => $context->getSiteDescription(), |
| 286 |
'siteUrl' => $context->getSiteUrl(), |
| 287 |
'homeUrl' => $context->getHomeUrl(), |
| 288 |
'masterSiteUrl' => $context->getMasterSiteUrl(), |
| 289 |
'isMultisite' => $context->isMultisite(), |
| 290 |
'public' => $context->optionGet('blog_public'), |
| 291 |
'siteId' => $context->getSiteId(), |
| 292 |
'currentUserId' => $context->getCurrentUser()->ID, |
| 293 |
'currentUserUsername' => $context->getCurrentUser()->user_login, |
| 294 |
'workerRealpath' => $this->container->getParameter('worker_realpath'), |
| 295 |
'workerVersion' => $this->container->getParameter('worker_version'), |
| 296 |
'workerRevision' => $this->container->getParameter('worker_revision'), |
| 297 |
'timezone' => $this->container->getWordPressContext()->optionGet('timezone_string'), |
| 298 |
'timezoneOffset' => $this->container->getWordPressContext()->optionGet('gmt_offset'), |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
public function getServerInfo(array $options = array()) |
| 303 |
{ |
| 304 |
$context = $this->container->getWordPressContext(); |
| 305 |
|
| 306 |
return array( |
| 307 |
// @todo: move the checks below to a separate component. |
| 308 |
'phpVersion' => PHP_VERSION, |
| 309 |
'mysqlVersion' => $context->getDb()->db_version(), |
| 310 |
'extensionPdoMysql' => extension_loaded('pdo_mysql'), |
| 311 |
'extensionOpenSsl' => extension_loaded('openssl'), |
| 312 |
'extensionFtp' => extension_loaded('ftp'), |
| 313 |
'extensionZlib' => extension_loaded('zlib'), |
| 314 |
'extensionBz2' => extension_loaded('bz2'), |
| 315 |
'extensionZip' => extension_loaded('zip'), |
| 316 |
'extensionCurl' => extension_loaded('curl'), |
| 317 |
'extensionGd' => extension_loaded('gd'), |
| 318 |
'extensionImagick' => extension_loaded('imagick'), |
| 319 |
'extensionSockets' => extension_loaded('sockets'), |
| 320 |
'extensionSsh2' => extension_loaded('ssh2'), |
| 321 |
'shellAvailable' => mwp_is_shell_available(), |
| 322 |
'safeMode' => mwp_is_safe_mode(), |
| 323 |
'memoryLimit' => mwp_format_memory_limit(ini_get('memory_limit')), |
| 324 |
'disabledFunctions' => mwp_get_disabled_functions(), |
| 325 |
'processArchitecture' => strlen(decbin(~0)), // Results in 32 or 62. |
| 326 |
'internalIp' => $this->container->getRequestStack()->getMasterRequest()->server['SERVER_ADDR'], |
| 327 |
'uname' => php_uname('a'), |
| 328 |
'hostname' => php_uname('n'), |
| 329 |
'os' => (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'windows' : 'unix', |
| 330 |
); |
| 331 |
} |
| 332 |
} |
| 333 |
|