home_url( '/' ), 'version' => FILLNOVA_VERSION, 'locale' => get_locale(), 'license' => (string) apply_filters( 'fillnova_ai_cloud_license', '' ), 'types' => Fillnova_AI::allowed_types(), ), $extra ); } /** * POST to the service. * * @param string $path Path. * @param array $payload Body. * @param int $timeout Timeout. * @return array|WP_Error Decoded body. */ protected static function call( $path, $payload, $timeout = 90 ) { $response = wp_remote_post( self::url() . $path, array( 'timeout' => $timeout, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => wp_json_encode( $payload ), ) ); if ( is_wp_error( $response ) ) { return new WP_Error( 'fillnova_cloud_unreachable', __( 'Fillnova AI could not be reached, so the built-in generator drafted this form. Try again in a moment for a full AI result.', 'fillnova' ) ); } $code = (int) wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $body ) ) { return new WP_Error( 'fillnova_cloud_unreachable', __( 'Fillnova AI could not be reached, so the built-in generator drafted this form. Try again in a moment for a full AI result.', 'fillnova' ) ); } if ( isset( $body['usage'] ) ) { self::remember( $body['usage'] ); } // Service missing or not set up yet (e.g. "rest_no_route"), or any server error: never show raw errors. $service_codes = array( 'fillnova_ai_quota', 'fillnova_ai_slow_down', 'fillnova_ai_prompt', 'fillnova_ai_pro', 'fillnova_ai_site', 'fillnova_cloud_unreachable' ); if ( 404 === $code || ( isset( $body['code'] ) && ! in_array( $body['code'], $service_codes, true ) ) ) { return new WP_Error( 'fillnova_cloud_unreachable', __( 'Fillnova AI is not available right now, so the built-in generator drafted this form. Try again later for a full AI result.', 'fillnova' ) ); } if ( $code >= 500 ) { return new WP_Error( 'fillnova_cloud_unreachable', isset( $body['message'] ) && 'fillnova_cloud_unreachable' === ( isset( $body['code'] ) ? $body['code'] : '' ) ? sanitize_text_field( $body['message'] ) : __( 'Fillnova AI is busy right now, so the built-in generator drafted this form.', 'fillnova' ) ); } if ( 200 !== $code ) { $err_code = isset( $body['code'] ) ? sanitize_key( $body['code'] ) : 'fillnova_cloud_error'; $message = isset( $body['message'] ) ? sanitize_text_field( $body['message'] ) : __( 'Fillnova AI could not create the form.', 'fillnova' ); return new WP_Error( $err_code, $message, array( 'status' => 400 ) ); } return $body; } /** * Generate a form. * * @param string $prompt Description. * @param array $options mode, language. * @return array|WP_Error */ public static function generate( $prompt, $options = array() ) { $body = self::call( '/generate', self::payload( array( 'prompt' => $prompt, 'mode' => isset( $options['mode'] ) ? sanitize_key( $options['mode'] ) : 'auto', 'language' => isset( $options['language'] ) ? sanitize_text_field( $options['language'] ) : '', ) ) ); return self::to_form( $body ); } /** * Change an existing form (Pro). * * @param string $instruction What to change. * @param array $form Compact current form { title, settings, fields }. * @param string $mode Mode. * @return array|WP_Error */ public static function refine( $instruction, $form, $mode = 'auto' ) { $body = self::call( '/refine', self::payload( array( 'prompt' => $instruction, 'form' => $form, 'mode' => sanitize_key( $mode ), ) ) ); return self::to_form( $body ); } /** * Service response → clean form. * * @param array|WP_Error $body Body. * @return array|WP_Error */ protected static function to_form( $body ) { if ( is_wp_error( $body ) ) { return $body; } if ( empty( $body['form'] ) || ! is_array( $body['form'] ) ) { return new WP_Error( 'fillnova_cloud_error', __( 'Fillnova AI returned an empty form. Please try again with a little more detail.', 'fillnova' ) ); } // Re-validate locally: never trust a remote schema. $parsed = Fillnova_AI::parse( wp_json_encode( $body['form'] ) ); if ( is_wp_error( $parsed ) ) { return $parsed; } $parsed['source'] = 'ai'; $parsed['remaining'] = self::remaining(); $parsed['usage'] = self::usage(); return $parsed; } /** * Store usage numbers from the service. * * @param array $u Usage. */ protected static function remember( $u ) { $u = (array) $u; update_option( self::OPTION, array( 'used' => isset( $u['used'] ) ? absint( $u['used'] ) : 0, 'limit' => isset( $u['limit'] ) ? (int) $u['limit'] : 0, 'remaining' => isset( $u['remaining'] ) ? (int) $u['remaining'] : 0, 'plan' => isset( $u['plan'] ) && 'pro' === $u['plan'] ? 'pro' : 'free', 'resets' => isset( $u['resets'] ) ? sanitize_text_field( $u['resets'] ) : '', 'month' => gmdate( 'Y-m' ), 'checked' => time(), ), false ); } /** * Usage numbers (cached; refreshed from the service when asked). * * @param bool $refresh Ask the service. * @return array { used, limit, remaining (-1 = unlimited), plan, resets, month, checked } or empty. */ public static function usage( $refresh = false ) { $u = get_option( self::OPTION, array() ); if ( $u && ! empty( $u['resets'] ) && isset( $u['month'] ) && gmdate( 'Y-m' ) !== $u['month'] ) { $u = array(); // Monthly allowance: new month, new quota. } if ( $refresh && self::enabled() ) { $body = self::call( '/usage', self::payload(), 15 ); if ( ! is_wp_error( $body ) ) { $u = get_option( self::OPTION, array() ); } } return is_array( $u ) ? $u : array(); } /** * Remaining generations: -1 unlimited, null unknown. * * @return int|null */ public static function remaining() { $u = self::usage(); return isset( $u['remaining'] ) ? (int) $u['remaining'] : null; } }