Beispiel #1
0
/**
 * Settings field
 *
 * Generate HTML for settings field
 *
 * @param $args array
 * @return string HTML element
 *
 */
function _kc_field($args)
{
    if (empty($args['field']['attr'])) {
        $args['field']['attr'] = array();
    }
    extract($args, EXTR_OVERWRITE);
    $i_text = kcForm::$i_text;
    $field_types = array_merge($i_text, array('checkbox', 'radio', 'select', 'multiselect', 'multiinput', 'special', 'file', 'media', 'textarea', 'editor'));
    $type = !empty($field['type']) && in_array($field['type'], $field_types) ? $field['type'] : 'input';
    # setup the input id and name attributes, also get the current value from db
    switch ($mode) {
        # 0. Plugin / Theme
        case 'plugin':
            $name = "{$prefix}_settings[{$section}][{$field['id']}]";
            $id = "{$section}__{$field['id']}";
            $db_value = kc_get_option($prefix, $section, $field['id']);
            break;
            # 1. Attachment
        # 1. Attachment
        case 'attachment':
            $id = $field['id'];
            $name = "attachments[{$object_id}][{$id}]";
            $db_value = get_metadata('post', $object_id, "_{$id}", true);
            break;
            # 2. Subfields of multiinput field
        # 2. Subfields of multiinput field
        case 'subfield':
            extract($args['data'], EXTR_OVERWRITE);
            break;
            # 3. Menu item
        # 3. Menu item
        case 'menu_item':
            $id = "edit-menu-item-{$section}-{$field['id']}-{$object_id}";
            $name = "kc-postmeta[{$section}][{$field['id']}][{$object_id}]";
            $key = "_{$field['id']}";
            $db_value = isset($object_id) && $object_id != '' ? get_metadata('post', $object_id, $key, true) : null;
            break;
            # 4. Nav menu
        # 4. Nav menu
        case 'menu_nav':
            $id = "kc-menu_navmeta-{$section}-{$field['id']}";
            $name = "kc-termmeta[{$section}][{$field['id']}]";
            $key = $field['id'];
            $db_value = isset($object_id) && $object_id != '' ? get_metadata('term', $object_id, $key, true) : null;
            break;
            # 5. Others: post, term & user meta
        # 5. Others: post, term & user meta
        default:
            $id = $field['id'];
            $name = "kc-{$mode}meta[{$section}][{$id}]";
            $key = $mode == 'post' ? "_{$id}" : $id;
            $db_value = isset($object_id) && $object_id != '' ? get_metadata($mode, $object_id, $key, true) : null;
            break;
    }
    $desc_tag = isset($desc_tag) ? $desc_tag : 'p';
    $desc_class = $mode == 'attachment' ? 'help' : 'description';
    $desc = isset($field['desc']) && !empty($field['desc']) ? "<{$desc_tag} class='{$desc_class}'>{$field['desc']}</{$desc_tag}>" : null;
    # Let user filter the output of the setting field
    $output = $mode !== 'subfield' ? apply_filters('kc_settings_field_before', '', $section, $field) : '';
    # Special option with callback
    if ($type == 'special') {
        $args['field']['name'] = $name;
        $cb_args = isset($field['args']) ? $field['args'] : '';
        if (!empty($field['args']) && is_callable($field['args'])) {
            $cb_args = call_user_func_array($field['args'], array('args' => $args, 'db_value' => $db_value));
        }
        $output .= call_user_func_array($field['cb'], array('args' => $args, 'db_value' => $db_value, 'cb_args' => $cb_args));
        $output .= $desc;
    } elseif ($type == 'file') {
        $output .= _kc_field_file(array('parent' => $mode === 'post' || $mode === 'menu_item' ? $object_id : 0, 'field' => $field, 'id' => $id, 'name' => $name, 'db_value' => $db_value));
        $output .= "\t{$desc}\n";
    } elseif ($type == 'media') {
        $output .= _kc_field_media(array('parent' => $mode === 'post' || $mode === 'menu_item' ? $object_id : 0, 'field' => $field, 'id' => $id, 'name' => $name, 'db_value' => $db_value));
        $output .= "\t{$desc}\n";
    } elseif ($type == 'multiinput') {
        $field['_id'] = $id;
        $output .= _kc_field_multiinput($name, $db_value, $field);
        $output .= "\t{$desc}\n";
    } else {
        // Attributes
        $field_attr = wp_parse_args($field['attr'], array('name' => $name, 'class' => "kcs-{$type}"));
        if ($type == 'multiselect') {
            $type = 'select';
            $field_attr['multiple'] = 'true';
            $field_attr['name'] .= '[]';
        }
        if ($type == 'checkbox') {
            $field_attr['name'] .= '[]';
        }
        if (!in_array($type, array('checkbox', 'radio'))) {
            $field_attr['id'] = $id;
        }
        if ($mode === 'attachment') {
            $field_attr['id'] = $name;
        }
        if (in_array($type, array_merge($i_text, array('textarea')))) {
            $field_attr['class'] .= ' kcs-input';
            if ($mode == 'menu_item') {
                $field_attr['class'] .= ' widefat';
            }
        }
        $field_args = array('type' => $type, 'attr' => $field_attr, 'current' => $db_value);
        foreach (array('options', 'none', 'editor_settings', 'args') as $key) {
            if (!empty($field[$key])) {
                $field_args[$key] = $field[$key];
            }
        }
        $output .= "\t" . kcForm::field($field_args) . "\n";
        $output .= "\t{$desc}\n";
    }
    # Let user filter the output of the setting field
    if ($mode !== 'subfield') {
        $output = apply_filters('kc_settings_field_after', $output, $section, $field);
    }
    if (isset($args['echo']) && $args['echo'] === true) {
        echo $output;
    } else {
        return $output;
    }
}
Beispiel #2
0
    public static function builder()
    {
        $options = self::$data['options'];
        $url = self::$data['url'];
        if (self::$item_to_edit) {
            $url = add_query_arg('action', 'new', $url);
            $values = self::$item_to_edit;
            $form_class = 'editing';
            $submit_text = __('Save Changes');
        } else {
            $values = self::$data['defaults'];
            $form_class = self::$table->current_action() == 'new' ? '' : 'hidden';
            $submit_text = __('Create Setting', 'kc-settings');
        }
        ?>
		<div class="wrap">
			<?php 
        screen_icon('tools');
        ?>
			<h2>
				<?php 
        _e('KC Settings', 'kc-settings');
        ?>
				<a id="new-kcsb" class="add-new-h2" href="<?php 
        esc_url($url);
        ?>
"><?php 
        _e('Add New');
        ?>
</a>
			</h2>
			<h3><?php 
        _e('Saved Settings', 'kc-settings');
        ?>
</h3>
			<form id="kcsb-table" action="" method="post">
				<?php 
        self::$table->prepare_items();
        self::$table->display();
        ?>
			</form>

			<?php 
        if (self::$item_to_export) {
            ?>
			<h3><?php 
            _e('Export Data', 'kc-settings');
            ?>
 <small class="hide-if-no-js"><a class='kc-sh' data-target='.kcsb-export' href="#"><?php 
            _e('Hide', 'kc-settings');
            ?>
</a></small></h3>
			<div class="kcsb-export">
				<textarea class="widefat" cols="30" rows="20"><?php 
            echo esc_textarea(self::$item_to_export);
            ?>
</textarea>
			</div>
			<?php 
        }
        ?>

			<!-- Start builder -->
			<p class="hide-if-js"><?php 
        _e('To create a setting, please enable javascript in your browser and reload this page.', 'kc-settings');
        ?>
</p>
			<div id="kcsb" class="<?php 
        echo $form_class;
        ?>
 form-table">
				<h3><?php 
        _e('KC Settings Builder', 'kc-settings');
        ?>
</h3>
				<p class="description"><?php 
        _e('Please <a href="#" class="kc-help-trigger">read the guide</a> before creating a setting.', 'kc-settings');
        ?>
</p>

				<form class="kcsb" action="options.php" method="post">
					<?php 
        settings_fields('kcsb');
        ?>
					<h4><?php 
        _e('Main', 'kc-settings');
        ?>
</h4>
					<ul class="general main">
						<li>
							<label for="_kcsb-id" class="ml"><?php 
        _e('ID', 'kc-settings');
        ?>
</label>
							<input id="_kcsb-id" class="mi kcsb-slug kcsb-ids required regular-text" type="text" name="kcsb[id]" value="<?php 
        echo $values['id'];
        ?>
" data-ids="settings" />
						</li>
						<li>
							<label for="_kcsb-status" class="ml"><?php 
        _e('Status');
        ?>
</label>
							<?php 
        echo kcForm::field(array('type' => 'select', 'attr' => array('id' => '_kcsb-status', 'name' => 'kcsb[status]', 'class' => 'mi'), 'options' => $options['status'], 'current' => isset($values['status']) ? $values['status'] : 1, 'none' => false));
        // xss ok
        ?>
						</li>
						<li>
							<label for="_kcsb-type" class="ml"><?php 
        _e('Type');
        ?>
</label>
							<?php 
        echo kcForm::field(array('type' => 'select', 'attr' => array('id' => '_kcsb-type', 'name' => 'kcsb[type]', 'class' => 'hasdep mi', 'data-child' => '.childType'), 'options' => $options['type'], 'current' => $values['type'], 'none' => false));
        // xss ok
        ?>
						</li>
						<li class="childType" data-dep="plugin">
							<label for="_kcsb-prefix" class="ml"><?php 
        _e('Prefix', 'kc-settings');
        ?>
</label>
							<input id="_kcsb-prefix" class="mi kcsb-slug required regular-text" type="text" name="kcsb[prefix]" value="<?php 
        echo esc_attr($values['prefix']);
        ?>
"/>
						</li>
						<li class="childType" data-dep='plugin'>
							<label for="_kcsb-menu_location" class="ml"><?php 
        _e('Menu location', 'kc-settings');
        ?>
</label>
							<?php 
        echo kcForm::field(array('type' => 'select', 'attr' => array('id' => '_kcsb-menu_location', 'name' => 'kcsb[menu_location]', 'class' => 'mi'), 'options' => $options['menu_location'], 'current' => $values['menu_location'], 'none' => false));
        // xss ok
        ?>
						</li>
						<li class="childType" data-dep='plugin'>
							<label for="_kcsb-menu_title" class="ml"><?php 
        _e('Menu title', 'kc-settings');
        ?>
</label>
							<input id="_kcsb-menu_title" class="mi required regular-text" type="text" name="kcsb[menu_title]" value="<?php 
        echo esc_attr($values['menu_title']);
        ?>
"/></li>
						<li class="childType" data-dep="plugin">
							<label for="_kcsb-page_title" class="ml"><?php 
        _e('Page title', 'kc-settings');
        ?>
</label>
							<input id="_kcsb-page_title" class="mi required regular-text" type="text" name="kcsb[page_title]" value="<?php 
        echo esc_attr($values['page_title']);
        ?>
" />
						</li>
						<li class="childType" data-dep='plugin'>
							<label for="_kcsb-display" class="ml"><?php 
        _e('Page mode', 'kc-settings');
        ?>
</label>
							<?php 
        echo kcForm::field(array('type' => 'select', 'attr' => array('id' => '_kcsb-display', 'name' => 'kcsb[display]', 'class' => 'hasdep mi', 'data-child' => '.childDisplay'), 'options' => $options['display'], 'current' => $values['display'], 'none' => false));
        // xss ok
        ?>
						</li>
						<li class="childType" data-dep='post'>
							<label for="_kcsb-post_type" class="ml"><?php 
        _e('Post type', 'kc-settings');
        ?>
</label>
							<?php 
        if (empty($options['post_types'])) {
            ?>
								<p><?php 
            __('No public post type found', 'kc-settings');
            ?>
</p>
								<?php 
        } else {
            echo kcForm::field(array('type' => 'select', 'attr' => array('id' => '_kcsb-post_type', 'name' => 'kcsb[post_type]', 'class' => 'mi'), 'options' => $options['post_types'], 'current' => $values['post_type'], 'none' => false));
            // xss ok
        }
        ?>
						</li>
						<li class="childType" data-dep='term'>
							<label for="_kcsb-taxonomies" class="ml"><?php 
        _e('Taxonomies', 'kc-settings');
        ?>
</label>
							<?php 
        if (empty($options['taxonomies'])) {
            ?>
								<p><?php 
            __('No public taxonomy found', 'kc-settings');
            ?>
</p>
								<?php 
        } else {
            echo kcForm::field(array('type' => 'select', 'attr' => array('id' => '_kcsb-taxonomies', 'name' => 'kcsb[taxonomy]', 'class' => 'mi'), 'options' => $options['taxonomies'], 'current' => $values['taxonomy'], 'none' => false));
            // xss ok
        }
        ?>
						</li>
					</ul>

					<h4><?php 
        _e('Sections', 'kc-settings');
        ?>
</h4>
					<ul class="sections kc-rows">
						<?php 
        $section_num = count($values['sections']);
        $section_idx = 0;
        foreach ($values['sections'] as $section_key => $section) {
            $s_name = "kcsb[sections][{$section_key}]";
            $s_val = $values['sections'][$section_key];
            $s_id = "_kcsb-sections-{$section_key}";
            $s_stat = $section_num === 1 && $section_idx === 0 ? ' open' : '';
            ?>
						<li class="row" data-mode="sections">
							<details<?php 
            echo esc_attr($s_stat);
            ?>
>
								<summary title="<?php 
            _e('Drag to reorder section', 'kc-settings');
            ?>
" class="actions">
								<h5><?php 
            printf(__('Section #%s', 'kc-settings'), '<span class="count">' . ($section_idx + 1) . '</span>');
            ?>
</h5>
								<p>(
									<a class="add" title="<?php 
            _e('Add new section', 'kc-settings');
            ?>
"><?php 
            _e('Add');
            ?>
</a>
									<a class="del" title="<?php 
            _e('Remove this section', 'kc-settings');
            ?>
"><?php 
            _e('Remove');
            ?>
</a>
								)</p>
								</summary>
								<ul class="main">
									<li>
										<label for="<?php 
            echo esc_attr("{$s_id}-id");
            ?>
" class="ml"><?php 
            _e('ID', 'kc-settings');
            ?>
</label>
										<input id="<?php 
            echo esc_attr("{$s_id}-id");
            ?>
" class="mi kcsb-slug kcsb-ids required regular-text" type="text" name="<?php 
            echo esc_attr($s_name);
            ?>
[id]" value="<?php 
            echo esc_attr($s_val['id']);
            ?>
" data-ids="sections" />
									</li>
									<li>
										<label for="<?php 
            echo esc_attr("{$s_id}-title");
            ?>
" class="ml"><?php 
            _e('Title');
            ?>
</label>
										<input id="<?php 
            echo esc_attr("{$s_id}-title");
            ?>
" class="mi required regular-text" type="text" name="<?php 
            echo esc_attr($s_name);
            ?>
[title]" value="<?php 
            echo esc_attr($s_val['title']);
            ?>
" />
									</li>
									<li>
										<label for="<?php 
            echo esc_attr("{$s_id}-desc");
            ?>
" class="ml"><?php 
            _e('Description', 'kc-settings');
            ?>
</label>
										<div class="mi">
											<textarea id="<?php 
            echo esc_attr("{$s_id}-desc");
            ?>
" name="<?php 
            echo esc_attr($s_name);
            ?>
[desc]" cols="25" rows="4"><?php 
            echo esc_textarea($s_val['desc']);
            ?>
</textarea>
											<p class="description"><?php 
            _e('Optional', 'kc-settings');
            ?>
</p>
										</div>
									</li>
									<?php 
            if (empty($options['role'])) {
                ?>
									<li class="childType" data-dep='post'>
										<label class="ml"><?php 
                _e('Roles', 'kc-settings');
                ?>
</label>
										<fieldset class="mi">
											<?php 
                echo kcForm::field(array('type' => 'checkbox', 'attr' => array('name' => "{$s_name}[role][]"), 'options' => $options['role'], 'current' => !empty($s_val['role']) ? $s_val['role'] : array()));
                // xss ok
                ?>
											<p class="description"><?php 
                _e('Check one or more to only show this section for certain user roles (optional).', 'kc-settings');
                ?>
</p>
										</fieldset>
									</li>
									<?php 
            }
            if (!isset($s_val['metabox'])) {
                $s_val['metabox'] = array('context' => 'normal', 'priority' => 'default');
            }
            ?>
									<li class="childType childDisplay" data-dep='["post", "metabox"]'>
										<label class="ml"><?php 
            _e('Metabox', 'kc-settings');
            ?>
</label>
										<ul class="mi main">
											<?php 
            foreach ($options['metabox'] as $mb_prop => $prop) {
                ?>
											<li class="kcsb-sub">
												<label for="<?php 
                echo "{$s_id}-metabox-{$mb_prop}";
                ?>
"><?php 
                echo $prop['label'];
                ?>
</label>
												<?php 
                echo kcForm::field(array('type' => 'select', 'attr' => array('id' => "{$s_id}-metabox-{$mb_prop}", 'name' => "{$s_name}[metabox][{$mb_prop}]", 'required' => 'required'), 'options' => $prop['options'], 'current' => $s_val['metabox'][$mb_prop], 'none' => false));
                // xss ok
                ?>
											</li>
											<?php 
            }
            ?>
										</ul>
									</li>
								</ul>
								<h5><?php 
            _e('Fields', 'kc-settings');
            ?>
</h5>
								<?php 
            self::_fields($section['fields'], $s_val, $s_name, $s_id);
            ?>
							</details>
						</li>
						<?php 
            $section_idx++;
        }
        ?>
					</ul>
					<div class="submit">
						<?php 
        submit_button($submit_text, 'primary', 'submit', false);
        ?>
						<a href="#" class="button alignright kcsb-cancel"><?php 
        _e('Cancel');
        ?>
</a>
					</div>
				</form>
			</div>
			<!-- End builder -->
		</div>
	<?php 
    }