WC_Admin_Settings::output_fields PHP Method

output_fields() public static method

Loops though the woocommerce options array and outputs each field.
public static output_fields ( array $options )
$options array Opens array to output
        public static function output_fields($options)
        {
            foreach ($options as $value) {
                if (!isset($value['type'])) {
                    continue;
                }
                if (!isset($value['id'])) {
                    $value['id'] = '';
                }
                if (!isset($value['title'])) {
                    $value['title'] = isset($value['name']) ? $value['name'] : '';
                }
                if (!isset($value['class'])) {
                    $value['class'] = '';
                }
                if (!isset($value['css'])) {
                    $value['css'] = '';
                }
                if (!isset($value['default'])) {
                    $value['default'] = '';
                }
                if (!isset($value['desc'])) {
                    $value['desc'] = '';
                }
                if (!isset($value['desc_tip'])) {
                    $value['desc_tip'] = false;
                }
                if (!isset($value['placeholder'])) {
                    $value['placeholder'] = '';
                }
                // Custom attribute handling
                $custom_attributes = array();
                if (!empty($value['custom_attributes']) && is_array($value['custom_attributes'])) {
                    foreach ($value['custom_attributes'] as $attribute => $attribute_value) {
                        $custom_attributes[] = esc_attr($attribute) . '="' . esc_attr($attribute_value) . '"';
                    }
                }
                // Description handling
                $field_description = self::get_field_description($value);
                extract($field_description);
                // Switch based on type
                switch ($value['type']) {
                    // Section Titles
                    case 'title':
                        if (!empty($value['title'])) {
                            echo '<h2>' . esc_html($value['title']) . '</h2>';
                        }
                        if (!empty($value['desc'])) {
                            echo wpautop(wptexturize(wp_kses_post($value['desc'])));
                        }
                        echo '<table class="form-table">' . "\n\n";
                        if (!empty($value['id'])) {
                            do_action('woocommerce_settings_' . sanitize_title($value['id']));
                        }
                        break;
                        // Section Ends
                    // Section Ends
                    case 'sectionend':
                        if (!empty($value['id'])) {
                            do_action('woocommerce_settings_' . sanitize_title($value['id']) . '_end');
                        }
                        echo '</table>';
                        if (!empty($value['id'])) {
                            do_action('woocommerce_settings_' . sanitize_title($value['id']) . '_after');
                        }
                        break;
                        // Standard text inputs and subtypes like 'number'
                    // Standard text inputs and subtypes like 'number'
                    case 'text':
                    case 'email':
                    case 'number':
                    case 'color':
                    case 'password':
                        $type = $value['type'];
                        $option_value = self::get_option($value['id'], $value['default']);
                        if ('color' === $value['type']) {
                            $type = 'text';
                            $value['class'] .= 'colorpick';
                            $description .= '<div id="colorPickerDiv_' . esc_attr($value['id']) . '" class="colorpickdiv" style="z-index: 100;background:#eee;border:1px solid #ccc;position:absolute;display:none;"></div>';
                        }
                        ?>
<tr valign="top">
						<th scope="row" class="titledesc">
							<label for="<?php 
                        echo esc_attr($value['id']);
                        ?>
"><?php 
                        echo esc_html($value['title']);
                        ?>
</label>
							<?php 
                        echo $tooltip_html;
                        ?>
						</th>
						<td class="forminp forminp-<?php 
                        echo sanitize_title($value['type']);
                        ?>
">
							<?php 
                        if ('color' == $value['type']) {
                            echo '<span class="colorpickpreview" style="background: ' . esc_attr($option_value) . ';"></span>';
                        }
                        ?>
							<input
								name="<?php 
                        echo esc_attr($value['id']);
                        ?>
"
								id="<?php 
                        echo esc_attr($value['id']);
                        ?>
"
								type="<?php 
                        echo esc_attr($type);
                        ?>
"
								style="<?php 
                        echo esc_attr($value['css']);
                        ?>
"
								value="<?php 
                        echo esc_attr($option_value);
                        ?>
"
								class="<?php 
                        echo esc_attr($value['class']);
                        ?>
"
								placeholder="<?php 
                        echo esc_attr($value['placeholder']);
                        ?>
"
								<?php 
                        echo implode(' ', $custom_attributes);
                        ?>
								/> <?php 
                        echo $description;
                        ?>
						</td>
					</tr><?php 
                        break;
                        // Textarea
                    // Textarea
                    case 'textarea':
                        $option_value = self::get_option($value['id'], $value['default']);
                        ?>
<tr valign="top">
						<th scope="row" class="titledesc">
							<label for="<?php 
                        echo esc_attr($value['id']);
                        ?>
"><?php 
                        echo esc_html($value['title']);
                        ?>
</label>
							<?php 
                        echo $tooltip_html;
                        ?>
						</th>
						<td class="forminp forminp-<?php 
                        echo sanitize_title($value['type']);
                        ?>
">
							<?php 
                        echo $description;
                        ?>

							<textarea
								name="<?php 
                        echo esc_attr($value['id']);
                        ?>
"
								id="<?php 
                        echo esc_attr($value['id']);
                        ?>
"
								style="<?php 
                        echo esc_attr($value['css']);
                        ?>
"
								class="<?php 
                        echo esc_attr($value['class']);
                        ?>
"
								placeholder="<?php 
                        echo esc_attr($value['placeholder']);
                        ?>
"
								<?php 
                        echo implode(' ', $custom_attributes);
                        ?>
								><?php 
                        echo esc_textarea($option_value);
                        ?>
</textarea>
						</td>
					</tr><?php 
                        break;
                        // Select boxes
                    // Select boxes
                    case 'select':
                    case 'multiselect':
                        $option_value = self::get_option($value['id'], $value['default']);
                        ?>
<tr valign="top">
						<th scope="row" class="titledesc">
							<label for="<?php 
                        echo esc_attr($value['id']);
                        ?>
"><?php 
                        echo esc_html($value['title']);
                        ?>
</label>
							<?php 
                        echo $tooltip_html;
                        ?>
						</th>
						<td class="forminp forminp-<?php 
                        echo sanitize_title($value['type']);
                        ?>
">
							<select
								name="<?php 
                        echo esc_attr($value['id']);
                        echo 'multiselect' === $value['type'] ? '[]' : '';
                        ?>
"
								id="<?php 
                        echo esc_attr($value['id']);
                        ?>
"
								style="<?php 
                        echo esc_attr($value['css']);
                        ?>
"
								class="<?php 
                        echo esc_attr($value['class']);
                        ?>
"
								<?php 
                        echo implode(' ', $custom_attributes);
                        ?>
								<?php 
                        echo 'multiselect' == $value['type'] ? 'multiple="multiple"' : '';
                        ?>
								>
								<?php 
                        foreach ($value['options'] as $key => $val) {
                            ?>
										<option value="<?php 
                            echo esc_attr($key);
                            ?>
" <?php 
                            if (is_array($option_value)) {
                                selected(in_array($key, $option_value), true);
                            } else {
                                selected($option_value, $key);
                            }
                            ?>
><?php 
                            echo $val;
                            ?>
</option>
										<?php 
                        }
                        ?>
							</select> <?php 
                        echo $description;
                        ?>
						</td>
					</tr><?php 
                        break;
                        // Radio inputs
                    // Radio inputs
                    case 'radio':
                        $option_value = self::get_option($value['id'], $value['default']);
                        ?>
<tr valign="top">
						<th scope="row" class="titledesc">
							<label for="<?php 
                        echo esc_attr($value['id']);
                        ?>
"><?php 
                        echo esc_html($value['title']);
                        ?>
</label>
							<?php 
                        echo $tooltip_html;
                        ?>
						</th>
						<td class="forminp forminp-<?php 
                        echo sanitize_title($value['type']);
                        ?>
">
							<fieldset>
								<?php 
                        echo $description;
                        ?>
								<ul>
								<?php 
                        foreach ($value['options'] as $key => $val) {
                            ?>
										<li>
											<label><input
												name="<?php 
                            echo esc_attr($value['id']);
                            ?>
"
												value="<?php 
                            echo $key;
                            ?>
"
												type="radio"
												style="<?php 
                            echo esc_attr($value['css']);
                            ?>
"
												class="<?php 
                            echo esc_attr($value['class']);
                            ?>
"
												<?php 
                            echo implode(' ', $custom_attributes);
                            ?>
												<?php 
                            checked($key, $option_value);
                            ?>
												/> <?php 
                            echo $val;
                            ?>
</label>
										</li>
										<?php 
                        }
                        ?>
								</ul>
							</fieldset>
						</td>
					</tr><?php 
                        break;
                        // Checkbox input
                    // Checkbox input
                    case 'checkbox':
                        $option_value = self::get_option($value['id'], $value['default']);
                        $visbility_class = array();
                        if (!isset($value['hide_if_checked'])) {
                            $value['hide_if_checked'] = false;
                        }
                        if (!isset($value['show_if_checked'])) {
                            $value['show_if_checked'] = false;
                        }
                        if ('yes' == $value['hide_if_checked'] || 'yes' == $value['show_if_checked']) {
                            $visbility_class[] = 'hidden_option';
                        }
                        if ('option' == $value['hide_if_checked']) {
                            $visbility_class[] = 'hide_options_if_checked';
                        }
                        if ('option' == $value['show_if_checked']) {
                            $visbility_class[] = 'show_options_if_checked';
                        }
                        if (!isset($value['checkboxgroup']) || 'start' == $value['checkboxgroup']) {
                            ?>
							<tr valign="top" class="<?php 
                            echo esc_attr(implode(' ', $visbility_class));
                            ?>
">
								<th scope="row" class="titledesc"><?php 
                            echo esc_html($value['title']);
                            ?>
</th>
								<td class="forminp forminp-checkbox">
									<fieldset>
						<?php 
                        } else {
                            ?>
							<fieldset class="<?php 
                            echo esc_attr(implode(' ', $visbility_class));
                            ?>
">
						<?php 
                        }
                        if (!empty($value['title'])) {
                            ?>
							<legend class="screen-reader-text"><span><?php 
                            echo esc_html($value['title']);
                            ?>
</span></legend>
						<?php 
                        }
                        ?>
						<label for="<?php 
                        echo $value['id'];
                        ?>
">
							<input
								name="<?php 
                        echo esc_attr($value['id']);
                        ?>
"
								id="<?php 
                        echo esc_attr($value['id']);
                        ?>
"
								type="checkbox"
								class="<?php 
                        echo esc_attr(isset($value['class']) ? $value['class'] : '');
                        ?>
"
								value="1"
								<?php 
                        checked($option_value, 'yes');
                        ?>
								<?php 
                        echo implode(' ', $custom_attributes);
                        ?>
							/> <?php 
                        echo $description;
                        ?>
						</label> <?php 
                        echo $tooltip_html;
                        ?>
					<?php 
                        if (!isset($value['checkboxgroup']) || 'end' == $value['checkboxgroup']) {
                            ?>
									</fieldset>
								</td>
							</tr>
						<?php 
                        } else {
                            ?>
							</fieldset>
						<?php 
                        }
                        break;
                        // Image width settings
                    // Image width settings
                    case 'image_width':
                        $image_size = str_replace('_image_size', '', $value['id']);
                        $size = wc_get_image_size($image_size);
                        $width = isset($size['width']) ? $size['width'] : $value['default']['width'];
                        $height = isset($size['height']) ? $size['height'] : $value['default']['height'];
                        $crop = isset($size['crop']) ? $size['crop'] : $value['default']['crop'];
                        $disabled_attr = '';
                        $disabled_message = '';
                        if (has_filter('woocommerce_get_image_size_' . $image_size)) {
                            $disabled_attr = 'disabled="disabled"';
                            $disabled_message = "<p><small>" . __('The settings of this image size have been disabled because its values are being overwritten by a filter.', 'woocommerce') . "</small></p>";
                        }
                        ?>
<tr valign="top">
						<th scope="row" class="titledesc"><?php 
                        echo esc_html($value['title']);
                        ?>
 <?php 
                        echo $tooltip_html . $disabled_message;
                        ?>
</th>
						<td class="forminp image_width_settings">

							<input name="<?php 
                        echo esc_attr($value['id']);
                        ?>
[width]" <?php 
                        echo $disabled_attr;
                        ?>
 id="<?php 
                        echo esc_attr($value['id']);
                        ?>
-width" type="text" size="3" value="<?php 
                        echo $width;
                        ?>
" /> &times; <input name="<?php 
                        echo esc_attr($value['id']);
                        ?>
[height]" <?php 
                        echo $disabled_attr;
                        ?>
 id="<?php 
                        echo esc_attr($value['id']);
                        ?>
-height" type="text" size="3" value="<?php 
                        echo $height;
                        ?>
" />px

							<label><input name="<?php 
                        echo esc_attr($value['id']);
                        ?>
[crop]" <?php 
                        echo $disabled_attr;
                        ?>
 id="<?php 
                        echo esc_attr($value['id']);
                        ?>
-crop" type="checkbox" value="1" <?php 
                        checked(1, $crop);
                        ?>
 /> <?php 
                        _e('Hard crop?', 'woocommerce');
                        ?>
</label>

							</td>
					</tr><?php 
                        break;
                        // Single page selects
                    // Single page selects
                    case 'single_select_page':
                        $args = array('name' => $value['id'], 'id' => $value['id'], 'sort_column' => 'menu_order', 'sort_order' => 'ASC', 'show_option_none' => ' ', 'class' => $value['class'], 'echo' => false, 'selected' => absint(self::get_option($value['id'])));
                        if (isset($value['args'])) {
                            $args = wp_parse_args($value['args'], $args);
                        }
                        ?>
<tr valign="top" class="single_select_page">
						<th scope="row" class="titledesc"><?php 
                        echo esc_html($value['title']);
                        ?>
 <?php 
                        echo $tooltip_html;
                        ?>
</th>
						<td class="forminp">
							<?php 
                        echo str_replace(' id=', " data-placeholder='" . esc_attr__('Select a page&hellip;', 'woocommerce') . "' style='" . $value['css'] . "' class='" . $value['class'] . "' id=", wp_dropdown_pages($args));
                        ?>
 <?php 
                        echo $description;
                        ?>
						</td>
					</tr><?php 
                        break;
                        // Single country selects
                    // Single country selects
                    case 'single_select_country':
                        $country_setting = (string) self::get_option($value['id']);
                        if (strstr($country_setting, ':')) {
                            $country_setting = explode(':', $country_setting);
                            $country = current($country_setting);
                            $state = end($country_setting);
                        } else {
                            $country = $country_setting;
                            $state = '*';
                        }
                        ?>
<tr valign="top">
						<th scope="row" class="titledesc">
							<label for="<?php 
                        echo esc_attr($value['id']);
                        ?>
"><?php 
                        echo esc_html($value['title']);
                        ?>
</label>
							<?php 
                        echo $tooltip_html;
                        ?>
						</th>
						<td class="forminp"><select name="<?php 
                        echo esc_attr($value['id']);
                        ?>
" style="<?php 
                        echo esc_attr($value['css']);
                        ?>
" data-placeholder="<?php 
                        esc_attr_e('Choose a country&hellip;', 'woocommerce');
                        ?>
" aria-label="<?php 
                        esc_attr_e('Country', 'woocommerce');
                        ?>
" class="wc-enhanced-select">
							<?php 
                        WC()->countries->country_dropdown_options($country, $state);
                        ?>
						</select> <?php 
                        echo $description;
                        ?>
						</td>
					</tr><?php 
                        break;
                        // Country multiselects
                    // Country multiselects
                    case 'multi_select_countries':
                        $selections = (array) self::get_option($value['id']);
                        if (!empty($value['options'])) {
                            $countries = $value['options'];
                        } else {
                            $countries = WC()->countries->countries;
                        }
                        asort($countries);
                        ?>
<tr valign="top">
						<th scope="row" class="titledesc">
							<label for="<?php 
                        echo esc_attr($value['id']);
                        ?>
"><?php 
                        echo esc_html($value['title']);
                        ?>
</label>
							<?php 
                        echo $tooltip_html;
                        ?>
						</th>
						<td class="forminp">
							<select multiple="multiple" name="<?php 
                        echo esc_attr($value['id']);
                        ?>
[]" style="width:350px" data-placeholder="<?php 
                        esc_attr_e('Choose countries&hellip;', 'woocommerce');
                        ?>
" aria-label="<?php 
                        esc_attr_e('Country', 'woocommerce');
                        ?>
" class="wc-enhanced-select">
								<?php 
                        if (!empty($countries)) {
                            foreach ($countries as $key => $val) {
                                echo '<option value="' . esc_attr($key) . '" ' . selected(in_array($key, $selections), true, false) . '>' . $val . '</option>';
                            }
                        }
                        ?>
							</select> <?php 
                        echo $description ? $description : '';
                        ?>
 <br /><a class="select_all button" href="#"><?php 
                        _e('Select all', 'woocommerce');
                        ?>
</a> <a class="select_none button" href="#"><?php 
                        _e('Select none', 'woocommerce');
                        ?>
</a>
						</td>
					</tr><?php 
                        break;
                        // Default: run an action
                    // Default: run an action
                    default:
                        do_action('woocommerce_admin_field_' . $value['type'], $value);
                        break;
                }
            }
        }

Usage Example

コード例 #1
0
/**
 * Output admin fields.
 *
 * Loops though the woocommerce options array and outputs each field.
 *
 * @param array $options Opens array to output
 */
function woocommerce_admin_fields($options)
{
    if (!class_exists('WC_Admin_Settings')) {
        include 'class-wc-admin-settings.php';
    }
    WC_Admin_Settings::output_fields($options);
}
All Usage Examples Of WC_Admin_Settings::output_fields