' . esc_html__( 'Settings', 'admindra-mcp' ) . '' ); return $links; } /** * Documentation and support under the plugin's description. * * @param string[] $links Existing links. * @param string $file Plugin basename of the row. * @return string[] */ public static function row_meta( $links, $file ) { if ( plugin_basename( ADMINDRA_MCP_FILE ) !== $file ) { return $links; } foreach ( array_slice( self::info_links(), 0, 2, true ) as $url => $label ) { $links[] = '' . esc_html( $label ) . ''; } return $links; } /** * The links on the settings screen; the Plugins screen shows the first two. * * @return array URL => label. */ private static function info_links() { return array( self::URL_DOCS => __( 'Documentation', 'admindra-mcp' ), self::URL_SUPPORT => __( 'Support', 'admindra-mcp' ), self::URL_TERMS => __( 'Terms', 'admindra-mcp' ), self::URL_PRIVACY => __( 'Privacy', 'admindra-mcp' ), ); } /** * Shown only on the Plugins screen and this plugin's own screen, and only * when the bundled OAuth library failed to load. */ public static function dependency_notice() { if ( ! current_user_can( 'manage_options' ) || class_exists( '\WPMedia\MCP\OAuth\Bootstrap' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! in_array( $screen->base, array( 'plugins', 'tools_page_' . self::PAGE ), true ) ) { return; } echo '

' . esc_html__( 'AdMindra MCP: the bundled OAuth library could not be loaded, so remote MCP access is not available. Reinstall the plugin from its original zip.', 'admindra-mcp' ) . '

'; } /** * Tools → AdMindra MCP. */ public static function menu() { add_management_page( __( 'AdMindra MCP', 'admindra-mcp' ), __( 'AdMindra MCP', 'admindra-mcp' ), 'manage_options', self::PAGE, array( __CLASS__, 'page' ) ); } /** * Whether the MCP Adapter's main class is loaded. * * @return bool */ private static function adapter_loaded() { return AdMindra_MCP_Setup::ready(); } /** * Whether the bundled OAuth library is loaded. * * @return bool */ public static function oauth_loaded() { return class_exists( '\WPMedia\MCP\OAuth\Bootstrap' ); } /** * The settings screen's own stylesheet and copy button, on that screen only. * * @param string $hook Admin page hook. */ public static function assets( $hook ) { if ( 'tools_page_' . self::PAGE !== $hook ) { return; } wp_enqueue_style( 'admindra-mcp-admin', plugins_url( 'assets/admin.css', ADMINDRA_MCP_FILE ), array(), ADMINDRA_MCP_VERSION ); wp_enqueue_script( 'admindra-mcp-admin', plugins_url( 'assets/admin.js', ADMINDRA_MCP_FILE ), array(), ADMINDRA_MCP_VERSION, true ); } /** * A status chip: green when ready, amber when not. * * @param string $label Label. * @param bool $ok Ready. * @return string */ private static function chip( $label, $ok ) { return '' . ( $ok ? '✓ ' : '! ' ) . esc_html( $label ) . ''; } /** * The settings screen. */ public static function page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to access this page.', 'admindra-mcp' ), '', array( 'response' => 403 ) ); } $https = 0 === strpos( home_url(), 'https://' ); $ready = self::adapter_loaded() && self::oauth_loaded() && $https && get_option( 'permalink_structure' ); $content = AdMindra_MCP_Content::enabled(); echo '
'; // WordPress moves admin notices under the first h1 or h2; this one keeps them out of the header. echo '

' . esc_html__( 'AdMindra MCP', 'admindra-mcp' ) . '

'; echo '
AdMindraMCP ' . esc_html( ADMINDRA_MCP_VERSION ) . '
'; echo '

' . esc_html__( 'Connect ChatGPT or Claude to this site. The assistant reads it, explains it and, when you allow it, edits its posts and pages.', 'admindra-mcp' ) . '

'; echo '
'; echo self::chip( __( 'MCP Adapter', 'admindra-mcp' ), self::adapter_loaded() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- chip() escapes. echo self::chip( __( 'OAuth', 'admindra-mcp' ), self::oauth_loaded() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- chip() escapes. echo self::chip( __( 'HTTPS', 'admindra-mcp' ), $https ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- chip() escapes. echo self::chip( __( 'Pretty permalinks', 'admindra-mcp' ), (bool) get_option( 'permalink_structure' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- chip() escapes. echo '' . esc_html( $content ? __( 'Content edits on', 'admindra-mcp' ) : __( 'Reads only', 'admindra-mcp' ) ) . ''; echo '
'; /* MCP Adapter has its own step below; this line is for what the site itself must change. */ if ( ! $ready && ( ! $https || ! get_option( 'permalink_structure' ) || ! self::oauth_loaded() ) ) { echo '

' . esc_html__( 'Fix what is amber above before connecting: the assistant signs in over OAuth, which needs HTTPS and pretty permalinks (Settings → Permalinks, anything but "Plain").', 'admindra-mcp' ) . '

'; } echo '
'; echo '
'; self::setup_box(); AdMindra_MCP_Connect::panel(); echo '

' . esc_html__( 'What the assistant may do', 'admindra-mcp' ) . '

    '; echo '
  • ' . esc_html__( 'Site status, plugins and themes, and the updates WordPress found', 'admindra-mcp' ) . '
  • '; echo '
  • ' . esc_html__( 'Find pages with shortcodes that are not registered', 'admindra-mcp' ) . '
  • '; echo '
  • ' . esc_html__( 'Find and read posts, pages and plugin or theme source files', 'admindra-mcp' ) . '
  • '; echo '
  • ' . esc_html__( 'Edit the title, text and excerpt of posts and pages, and the site title and tagline', 'admindra-mcp' ) . '
  • '; echo '
'; echo '
'; wp_nonce_field( 'admindra_mcp_content_toggle' ); echo '
' . esc_html__( 'Content edits', 'admindra-mcp' ) . '' . esc_html( $content ? __( 'On. Every change keeps a revision you can restore in the editor.', 'admindra-mcp' ) : __( 'Off. The assistant only reads.', 'admindra-mcp' ) ) . '
'; echo ''; echo '
'; echo '

' . esc_html__( 'Never: installing, updating or deleting plugins and themes, writing files, publishing or deleting posts, users or other settings.', 'admindra-mcp' ) . '

'; echo '
'; if ( has_action( 'admindra_mcp_addon_panel' ) ) { /* The add-on draws its own part here: its connection to AdMindra, its switch and its log. */ do_action( 'admindra_mcp_addon_panel' ); } else { self::addon_box(); } echo '
'; self::more_box(); echo '
'; AdMindra_MCP_Connect::advanced(); echo '

'; printf( /* translators: %s: link to admindra.com */ esc_html__( 'AdMindra MCP is free software under the GPL, by Viktor Grahn at 3on Reklambyrå & Webbyrå AB, the company behind %s. It sends nothing to AdMindra.', 'admindra-mcp' ), 'AdMindra' ); echo '

'; $parts = array(); foreach ( self::info_links() as $url => $label ) { $parts[] = '' . esc_html( $label ) . ''; } echo implode( ' · ', $parts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each part is escaped above. echo '

'; } /** * The step before anything can connect, while MCP Adapter is missing or * inactive; a confirmation once WordPress has activated it. */ private static function setup_box() { $state = AdMindra_MCP_Setup::state(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only chooses which confirmation to show. $done = isset( $_GET['adm-setup'] ) && 'done' === $_GET['adm-setup']; if ( 'active' === $state ) { if ( $done ) { echo '
' . esc_html__( 'Ready', 'admindra-mcp' ) . ''; echo '

' . esc_html__( 'MCP Adapter is active', 'admindra-mcp' ) . '

'; echo '

' . esc_html__( 'Copy the MCP URL below into ChatGPT or Claude to connect.', 'admindra-mcp' ) . '

'; } return; } echo '
' . esc_html__( 'One step left', 'admindra-mcp' ) . ''; echo '

' . esc_html( 'missing' === $state ? __( 'Install MCP Adapter', 'admindra-mcp' ) : __( 'Activate MCP Adapter', 'admindra-mcp' ) ) . '

'; echo '

' . esc_html__( 'ChatGPT and Claude reach your site through MCP Adapter, the free plugin from the WordPress AI team. Until it is active nothing can connect; everything on this screen already works.', 'admindra-mcp' ) . '

'; if ( AdMindra_MCP_Setup::allowed( $state ) ) { echo '

' . esc_html__( 'WordPress installs it from WordPress.org on its own screen, then brings you back here.', 'admindra-mcp' ) . '

'; echo '
' . AdMindra_MCP_Setup::button( $state ) . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- button() escapes. } else { echo '

' . esc_html__( 'Ask an administrator who can install plugins to install and activate MCP Adapter.', 'admindra-mcp' ) . '

'; } } /** * What the add-on adds, on this screen only. */ private static function addon_box() { echo '
' . esc_html__( 'Add-on · free with an AdMindra account', 'admindra-mcp' ) . ''; echo '

' . esc_html__( 'Let the assistant update and fix plugins and themes too', 'admindra-mcp' ) . '

'; echo '

' . esc_html__( 'AdMindra MCP Maintenance lets the same assistant replace plugin and theme files and run WordPress.org updates. Every change is syntax-checked and backed up encrypted, the site is checked before and after, and it is rolled back by itself if the site stops answering.', 'admindra-mcp' ) . '

'; echo '
  1. ' . esc_html__( 'Create a free AdMindra account', 'admindra-mcp' ) . '
  2. ' . esc_html__( 'Download the add-on under WordPress in AdMindra', 'admindra-mcp' ) . '
  3. ' . esc_html__( 'Paste the code AdMindra shows here', 'admindra-mcp' ) . '
'; echo '
'; } /** * More from AdMindra, on this screen only. */ private static function more_box() { $items = array( array( self::URL_NOTES, __( 'MindraNotes', 'admindra-mcp' ), __( 'Notes, lists and tasks that sort themselves. Free.', 'admindra-mcp' ) ), array( self::URL_NODE, __( 'AdMindra Node', 'admindra-mcp' ), __( 'Free AI on your own computer. Private by default.', 'admindra-mcp' ) ), array( self::URL_HOME, __( 'AdMindra', 'admindra-mcp' ), __( 'Ads, tracking, sales and websites in one place.', 'admindra-mcp' ) ), ); echo '

' . esc_html__( 'More from AdMindra', 'admindra-mcp' ) . '

'; foreach ( $items as $item ) { echo '' . esc_html( $item[1] ) . '' . esc_html( $item[2] ) . ''; } echo '
'; } /** * Every ability: a logged-in administrator. * * @return bool */ public static function permission() { return is_user_logged_in() && current_user_can( 'manage_options' ); } /** * A closed object schema. * * @param array $props Properties. * @param string[] $required Required property names. * @return array */ private static function schema( $props = array(), $required = array() ) { $schema = array( 'type' => 'object', 'properties' => $props, 'required' => $required, 'additionalProperties' => false, ); // A client that sends no arguments at all sends null, which is not an // object; with a default WordPress substitutes an empty one. if ( ! $required ) { $schema['default'] = array(); } return $schema; } /** * Register one ability. * * @param string $slug Name after "admindra-mcp/". * @param string $label Label. * @param string $desc Description the AI client reads. * @param array $schema Input schema. * @param callable $fn Execute callback. * @param bool $write Whether it changes content. */ private static function reg( $slug, $label, $desc, $schema, $fn, $write = false ) { wp_register_ability( 'admindra-mcp/' . $slug, array( 'label' => $label, 'description' => $desc, 'category' => 'admindra-mcp', 'input_schema' => $schema, 'execute_callback' => $fn, 'permission_callback' => array( __CLASS__, 'permission' ), 'meta' => array( 'public' => true, // A content edit changes the site but keeps a revision, so it // is not destructive. 'annotations' => array( 'readonly' => ! $write, 'destructive' => false, 'idempotent' => true, ), ), ) ); } /** * The ability category. */ public static function category() { if ( function_exists( 'wp_register_ability_category' ) ) { wp_register_ability_category( 'admindra-mcp', array( 'label' => __( 'AdMindra MCP', 'admindra-mcp' ), 'description' => __( 'Read-only WordPress diagnostics.', 'admindra-mcp' ), ) ); } } /** * Register every ability. */ public static function abilities() { if ( ! function_exists( 'wp_register_ability' ) ) { return; } $path = array( 'type' => 'string', 'maxLength' => 250, 'description' => 'Relative file path inside the selected plugin or theme', ); $kind = array( 'type' => 'string', 'enum' => array( 'plugin', 'theme' ) ); $slug = array( 'type' => 'string', 'maxLength' => 120 ); $target = array( 'kind' => $kind, 'slug' => $slug, 'path' => $path ); self::reg( 'status', 'Site status', 'WordPress, PHP and MCP readiness of this site.', self::schema(), array( __CLASS__, 'status' ) ); self::reg( 'plugins', 'Plugin inventory', 'Installed plugins with version and whether each is active.', self::schema(), array( __CLASS__, 'plugins' ) ); self::reg( 'themes', 'Theme inventory', 'Installed themes with version.', self::schema(), array( __CLASS__, 'themes' ) ); self::reg( 'pages', 'Registration page audit', 'Published pages that mention registration or sign-up (or the given search text), with each shortcode on them and whether it is registered.', self::schema( array( 'search' => array( 'type' => 'string', 'maxLength' => 100, 'description' => 'Optional text to look for instead of registration words' ) ) ), array( __CLASS__, 'pages' ) ); self::reg( 'updates', 'Available updates', 'Plugin and theme updates that WordPress\'s own update check last found for this site, and when it checked: name, installed and offered version, the WordPress and PHP versions each needs and whether it fits this site. Reading only: the administrator runs updates on WordPress\'s Updates screen (update_screen).', self::schema(), array( __CLASS__, 'updates' ) ); $id = array( 'type' => 'integer', 'minimum' => 1 ); self::reg( 'find-posts', 'Find posts and pages', 'Posts and pages (and other public content types) by text, newest change first, with id, status, title and when they were last changed.', self::schema( array( 'search' => array( 'type' => 'string', 'maxLength' => 100 ), 'type' => array( 'type' => 'string', 'maxLength' => 40 ), 'limit' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50 ), ) ), array( 'AdMindra_MCP_Content', 'find' ) ); self::reg( 'read-post', 'Read post or page', 'The raw title, content and excerpt of one post or page, with modified_gmt, which update-post needs.', self::schema( array( 'id' => $id ), array( 'id' ) ), array( 'AdMindra_MCP_Content', 'read' ) ); self::reg( 'update-post', 'Edit post or page', 'Change the title, content or excerpt of one post or page. Needs content edits switched on and the modified_gmt from read-post; status, author and date stay as they are, and WordPress keeps the old text as a revision.', self::schema( array( 'id' => $id, 'expected_modified_gmt' => array( 'type' => 'string', 'maxLength' => 19 ), 'title' => array( 'type' => 'string', 'maxLength' => 1000 ), 'content' => array( 'type' => 'string', 'maxLength' => AdMindra_MCP_Content::LIMIT ), 'excerpt' => array( 'type' => 'string', 'maxLength' => 5000 ), ), array( 'id', 'expected_modified_gmt' ) ), array( 'AdMindra_MCP_Content', 'update' ), true ); self::reg( 'update-site-identity', 'Edit site title and tagline', 'Change the site title or tagline (Settings → General). Needs content edits switched on.', self::schema( array( 'title' => array( 'type' => 'string', 'maxLength' => 200 ), 'tagline' => array( 'type' => 'string', 'maxLength' => 200 ), ) ), array( 'AdMindra_MCP_Content', 'identity' ), true ); self::reg( 'read-file', 'Read source file', 'Read one PHP, JS, CSS, JSON, HTML, TXT or Twig file of an installed plugin or theme (max 128 KiB).', self::schema( $target, array( 'kind', 'slug', 'path' ) ), array( __CLASS__, 'read_file' ) ); } /** * Ability: site status. * * @return array */ public static function status() { $status = array( 'wordpress' => get_bloginfo( 'version' ), 'php' => PHP_VERSION, 'site' => home_url(), 'plugin_version' => ADMINDRA_MCP_VERSION, 'content_edits' => AdMindra_MCP_Content::enabled(), 'adapter_loaded' => self::adapter_loaded(), ); /* The add-on reports itself here: whether it is connected to AdMindra and switched on. */ return (array) apply_filters( 'admindra_mcp_status', $status ); } /** * Ability: plugin inventory. * * @return array */ public static function plugins() { require_once ABSPATH . 'wp-admin/includes/plugin.php'; $out = array(); foreach ( get_plugins() as $file => $data ) { $out[] = array( 'file' => $file, 'name' => $data['Name'], 'version' => $data['Version'], 'active' => is_plugin_active( $file ), ); } return $out; } /** * Ability: theme inventory. * * @return array */ public static function themes() { return array_map( static function ( $theme ) { return array( 'slug' => $theme->get_stylesheet(), 'name' => $theme->get( 'Name' ), 'version' => $theme->get( 'Version' ), ); }, array_values( wp_get_themes() ) ); } /** * Ability: updates on offer. * * What WordPress's own update check last found, read from its cache. The * plugin never asks WordPress.org itself: WordPress checks on its own * schedule, as it does without this plugin, and `checked_at` says when. * * @return array */ public static function updates() { $plugins = get_site_transient( 'update_plugins' ); $themes = get_site_transient( 'update_themes' ); $when = static function ( $cache ) { return is_object( $cache ) && ! empty( $cache->last_checked ) ? gmdate( 'c', (int) $cache->last_checked ) : null; }; /* What WordPress.org said about each offer, as stored; never the download link. Whether it fits this site is WordPress's own test. */ $offer = static function ( $o, $name, $current ) { $o = (array) $o; $requires = (string) ( $o['requires'] ?? '' ); $php = (string) ( $o['requires_php'] ?? '' ); return array( 'name' => $name, 'current_version' => $current, 'target_version' => (string) ( $o['new_version'] ?? '' ), 'tested_up_to' => (string) ( $o['tested'] ?? '' ), 'requires_wp' => $requires, 'requires_php' => $php, 'compatible' => is_wp_version_compatible( $requires ) && is_php_version_compatible( $php ), 'details_url' => esc_url_raw( (string) ( $o['url'] ?? '' ) ), ); }; if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $installed = get_plugins(); $out = array( 'plugins' => array(), 'themes' => array(), 'checked_at' => array( 'plugins' => $when( $plugins ), 'themes' => $when( $themes ), ), /* This plugin cannot run updates; an administrator does, on WordPress's own screen. */ 'update_screen' => is_multisite() ? network_admin_url( 'update-core.php' ) : admin_url( 'update-core.php' ), 'how' => 'This plugin cannot run updates. An administrator runs them on WordPress\'s Updates screen (update_screen).', ); foreach ( (array) ( $plugins->response ?? array() ) as $file => $o ) { $out['plugins'][ $file ] = $offer( $o, (string) ( $installed[ $file ]['Name'] ?? $file ), (string) ( $installed[ $file ]['Version'] ?? '' ) ); } foreach ( (array) ( $themes->response ?? array() ) as $slug => $o ) { $theme = wp_get_theme( $slug ); $out['themes'][ $slug ] = $offer( $o, (string) $theme->get( 'Name' ), (string) $theme->get( 'Version' ) ); } if ( function_exists( 'wp_get_ability' ) && wp_get_ability( 'admindra-mcp/update-plugin' ) ) { $out['how'] .= ' The add-on AdMindra MCP Maintenance is connected here, so update-plugin and update-theme can run them too.'; } else { $out['addon'] = array( 'name' => 'AdMindra MCP Maintenance', 'what' => 'An add-on that lets the assistant run these updates itself, with a snapshot first and the old version put back if the site stops answering. Free with an AdMindra account.', 'url' => self::URL_ADDON, ); } return $out; } /** * Ability: registration page audit. * * Words, not a site's own name: what a page is called differs per site, * so the client passes `search` when the default words do not find it. * * @param array|null $a Input. * @return array */ public static function pages( $a = null ) { $search = is_array( $a ) && isset( $a['search'] ) && is_string( $a['search'] ) ? trim( $a['search'] ) : ''; $pattern = '' !== $search ? '/' . preg_quote( $search, '/' ) . '/iu' : '/registr|register|sign[ -]?up|anmäl/iu'; $posts = get_posts( array( 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => 100, ) ); $out = array(); foreach ( $posts as $post ) { if ( ! preg_match( $pattern, $post->post_title . ' ' . $post->post_content ) ) { continue; } preg_match_all( '/\[(?!\[)([a-zA-Z][a-zA-Z0-9_-]*)\b/', $post->post_content, $candidates ); $shortcodes = array(); $names = array_unique( $candidates[1] ?? array() ); if ( $names ) { preg_match_all( '/' . get_shortcode_regex( $names ) . '/s', $post->post_content, $matches ); foreach ( array_values( array_unique( $matches[2] ?? array() ) ) as $tag ) { $shortcodes[] = array( 'tag' => $tag, 'registered' => shortcode_exists( $tag ), ); } } $out[] = array( 'id' => $post->ID, 'title' => $post->post_title, 'url' => get_permalink( $post ), 'shortcodes' => $shortcodes, 'template' => get_page_template_slug( $post->ID ), ); } return array( 'pages' => $out, 'search' => '' !== $search ? $search : null, 'scan_limit' => 100, 'scanned' => count( $posts ), 'registration_enabled' => (bool) get_option( 'users_can_register' ), 'permalink_structure' => get_option( 'permalink_structure' ), ); } /** * Ability: read a file. * * @param array $a Input. * @return array|WP_Error */ public static function read_file( $a ) { return AdMindra_MCP_File_Guard::read( $a ); } }