* This file is part of the ManageWP Worker plugin.
* (c) ManageWP LLC <contact@managewp.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
class MWP_Action_GetState extends MWP_Action_Abstract
const COMMENTS = 'comments';
const SQL_RESULT = 'sqlResult';
const SINGLE_SQL_RESULT = 'singleSqlResult';
const PLUGINS = 'plugins';
const PLUGIN_UPDATES = 'pluginUpdates';
const THEME_UPDATES = 'themeUpdates';
const CORE_UPDATES = 'coreUpdates';
const SITE_INFO = 'siteInfo';
const SERVER_INFO = 'serverInfo';
public function execute(array $params = array())
foreach ($params as $fieldName => $queryInfo) {
$start = microtime(true);
mwp_logger()->debug('Started getting field '.$queryInfo['type']);
$queryResult = $this->getField($queryInfo['type'], $queryInfo['options']);
mwp_logger()->debug('Finished getting field '.$queryInfo['type']);
$end = sprintf("%.6f", microtime(true) - $start);
$result[$fieldName] = array(
'type' => $queryInfo['type'],
'result' => $queryResult,
private function getField($type, $options)
return $this->getUsers($options);
return $this->getPosts($options);
return $this->getComments($options);
return $this->getSqlResult($options);
case self::SINGLE_SQL_RESULT:
return $this->getSingleSqlResult($options);
return $this->getPlugins($options);
return $this->getThemes($options);
case self::PLUGIN_UPDATES:
return $this->getPluginUpdates($options);
case self::THEME_UPDATES:
return $this->getThemeUpdates($options);
return $this->getCoreUpdates($options);
return $this->getRoles($options);
return $this->getSiteInfo($options);
return $this->getServerInfo($options);
throw new RuntimeException(sprintf('Undefined field type provided: "%s".', $type));
protected function getUsers(array $options = array())
$userQuery = $this->container->getUserQuery();
$users = $userQuery->query($options);
if (function_exists('is_main_site') && is_main_site()) {
$this->includeAllSuperAdmins($users);
private function includeAllSuperAdmins(array &$users)
foreach (get_super_admins() as $superAdminUsername) {
foreach ($users as &$user) {
if ($user['username'] !== $superAdminUsername || $user['roles'] !== false) {
$user['roles'] = array('administrator' => true);
protected function getPosts(array $options = array())
$postQuery = $this->container->getPostQuery();
$posts = $postQuery->query($options);
protected function getComments(array $options = array())
$commentQuery = $this->container->getCommentQuery();
$comments = $commentQuery->query($options);
protected function getSingleSqlResult(array $options = array())
return $this->container->getWordPressContext()->getDb()->get_var($options['query']);
protected function getSqlResult(array $options = array())
return $this->container->getWordPressContext()->getDb()->get_results($options['query']);
protected function getPlugins(array $options = array())
'fetchDescription' => false,
'fetchAutoUpdate' => true,
'fetchAvailableUpdate' => false,
'fetchActivatedAt' => true,
$pluginProvider = $this->container->getPluginProvider();
$plugins = $pluginProvider->fetch($options);
$autoUpdateManager = $this->container->getAutoUpdateManager();
if ($options['fetchAutoUpdate']) {
foreach ($plugins as &$plugin) {
$plugin['autoUpdate'] = isset($plugin['slug']) ? $autoUpdateManager->isEnabledForPlugin($plugin['slug']) : false;
if ($options['fetchAvailableUpdate']) {
$um = $this->container->getUpdateManager();
foreach ($plugins as &$plugin) {
if (!isset($plugin['basename'])) {
$update = $um->getPluginUpdate($plugin['basename']);
$plugin['updateVersion'] = $update->version;
$plugin['updatePackage'] = $update->package;
if ($options['fetchActivatedAt']) {
$recentlyActivated = $this->container->getWordPressContext()->optionGet('recently_activated');
foreach ($plugins as &$plugin) {
if (isset($recentlyActivated[$plugin['basename']])) {
$plugin['activatedAt'] = date('Y-m-d\TH:i:sO', $recentlyActivated[$plugin['basename']]);
protected function getThemes(array $options = array())
'fetchDescription' => false,
'fetchAutoUpdate' => true,
'fetchAvailableUpdate' => false,
$themeProvider = $this->container->getThemeProvider();
$themes = $themeProvider->fetch($options);
$autoUpdateManager = $this->container->getAutoUpdateManager();
if ($options['fetchAutoUpdate']) {
foreach ($themes as &$theme) {
$theme['autoUpdate'] = $autoUpdateManager->isEnabledForTheme($theme['name']);
if ($options['fetchAvailableUpdate']) {
$um = $this->container->getUpdateManager();
foreach ($themes as &$theme) {
if (!isset($theme['slug'])) {
$update = $um->getThemeUpdate($theme['slug']);
$theme['updateVersion'] = $update->version;
$theme['updatePackage'] = $update->package;
public function getPluginUpdates(array $options = array())
$um = $this->container->getUpdateManager();
return $um->getPluginUpdates();
public function getThemeUpdates(array $options = array())
$um = $this->container->getUpdateManager();
return $um->getThemeUpdates();
public function getCoreUpdates(array $options = array())
$um = $this->container->getUpdateManager();
return $um->getCoreUpdates();
public function getRoles(array $options = array())
$roles = $this->container->getWordPressContext()->getUserRoles();
foreach ($roles as $roleSlug => $roleInfo) {
'name' => $roleInfo['name'],
if ($options['capabilities']) {
$role['capabilities'] = $roleInfo['capabilities'];
public function getSiteInfo(array $options = array())
$context = $this->container->getWordPressContext();
'title' => $context->getSiteTitle(),
'description' => $context->getSiteDescription(),
'siteUrl' => $context->getSiteUrl(),
'homeUrl' => $context->getHomeUrl(),
'masterSiteUrl' => $context->getMasterSiteUrl(),
'isMultisite' => $context->isMultisite(),
'public' => $context->optionGet('blog_public'),
'siteId' => $context->getSiteId(),
'currentUserId' => $context->getCurrentUser()->ID,
'currentUserUsername' => $context->getCurrentUser()->user_login,
'workerRealpath' => $this->container->getParameter('worker_realpath'),
'workerVersion' => $this->container->getParameter('worker_version'),
'workerRevision' => $this->container->getParameter('worker_revision'),
'timezone' => $this->container->getWordPressContext()->optionGet('timezone_string'),
'timezoneOffset' => $this->container->getWordPressContext()->optionGet('gmt_offset'),
public function getServerInfo(array $options = array())
$context = $this->container->getWordPressContext();
// @todo: move the checks below to a separate component.
'phpVersion' => PHP_VERSION,
'mysqlVersion' => $context->getDb()->db_version(),
'extensionPdoMysql' => extension_loaded('pdo_mysql'),
'extensionOpenSsl' => extension_loaded('openssl'),
'extensionFtp' => extension_loaded('ftp'),
'extensionZlib' => extension_loaded('zlib'),
'extensionBz2' => extension_loaded('bz2'),
'extensionZip' => extension_loaded('zip'),
'extensionCurl' => extension_loaded('curl'),
'extensionGd' => extension_loaded('gd'),
'extensionImagick' => extension_loaded('imagick'),
'extensionSockets' => extension_loaded('sockets'),
'extensionSsh2' => extension_loaded('ssh2'),
'shellAvailable' => mwp_is_shell_available(),
'safeMode' => mwp_is_safe_mode(),
'memoryLimit' => mwp_format_memory_limit(ini_get('memory_limit')),
'disabledFunctions' => mwp_get_disabled_functions(),
'processArchitecture' => strlen(decbin(~0)), // Results in 32 or 62.
'internalIp' => $this->container->getRequestStack()->getMasterRequest()->server['SERVER_ADDR'],
'uname' => php_uname('a'),
'hostname' => php_uname('n'),
'os' => (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'windows' : 'unix',