namespace, '/overview', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'overview' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); register_rest_route( $this->namespace, '/dashboard', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'dashboard' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); register_rest_route( $this->namespace, '/readiness', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'readiness' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'campaign_id' => array( 'type' => 'integer', 'default' => 0, 'minimum' => 0, 'sanitize_callback' => 'absint', ), ), ) ); register_rest_route( $this->namespace, '/module-settings', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'get_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'save_settings' ), 'permission_callback' => array( $this, 'check_permission' ), ), ) ); register_rest_route( $this->namespace, '/mcp-status', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'mcp_status' ), 'permission_callback' => array( $this, 'check_permission' ), ) ); register_rest_route( $this->namespace, '/onboarding', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'onboarding' ), 'permission_callback' => array( $this, 'check_permission' ), ), array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'onboarding_event' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'event' => array( 'type' => 'string', 'required' => true, 'enum' => array( 'start', 'step', 'done', 'skip' ), ), 'step' => array( 'type' => 'string', 'default' => '', ), ), ), ) ); } /** * @return WP_REST_Response */ public function onboarding() { return rest_ensure_response( Mailpuff_Onboarding::get() ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function onboarding_event( $request ) { $state = Mailpuff_Onboarding::record( (string) $request->get_param( 'event' ), sanitize_key( (string) $request->get_param( 'step' ) ) ); return is_wp_error( $state ) ? $state : rest_ensure_response( $state ); } /** * @return WP_REST_Response */ public function overview() { return rest_ensure_response( array( 'setup' => Mailpuff_Admin_Data::setup(), 'notices' => Mailpuff_Admin_Data::notices(), 'status' => Mailpuff_Admin_Data::status(), 'agent' => array( 'used' => Mailpuff_Abilities::used() ), ) ); } /** * @return WP_REST_Response */ public function dashboard() { return rest_ensure_response( Mailpuff_Dashboard::summary() ); } /** * The safe-sending checklist (Mailpuff_Readiness), for the site or one * campaign: the same list AI apps get from check-send-readiness. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function readiness( $request ) { $campaign = null; $id = (int) $request->get_param( 'campaign_id' ); if ( $id ) { $campaign = Mailpuff_Campaign::find( $id ); if ( ! $campaign ) { return new WP_Error( 'mailpuff_not_found', __( 'No campaign has that id.', 'mailpuff' ), array( 'status' => 404 ) ); } } return rest_ensure_response( Mailpuff_Readiness::check( $campaign ) ); } /** * @return WP_REST_Response */ public function get_settings() { return rest_ensure_response( array( 'fields' => Mailpuff_Admin_Data::fields(), 'values' => Mailpuff_Admin_Data::read(), ) ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function save_settings( $request ) { $values = $request->get_param( 'values' ); if ( ! is_array( $values ) ) { return new WP_Error( 'mailpuff_invalid_settings', __( 'Send the settings to save as values.', 'mailpuff' ), array( 'status' => 400 ) ); } $errors = Mailpuff_Admin_Data::validate( $values ); if ( ! empty( $errors ) ) { return new WP_Error( 'mailpuff_invalid_settings', __( 'Some settings need a change.', 'mailpuff' ), array( 'status' => 400, 'fields' => $errors, ) ); } Mailpuff_Admin_Data::write( $values ); return rest_ensure_response( array( 'values' => Mailpuff_Admin_Data::read() ) ); } /** * Settings, MCP & API: the server that offers Mailpuff's tools to AI * apps, its address and how many tools it offers. * * @return WP_REST_Response */ public function mcp_status() { return rest_ensure_response( self::mcp_server( class_exists( 'Saddle_Integrations' ), rest_get_server()->get_routes() ) ); } /** * Which MCP server offers Mailpuff's tools. With Saddle active it is * Saddle's endpoint (its roles and preview step apply), and the tools are * not on the MCP Adapter's default server (Mailpuff_Abilities::mcp_public()). * Without Saddle it is that default server, while its route is up, and * only the tools marked `mcp.public` count. Nothing here is a secret. * * @param bool $saddle Saddle is active. * @param array $routes The REST routes, keyed by path. * @return array{server:string,available:bool,url:string,tools:int,connect_url:string} * `server` is 'saddle', 'mcp-adapter' or '' (none); * `available` says its route is up; `connect_url` is * Saddle's MCP page, where apps are connected. */ public static function mcp_server( $saddle, array $routes ) { $server = ''; $route = ''; if ( $saddle ) { $server = 'saddle'; $route = class_exists( 'Saddle_MCP' ) ? '/' . Saddle_MCP::REST_NAMESPACE . Saddle_MCP::ROUTE : self::SADDLE_MCP_ROUTE; } elseif ( isset( $routes[ self::MCP_ADAPTER_ROUTE ] ) ) { $server = 'mcp-adapter'; $route = self::MCP_ADAPTER_ROUTE; } $available = '' !== $route && isset( $routes[ $route ] ); return array( 'server' => $server, 'available' => $available, 'url' => $available ? rest_url( ltrim( $route, '/' ) ) : '', 'tools' => self::mcp_tools( ! $saddle ), 'connect_url' => $saddle ? admin_url( 'admin.php?page=saddle-connections' ) : '', ); } /** * How many mailpuff/* abilities are registered: every one (Saddle wraps * them all), or only those the MCP Adapter's default server offers. * * @param bool $public_only Count only tools marked `mcp.public`. * @return int */ private static function mcp_tools( $public_only ) { if ( ! function_exists( 'wp_get_abilities' ) ) { return 0; } $count = 0; foreach ( wp_get_abilities() as $ability ) { if ( 0 !== strpos( $ability->get_name(), Mailpuff_Abilities::CATEGORY . '/' ) ) { continue; } $meta = $ability->get_meta(); if ( $public_only && empty( $meta['mcp']['public'] ) ) { continue; } ++$count; } return $count; } }