JFIF $ d d 7 * Z qn )ZP 0 !1 A "#a$2Qr D8 a Ri [f \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H ~ , l s / 9 ć: [=/#f ; ) x { ٛE Q )~ = 𘙲r*2~ a _V = ' ku m F D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq 1dđ #̩ ͑ 0 ; H ] u 搂 @:~ ו K L N s h } O I R* 8 :2 ! lD JV o ( 3 = M ( z Ȱ+i *N A r6K n S l ) ! J J Ӂ* % ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c P A\IL Nj! J ۱+ O\ ͔ ] ޒ S J ȧ c%A N ol ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0 'G=i 2N +0 e 2 ]] 9 VT P O 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P < 0 x < !1AQ "Raq 02Br #S CTb ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV YT D I 6 2 '8Y2 7'\T P.6 d &˦ @V q i |8 -O Ε ] ʔ U= T L 8= ;6c | !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U#6w + +s &r+n ڐ { @ 29gL u " T Ù M=6 (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 ! OS r D 5 7 % | ?x> L | / nD 6?/8 w # [ ) L 7 +6〼T A T g ! %5 Mm Z /c -{1_Je "| ^$ 'O & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@ u6 BԒ i YN * f 0 _ GhKW u lT <nj- 0{ M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q $ H8o Q˼EV ~ k 0 h' L Pp M -M" m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @~p # !1QAa q ? AL0L x LU 4 d ; x " 16ad0 7.\ߋ 0 = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X &Q З non]я u ג./ ; gZ$ M QР 2M ]h ni 0 P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _ 1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3 6 #Ik+ v { v i ( ވ ? a e) X xy/*@Q Ǽ Ծ ) $ IWO l c G; !i k{ gOŀ L ('x T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ) X 5 c QɎMy W ? Q *!R > 6=7)Xj 5`J ] e 8%t ! + ' ! 1Q 5 !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $ v , ,m 㵜5u bV =sY+ @d { N ! dn O < .- B; _w J t 6;Q Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш e黅X ۢ F :)[ F G X ƹ / w _c MeD ,ʡc c . W D tA $ j@ : ) - #u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P 6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L ! ~ y5 g9T̅J kbM' +s :S +B) v @ M j e C f j E 0 Y\Qn z G 1 д~ W o{T 9?` Rm yhsy3 !HAD ] m c1 ~ 2 L S u7x T ;j$ `} 4-> L# vzŏ I L S ֭ T { r jGK C ; b pU= - ` Bs K. S F w4 M q] Z dH S 0 ) tLg
<?php
namespace WPForms\Integrations\AI\API;
use WPForms\Integrations\AI\Admin\Ajax\Forms as FormsAjax;
use WPForms\Integrations\AI\Helpers;
use WPForms\Forms\Fields\Pagebreak\Field as PagebreakField;
/**
* Form API class.
*
* @since 1.9.2
*/
class Forms extends API {
/**
* API endpoint.
*
* @since 1.9.2
*/
private const ENDPOINT = '/ai-forms';
/**
* Get form from the API.
*
* @since 1.9.2
*
* @param string $prompt Prompt to get the form.
* @param string $session_id Session ID.
*
* @return array
* @noinspection PhpUndefinedConstantInspection
*/
public function form( string $prompt, string $session_id = '' ): array {
$args = [
'userPrompt' => $this->prepare_prompt( $prompt ),
'limit' => $this->get_limit(),
];
if ( ! empty( $session_id ) ) {
$args['sessionId'] = $session_id;
}
// Flag requests from Lite plugin.
$args['lite'] = ! wpforms()->is_pro();
// Add available addons to the request arguments.
$args['addons'] = $this->get_available_addons();
// Add GDPR setting to the request arguments.
$args['gdpr'] = wpforms_setting( 'gdpr' );
// Add a Page break field support.
$args['pagebreak'] = true;
// Add prompt debug info support.
$args['debug'] = defined( 'WPFORMS_AI_DEBUG' ) && WPFORMS_AI_DEBUG;
// Add global settings (captcha, geolocation) to the request arguments.
$args['global_settings'] = $this->get_global_settings();
$response = $this->request->post( self::ENDPOINT, $args );
if ( $response->has_errors() ) {
$error_data = $response->get_error_data();
Helpers::log_error( $response->get_log_message( $error_data ), self::ENDPOINT, $args );
return $error_data;
}
return $this->normalize_form_data( $response->get_body() );
}
/**
* Get available addons.
*
* @since 1.9.2
*
* @return array
*/
private function get_available_addons(): array {
// Since starting from 1.9.4, we display unavailable addon fields in Lite and all Pro licenses,
// we need to return all required addons to let AI generate addon fields.
return FormsAjax::FORM_GENERATOR_REQUIRED_ADDONS;
}
/**
* Normalize form data.
*
* @since 1.9.2
*
* @param array $form_data Form data.
*
* @return array
*/
private function normalize_form_data( array $form_data ): array {
// Recursively normalize form data.
$form_data = $this->normalize_form_data_recursive( $form_data );
// Fix fields data.
$form_data = $this->fix_fields_data( $form_data );
// Notifications and confirmations arrays should be indexed from 1.
if ( ! empty( $form_data['settings']['notifications'] ) ) {
$form_data['settings']['notifications'] = array_combine(
range( 1, count( $form_data['settings']['notifications'] ) ),
array_values( $form_data['settings']['notifications'] )
);
}
if ( ! empty( $form_data['settings']['confirmations'] ) ) {
$form_data['settings']['confirmations'] = array_combine(
range( 1, count( $form_data['settings']['confirmations'] ) ),
array_values( $form_data['settings']['confirmations'] )
);
}
$form_data['form_title'] = empty( $form_data['form_title'] ) ? esc_html__( 'Untitled Form', 'wpforms-lite' ) : $form_data['form_title'];
$form_data['settings']['form_title'] = $form_data['form_title'];
return $form_data;
}
/**
* Normalize form data recursive.
*
* @since 1.9.2
*
* @param array $form_data Form data.
*
* @return array
*/
private function normalize_form_data_recursive( array $form_data ): array {
foreach ( $form_data as $key => $value ) {
if ( is_array( $value ) ) {
$form_data[ $key ] = $this->normalize_form_data_recursive( $value );
}
// Convert `false` and `true` values to '0' and '1'.
$form_data[ $key ] = $form_data[ $key ] === false ? '0' : $form_data[ $key ];
$form_data[ $key ] = $form_data[ $key ] === true ? '1' : $form_data[ $key ];
// Remove null values.
if ( $form_data[ $key ] === null ) {
unset( $form_data[ $key ] );
}
}
return $form_data;
}
/**
* Fix fields' data.
*
* @since 1.9.2
*
* @param array $form_data Form data.
*
* @return array
*/
private function fix_fields_data( array $form_data ): array {
$updated_fields_data = [];
$page_breaks = [];
// Fix array keys. The key should be identical to `id`.
foreach ( $form_data['fields'] as $field_data ) {
$updated_fields_data[ (string) $field_data['id'] ] = $field_data;
}
$form_data['fields'] = $updated_fields_data;
// Fix choice values and choices array indexes.
foreach ( $form_data['fields'] as $id => $field_data ) {
$field_data = $this->fix_field_defaults( $field_data );
$form_data['fields'][ $id ] = $this->fix_choices( $field_data );
}
// Fix conditional logic rules and detect page breaks.
foreach ( $form_data['fields'] as $id => $field_data ) {
$form_data['fields'][ $id ] = $this->fix_field_cl( $field_data, $form_data );
if ( $field_data['type'] === 'pagebreak' ) {
$page_breaks[] = $id;
}
}
// Fix page breaks.
if ( ! empty( $page_breaks ) ) {
$form_data = $this->fix_page_breaks( $form_data, $page_breaks );
}
return $form_data;
}
/**
* Fix field's conditional logic rules.
*
* @since 1.9.2
*
* @param array $field Field data.
* @param array $form_data Form data.
*
* @return array
*/
private function fix_field_cl( array $field, array $form_data ): array {
if ( empty( $field['conditionals'] ) || empty( $field['conditional_logic'] ) ) {
return $field;
}
// Loop groups.
foreach ( $field['conditionals'] as $group_key => $group ) {
// Loop rules.
foreach ( $group as $rule_key => $rule ) {
$choices = $form_data['fields'][ $rule['field'] ]['choices'] ?? [];
// We only need to update rules for choice-based fields.
if ( empty( $choices ) ) {
continue;
}
// AI uses choice value, but we should use the index of the choice in the `choices` array.
$field['conditionals'][ $group_key ][ $rule_key ]['value'] = $this->get_choice_index( $choices, $rule['value'] );
// Continue if the operator is supported by the choice-based field.
if ( in_array( $rule['operator'], [ '==', '!=', 'e', '!e' ], true ) ) {
continue;
}
// Fix `operator` value for choice-based fields.
$rule['operator'] = in_array( $rule['operator'], [ 'c', '^', '>', '<' ], true ) ? '==' : $rule['operator'];
$rule['operator'] = in_array( $rule['operator'], [ '!c', '~' ], true ) ? '!=' : $rule['operator'];
$field['conditionals'][ $group_key ][ $rule_key ]['operator'] = $rule['operator'];
}
}
return $field;
}
/**
* Find choice index in the `choices` array.
*
* @since 1.9.2
*
* @param array $choices Choices data.
* @param string $value Value to find in choices.
*
* @return string|null
*/
private function get_choice_index( array $choices, string $value ) {
$index = array_search( $value, array_column( $choices, 'value' ), true );
if ( $index === false ) {
$index = array_search( $value, array_column( $choices, 'label' ), true );
}
$choices_keys = array_keys( $choices );
return $index === false ? null : $choices_keys[ $index ];
}
/**
* Add missing default attributes to the field.
*
* @since 1.9.4
*
* @param array $field Field data.
*
* @return array
*/
private function fix_field_defaults( array $field ): array {
/**
* Allow the default field settings to be filtered.
*
* @since 1.0.8
*
* @param array $field Default field settings.
*/
$field = (array) apply_filters( 'wpforms_field_new_default', $field ); // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
// Set the defaults for certain fields.
if ( $field['type'] === 'content' ) {
$field['label'] = empty( $field['label'] ) ? esc_html__( 'Content', 'wpforms-lite' ) : $field['label'];
}
if ( $field['type'] === 'richtext' ) {
$field['default_value'] = '';
}
return $field;
}
/**
* Fix choices.
*
* Remove unnecessary values from choices.
*
* @since 1.9.2
*
* @param array $field Field data.
*
* @return array
*/
private function fix_choices( array $field ): array {
if ( empty( $field['choices'] ) ) {
return $field;
}
// Remove values from choices for non-payment fields.
if ( ! in_array( $field['type'], [ 'payment-multiple', 'payment-checkbox', 'payment-select' ], true ) ) {
// Remove values from choices.
foreach ( $field['choices'] as $i => $choice ) {
$field['choices'][ $i ]['value'] = '';
}
}
$updated_choices = [];
// Update array keys to start from 1.
foreach ( $field['choices'] as $i => $choice ) {
$updated_choices[ $i + 1 ] = $choice;
}
$field['choices'] = $updated_choices;
return $field;
}
/**
* Fix page breaks.
*
* Add top and bottom page breaks to the form, set `nav_align` for all page breaks.
*
* @since 1.9.3
*
* @param array $form_data Form data.
* @param array $page_breaks Page break IDs.
*
* @return array
*/
private function fix_page_breaks( array $form_data, array $page_breaks ): array { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
reset( $form_data['fields'] );
$max_field_id = max( array_keys( $form_data['fields'] ) );
// Update or add the top page break.
$first_field_id = key( $form_data['fields'] );
// If the first field is a page break, use its ID, otherwise create a new one.
if ( $form_data['fields'][ $first_field_id ]['type'] === 'pagebreak_top' ) {
$top_id = $first_field_id;
} else {
$top_id = '0';
$form_data['fields'] = [ $top_id => [] ] + $form_data['fields'];
}
$form_data['fields'][ $top_id ] = [
'type' => 'pagebreak',
'id' => $top_id,
'position' => 'top',
'indicator' => 'progress',
'indicator_color' => PagebreakField::get_default_indicator_color(),
'title' => $form_data['fields'][ $top_id ]['title'] ?? '',
'nav_align' => $form_data['fields'][ $top_id ]['nav_align'] ?? 'left',
];
// Remove the Previous button from the first normal pagebreak.
$form_data['fields'][ $page_breaks[0] ]['prev'] = '';
$form_data['fields'][ $page_breaks[0] ]['prev_toggle'] = '';
end( $form_data['fields'] );
// Update or add the bottom page break.
// If the last field is a page break, use its ID, otherwise create a new one.
$last_field_id = key( $form_data['fields'] );
$last_field = $form_data['fields'][ key( $form_data['fields'] ) ];
$bottom_id = $last_field['type'] === 'pagebreak_bottom' ? $last_field_id : (string) ++$max_field_id;
$form_data['fields'][ $bottom_id ] = [
'type' => 'pagebreak',
'id' => $bottom_id,
'position' => 'bottom',
'title' => '',
'prev' => $form_data['fields'][ $bottom_id ]['prev'] ?? '',
'prev_toggle' => $form_data['fields'][ $bottom_id ]['prev_toggle'] ?? '',
];
// Remove the Previous button from the bottom pagebreak.
if ( empty( $form_data['fields'][ $bottom_id ]['prev_toggle'] ) ) {
unset( $form_data['fields'][ $bottom_id ]['prev'], $form_data['fields'][ $bottom_id ]['prev_toggle'] );
}
// Prevent wrong pagebreaks.
foreach ( $form_data['fields'] as $d => $field ) {
$field['type'] = $field['type'] === 'pagebreak_top' ? 'pagebreak' : $field['type'];
$field['type'] = $field['type'] === 'pagebreak_bottom' ? 'pagebreak' : $field['type'];
$form_data['fields'][ $d ] = $field;
}
return $form_data;
}
}