Files
advanced-mcp-plugin/includes/Tools/SiteTools.php
T

110 lines
4.1 KiB
PHP

<?php
namespace AskTamMCP\Tools;
defined('ABSPATH') || exit;
class SiteTools {
public static function getSiteInfo(array $args): array {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$active = count(get_option('active_plugins', []));
$theme = wp_get_theme();
return [
'site_name' => get_bloginfo('name'),
'site_url' => get_bloginfo('url'),
'wp_version' => get_bloginfo('version'),
'php_version' => PHP_VERSION,
'language' => get_bloginfo('language'),
'charset' => get_bloginfo('charset'),
'timezone' => get_option('timezone_string') ?: 'UTC',
'active_plugins' => $active,
'active_theme' => $theme->get('Name'),
'is_multisite' => is_multisite(),
];
}
public static function getHealth(array $args): array {
// Load required files safely
require_once ABSPATH . 'wp-admin/includes/misc.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/template.php';
require_once ABSPATH . 'wp-admin/includes/user.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if (!class_exists('WP_Site_Health')) {
require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
}
$health = \WP_Site_Health::get_instance();
$tests = $health->get_tests();
$results = [];
foreach ($tests['direct'] as $id => $test) {
if (!is_callable($test['test'])) continue;
try {
$result = call_user_func($test['test']);
$results[] = [
'id' => $id,
'label' => $result['label'] ?? $id,
'status' => $result['status'] ?? 'unknown',
'description' => wp_strip_all_tags($result['description'] ?? ''),
];
} catch (\Throwable $e) {
$results[] = ['id' => $id, 'label' => $id, 'status' => 'error', 'description' => $e->getMessage()];
}
}
return ['health_checks' => $results, 'count' => count($results)];
}
public static function runCron(array $args): array {
if (!defined('DOING_CRON')) define('DOING_CRON', true);
$crons = _get_cron_array();
$ran = [];
$now = time();
foreach ((array) $crons as $timestamp => $hooks) {
if ($timestamp > $now) continue;
foreach ($hooks as $hook => $events) {
foreach ($events as $key => $event) {
do_action_ref_array($hook, $event['args']);
$ran[] = $hook;
wp_unschedule_event($timestamp, $hook, $event['args']);
}
}
}
return ['executed_hooks' => $ran, 'count' => count($ran)];
}
public static function searchContent(array $args): array {
$query = new \WP_Query([
's' => $args['query'],
'post_type' => $args['post_types'] ?? ['post', 'page'],
'posts_per_page' => (int) ($args['per_page'] ?? 20),
'post_status' => 'publish',
]);
$results = [];
foreach ($query->posts as $p) {
$results[] = [
'id' => $p->ID,
'type' => $p->post_type,
'title' => $p->post_title,
'excerpt' => wp_trim_excerpt('', $p),
'link' => get_permalink($p->ID),
'date' => $p->post_date,
];
}
return ['results' => $results, 'total' => $query->found_posts];
}
public static function getPermalinkStructure(array $args): array {
return [
'permalink_structure' => get_option('permalink_structure'),
'common_bases' => ['plain'=>'','day_name'=>'/%year%/%monthnum%/%day%/%postname%/','month_name'=>'/%year%/%monthnum%/%postname%/','numeric'=>'/archives/%post_id%','post_name'=>'/%postname%/']
];
}
}