芝麻web文件管理V1.00
编辑当前文件:/home/freeclou/optimyar/wp-content/plugins/wp-statistics/src/CLI/CliCommands.php
dataProvider = new CliDataProvider(); } /** * Show summary of statistics. * * ## OPTIONS * * [--format=
] * : Render output in a particular format. * --- * default: table * options: * - table * - csv * - json * - count * - yaml * --- * * @alias overview * @throws Exception */ public function summary($args, $assocArgs) { $data = $this->dataProvider->getSummaryData(); $usersOnline = Helper::formatNumberWithUnit($data['online'], 1); \WP_CLI::line("Users Online: " . $usersOnline); $items = []; foreach ($data['labels'] as $i => $label) { $items[] = [ 'Time' => $label, 'Visitors' => Helper::formatNumberWithUnit($data['visitors'][$i], 1), 'Views' => Helper::formatNumberWithUnit($data['hits'][$i], 1), ]; } \WP_CLI\Utils\format_items( \WP_CLI\Utils\get_flag_value($assocArgs, 'format', 'table'), $items, ['Time', 'Visitors', 'Views'] ); } /** * Show list of users online. * * ## OPTIONS * * [--number=
] * : Number of users to return. Default 15. * * [--format=
] * : Render output in a particular format. * --- * default: table * options: * - table * - csv * - json * - count * - yaml * --- * * @throws Exception */ public function online($args, $assocArgs) { $number = \WP_CLI\Utils\get_flag_value($assocArgs, 'number', 15); $lists = $this->dataProvider->getOnlineData(['per_page' => $number]); if (empty($lists)) { \WP_CLI::error("There are no users online."); } $columns = ['IP', 'Browser', 'Online For', 'Referrer', 'Page', 'User ID', 'Country']; $items = []; foreach ($lists as $row) { $items[] = [ 'IP' => $row->getIp() ?? '-', 'Browser' => $row->getBrowser()->getName() ?? '-', 'Online For' => $row->getOnlineTime() ?? '-', 'Referrer' => wp_strip_all_tags($row->getReferral()->getRawReferrer() ?? '-'), 'Page' => $row->getLastPage()['title'] ?? '-', 'User ID' => !empty($row->getUserId()) ? $row->getUserId() : '-', 'Country' => $row->getLocation()->getCountryName() ?? '-', ]; } \WP_CLI\Utils\format_items( \WP_CLI\Utils\get_flag_value($assocArgs, 'format', 'table'), $items, $columns ); } /** * Show list of visitors. * * ## OPTIONS * * [--number=
] * : Number of visitors to return. Default 15. * * [--format=
] * : Render output in a particular format. * --- * default: table * options: * - table * - csv * - json * - count * - yaml * --- * * @alias visitor * @throws Exception */ public function visitors($args, $assocArgs) { $number = \WP_CLI\Utils\get_flag_value($assocArgs, 'number', 15); $lists = $this->dataProvider->getVisitorsData(['per_page' => $number]); if (empty($lists)) { \WP_CLI::error("There are no visitors."); } $columns = ['IP', 'Last View', 'Browser', 'Referrer', 'Operating System', 'User ID', 'Country']; $items = []; foreach ($lists as $row) { $items[] = [ 'IP' => $row->getIp() ?? '-', 'Last View' => $row->getLastView() ?? '-', 'Browser' => $row->getBrowser()->getName() ?? '-', 'Referrer' => wp_strip_all_tags($row->getReferral()->getRawReferrer() ?? '-'), 'Operating System' => $row->getOs()->getName() ?? '-', 'User ID' => !empty($row->getUserId()) ? $row->getUserId() : '-', 'Country' => $row->getLocation()->getCountryName() ?? '-', ]; } \WP_CLI\Utils\format_items( \WP_CLI\Utils\get_flag_value($assocArgs, 'format', 'table'), $items, $columns ); } /** * Create all database tables if missing. * * @throws Exception */ public function create_tables($args, $assocArgs) { TableHandler::createAllTables(); \WP_CLI::success('All WP Statistics tables created (if not already existing).'); } /** * Record a hit. * * ## OPTIONS * * [--ip=
] * [--user_agent=
] * [--referrer=
] * [--user_id=
] * [--request_uri=
] * [--resource_type=
] * [--resource_id=
] * * @throws Exception */ public function record($args, $assocArgs) { $visitorProfile = new VisitorProfile(); add_filter('wp_statistics_current_page', function ($currentPage) use ($assocArgs) { return [ 'type' => $assocArgs['resource_type'] ?? 'post', 'id' => (int)($assocArgs['resource_id'] ?? 1), 'search_query' => '' ]; }); if (!empty($assocArgs['ip'])) { $_SERVER['REMOTE_ADDR'] = $assocArgs['ip']; } if (!empty($assocArgs['user_agent'])) { $_SERVER['HTTP_USER_AGENT'] = $assocArgs['user_agent']; } if (!empty($assocArgs['referrer'])) { $_SERVER['HTTP_REFERER'] = $assocArgs['referrer']; } if (!empty($assocArgs['request_uri'])) { $uri = trim($assocArgs['request_uri'], '/'); $_SERVER['REQUEST_URI'] = '/' . $uri . '/'; } if (!empty($assocArgs['user_id'])) { $userId = (int)$assocArgs['user_id']; add_filter('wp_statistics_user_id', function () use ($userId) { return $userId; }); } try { Hits::record($visitorProfile); \WP_CLI::success('Hit recorded successfully.'); } catch (Exception $e) { \WP_CLI::error(sprintf('Exclusion matched: %s', $e->getMessage())); } } }