/** * Get displayed values for separate values, data from entries, and other option. * Capitalizes first letter of each option * * @since 2.0 * * @param array $temp_values * @param object $field */ public static function get_displayed_values(&$temp_values, $field) { $temp_array = array(); // If data from entries field if ($field->type == 'data') { // Get DFE text foreach ($temp_values as $entry_id => $total) { $linked_field = $field->field_options['form_select']; $text_val = FrmProEntriesController::get_field_value_shortcode(array('field_id' => $linked_field, 'entry_id' => $entry_id)); $temp_array[$text_val] = $total; unset($entry_id, $total, $linked_field, $text_val); } } else { $other_label = false; foreach ($field->options as $opt_key => $opt) { if (!$opt) { continue; } // If field option is "other" option if (FrmFieldsHelper::is_other_opt($opt_key)) { // For radio button field, combine all extra counts/totals into one "Other" count/total if ($field->type == 'radio' || $field->type == 'select') { $other_label = strtolower($opt); continue; // For checkbox fields, set value and label } else { $opt_value = strtolower($opt_key); $opt_label = strtolower($opt); } // If using separate values } else { if (is_array($opt)) { $opt_label = strtolower($opt['label']); $opt_value = strtolower($opt['value']); if (!$opt_value || !$opt_label) { continue; } } else { $opt_label = $opt_value = strtolower($opt); } } // Set displayed value total in new array, unset original value in old array if (isset($temp_values[$opt_value])) { $temp_array[$opt_label] = $temp_values[$opt_value]; unset($temp_values[$opt_value]); } unset($opt_key, $opt, $opt_label, $opt_value); } // Applies to radio buttons only (with other option) // Combines all extra counts/totals into one "Other" count/total if ($other_label) { $temp_array[$other_label] = array_sum($temp_values); } } // Copy new array $temp_values = $temp_array; }
/** * If shortcode must return an array, bypass the WP do_shortcode function * This is set up to return arrays for frm-field-value shortcode in multiple select fields * * @param $value - string which will be switched to array, pass by reference * @param $return_array - boolean keeps track of whether or not an array should be returned * @return boolean to tell calling function (do_shortcode) if final value is set */ private static function do_array_shortcode(&$value, $return_array) { if (!$return_array || is_array($value)) { return false; } // If frm-field-value shortcode and it should return an array, bypass the WP do_shortcode function if (strpos($value, '[frm-field-value ') !== false) { preg_match_all('/\\[(frm-field-value)\\b(.*?)(?:(\\/))?\\]/s', $value, $matches, PREG_PATTERN_ORDER); foreach ($matches[0] as $short_key => $tag) { $atts = shortcode_parse_atts($matches[2][$short_key]); $atts['return_array'] = $return_array; $value = FrmProEntriesController::get_field_value_shortcode($atts); } return true; } return false; }
/** * @covers FrmProEntriesController::get_field_value_shortcode * TODO: Test with post fields, IP, and user_id parameters */ function test_get_field_value_shortcode() { $tests = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13); $field_id = $this->factory->field->get_id_by_key('493ito'); $entry_id = $this->factory->entry->get_id_by_key('jamie_entry_key'); foreach ($tests as $test) { $sc_atts = self::_setup_frm_field_value_sc_atts($test, $field_id, $entry_id); $sc_atts_list = self::_get_sc_atts_list($sc_atts); $expected_result = self::_get_expected_frm_field_value($test); $value = FrmProEntriesController::get_field_value_shortcode($sc_atts); $this->assertEquals($expected_result, $value, 'The frm-field-value shortcode is not retrieving the correct value with the following parameters: ' . $sc_atts_list); } }