| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 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_IncrementalBackup_Stats extends MWP_Action_IncrementalBackup_AbstractFiles |
| 12 |
{ |
| 13 |
|
| 14 |
public function __construct() |
| 15 |
{ |
| 16 |
parent::__construct(); |
| 17 |
} |
| 18 |
|
| 19 |
public function execute(array $params = array()) |
| 20 |
{ |
| 21 |
|
| 22 |
$wpdb = $this->container->getWordPressContext()->getDb(); |
| 23 |
$getState = new MWP_Action_GetState(); |
| 24 |
$getState->setContainer($this->container); |
| 25 |
$themesKey = MWP_Action_GetState::THEMES; |
| 26 |
$themes = $getState->execute(array($themesKey => array('type' => $themesKey, 'options' => array()))); |
| 27 |
$pluginsKey = MWP_Action_GetState::PLUGINS; |
| 28 |
$plugins = $getState->execute(array($pluginsKey => array('type' => $pluginsKey, 'options' => array()))); |
| 29 |
$activePlugins = array_filter($plugins[$pluginsKey]['result'], array($this, 'activePluginFilter')); |
| 30 |
$statistics = array(); |
| 31 |
$statistics['themes'] = count($themes[$themesKey]['result']); |
| 32 |
$statistics['themes_list'] = $themes[$themesKey]['result']; |
| 33 |
$statistics['plugins'] = count($plugins[$pluginsKey]['result']); |
| 34 |
$statistics['plugins_list'] = $plugins[$pluginsKey]['result']; |
| 35 |
$statistics['active_plugins'] = count($activePlugins); |
| 36 |
$statistics['posts'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='post'"); |
| 37 |
$statistics['published_posts'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='post' AND post_status='publish'"); |
| 38 |
$statistics['pages'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='page'"); |
| 39 |
$statistics['published_pages'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='page' AND post_status='publish'"); |
| 40 |
$statistics['uploads'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='attachment'"); |
| 41 |
$statistics['comments'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} c INNER JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID WHERE p.post_status = 'publish'"); |
| 42 |
$statistics['comments_approved'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->comments} c INNER JOIN {$wpdb->posts} p ON c.comment_post_ID = p.ID WHERE comment_approved = 1 AND p.post_status = 'publish'"); |
| 43 |
$latestPost = $wpdb->get_row("SELECT * FROM {$wpdb->posts} WHERE post_type='post' AND post_status='publish' ORDER BY ID DESC LIMIT 1"); |
| 44 |
$statistics['latest_post_title'] = isset($latestPost->post_title) ? $latestPost->post_title : ''; |
| 45 |
$statistics['latest_post_title'] = seems_utf8($statistics['latest_post_title']) ? $statistics['latest_post_title'] : utf8_encode($statistics['latest_post_title']); |
| 46 |
$statistics['latest_post_url'] = isset($latestPost) ? sprintf("%s?p=%d", get_site_url(), $latestPost->ID) : ''; |
| 47 |
$statistics['wp_version'] = $this->container->getWordPressContext()->getVersion(); |
| 48 |
$statistics['worker_version'] = $this->container->getParameter('worker_version'); |
| 49 |
$currentTheme = $this->container->getWordPressContext()->getCurrentTheme(); |
| 50 |
$statistics['active_theme'] = $currentTheme['Name'].' v'.$currentTheme['Version']; |
| 51 |
$statistics['platform'] = strtoupper(substr(PHP_OS, 0, 3)); |
| 52 |
$statistics['external_config'] = !@file_exists(ABSPATH.'wp-config.php') && @file_exists(dirname(ABSPATH).'/wp-config.php') && !@file_exists(dirname(ABSPATH).'/wp-settings.php'); |
| 53 |
$statistics['blog_list'] = $this->getBlogs(); |
| 54 |
$statistics['db_prefix'] = $wpdb->prefix; |
| 55 |
$statistics['mu_upload_blogs_dir'] = $this->getOldMuBlogsUploadDir(); |
| 56 |
|
| 57 |
if (!empty($params['file_count'])) { |
| 58 |
$paths = !empty($params['file_paths']) ? $params['file_paths'] : array(ABSPATH); |
| 59 |
$pathSize = array(); |
| 60 |
foreach ($paths as $path) { |
| 61 |
$pathSize[$this->replaceWindowsPath($path)] = $this->getFileCount($path); |
| 62 |
} |
| 63 |
$statistics['file_count'] = $pathSize; |
| 64 |
} |
| 65 |
|
| 66 |
return $this->createResult(array('statistic' => $statistics)); |
| 67 |
} |
| 68 |
|
| 69 |
public function activePluginFilter($plugin) |
| 70 |
{ |
| 71 |
return !empty($plugin['status']) && $plugin['status'] === 'active'; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param $path |
| 76 |
* |
| 77 |
* @return int |
| 78 |
*/ |
| 79 |
private function getFileCount($path) |
| 80 |
{ |
| 81 |
$size = 0; |
| 82 |
$ignore = array('.', '..'); |
| 83 |
$files = @scandir($path); |
| 84 |
if ($files === false) { |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ($files as $file) { |
| 89 |
if (in_array($file, $ignore)) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
if (is_dir(rtrim($path, '/').'/'.$file) && !is_link(rtrim($path, '/').'/'.$file)) { |
| 93 |
$size += $this->getFileCount(rtrim($path, '/').'/'.$file); |
| 94 |
} else { |
| 95 |
$size++; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $size; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return array |
| 104 |
*/ |
| 105 |
private function getBlogs() |
| 106 |
{ |
| 107 |
$limit = 10000; |
| 108 |
$blogs = array(); |
| 109 |
|
| 110 |
if (function_exists('get_sites') && class_exists('WP_Site_Query')) { // WP 4.6+ |
| 111 |
/** @handled function */ |
| 112 |
$sites = get_sites(array('number' => $limit)); |
| 113 |
foreach ($sites as $site) { |
| 114 |
$row = array(); |
| 115 |
$row['blog_id'] = $site->blog_id; |
| 116 |
$row['domain'] = $site->domain; |
| 117 |
$row['path'] = $site->path; |
| 118 |
$blogs[] = $row; |
| 119 |
} |
| 120 |
} elseif (function_exists('wp_get_sites')) { //WP 3.7 - 4.6 |
| 121 |
/** @handled function */ |
| 122 |
$sites = wp_get_sites(array('limit' => $limit)); |
| 123 |
foreach ($sites as $site) { |
| 124 |
$row = array(); |
| 125 |
|
| 126 |
$row['blog_id'] = $site['blog_id']; |
| 127 |
$row['domain'] = $site['domain']; |
| 128 |
$row['path'] = $site['path']; |
| 129 |
$blogs[] = $row; |
| 130 |
} |
| 131 |
} elseif (function_exists('get_blog_list')) { // WP < 3.7 |
| 132 |
/** @handled function */ |
| 133 |
$sites = get_blog_list(0, 'all'); |
| 134 |
foreach ($sites as $site) { |
| 135 |
$row = array(); |
| 136 |
|
| 137 |
$row['blog_id'] = $site['blog_id']; |
| 138 |
$row['domain'] = $site['domain']; |
| 139 |
$row['path'] = $site['path']; |
| 140 |
$blogs[] = $row; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $blogs; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @return string |
| 149 |
*/ |
| 150 |
private function getOldMuBlogsUploadDir() |
| 151 |
{ |
| 152 |
if (defined('UPLOADBLOGSDIR')) { |
| 153 |
return UPLOADBLOGSDIR; |
| 154 |
} |
| 155 |
|
| 156 |
return ''; |
| 157 |
} |
| 158 |
} |
| 159 |
|