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