From 3db43db198f30a16773e84672c1f830a84a7f883 Mon Sep 17 00:00:00 2001 From: jdub233 Date: Tue, 1 Nov 2016 13:51:23 -0400 Subject: [PATCH 1/6] add field handler for mini form --- bu-liaison-inquiry.php | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/bu-liaison-inquiry.php b/bu-liaison-inquiry.php index 57e9a0e..ed2b2d2 100644 --- a/bu-liaison-inquiry.php +++ b/bu-liaison-inquiry.php @@ -128,8 +128,20 @@ function liaison_inquiry_form( $atts ) { wp_enqueue_script( 'field_rules_form_library' ); wp_enqueue_script( 'field_rules_handler' ); + // Setup field ids if a restricted field set was specified in the shortcode. + $field_ids = array(); + if ( isset( $atts['fields'] ) ) { + // Parse fields attribute. + $fields = explode( ',', $atts['fields'] ); + foreach ( $fields as $field ) { + // Only use integer values. + if ( ctype_digit( $field ) ) { + $field_ids[] = $field; + } + } + } - $inquiry_form = $inquiry_form_decode->data; + $inquiry_form = $this->process_form_definition( $inquiry_form_decode->data, $field_ids ); // Setup nonce for form to protect against various possible attacks. $nonce = wp_nonce_field( 'liaison_inquiry', 'liaison_inquiry_nonce', false, false ); @@ -144,7 +156,28 @@ function liaison_inquiry_form( $atts ) { } /** - * Shortcode definition that creates the Liaison inquiry form. + * Takes the form definition returned by the Liaison API, strips out any unspecified fields for the mini form, and applies other formatting + * + * @param array $inquiry_form Parsed JSON data from Liaison API. + * @param array $field_ids List of fields to show. If not specified, the full form is returned. + * @return string Returns the result of the form submission as a JSON formatted array for the javascript validation script. + */ + function process_form_definition( $inquiry_form, $field_ids ) { + // If field_ids are specified, remove any fields that aren't in the specified set. + if ( 0 < count( $field_ids ) ) { + foreach ( $inquiry_form->sections as $section ) { + foreach ( $section->fields as $field_key => $field ) { + if ( ! in_array( $field->id, $field_ids ) ) { + unset( $section->fields[ $field_key ] ); + } + } + } + } + return $inquiry_form; + } + + /** + * Handle incoming form data * * @return string Returns the result of the form submission as a JSON formatted array for the javascript validation script */ From 339f414659bb470e3b23ef88be6ca8b7623f7c0c Mon Sep 17 00:00:00 2001 From: jdub233 Date: Tue, 1 Nov 2016 16:49:20 -0400 Subject: [PATCH 2/6] add a basic field inventory to the admin interface to assist in selecting fields for the mini-form --- admin/admin.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/admin/admin.php b/admin/admin.php index 73ab4fe..592b475 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -157,4 +157,38 @@ function bu_liaison_inquiry_options_page_html() { get_error_message() ); + return 'Form Error: ' . $api_response->get_error_message(); + } + $inquiry_form_decode = json_decode( $api_response['body'] ); + + // Check that the response from the API contains actual form data. + if ( ! isset( $inquiry_form_decode->data ) ) { + error_log( 'Bad response from Liaison API server: ' . $inquiry_form_decode->message ); + return 'Error in Form API response'; + } + + $inquiry_form = $inquiry_form_decode->data; + + $fields_html = '

Field inventory

'; + + foreach ( $inquiry_form->sections as $section ) { + foreach ( $section->fields as $field_key => $field ) { + $fields_html .= sprintf( '

%s = %s

', $field->displayName, $field->id ); + } + } + return $fields_html; } From 96362c0033715b48369d5464e182e06611312093 Mon Sep 17 00:00:00 2001 From: jdub233 Date: Wed, 2 Nov 2016 11:02:10 -0400 Subject: [PATCH 3/6] doc fix --- bu-liaison-inquiry.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bu-liaison-inquiry.php b/bu-liaison-inquiry.php index ed2b2d2..351a35d 100644 --- a/bu-liaison-inquiry.php +++ b/bu-liaison-inquiry.php @@ -160,7 +160,7 @@ function liaison_inquiry_form( $atts ) { * * @param array $inquiry_form Parsed JSON data from Liaison API. * @param array $field_ids List of fields to show. If not specified, the full form is returned. - * @return string Returns the result of the form submission as a JSON formatted array for the javascript validation script. + * @return array Returns a data array of the processed form data to be passed to the template */ function process_form_definition( $inquiry_form, $field_ids ) { // If field_ids are specified, remove any fields that aren't in the specified set. From 9c9b5c35f9db1d2d19f020a3f3b6903c6b6ce50d Mon Sep 17 00:00:00 2001 From: jdub233 Date: Wed, 2 Nov 2016 11:03:59 -0400 Subject: [PATCH 4/6] add a const for dummy data --- bu-liaison-inquiry.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bu-liaison-inquiry.php b/bu-liaison-inquiry.php index 351a35d..ba42c09 100644 --- a/bu-liaison-inquiry.php +++ b/bu-liaison-inquiry.php @@ -24,6 +24,9 @@ class BU_Liaison_Inquiry { const CLIENT_RULES_PATH = 'field_rules/client_rules'; const FIELD_OPTIONS_PATH = 'field_rules/field_options'; + // Setup dummy value for required fields that aren't part of the mini form. + const MINI_DUMMY_VALUE = 'mini-form'; + /** * Path to plugin directory. * From 9abdebb47fe9ce71087e37d524782bf28a895d48 Mon Sep 17 00:00:00 2001 From: jdub233 Date: Wed, 2 Nov 2016 13:17:30 -0400 Subject: [PATCH 5/6] basic hidden field support --- bu-liaison-inquiry.php | 12 +++++++++++- templates/form-template.php | 9 ++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bu-liaison-inquiry.php b/bu-liaison-inquiry.php index ba42c09..4e9e66a 100644 --- a/bu-liaison-inquiry.php +++ b/bu-liaison-inquiry.php @@ -170,8 +170,15 @@ function process_form_definition( $inquiry_form, $field_ids ) { if ( 0 < count( $field_ids ) ) { foreach ( $inquiry_form->sections as $section ) { foreach ( $section->fields as $field_key => $field ) { + // Field by field processing. if ( ! in_array( $field->id, $field_ids ) ) { - unset( $section->fields[ $field_key ] ); + // If a field isn't listed and isn't required, just remove it. + if ( '1' != $field->required ) { + unset( $section->fields[ $field_key ] ); + } else { + $field->hidden = true; + $field->hidden_value = self::MINI_DUMMY_VALUE; + } } } } @@ -194,6 +201,9 @@ function handle_liaison_inquiry() { return; } + // Clear the verified nonce from $_POST so that it doesn't get passed on to Liaison. + unset( $_POST['liaison_inquiry_nonce'] ); + // Necessary to get the API key from the options, can't expose the key by passing it through the form. $options = get_option( 'bu_liaison_inquiry_options' ); // Check for a valid API key value. diff --git a/templates/form-template.php b/templates/form-template.php index cc0604e..acdef43 100644 --- a/templates/form-template.php +++ b/templates/form-template.php @@ -32,8 +32,15 @@ $label = ''; } //end setup + ?> + + hidden ) && $field->hidden ) : ?> + + htmlElement == 'input-text' ) : + elseif ( $field->htmlElement == 'input-text' ) : //begin input text $class = ''; From 11171df9a6f6f7b443bf7f84576a602a8c249487 Mon Sep 17 00:00:00 2001 From: jdub233 Date: Wed, 2 Nov 2016 14:24:08 -0400 Subject: [PATCH 6/6] add preset parser --- bu-liaison-inquiry.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/bu-liaison-inquiry.php b/bu-liaison-inquiry.php index 4e9e66a..39c7d00 100644 --- a/bu-liaison-inquiry.php +++ b/bu-liaison-inquiry.php @@ -101,8 +101,14 @@ function liaison_inquiry_form( $atts ) { $api_key = $options['APIKey']; $client_id = $options['ClientID']; - // Optionally override the API key with the shortcode attribute if present. - if ( isset( $atts['api_key'] ) ) { $api_key = $atts['api_key']; } + // Assign any preset field ids in the shortcode attributes. + $presets = array(); + foreach ( $atts as $att_key => $att ) { + // Look for integer numbers, these are field ids. + if ( intval( $att_key ) === $att_key ) { + $presets[ $att_key ] = $att; + } + } // Get info from EMP about the fields that should be displayed for the form. $api_query = self::$requirements_url . '?IQS-API-KEY=' . $api_key; @@ -144,7 +150,7 @@ function liaison_inquiry_form( $atts ) { } } - $inquiry_form = $this->process_form_definition( $inquiry_form_decode->data, $field_ids ); + $inquiry_form = $this->process_form_definition( $inquiry_form_decode->data, $field_ids, $presets ); // Setup nonce for form to protect against various possible attacks. $nonce = wp_nonce_field( 'liaison_inquiry', 'liaison_inquiry_nonce', false, false ); @@ -163,9 +169,10 @@ function liaison_inquiry_form( $atts ) { * * @param array $inquiry_form Parsed JSON data from Liaison API. * @param array $field_ids List of fields to show. If not specified, the full form is returned. + * @param array $presets Array of preset field ids and values. * @return array Returns a data array of the processed form data to be passed to the template */ - function process_form_definition( $inquiry_form, $field_ids ) { + function process_form_definition( $inquiry_form, $field_ids, $presets ) { // If field_ids are specified, remove any fields that aren't in the specified set. if ( 0 < count( $field_ids ) ) { foreach ( $inquiry_form->sections as $section ) { @@ -177,7 +184,11 @@ function process_form_definition( $inquiry_form, $field_ids ) { unset( $section->fields[ $field_key ] ); } else { $field->hidden = true; - $field->hidden_value = self::MINI_DUMMY_VALUE; + if ( isset( $presets[ $field->id ] ) ) { + $field->hidden_value = $presets[ $field->id ]; + } else { + $field->hidden_value = self::MINI_DUMMY_VALUE; + } } } }