/*************************************************************
* Copyright (c) 2011 Prelovac Media
**************************************************************/
class MMB_Stats extends MMB_Core
public function get_site_statistics()
$siteStatistics = array();
$basePrefix = $wpdb->base_prefix;
if (!$this->mmb_multisite) {
$siteStatistics['users'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$basePrefix}users");
$siteStatistics['users'] = count(get_users(
'blog_id' => $wpdb->blogid,
$siteStatistics['approvedComments'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}comments c INNER JOIN {$prefix}posts p ON c.comment_post_ID = p.ID WHERE comment_approved = '1' AND p.post_status = 'publish'");
$siteStatistics['activePlugins'] = count((array)get_option('active_plugins', array()));
$siteStatistics['publishedPosts'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='post' AND post_status='publish'");
$siteStatistics['draftPosts'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='post' AND post_status='draft'");
$siteStatistics['publishedPages'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='page' AND post_status='publish'");
$siteStatistics['draftPages'] = (int)$wpdb->get_var("SELECT COUNT(*) FROM {$prefix}posts WHERE post_type='page' AND post_status='draft'");
public function get_core_update()
$core = $this->mmb_get_transient('update_core');
if (empty($core->updates)) {
foreach ($core->updates as $availableUpdate) {
if ($availableUpdate->locale == $locale && strtolower($availableUpdate->response) === "upgrade") {
$update = $availableUpdate;
$update = $core->updates[0];
// WordPress can actually have an update to the same version and locale if locale has not been updated
if ($update->response === 'development' || $update->response === 'upgrade' || version_compare($wp_version, $update->current, '<') || $locale !== $update->locale) {
$update->current_version = $wp_version;
public function get_comments($type, $limit, $trimlen = 200)
$comments = (array)get_comments('status='.$type.'&number='.$limit);
foreach ($comments as &$comment) {
$commented_post = get_post($comment->comment_post_ID);
$comment->post_title = $commented_post->post_title;
$comment->comment_content = $this->trim_content($comment->comment_content, $trimlen);
$comment->comment_content = seems_utf8($comment->comment_content) ? $comment->comment_content : utf8_encode($comment->comment_content);
unset($comment->comment_author_IP);
unset($comment->comment_karma);
unset($comment->comment_agent);
unset($comment->comment_type);
unset($comment->comment_parent);
public function get_posts($limit)
$userIdMap = $this->getUsersIDs();
$posts = (array)get_posts('post_status=publish&numberposts='.$limit.'&orderby=post_date&order=desc');
foreach ($posts as $id => $post) {
$add->post_permalink = get_permalink($post->ID);
$add->post_date = $post->post_date;
$add->post_title = $post->post_title;
$add->post_type = $post->post_type;
$add->comment_count = (int)$post->comment_count;
$author_name = isset($userIdMap[$post->post_author]) ? $userIdMap[$post->post_author] : '';
$add->post_author_name = array('author_id' => $post->post_author, 'author_name' => $author_name);
$posts = (array)get_pages('post_status=publish&numberposts='.$limit.'&orderby=post_date&order=desc');
foreach ($posts as $id => $post) {
$add->post_permalink = get_permalink($post->ID);
$add->post_type = $post->post_type;
$add->post_date = $post->post_date;
$add->post_title = $post->post_title;
$author_name = isset($userIdMap[$post->post_author]) ? $userIdMap[$post->post_author] : '';
$add->post_author = array('author_id' => $post->post_author, 'author_name' => $author_name);
usort($list, array($this, 'cmp_posts_worker'));
return array_slice($list, 0, $limit);
public function get_drafts($limit)
$posts = (array)get_posts('post_status=draft&numberposts='.$limit.'&orderby=post_date&order=desc');
foreach ($posts as $id => $post) {
$add->post_permalink = get_permalink($post->ID);
$add->post_type = $post->post_type;
$add->post_date = $post->post_date;
$add->post_title = $post->post_title;
$posts = (array)get_pages('post_status=draft&numberposts='.$limit.'&orderby=post_date&order=desc');
foreach ($posts as $id => $post) {
$add->post_permalink = get_permalink($post->ID);
$add->post_type = $post->post_type;
$add->post_date = $post->post_date;
$add->post_title = $post->post_title;
usort($list, array($this, 'cmp_posts_worker'));
return array_slice($list, 0, $limit);
public function get_scheduled($numberOfItems)
$posts = (array)get_posts('post_status=future&numberposts='.$numberOfItems.'&orderby=post_date&order=desc');
foreach ($posts as $id => $post) {
$add->post_permalink = get_permalink($post->ID);
$add->post_date = $post->post_date;
$add->post_type = $post->post_type;
$add->post_title = $post->post_title;
$posts = (array)get_pages('post_status=future&numberposts='.$numberOfItems.'&orderby=post_date&order=desc');
foreach ((array)$posts as $id => $post) {
$add->post_permalink = get_permalink($post->ID);
$add->post_type = $post->post_type;
$add->post_date = $post->post_date;
$add->post_title = $post->post_title;
usort($list, array($this, 'cmp_posts_worker'));
return array_slice($list, 0, $numberOfItems);
* @return array Map of wp_users.ID (string) => wp_users.display_name (string) where wp_users.user_status == 0.
private function getUsersIDs()
$users_authors = array();
$users = $wpdb->get_results("SELECT ID as user_id, display_name FROM $wpdb->users WHERE user_status=0");
foreach ($users as $user_key => $user_val) {
$users_authors[$user_val->user_id] = $user_val->display_name;
public function get_updates($stats, $options = array())
if (isset($options['themes']) && $options['themes']) {
$upgrades = $this->get_installer_instance()->get_upgradable_themes();
$stats['upgradable_themes'] = $upgrades;
if (isset($options['plugins']) && $options['plugins']) {
$upgrades = $this->get_installer_instance()->get_upgradable_plugins();
$stats['upgradable_plugins'] = $upgrades;
if (isset($options['translations']) && $options['translations']) {
$upgrades = $this->get_installer_instance()->get_upgradable_translations();
$stats['upgradable_translations'] = $upgrades;
public function getUserList()
$users = $this->get_user_instance()->get_users($filter);
if (empty($users['users']) || !is_array($users['users'])) {
foreach ($users['users'] as $user) {
$userList[] = $user['user_login'];
private function stats_arg(array $params, $widget, $arg)
// Cleanup plugin (the only one) has the following keys that hold options: overhead, revisions, spam.
if (isset($params['item_filter']['get_stats']['plugins']['cleanup'][$widget][$arg])) {
return $params['item_filter']['get_stats']['plugins']['cleanup'][$widget][$arg];
// Remaining "widgets" are number-indexed.
foreach ((array)@$params['item_filter']['get_stats'] as $key => $data) {
if (isset($data[1][$arg])) {
private function get_expired_transients(array $transientsData)
foreach ($transientsData as $transient) {
$expiredTransients += $this->get_expired_transients_size($transient['name'], $transient['suffix'], $transient['timeout'], $transient['mask'], $transient['limit']);
return $expiredTransients;
private function get_expired_transients_size($name, $suffix, $timeout, $mask, $limit)
$timeoutName = $name.$suffix;
$subStrLength = strlen($timeoutName) + 1;
$escapedTimeoutName = str_replace('_', '\_', $timeoutName);
$selectTimeOutedTransients = <<<SQL
SELECT SUBSTR(option_name, {$subStrLength}) AS transient_name FROM {$prefix}options WHERE option_name LIKE '{$escapedTimeoutName}{$mask}' AND option_value < {$timeout} LIMIT {$limit}
$transientsToDelete = $wpdb->get_col($selectTimeOutedTransients);
$timeoutsToDelete = array();
$transientsTotalSize = 0;
if (count($transientsToDelete) === 0) {
return $transientsTotalSize;
foreach ($transientsToDelete as &$transient) {
$timeoutsToDelete[] = "'".$timeoutName.$transient."'";
$transient = "'".$name.$transient."'";
$transientSizeClause = implode(',', $transientsToDelete);
$transientsSizeQuery = <<<SQL
SELECT SUM(LENGTH(option_value)) as valueSize, SUM(LENGTH(option_name)) as nameSize FROM {$prefix}options WHERE option_name IN ({$transientSizeClause})
$transientsSize = $wpdb->get_results($transientsSizeQuery);
$transientsTotalSize += ((int)$transientsSize[0]->nameSize);
$transientsTotalSize += ((int)$transientsSize[0]->valueSize);
$transientSizeClause = implode(',', $timeoutsToDelete);
$transientsSizeQuery = <<<SQL
SELECT SUM(LENGTH(option_value)) as valueSize, SUM(LENGTH(option_name)) as nameSize FROM {$prefix}options WHERE option_name IN ({$transientSizeClause})
$timeoutsSize = $wpdb->get_results($transientsSizeQuery);
$transientsTotalSize += ((int)$timeoutsSize[0]->valueSize);
$transientsTotalSize += ((int)$timeoutsSize[0]->nameSize);
return $transientsTotalSize;
public function get_stats(array $params)
$revisionLimit = (int)str_replace('r_', '', (string)$this->stats_arg($params, 'revisions', 'num_to_keep'));
$commentLimit = (int)$this->stats_arg($params, 'comments', 'numberposts');
// All post limits are the same on the API side.
$postLimit = (int)$this->stats_arg($params, 'posts', 'numberposts');
$transientsParams = (array)$this->stats_arg($params, 'transients', 'expire_data');
$save = array('plugins' => array('cleanup' => array('revisions' => array('num_to_keep' => $revisionLimit))));
// These options are only used for the revision cleanup action.
update_option('mmb_stats_filter', $save);
include_once ABSPATH.'wp-includes/update.php';
include_once ABSPATH.'wp-admin/includes/update.php';
mwp_logger()->debug('Started initializing stats');
'upgradable_themes' => $this->get_installer_instance()->get_upgradable_themes(),
'upgradable_plugins' => $this->get_installer_instance()->get_upgradable_plugins(),
'upgradable_translations' => $this->get_installer_instance()->get_upgradable_translations(),
'core_updates' => $this->get_core_update(),
'posts' => $this->get_posts($postLimit),
'drafts' => $this->get_drafts($postLimit),
'scheduled' => $this->get_scheduled($postLimit),
'hit_counter' => get_option('user_hit_count'),
'pending' => $this->get_comments('hold', $commentLimit),
'approved' => $this->get_comments('approve', $commentLimit),
'site_statistics' => $this->get_site_statistics(),
'num_revisions' => mmb_num_revisions($revisionLimit),
'overhead' => mmb_handle_overhead(false),
'expired_transients' => $this->get_expired_transients($transientsParams),
'num_spam_comments' => mmb_num_spam_comments(),
mwp_logger()->debug('Finished initializing stats');
global $wpdb, $wp_version, $mmb_plugin_dir;
$stats['worker_version'] = $GLOBALS['MMB_WORKER_VERSION'];
$stats['worker_revision'] = $GLOBALS['MMB_WORKER_REVISION'];
$stats['wordpress_version'] = $wp_version;
$stats['wordpress_locale_pckg'] = get_locale();
$stats['php_version'] = phpversion();
$stats['mysql_version'] = $wpdb->db_version();
$stats['wp_multisite'] = $this->mmb_multisite;
$stats['network_install'] = $this->network_admin_install;
mwp_logger()->debug('Started encrypting cookies...');
$stats['cookies'] = $this->get_stat_cookies();
mwp_logger()->debug('Finished encrypting cookies...');
$absPath = trailingslashit(ABSPATH); // This will prevent 2 backslash when WP in root
$stats['admin_usernames'] = $this->getUserList();
$stats['site_title'] = get_bloginfo('name');
$stats['site_tagline'] = get_bloginfo('description');
$stats['blog_public'] = get_option('blog_public');
$stats['timezone'] = get_option('timezone_string');
$stats['timezone_offset'] = get_option('gmt_offset');
$stats['server_ip'] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : null;
$stats['hostname'] = php_uname('n');
$stats['db_name'] = $this->get_active_db();
$stats['db_prefix'] = $wpdb->prefix;
$stats['content_path'] = WP_CONTENT_DIR;
$stats['absolute_path'] = $absPath;
$stats['worker_path'] = $mmb_plugin_dir;
$stats['site_home'] = get_option('home');
$fs = new Symfony_Filesystem_Filesystem();
if (defined('WP_CONTENT_DIR')) {
$contentDir = WP_CONTENT_DIR;
// This will prevent 2 backslash when WP in root
if (substr($contentDir, 0, 2) === '//') {
$contentDir = substr(WP_CONTENT_DIR, 1);
$stats['content_path'] = $contentDir;
if (substr($contentDir, 0, 1) != '/' && strpos($contentDir, ABSPATH) === false) {
$contentDir = ABSPATH.$contentDir;
$stats['content_relative_path'] = $fs->makePathRelative($contentDir, $absPath);
if (defined('WP_PLUGIN_DIR')) {
$pluginDir = WP_PLUGIN_DIR;
// This will prevent 2 backslash when WP in root
if (substr($pluginDir, 0, 2) === '//') {
$pluginDir = substr(WP_PLUGIN_DIR, 1);
if (substr($pluginDir, 0, 1) != '/' && strpos($pluginDir, ABSPATH) === false) {
$pluginDir = ABSPATH.$pluginDir;
$stats['plugin_relative_path'] = $fs->makePathRelative($pluginDir, $absPath);
if (defined('WPMU_PLUGIN_DIR')) {
$muPluginDir = WPMU_PLUGIN_DIR;
// This will prevent 2 backslash when WP in root
if (substr($muPluginDir, 0, 2) === '//') {
$muPluginDir = substr(WPMU_PLUGIN_DIR, 1);
if (substr($muPluginDir, 0, 1) != '/' && strpos($muPluginDir, ABSPATH) === false) {
$muPluginDir = ABSPATH.$muPluginDir;
$stats['mu_plugin_relative_path'] = $fs->makePathRelative($muPluginDir, $absPath);
$uploadDirArray = wp_upload_dir();
if (false === $uploadDir = realpath($uploadDirArray['basedir'])) {
$uploadDir = $uploadDirArray['basedir'];
$stats['uploads_relative_path'] = $fs->makePathRelative($uploadDir, $absPath);
$stats['writable'] = $this->is_server_writable();
$stats['fs_method'] = !$this->check_if_pantheon() ? get_filesystem_method() : '';
$mmode = get_option('mwp_maintenace_mode');
if (!empty($mmode) && isset($mmode['active']) && $mmode['active'] == true) {
$stats['maintenance'] = true;
if ($this->mmb_multisite) {
$stats = array_merge($stats, $this->get_multisite_stats($stats));
public function get_multisite_stats()
global $current_user, $wpdb;
$user_blogs = get_blogs_of_user($current_user->ID);
$network_blogs = (array)$wpdb->get_results("select `blog_id`, `site_id` from `{$wpdb->blogs}`");
$user_id = !empty($GLOBALS['mwp_user_id']) ? $GLOBALS['mwp_user_id'] : false;
$mainBlogId = defined('BLOG_ID_CURRENT_SITE') ? BLOG_ID_CURRENT_SITE : false;
if ($this->network_admin_install != '1' || !is_super_admin($user_id) || empty($network_blogs)) {
$stats = array('network_blogs' => array(), 'other_blogs' => array());
foreach ($network_blogs as $details) {
if (($mainBlogId !== false && $details->blog_id == $mainBlogId) || ($mainBlogId === false && $details->site_id == $details->blog_id)) {
$data = get_blog_details($details->blog_id);
if (in_array($details->blog_id, array_keys($user_blogs))) {
$stats['network_blogs'][] = $data->siteurl;