Last active
March 21, 2026 08:59
-
-
Save westonruter/dbc4c97ccbbb64bf64d5c6a51c205b95 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| /** | |
| * Cache WP AI Generate Responses Plugin for WordPress | |
| * | |
| * @package CacheWpAiGenerateResponses | |
| * @author Weston Ruter | |
| * @license GPL-2.0-or-later | |
| * @copyright Copyleft 2026, Weston Ruter | |
| * | |
| * @wordpress-plugin | |
| * Plugin Name: Cache WP AI Generate Responses | |
| * Plugin URI: https://gist.github.com/westonruter/dbc4c97ccbbb64bf64d5c6a51c205b95 | |
| * Description: Caches responses from the wp-ai/v1/generate REST API endpoint using transients, keyed by MD5 hash of the request body. | |
| * Requires at least: 6.0 | |
| * Requires PHP: 7.4 | |
| * Version: 0.1.1 | |
| * Author: Weston Ruter | |
| * Author URI: https://weston.ruter.net/ | |
| * License: GPLv2 or later | |
| * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
| * Update URI: https://gist.github.com/westonruter/dbc4c97ccbbb64bf64d5c6a51c205b95 | |
| * Gist Plugin URI: https://gist.github.com/westonruter/dbc4c97ccbbb64bf64d5c6a51c205b95 | |
| * Primary Branch: main | |
| */ | |
| namespace CacheWpAiGenerateResponses; | |
| use WP_REST_Request; | |
| use WP_REST_Response; | |
| use WP_REST_Server; | |
| const CACHE_KEY_PARAM = '_ai_cache_key'; | |
| /** | |
| * Short-circuits the wp-ai/v1/generate endpoint with a cached response if available. | |
| * | |
| * @param mixed $result Response to replace the requested version with. | |
| * @param WP_REST_Server $server Server instance. | |
| * @param WP_REST_Request $request Current request. | |
| * @return mixed|WP_REST_Response | |
| */ | |
| function filter_rest_pre_dispatch( $result, WP_REST_Server $server, WP_REST_Request $request ) { | |
| if ( null !== $result ) { | |
| return $result; | |
| } | |
| if ( $request->get_route() !== '/wp-ai/v1/generate' ) { | |
| return $result; | |
| } | |
| $body = $request->get_body(); | |
| $cache_key = 'wp_ai_gen_' . md5( $body ); | |
| $cached = get_transient( $cache_key ); | |
| if ( false !== $cached ) { | |
| return rest_ensure_response( $cached ); | |
| } | |
| // Store the cache key so we can save the response after dispatch. | |
| $request->set_param( CACHE_KEY_PARAM, $cache_key ); | |
| return $result; | |
| } | |
| add_filter( 'rest_pre_dispatch', __NAMESPACE__ . '\filter_rest_pre_dispatch', 10, 3 ); | |
| /** | |
| * Caches a successful wp-ai/v1/generate response in a transient. | |
| * | |
| * @param WP_REST_Response $response Result to send to the client. | |
| * @param WP_REST_Server $server Server instance. | |
| * @param WP_REST_Request $request Request used to generate the response. | |
| * @return WP_REST_Response | |
| */ | |
| function filter_rest_post_dispatch( WP_REST_Response $response, WP_REST_Server $server, WP_REST_Request $request ): WP_REST_Response { | |
| $cache_key = $request->get_param( CACHE_KEY_PARAM ); | |
| if ( null === $cache_key ) { | |
| return $response; | |
| } | |
| if ( $response->get_status() >= 200 && $response->get_status() < 300 ) { | |
| set_transient( $cache_key, $response->get_data(), WEEK_IN_SECONDS ); | |
| } | |
| return $response; | |
| } | |
| add_filter( 'rest_post_dispatch', __NAMESPACE__ . '\filter_rest_post_dispatch', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment