/** * Helper function for outputting form fields. * * @args array array of field attributes (name, id, class, type, value, onchange, * onclick, onload, readonly, size, my_selection) */ protected function outputFormField($args) { // explode args array into directly addressable variables extract($args); // check class, onchange, onclick, onload, and readonly properties $class = isset($class) ? 'class="' . $class . '" ' : ''; $cols = isset($cols) ? 'cols="' . $cols . '" ' : ''; $multiple = isset($multiple) ? $multiple : ''; $onchange = isset($onchange) ? 'onchange="' . $onchange . '()" ' : ''; $onclick = isset($onclick) ? 'onclick="' . $onclick . '()" ' : ''; $onload = isset($onload) ? 'onload="' . $onload . '()" ' : ''; $readonly = isset($readonly) ? 'readonly="' . $readonly . '" ' : ''; $rows = isset($rows) ? 'rows="' . $rows . '" ' : ''; $size = isset($size) ? 'size="' . $size . '" ' : ''; $style = isset($style) ? 'style="' . $style . '" ' : ''; if (isset($value)) { if ($type === 'checkbox') { // detect check box and set value $checked = $value === 'enabled' ? 'checked="checked" ' : ''; $value = 'value="enabled" '; } else { $value = 'value="' . $value . '" '; $checked = ''; } } else { $value = ''; $checked = ''; } // determine open and close tags based on type if ($type === 'dropdown' || $type === 'multi-select') { $open_tag = '<p><label for="' . $this->get_field_id($field) . '">' . $label . '</label><select id="'; $close_tag = '>'; } else { $open_tag = '<p><label for="' . $this->get_field_id($field) . '">' . $label . '</label><input id="'; $close_tag = ' />'; } // alter name if it is a multi-select for array if ($type != 'multi-select') { $name = 'name="' . $this->get_field_name($field) . '" '; } else { $name = 'name="' . $this->get_field_name($field) . '[]" '; } // output field $field = $open_tag . $this->get_field_id($field) . '" ' . $name . $checked . $class . $cols . $multiple . $onchange . $onclick . $onload . $readonly . $rows . $size . $style; if ($type != 'multi-select') { $field .= ' type="' . $type . '" '; } $field .= $value . $close_tag; echo $field; // continue to populate dropdown if ($type === 'dropdown' || $type === 'multi-select') { // check to make sure selections is an array if (is_array($selections)) { if (!wp_PluginUtilities::isAssociative($selections)) { foreach ($selections as $key => $item) { if ($type === 'dropdown') { $selected = $my_selection == $item ? 'selected="selected"' : ''; echo "<option value='{$item}' {$selected}>{$item}</option>"; } else { $selected = ''; if (is_array($my_selections)) { foreach ($my_selections as $selection) { if ($selection === $item) { $selected = 'selected="selected"'; break; } } } echo "<option value='{$item}' {$selected}>{$item}</option>"; } } } else { foreach ($selections as $key => $item) { if ($type === 'dropdown') { $selected = $my_selection == $key ? 'selected="selected"' : ''; echo "<option value='{$key}' {$selected}>{$item}</option>"; } else { $selected = ''; if (is_array($my_selections)) { foreach ($my_selections as $selection) { if ($selection == $key) { $selected = 'selected="selected"'; break; } } } echo "<option value='{$key}' {$selected}>{$item}</option>"; } } } } echo "</select>"; } echo "</p>"; }