→ any whitelisted context file * POST /generate/keywords → run Keywords::generate() * POST /generate/post → run Generator::manual_run() * * @package AiPublisher\PostPublisher */ namespace AiPublisher; if ( ! defined( 'ABSPATH' ) ) { exit; } final class REST_Controller { /** List of context JSON files that can be downloaded verbatim */ private const WHITELIST = [ 'brand-strategy.json', 'keywords.json', 'draft-post.json', 'post-qa.json', 'final-post.json', 'image-prompt.txt', ]; /* -------------------------------------------------------------------- * Register routes * ------------------------------------------------------------------*/ public static function register(): void { register_rest_route( 'txp/v1', '/brand-strategy', [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_brand_strategy' ], 'permission_callback' => [ __CLASS__, 'can_manage' ], ] ); register_rest_route( 'txp/v1', '/keywords', [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_keywords' ], 'permission_callback' => [ __CLASS__, 'can_manage' ], ] ); register_rest_route( 'txp/v1', '/context/(?P[a-z0-9\-_\.]+)', [ 'methods' => 'GET', 'callback' => [ __CLASS__, 'get_context_file' ], 'permission_callback' => [ __CLASS__, 'can_manage' ], 'args' => [ 'file' => [ 'sanitize_callback' => 'sanitize_file_name', 'validate_callback' => function ( $value ) { return in_array( $value, self::WHITELIST, true ); }, ], ], ] ); register_rest_route( 'txp/v1', '/generate/keywords', [ 'methods' => 'POST', 'callback' => [ __CLASS__, 'post_generate_keywords' ], 'permission_callback' => [ __CLASS__, 'can_manage' ], ] ); register_rest_route( 'txp/v1', '/generate/post', [ 'methods' => 'POST', 'callback' => [ __CLASS__, 'post_generate_post' ], 'permission_callback' => [ __CLASS__, 'can_manage' ], ] ); } /* -------------------------------------------------------------------- * Permission helper * ------------------------------------------------------------------*/ public static function can_manage(): bool { return current_user_can( 'manage_options' ); } /* -------------------------------------------------------------------- * Read-only endpoints * ------------------------------------------------------------------*/ public static function get_brand_strategy(): \WP_REST_Response { return self::json_response( Context::load_json( 'brand-strategy.json' ) ); } public static function get_keywords(): \WP_REST_Response { return self::json_response( Context::load_json( 'keywords.json' ) ); } public static function get_context_file( \WP_REST_Request $req ): \WP_REST_Response { $file = $req->get_param( 'file' ); $path = Context::dir() . $file; if ( ! in_array( $file, self::WHITELIST, true ) || ! file_exists( $path ) ) { return new \WP_REST_Response( [ 'error' => 'File not found or not allowed.' ], 404 ); } $data = pathinfo( $file, PATHINFO_EXTENSION ) === 'json' ? json_decode( file_get_contents( $path ), true ) : file_get_contents( $path ); return self::json_response( $data ); } /* -------------------------------------------------------------------- * Write / trigger endpoints * ------------------------------------------------------------------*/ public static function post_generate_keywords(): \WP_REST_Response { try { Keywords::generate(); return self::json_response( [ 'status' => 'success', 'keywords' => get_option( 'aipub_keywords', [] ), ] ); } catch ( \Throwable $e ) { return new \WP_REST_Response( [ 'error' => $e->getMessage() ], 500 ); } } public static function post_generate_post(): \WP_REST_Response { try { $post_id = Generator::manual_run(); return self::json_response( [ 'status' => 'success', 'post_id' => $post_id, 'url' => admin_url( 'post.php?post=' . $post_id . '&action=edit' ), ] ); } catch ( \Throwable $e ) { return new \WP_REST_Response( [ 'error' => $e->getMessage() ], 500 ); } } /* -------------------------------------------------------------------- * Response helper * ------------------------------------------------------------------*/ private static function json_response( $data, int $code = 200 ): \WP_REST_Response { $resp = new \WP_REST_Response( $data, $code ); $resp->set_headers( [ 'Cache-Control' => 'no-cache' ] ); return $resp; } } /* Register at REST bootstrap */ add_action( 'rest_api_init', [ \AiPublisher\REST_Controller::class, 'register' ] );