PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.1
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.1
3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 2.8.17 2.8.18 All 259 releases
wpfunnels / includes / core / rest-api / Controllers / SingleShotAiController.php

SingleShotAiController.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.1, at includes/core/rest-api/Controllers/SingleShotAiController.php

105 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SingleShotAiController — REST API controller for single-shot AI tasks (POST /wpfunnels/v1/ai/generate).
4 *
5 * @package WPFunnels\Rest\Controllers
6 * @since 3.13.0
7 */
8
9 namespace WPFunnels\Rest\Controllers;
10
11 defined( 'ABSPATH' ) || exit;
12
13 use WP_Error;
14 use WP_REST_Request;
15 use WP_REST_Response;
16 use WP_REST_Server;
17 use WPFunnels\AI\AIInit;
18 use WPFunnels\AI\Settings\AISettings;
19
20 require_once dirname( __DIR__, 2 ) . '/AI/SingleShotAIService.php';
21
22 /**
23 * Class SingleShotAiController
24 */
25 class SingleShotAiController extends Wpfnl_REST_Controller {
26
27 /**
28 * Endpoint namespace.
29 *
30 * @var string
31 */
32 protected $namespace = 'wpfunnels/v1';
33
34 /**
35 * Route base.
36 *
37 * @var string
38 */
39 protected $rest_base = 'ai';
40
41 /**
42 * Check permissions.
43 *
44 * @param WP_REST_Request $request Request.
45 * @return bool|WP_Error
46 */
47 public function check_permission( $request ) {
48 if ( ! is_user_logged_in() ) {
49 return new WP_Error(
50 'wpfunnels_rest_cannot_access',
51 __( 'You must be logged in to access AI generation.', 'wpfnl' ),
52 [ 'status' => rest_authorization_required_code() ]
53 );
54 }
55
56 if ( ! current_user_can( 'wpf_manage_funnels' ) && ! current_user_can( 'manage_options' ) ) {
57 return new WP_Error(
58 'wpfunnels_rest_cannot_access',
59 __( 'Sorry, you do not have permission to access AI generation.', 'wpfnl' ),
60 [ 'status' => rest_authorization_required_code() ]
61 );
62 }
63
64 return true;
65 }
66
67 /**
68 * Register REST routes.
69 *
70 * @return void
71 */
72 public function register_routes() {
73 register_rest_route(
74 $this->namespace,
75 '/' . $this->rest_base . '/generate',
76 [
77 [
78 'methods' => WP_REST_Server::CREATABLE,
79 'callback' => [ $this, 'generate' ],
80 'permission_callback' => [ $this, 'check_permission' ],
81 ],
82 ]
83 );
84 }
85
86 /**
87 * Single-shot generation endpoint handler.
88 *
89 * @param WP_REST_Request $request Request.
90 * @return WP_REST_Response|WP_Error
91 */
92 public function generate( $request ) {
93 $task = sanitize_text_field( (string) $request->get_param( 'task' ) );
94 $prompt = sanitize_textarea_field( (string) $request->get_param( 'prompt' ) );
95 $params = $request->get_param( 'params' ) ?: [];
96
97 $result = \WPFunnels\AI\SingleShotAIService::executeTask( $task, $prompt, $params );
98 if ( is_wp_error( $result ) ) {
99 return $result;
100 }
101
102 return rest_ensure_response( $result );
103 }
104 }
105