Last active
October 25, 2024 11:19
-
-
Save numpde/38470197f38febc328b8a8b31683c8c6 to your computer and use it in GitHub Desktop.
WP: Custom REST API endpoint to report using GPT actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: GPT Action Feedback | |
Description: Allows custom GPTs to report issues or concerns via a REST API endpoint, restricted to users with a specific capability. | |
Version: 1.0 | |
Author: RA | |
*/ | |
// Register the custom REST API endpoint only when the plugin is active. | |
add_action('rest_api_init', 'gpt_register_feedback_endpoint'); | |
/** | |
* Registers the custom REST API endpoint for reporting feedback. | |
*/ | |
function gpt_register_feedback_endpoint() { | |
register_rest_route('gpt/v1', '/action-feedback', [ | |
'methods' => 'POST', | |
'callback' => 'gpt_report_feedback_callback', | |
'permission_callback' => 'gpt_action_feedback_permission_callback', | |
]); | |
} | |
/** | |
* Callback function to handle feedback reporting. | |
* | |
* Expected Request Body (JSON): | |
* { | |
* "subject": "Brief description of the issue or feedback", | |
* "message": "Detailed description or message to the admin", | |
* "gpt_id": "Optional identifier for the GPT reporting the issue" | |
* } | |
* | |
* @param WP_REST_Request $request The request object containing the POST data. | |
* @return WP_REST_Response The response object indicating success or failure. | |
*/ | |
function gpt_report_feedback_callback(WP_REST_Request $request) { | |
$to = get_option('admin_email'); | |
$subject = sanitize_text_field($request->get_param('subject')); | |
$message = sanitize_textarea_field($request->get_param('message')); | |
$gpt_id = sanitize_text_field($request->get_param('gpt_id')); // Optional: Identify the GPT. | |
// Construct the email body. | |
$email_body = "Feedback reported by GPT: {$gpt_id}\n\n"; | |
$email_body .= "Message:\n{$message}"; | |
// Prepare headers for plain text email. | |
$headers = ['Content-Type: text/plain; charset=UTF-8']; | |
// Send the email. | |
$sent = wp_mail($to, $subject, $email_body, $headers); | |
// Return a response indicating the success or failure of the email. | |
if ($sent) { | |
return new WP_REST_Response('Feedback reported to admin successfully', 200); | |
} else { | |
return new WP_REST_Response('Failed to report the feedback', 500); | |
} | |
} | |
/** | |
* Custom permission callback to restrict access to the endpoint. | |
* Only users with the 'gpt_action_feedback' capability can access this endpoint. | |
*/ | |
function gpt_action_feedback_permission_callback() { | |
return current_user_can('gpt_action_feedback'); | |
} | |
/** | |
* Add the custom capability to the administrator role upon plugin activation. | |
*/ | |
function gpt_action_feedback_activate() { | |
$role = get_role('administrator'); | |
if ($role) { | |
$role->add_cap('gpt_action_feedback'); | |
} | |
} | |
register_activation_hook(__FILE__, 'gpt_action_feedback_activate'); | |
/** | |
* Remove the custom capability upon plugin deactivation for cleanup. | |
*/ | |
function gpt_action_feedback_deactivate() { | |
$role = get_role('administrator'); | |
if ($role) { | |
$role->remove_cap('gpt_action_feedback'); | |
} | |
} | |
register_deactivation_hook(__FILE__, 'gpt_action_feedback_deactivate'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
openapi: 3.1.0 | |
info: | |
title: GPT Action Feedback API | |
description: REST API for reporting issues or feedback from custom GPTs to the WordPress admin. | |
version: 1.0.0 | |
servers: | |
- url: https://your-site.com/wp-json/gpt/v1 | |
description: Production server | |
paths: | |
/action-feedback: | |
post: | |
summary: Report feedback or issues from GPTs | |
description: Accepts feedback from GPT instances and sends it to the site administrator via email. | |
operationId: reportFeedback | |
tags: | |
- Feedback | |
requestBody: | |
required: true | |
content: | |
application/json: | |
schema: | |
$ref: '#/components/schemas/FeedbackRequest' | |
responses: | |
'200': | |
description: Feedback reported successfully. | |
content: | |
application/json: | |
schema: | |
type: object | |
properties: | |
message: | |
type: string | |
example: "Feedback reported to admin successfully" | |
'400': | |
description: Invalid request. | |
content: | |
application/json: | |
schema: | |
type: object | |
properties: | |
message: | |
type: string | |
example: "Subject and message are required fields." | |
'500': | |
description: Server error. | |
content: | |
application/json: | |
schema: | |
type: object | |
properties: | |
message: | |
type: string | |
example: "Failed to report the feedback" | |
security: | |
- basicAuth: [] | |
components: | |
securitySchemes: | |
basicAuth: | |
type: http | |
scheme: basic | |
description: Use your WordPress username and application password for authentication. | |
schemas: | |
FeedbackRequest: | |
type: object | |
required: | |
- subject | |
- message | |
properties: | |
subject: | |
type: string | |
description: Brief description of the issue or feedback. | |
example: "Feedback from GPT" | |
message: | |
type: string | |
description: Detailed message to the admin. | |
example: "The GPT system encountered an unexpected error." | |
gpt_id: | |
type: string | |
description: Optional identifier for the GPT instance. | |
example: "GPT-1234" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment