'builtin', 'shortcode' => '', 'title' => 'Send us a message', 'intro' => 'Leave your details and we will get back to you.', 'button' => 'Send message', 'thanks' => 'Thanks - we have your message and will be in touch.', 'phone' => 'optional', 'company' => 'off', 'message_label' => 'How can we help?', 'custom' => array(), ); } /** * Returns the saved contact form settings, merged with the defaults. * * @return array */ function cwp_ai_form_get() { $saved = get_option( 'cwp_ai_form', array() ); return array_merge( cwp_ai_form_defaults(), is_array( $saved ) ? $saved : array() ); } /** * Cleans submitted contact form settings. * * @param array $in The submitted settings. * @return array */ function cwp_ai_form_sanitize( $in ) { $d = cwp_ai_form_defaults(); $in = is_array( $in ) ? $in : array(); $level = function ( $value, $fallback ) { return in_array( $value, array( 'off', 'optional', 'required' ), true ) ? $value : $fallback; }; $custom = array(); foreach ( (array) ( $in['custom'] ?? array() ) as $field ) { $label = sanitize_text_field( $field['label'] ?? '' ); if ( '' === $label ) { continue; } $custom[] = array( 'label' => $label, 'required' => ! empty( $field['required'] ), ); if ( count( $custom ) >= 3 ) { break; } } return array( 'mode' => ( $in['mode'] ?? '' ) === 'shortcode' ? 'shortcode' : 'builtin', 'shortcode' => trim( sanitize_text_field( $in['shortcode'] ?? '' ) ), 'title' => cwp_ai_or( sanitize_text_field( $in['title'] ?? '' ), $d['title'] ), 'intro' => sanitize_text_field( $in['intro'] ?? '' ), 'button' => cwp_ai_or( sanitize_text_field( $in['button'] ?? '' ), $d['button'] ), 'thanks' => cwp_ai_or( sanitize_text_field( $in['thanks'] ?? '' ), $d['thanks'] ), 'phone' => $level( $in['phone'] ?? '', $d['phone'] ), 'company' => $level( $in['company'] ?? '', $d['company'] ), 'message_label' => cwp_ai_or( sanitize_text_field( $in['message_label'] ?? '' ), $d['message_label'] ), 'custom' => $custom, ); } /** * Returns the contact form settings the chat window needs. * * @return array */ function cwp_ai_form_config() { $form = cwp_ai_form_get(); $out = $form; $out['shortcodeUrl'] = ''; if ( 'shortcode' === $form['mode'] && '' !== $form['shortcode'] ) { $out['shortcodeUrl'] = add_query_arg( 'cwp_ai_form', '1', home_url( '/' ) ); } unset( $out['shortcode'] ); return $out; } add_action( 'template_redirect', function () { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- a read-only page that renders the owner's own saved form. if ( empty( $_GET['cwp_ai_form'] ) ) { return; } $form = cwp_ai_form_get(); if ( 'shortcode' !== $form['mode'] || '' === $form['shortcode'] ) { status_header( 404 ); exit; } nocache_headers(); wp_enqueue_style( 'cwp-ai-form-page' ); wp_enqueue_script( 'cwp-ai-form-page' ); ?> >