business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
3 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
3 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
3 years ago
class-wpcom-rest-api-v2-endpoint-following.php
3 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
3 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
3 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
3 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
3 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-gather.php
3 years ago
class-wpcom-rest-api-v2-endpoint-tweetstorm-parse.php
3 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
3 years ago
gutenberg-available-extensions.php
4 years ago
hello.php
4 years ago
memberships.php
3 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
3 years ago
publicize-services.php
3 years ago
service-api-keys.php
3 years ago
sites-posts-featured-media-url.php
4 years ago
subscribers.php
3 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
150 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API endpoint for the Jetpack AI blocks. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 11.8 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Class WPCOM_REST_API_V2_Endpoint_AI |
| 11 | */ |
| 12 | class WPCOM_REST_API_V2_Endpoint_AI extends WP_REST_Controller { |
| 13 | /** |
| 14 | * Namespace prefix. |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | public $namespace = 'wpcom/v2'; |
| 19 | |
| 20 | /** |
| 21 | * Endpoint base route. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public $rest_base = 'jetpack-ai'; |
| 26 | |
| 27 | /** |
| 28 | * WPCOM_REST_API_V2_Endpoint_AI constructor. |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | $this->is_wpcom = true; |
| 32 | $this->wpcom_is_wpcom_only_endpoint = true; |
| 33 | |
| 34 | if ( ! class_exists( 'Jetpack_AI_Helper' ) ) { |
| 35 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php'; |
| 36 | } |
| 37 | |
| 38 | // Register routes that don't require Jetpack AI to be enabled. |
| 39 | add_action( 'rest_api_init', array( $this, 'register_basic_routes' ) ); |
| 40 | |
| 41 | if ( ! \Jetpack_AI_Helper::is_enabled() ) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // Register routes that require Jetpack AI to be enabled. |
| 46 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Register routes. |
| 51 | */ |
| 52 | public function register_routes() { |
| 53 | register_rest_route( |
| 54 | $this->namespace, |
| 55 | $this->rest_base . '/completions', |
| 56 | array( |
| 57 | array( |
| 58 | 'methods' => WP_REST_Server::CREATABLE, |
| 59 | 'callback' => array( $this, 'request_gpt_completion' ), |
| 60 | 'permission_callback' => array( 'Jetpack_AI_Helper', 'get_status_permission_check' ), |
| 61 | ), |
| 62 | 'args' => array( |
| 63 | 'content' => array( |
| 64 | 'type' => 'string', |
| 65 | 'required' => true, |
| 66 | 'sanitize_callback' => 'sanitize_textarea_field', |
| 67 | ), |
| 68 | 'post_id' => array( |
| 69 | 'required' => false, |
| 70 | 'type' => 'integer', |
| 71 | ), |
| 72 | 'skip_cache' => array( |
| 73 | 'required' => false, |
| 74 | 'type' => 'boolean', |
| 75 | 'description' => 'Whether to skip the cache and make a new request', |
| 76 | ), |
| 77 | ), |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | register_rest_route( |
| 82 | $this->namespace, |
| 83 | $this->rest_base . '/images/generations', |
| 84 | array( |
| 85 | array( |
| 86 | 'methods' => WP_REST_Server::CREATABLE, |
| 87 | 'callback' => array( $this, 'request_dalle_generation' ), |
| 88 | 'permission_callback' => array( 'Jetpack_AI_Helper', 'get_status_permission_check' ), |
| 89 | ), |
| 90 | 'args' => array( |
| 91 | 'prompt' => array( |
| 92 | 'type' => 'string', |
| 93 | 'required' => true, |
| 94 | 'sanitize_callback' => 'sanitize_textarea_field', |
| 95 | ), |
| 96 | 'post_id' => array( |
| 97 | 'required' => false, |
| 98 | 'type' => 'integer', |
| 99 | ), |
| 100 | ), |
| 101 | ) |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Register routes that don't require Jetpack AI to be enabled. |
| 107 | */ |
| 108 | public function register_basic_routes() { |
| 109 | register_rest_route( |
| 110 | $this->namespace, |
| 111 | $this->rest_base . '/ai-assistant-feature', |
| 112 | array( |
| 113 | array( |
| 114 | 'methods' => WP_REST_Server::READABLE, |
| 115 | 'callback' => array( $this, 'request_get_ai_assistance_feature' ), |
| 116 | 'permission_callback' => array( 'Jetpack_AI_Helper', 'get_status_permission_check' ), |
| 117 | ), |
| 118 | ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get completions for a given text. |
| 124 | * |
| 125 | * @param WP_REST_Request $request The request. |
| 126 | */ |
| 127 | public function request_gpt_completion( $request ) { |
| 128 | return Jetpack_AI_Helper::get_gpt_completion( $request['content'], $request['post_id'], $request['skip_cache'] ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get image generations for a given prompt. |
| 133 | * |
| 134 | * @param WP_REST_Request $request The request. |
| 135 | */ |
| 136 | public function request_dalle_generation( $request ) { |
| 137 | return Jetpack_AI_Helper::get_dalle_generation( $request['prompt'], $request['post_id'] ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Collect and provide relevat data about the AI feature, |
| 142 | * such as the number of requests made. |
| 143 | */ |
| 144 | public function request_get_ai_assistance_feature() { |
| 145 | return Jetpack_AI_Helper::get_ai_assistance_feature(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_AI' ); |
| 150 |