nt' => true, ], ], ] ); } /** * Checks if the current user has permission to execute this ability. * * @return bool */ public function check_permissions(): bool { return current_user_can( 'rocket_manage_options' ); } /** * Executes the ability to get Rocket Insights recommendations. * * @param mixed $input Unused input parameter. * * @return array */ public function execute( $input = null ): array { $this->track_event( 'MCP Ability Executed', [ 'ability' => 'wp-rocket/get-recommendations', 'context' => 'wp_plugin_mcp', ] ); $data = $this->data_manager->get_recommendations(); if ( false === $data ) { return [ 'status' => 'expired', 'recommendations' => [], ]; } // Status whitelist guard — coerce any unrecognised or transitional status // to a value within the closed output_schema enum. // 'pending' maps to 'loading' (matches Recommendations\Render::map_status_to_state()). // Anything outside the known set falls back to 'failed'. $known_statuses = [ 'loading', 'completed', 'failed' ]; if ( 'pending' === $data['status'] ) { $status = 'loading'; } elseif ( in_array( $data['status'], $known_statuses, true ) ) { $status = $data['status']; } else { $status = 'failed'; } $formatted = []; foreach ( $data['recommendations'] ?? [] as $recommendation ) { $slug = $recommendation['option_slug'] ?? ''; $actionable = in_array( $slug, self::MCP_ACTIONABLE_OPTION_SLUGS, true ); $formatted[] = [ 'option_slug' => $slug, 'title' => $recommendation['title'] ?? '', 'description' => $recommendation['description'] ?? '', 'mcp_actionable' => $actionable, 'mcp_ability' => $actionable ? 'wp-rocket/set-option' : null, ]; } return [ 'status' => $status, 'recommendations' => $formatted, ]; } }