Admin_Apple_Settings_Section::render_field PHP Method

render_field() public method

Render a settings field.
public render_field ( array $args )
$args array
    public function render_field($args)
    {
        list($name, $default_value, $callback) = $args;
        // If the field has it's own render callback, use that here.
        // This is because the options page doesn't actually use do_settings_section.
        if (!empty($callback)) {
            return call_user_func($callback);
        }
        $type = $this->get_type_for($name);
        $settings = get_option(self::$option_name);
        $value = self::get_value($name, $settings);
        $field = null;
        // Get the field size
        $size = $this->get_size_for($name);
        // FIXME: A cleaner object-oriented solution would create Input objects
        // and instantiate them according to their type.
        if (is_array($type)) {
            // Check if this is a multiple select
            $multiple_name = $multiple_attr = '';
            if ($this->is_multiple($name)) {
                $multiple_name = '[]';
                $multiple_attr = 'multiple="multiple"';
            }
            // Check if we're using names as values
            $keys = array_keys($type);
            $use_name_as_value = array_keys($keys) === $keys;
            // Use select2 only when there is a considerable ammount of options available
            if (count($type) > 10) {
                $field = '<select class="select2 standard" id="%s" name="%s' . $multiple_name . '" ' . $multiple_attr . '>';
            } else {
                $field = '<select id="%s" name="%s' . $multiple_name . '" ' . $multiple_attr . '>';
            }
            foreach ($type as $key => $option) {
                $store_value = $use_name_as_value ? $option : $key;
                $field .= "<option value='" . esc_attr($store_value) . "' ";
                if ($this->is_multiple($name)) {
                    if (in_array($store_value, $value)) {
                        $field .= 'selected="selected"';
                    }
                } else {
                    $field .= selected($value, $store_value, false);
                }
                $field .= ">" . esc_html($option) . "</option>";
            }
            $field .= '</select>';
        } else {
            if ('font' == $type) {
                $field = '<select class="select2 font" id="%s" name="%s">';
                foreach (self::$fonts as $option) {
                    $field .= "<option value='" . esc_attr($option) . "'";
                    if ($option == $value) {
                        $field .= ' selected ';
                    }
                    $field .= ">" . esc_html($option) . "</option>";
                }
                $field .= '</select>';
            } else {
                if ('boolean' == $type) {
                    $field = '<select name="%s">';
                    $field .= '<option value="yes"';
                    if ('yes' == $value) {
                        $field .= ' selected ';
                    }
                    $field .= '>Yes</option>';
                    $field .= '<option value="no"';
                    if ('yes' != $value) {
                        $field .= ' selected ';
                    }
                    $field .= '>No</option>';
                    $field .= '</select>';
                } else {
                    if ('integer' == $type) {
                        $field = '<input type="number" id="%s" name="%s" value="%s" size="%s" %s>';
                    } else {
                        if ('float' == $type) {
                            $field = '<input class="input-float" placeholder="' . esc_attr($default_value) . '" type="text" step="any" id="%s" name="%s" value="%s" size="%s">';
                        } else {
                            if ('color' == $type) {
                                $field = '<input type="text" id="%s" name="%s" value="%s" class="apple-news-color-picker" %s>';
                            } else {
                                if ('password' == $type) {
                                    $field = '<input type="password" id="%s" name="%s" value="%s" size="%s" %s>';
                                } else {
                                    // If nothing else matches, it's a string.
                                    $field = '<input type="text" id="%s" name="%s" value="%s" size="%s" %s>';
                                }
                            }
                        }
                    }
                }
            }
        }
        // Add a description, if set.
        $description = $this->get_description_for($name);
        if (!empty($description)) {
            $field .= apply_filters('apple_news_field_description_output_html', '<br/><i>' . $description . '</i>', $name);
        }
        // Use the proper template to build the field
        if (is_array($type) || 'font' === $type || 'boolean' === $type) {
            return sprintf($field, esc_attr($name), esc_attr($name));
        } else {
            return sprintf($field, esc_attr($name), esc_attr($name), esc_attr($value), intval($size), esc_attr($this->is_required($name)));
        }
    }