generation = $generation; $this->settings = $settings; $this->features = $features; } public function register(): void { add_action( 'rest_api_init', array( $this, 'register_routes' ) ); } public function register_routes(): void { register_rest_route( self::NAMESPACE, '/generate', array( 'methods' => 'POST', 'callback' => array( $this, 'handle_generate' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'field' => array( 'type' => 'string', 'required' => true, ), 'post_id' => array( 'type' => 'integer', 'required' => false, ), 'attachment_id' => array( 'type' => 'integer', 'required' => false, ), 'confirm_over_limit' => array( 'type' => 'boolean', 'required' => false, ), 'mode' => array( 'type' => 'string', 'required' => false, ), 'brief' => array( 'type' => 'string', 'required' => false, ), 'length' => array( 'type' => 'string', 'required' => false, ), ), ) ); register_rest_route( self::NAMESPACE, '/test-provider', array( 'methods' => 'POST', 'callback' => array( $this, 'handle_test_provider' ), 'permission_callback' => array( $this, 'check_manage_options' ), 'args' => array( 'provider' => array( 'type' => 'string', 'required' => true, ), 'api_key' => array( 'type' => 'string', 'required' => false, ), 'model' => array( 'type' => 'string', 'required' => false, ), ), ) ); } public function check_manage_options(): bool { return current_user_can( \AcezSeo\Support\Capabilities::manage() ); } public function handle_test_provider( \WP_REST_Request $request ): \WP_REST_Response { $provider_id = sanitize_key( (string) $request->get_param( 'provider' ) ); $posted_key = trim( (string) $request->get_param( 'api_key' ) ); $posted_model = trim( (string) $request->get_param( 'model' ) ); $api_key = $posted_key; $model = $posted_model; // Blank field = "use whatever is already saved" (mirrors the Save form's // own convention, so testing works right after a page reload too). if ( '' === $api_key ) { $api_key = $this->settings->get_api_key( $provider_id ); } if ( '' === $model ) { $model = $this->settings->get_model( $provider_id ); } $temp_registry = new AiProviderRegistry( new AdHocAiSettings( $provider_id, $api_key, $model ) ); $provider = $temp_registry->get( $provider_id ); if ( null === $provider ) { return new \WP_REST_Response( array( 'message' => __( 'Unknown provider.', 'acez-seo' ) ), 404 ); } try { $provider->test_connection(); } catch ( AiProviderException $exception ) { return new \WP_REST_Response( array( 'message' => $exception->getMessage() ), 400 ); } // Both fields blank = the SAVED credentials were tested — mark them // verified right away (no save step will follow in this flow). if ( '' === $posted_key && '' === $posted_model ) { $this->settings->mark_tested( $provider_id, true ); } // Best-effort: a working connection is the primary success signal, // so a failure to list models (e.g. account lacks that permission) // must not turn an otherwise-successful test into an error. try { $models = $provider->list_models(); } catch ( AiProviderException $exception ) { $models = array(); } return new \WP_REST_Response( array( 'message' => __( 'The connection works correctly.', 'acez-seo' ), 'models' => $models, ), 200 ); } public function check_permission( \WP_REST_Request $request ): bool { $field = sanitize_key( (string) $request->get_param( 'field' ) ); if ( 'alt_text' === $field ) { $attachment_id = (int) $request->get_param( 'attachment_id' ); return $attachment_id > 0 && current_user_can( 'edit_post', $attachment_id ); } // Site-level fields describe the whole site, not a post, so there is no // `edit_post` target to check — they are settings, and take the same // gate as every other settings-level action (M13 decision). if ( in_array( $field, LlmsAiPrompt::FIELDS, true ) ) { return current_user_can( Capabilities::manage() ); } $post_id = (int) $request->get_param( 'post_id' ); return $post_id > 0 && current_user_can( 'edit_post', $post_id ); } public function handle_generate( \WP_REST_Request $request ): \WP_REST_Response { // Master switch (Dashboard → Funkcje → Obsługa AI) — gate inside the // callback, like every other feature flag. if ( null !== $this->features && ! $this->features->enabled( 'ai' ) ) { return new \WP_REST_Response( array( 'message' => __( 'AI support is disabled in Acez SEO AI → Dashboard → Features.', 'acez-seo' ) ), 403 ); } $field = sanitize_key( (string) $request->get_param( 'field' ) ); $confirmed = (bool) $request->get_param( 'confirm_over_limit' ); try { if ( 'alt_text' === $field ) { $text = $this->generation->generate_alt_text( (int) $request->get_param( 'attachment_id' ), $confirmed ); } elseif ( in_array( $field, LlmsAiPrompt::FIELDS, true ) ) { // Site-level: no post is involved, so this goes through the // generic filtered-field seam (LlmsAiPrompt supplies the // prompt and the site context) rather than the post pipeline. $text = $this->generation->generate_for_filtered_field( 'llms', $field, null, $confirmed ); } else { $post = get_post( (int) $request->get_param( 'post_id' ) ); if ( ! $post instanceof \WP_Post ) { return new \WP_REST_Response( array( 'message' => __( 'Post not found.', 'acez-seo' ) ), 404 ); } if ( in_array( $field, AiGenerationService::SCHEMA_NODE_FIELDS, true ) ) { $text = $this->generation->generate_schema_node( $field, $post, $confirmed ); } elseif ( null !== ( $claimed = $this->claimed_by_addon( $field, $post, $confirmed ) ) ) { $text = $claimed; } elseif ( 'article' === $field ) { $text = $this->generation->generate_article( $post, sanitize_key( (string) $request->get_param( 'mode' ) ), (string) $request->get_param( 'brief' ), sanitize_key( (string) $request->get_param( 'length' ) ), $confirmed ); } else { $text = $this->generation->generate_for_post_field( $field, $post, $confirmed ); } } } catch ( AiLimitException $exception ) { // Soft limit: the client asks the user to confirm and retries // with confirm_over_limit=true — never a hard block. return new \WP_REST_Response( array( 'message' => $exception->getMessage(), 'code' => 'acez_seo_over_limit', ), 409 ); } catch ( AiProviderException $exception ) { return new \WP_REST_Response( array( 'message' => $exception->getMessage() ), 400 ); } return new \WP_REST_Response( array( 'text' => $text ), 200 ); } /** * Lets an addon claim a field name this controller doesn't know. * * M69 Etap 5: `translate_title`/`translate_description` used to be a * branch right here, calling a base method gated on a licence — the * pattern WordPress.org's Guideline 5 forbids for a package shipped from * the directory. The route itself stays base infrastructure (auth, the * AI master switch, the soft-limit and provider-error responses below all * still apply); only the field's meaning moved out. * * It runs BEFORE generate_for_post_field(), which is the catch-all: a * claimed field must never fall through to the generic prompt. Listeners * return a string to claim the field, or null to pass; they may throw * AiProviderException/AiLimitException, which this method's caller * already turns into the right HTTP response. * * @throws AiProviderException */ private function claimed_by_addon( string $field, \WP_Post $post, bool $confirmed ): ?string { /** * Filters the AI result for a field the base plugin doesn't handle. * * @param string|null $text Null = not claimed; a string claims the field. * @param string $field Sanitized field key from the request. * @param \WP_Post $post The post being generated for. * @param bool $confirmed The user confirmed going over the soft monthly limit. */ $claimed = apply_filters( 'acez_seo_ai_generate_for_field', null, $field, $post, $confirmed ); return is_string( $claimed ) ? $claimed : null; } }