<?php

add_action("plugins_loaded", "init_simple_fields_field_minimalexample");
function init_simple_fields_field_minimalexample()
{
    class simple_fields_field_minimalexample extends simple_fields_field
    {
        public $key = "minimalexample", $name = "Minimalistic example field";
        function __construct()
        {
            parent::__construct();
        }
        function options_output($existing_vals)
        {
            return sprintf('
				<p>
					<label>Default name</label>
					<span class="description">Enter a name that will be shown as default</span>
					<input type="text" name="%1$s" value="%2$s">
				</p>', $this->get_options_name("textDefaultName"), isset($existing_vals["textDefaultName"]) ? esc_attr($existing_vals["textDefaultName"]) : "");
        }
        function edit_output($saved_values, $options)
        {
            return sprintf('<input type="text" name="%1$s" value="%2$s">', $this->get_options_name("name"), empty($saved_values["name"]) ? esc_attr($options["textDefaultName"]) : esc_attr($saved_values["name"]));
        }
    }
    simple_fields::register_field_type("simple_fields_field_minimalexample");
}
    }
    /**
     * Filters content and keeps only allowable HTML elements.
     *
     * This is the same function as built into WP, but with optional allowing of keeping "&"
     *
     * @param string $string Content to filter through kses
     * @param array $allowed_html List of allowed HTML elements
     * @param array $allowed_protocols Optional. Allowed protocol in links.
     * @return string Filtered content with only allowed HTML elements
     */
    function wp_kses($string, $allowed_html, $allowed_protocols = array(), $skip_normalize_entities = false)
    {
        if (empty($allowed_protocols)) {
            $allowed_protocols = wp_allowed_protocols();
        }
        $string = wp_kses_no_null($string);
        $string = wp_kses_js_entities($string);
        if (!$skip_normalize_entities) {
            $string = wp_kses_normalize_entities($string);
        }
        $string = wp_kses_hook($string, $allowed_html, $allowed_protocols);
        // WP changed the order of these funcs and added args to wp_kses_hook
        return wp_kses_split($string, $allowed_html, $allowed_protocols);
    }
}
// end class
// Boot it up!
global $sf;
$sf = new simple_fields();
$sf->init();
         * Add Extended Return Values and then return the values
         */
        function return_values($values = null, $parsed_options_for_this_field = null)
        {
            // @todo: what if no value?
            $arr_extended = array();
            foreach ($values as $key => $one_value) {
                $one_value_unix = strtotime($one_value);
                // Detect full date or just time
                if (preg_match('!^\\d{4}-\\d{2}-\\d{2}$!', $one_value)) {
                    // echo "date"; [saved_date_time] => 1970-01-01
                    $arr_extended = array("type" => "date", "date_unixtime" => $one_value_unix, "ISO_8601" => date("Y-m-d", $one_value_unix), "RFC_2822" => date("r", $one_value_unix), "Y-m-d" => date("Y-m-d", $one_value_unix), "date_format" => date_i18n(get_option('date_format'), $one_value_unix));
                } else {
                    if (preg_match('!^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}$!', $one_value)) {
                        // [saved_date_time] => 1970-01-01 01:00
                        $arr_extended = array("type" => "datetime", "date_unixtime" => $one_value_unix, "ISO_8601" => date("Y-m-d H:i", $one_value_unix), "RFC_2822" => date("r", $one_value_unix), "Y-m-d" => date("Y-m-d", $one_value_unix), "Y-m-d H:i" => date("Y-m-d H:i", $one_value_unix), "date_format" => date_i18n(get_option('date_format'), $one_value_unix), "date_time_format" => date_i18n(get_option('date_format') . " " . get_option('time_format'), $one_value_unix));
                    } else {
                        if (preg_match('!^\\d{2}:\\d{2}$!', $one_value)) {
                            // [saved_date_time] => 01:00
                            $arr_extended = array("type" => "time", "ISO_8601" => date("H:i", $one_value_unix), "time_format" => date_i18n(get_option('time_format'), $one_value_unix));
                        }
                    }
                }
                $values[$key] = $arr_extended;
            }
            return $values;
        }
    }
    // end class
    simple_fields::register_field_type("simple_fields_field_date_v2");
}
            // Generate the src for a static map for each map, for each image size
            // https://developers.google.com/maps/documentation/staticmaps/
            $static_map_base = "http://maps.googleapis.com/maps/api/staticmap?sensor=false";
            foreach ($values as $key => $val) {
                $arr_static_maps = array();
                foreach ($arr_sizes as $size_key => $size_vals) {
                    $markers = "";
                    if ($options["static_maps_marker_show"]) {
                        $markers = "";
                        $markers .= "size:" . $options["static_maps_marker_size"];
                        $markers .= "|";
                        $markers .= "color:" . $options["static_maps_marker_color"];
                        $markers .= "|";
                        $markers .= $val["lat"] . "," . $val["lng"];
                    }
                    $static_map = add_query_arg(array("center" => $val["lat"] . "," . $val["lng"], "zoom" => $options["static_maps_zoom"], "size" => $size_vals["width"] . "x" . $size_vals["height"], "scale" => $options["static_maps_scale"], "maptype" => $options["static_maps_maptype"], "markers" => $markers), $static_map_base);
                    $arr_static_maps[$size_key] = $static_map;
                    // echo "<p>$size_key:<br><img src='$static_map'></p>";
                }
                $values[$key]["static_maps"] = $arr_static_maps;
                // If address_components exists, decode the json to array
                if (isset($values[$key]["address_components"]) && $values[$key]["address_components"]) {
                    $values[$key]["address_components"] = json_decode($values[$key]["address_components"]);
                }
            }
            return $values;
        }
    }
    // class
    simple_fields::register_field_type("simple_fields_field_googlemaps");
}
    /**
     * Output all stuff for the options page
     * Should be modularized a bit, it's way to long/big right now
     */
    function options_page()
    {
        global $sf;
        $field_groups = $this->get_field_groups();
        $post_connectors = $this->get_post_connectors();
        // for debug purposes, here we can reset the option
        #$field_groups = array(); update_option("simple_fields_groups", $field_groups);
        #$post_connectors = array(); update_option("simple_fields_post_connectors", $post_connectors);
        // sort them by name
        function simple_fields_uasort($a, $b)
        {
            if ($a["name"] == $b["name"]) {
                return 0;
            }
            return strcasecmp($a["name"], $b["name"]);
        }
        uasort($field_groups, "simple_fields_uasort");
        uasort($post_connectors, "simple_fields_uasort");
        ?>
		<div class="wrap">
	
			<h2><?php 
        echo SIMPLE_FIELDS_NAME;
        ?>
</h2>
	
			<div class="clear"></div>
			
			<!-- 
			<div class="simple-fields-bonny-plugins-inner-sidebar">
				<h3>Keep this plugin alive</h3>
				<p>
					I develop this plugin mostly on my spare time. Please consider <a href="http://eskapism.se/sida/donate/">donating</a>
					or <a href="https://flattr.com/thing/116510/Simple-Fields">Flattr</a>
					to keep the development going.
				</p>
	
				<h3>Support</h3>
				<p>If you have any problems with this plugins please check out the <a href="http://wordpress.org/tags/simple-fields?forum_id=10">support forum</a>.</p>
				<p>You can <a href="https://github.com/bonny/WordPress-Simple-Fields">follow the development of this plugin at GitHub</a>.</p>
										
			</div>
			-->
	
		<div class="simple-fields-settings-wrap">
	
			<?php 
        $action = isset($_GET["action"]) ? $_GET["action"] : null;
        /**
         * save post type defaults
         */
        if ("edit-post-type-defaults-save" == $action) {
            $post_type = $_POST["simple_fields_save-post_type"];
            $post_type_connector = $_POST["simple_fields_save-post_type_connector"];
            simple_fields_register_post_type_default($post_type_connector, $post_type);
            $simple_fields_did_save_post_type_defaults = true;
            $action = "";
        }
        /**
         * edit post type defaults
         */
        if ("edit-post-type-defaults" == $action) {
            $post_type = $_GET["post-type"];
            global $wp_post_types;
            if (isset($wp_post_types[$post_type])) {
                $selected_post_type = $wp_post_types[$post_type];
                ?>
					<h3><?php 
                echo __(sprintf('Edit default post connector for post type %1$s', $selected_post_type->label), "simple-fields");
                ?>
</h3>
					
					<form action="<?php 
                echo SIMPLE_FIELDS_FILE;
                ?>
&amp;action=edit-post-type-defaults-save" method="post">
						<table class="form-table">
							<tr>
								<th><?php 
                _e('Default post connector', 'simple-fields');
                ?>
</th>
								<td>
									<?php 
                $arr_post_connectors = $this->get_post_connectors_for_post_type($post_type);
                if ($arr_post_connectors) {
                    $selected_post_type_default = $this->get_default_connector_for_post_type($post_type);
                    ?>
										<select name="simple_fields_save-post_type_connector">
											<option <?php 
                    echo $selected_post_type_default === "__none__" ? " selected='selected' " : "";
                    ?>
 value="__none__"><?php 
                    _e('No post connector', 'simple-fields');
                    ?>
</option>
											<option <?php 
                    echo $selected_post_type_default === "__inherit__" ? " selected='selected' " : "";
                    ?>
 value="__inherit__"><?php 
                    _e('Inherit from parent post', 'simple-fields');
                    ?>
</option>
											<?php 
                    foreach ($arr_post_connectors as $one_post_connector) {
                        echo "<option " . (intval($selected_post_type_default) == intval($one_post_connector["id"]) ? " selected='selected' " : "") . "value='{$one_post_connector["id"]}'>" . $one_post_connector["name"] . "</option>";
                    }
                    ?>
										</select>
										<?php 
                } else {
                    ?>
<p><?php 
                    _e('There are no post connectors for this post type.', 'simple-fields');
                    ?>
</p><?php 
                }
                ?>
								</td>
							</tr>
						</table>
						<p class="submit">
							<input class="button-primary" type="submit" value="<?php 
                _e("Save changes", "simple-fields");
                ?>
" />
							<input type="hidden" name="simple_fields_save-post_type" value="<?php 
                echo $post_type;
                ?>
" />
							<?php 
                _e('or', 'simple_fields');
                ?>
							<a href="<?php 
                echo SIMPLE_FIELDS_FILE;
                ?>
"><?php 
                _e('cancel', 'simple-fields');
                ?>
</a>
						</p>
					</form>
					<?php 
            }
        }
        /**
         * Delete a field group
         */
        if ("delete-field-group" == $action) {
            $field_group_id = (int) $_GET["group-id"];
            $field_groups[$field_group_id]["deleted"] = true;
            update_option("simple_fields_groups", $field_groups);
            $this->clear_caches();
            $simple_fields_did_delete = true;
            $action = "";
        }
        /**
         * Delete a post connector
         */
        if ("delete-post-connector" == $action) {
            $post_connector_id = (int) $_GET["connector-id"];
            $post_connectors[$post_connector_id]["deleted"] = 1;
            update_option("simple_fields_post_connectors", $post_connectors);
            $this->clear_caches();
            $simple_fields_did_delete_post_connector = true;
            $action = "";
        }
        /**
         * save a field group
         * including fields
         */
        if ("edit-field-group-save" == $action) {
            if ($_POST) {
                #sf_d($_POST);
                $field_group_id = (int) $_POST["field_group_id"];
                $field_groups[$field_group_id]["name"] = stripslashes($_POST["field_group_name"]);
                $field_groups[$field_group_id]["description"] = stripslashes($_POST["field_group_description"]);
                $field_groups[$field_group_id]["slug"] = stripslashes($_POST["field_group_slug"]);
                $field_groups[$field_group_id]["repeatable"] = (bool) isset($_POST["field_group_repeatable"]);
                $field_groups[$field_group_id]["fields"] = (array) stripslashes_deep($_POST["field"]);
                // Since 0.6 we really want all things to have slugs, so add one if it's not set
                if (empty($field_groups[$field_group_id]["slug"])) {
                    $field_groups[$field_group_id]["slug"] = "field_group_" . $field_group_id;
                }
                /*
                if just one empty array like this, unset first elm
                happens if no fields have been added (now why would you do such an evil thing?!)
                */
                if (sizeof($field_groups[$field_group_id]["fields"]) == 1 && empty($field_groups[$field_group_id]["fields"][0])) {
                    unset($field_groups[$field_group_id]["fields"][0]);
                }
                update_option("simple_fields_groups", $field_groups);
                $this->clear_caches();
                // we can have changed the options of a field group, so update connectors using this field group
                $post_connectors = (array) $this->get_post_connectors();
                foreach ($post_connectors as $connector_id => $connector_options) {
                    if (isset($connector_options["field_groups"][$field_group_id])) {
                        // field group existed, update name
                        $post_connectors[$connector_id]["field_groups"][$field_group_id]["name"] = stripslashes($_POST["field_group_name"]);
                    }
                }
                update_option("simple_fields_post_connectors", $post_connectors);
                $this->clear_caches();
                $simple_fields_did_save = true;
            }
            $action = "";
        }
        /**
         * save a post connector
         */
        if ("edit-post-connector-save" == $action) {
            if ($_POST) {
                $connector_id = (int) $_POST["post_connector_id"];
                $post_connectors[$connector_id]["name"] = (string) stripslashes($_POST["post_connector_name"]);
                $post_connectors[$connector_id]["slug"] = (string) $_POST["post_connector_slug"];
                $post_connectors[$connector_id]["field_groups"] = (array) @$_POST["added_fields"];
                $post_connectors[$connector_id]["post_types"] = (array) @$_POST["post_types"];
                $post_connectors[$connector_id]["hide_editor"] = (bool) @$_POST["hide_editor"];
                // a post type can only have one default connector, so make sure only the connector
                // that we are saving now has it; remove it from all others;
                /*
                $post_types_type_default = (array) $_POST["post_types_type_default"];
                foreach ($post_types_type_default as $one_default_post_type) {
                	foreach ($post_connectors as $one_post_connector) {
                		if (in_array($one_default_post_type, $one_post_connector["post_types_type_default"])) {
                			$array_key = array_search($one_default_post_type, $one_post_connector["post_types_type_default"]);
                			if ($array_key !== false) {
                				unset($post_connectors[$one_post_connector["id"]]["post_types_type_default"][$array_key]);
                			}
                		}
                	}
                }
                $post_connectors[$connector_id]["post_types_type_default"] = $post_types_type_default;
                */
                // for some reason I got an empty connector (array key was empty) so check for these and remove
                $post_connectors_tmp = array();
                foreach ($post_connectors as $key => $one_connector) {
                    if (!empty($one_connector)) {
                        $post_connectors_tmp[$key] = $one_connector;
                    }
                }
                $post_connectors = $post_connectors_tmp;
                update_option("simple_fields_post_connectors", $post_connectors);
                $this->clear_caches();
                $simple_fields_did_save_connector = true;
            }
            $action = "";
        }
        /**
         * edit new or existing post connector
         * If new then connector-id = 0
         */
        if ("edit-post-connector" == $action) {
            $connector_id = isset($_GET["connector-id"]) ? intval($_GET["connector-id"]) : false;
            $highest_connector_id = 0;
            // if new, save it as unnamed, and then set to edit that
            if ($connector_id === 0) {
                // is new connector
                $post_connector_in_edit = simple_fields_register_post_connector();
            } else {
                // existing post connector
                // set a default value for hide_editor if it does not exist. did not exist until 0.5
                $post_connectors[$connector_id]["hide_editor"] = (bool) @$post_connectors[$connector_id]["hide_editor"];
                $post_connector_in_edit = $post_connectors[$connector_id];
            }
            ?>
				<h3><?php 
            _e('Post Connector details', 'simple-fields');
            ?>
</h3>
	
				<form method="post" action="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
&amp;action=edit-post-connector-save">
	
					<table class="form-table">

						<tr>
							<th><label><?php 
            _e('Name', 'simple-fields');
            ?>
</label></th>
							<td><input type="text" id="post_connector_name" name="post_connector_name" class="regular-text" value="<?php 
            echo esc_html($post_connector_in_edit["name"]);
            ?>
" /></td>
						</tr>

						<tr>
							<th>
								<label for="post_connector_slug"><?php 
            _e('Slug', 'simple-fields');
            ?>
</label>
							</th>
							<td>
								<input 	type="text" name="post_connector_slug" id="post_connector_slug" class="regular-text" 
										value="<?php 
            echo esc_html(@$post_connector_in_edit["slug"]);
            ?>
"
										pattern='<?php 
            echo $this->get_slug_pattern();
            ?>
'
										title='<?php 
            echo $this->get_slug_title();
            ?>
'
										required
										 />
								 <br>
								 <span class="description"><?php 
            echo __("A unique identifier for this connector", 'simple-fields');
            ?>
</span>
								 <?php 
            // sf_d($post_connector_in_edit);
            ?>
							</td>
						</tr>

						<tr>
							<th><?php 
            _e('Field Groups', 'simple-fields');
            ?>
</th>
							<td>
								<p>
									<select id="simple-fields-post-connector-add-fields">
										<option value=""><?php 
            _e('Add field group...', 'simple-fields');
            ?>
</option>
										<?php 
            foreach ($field_groups as $one_field_group) {
                if ($one_field_group["deleted"]) {
                    continue;
                }
                ?>
<option value='<?php 
                echo $one_field_group["id"];
                ?>
'><?php 
                echo esc_html($one_field_group["name"]);
                ?>
</option><?php 
            }
            ?>
									</select>
								</p>
								<ul id="simple-fields-post-connector-added-fields">
									<?php 
            foreach ($post_connector_in_edit["field_groups"] as $one_post_connector_added_field) {
                if ($one_post_connector_added_field["deleted"]) {
                    continue;
                }
                #d($one_post_connector_added_field);
                ?>
										<li>
											<div class='simple-fields-post-connector-addded-fields-handle'></div>
											<div class='simple-fields-post-connector-addded-fields-field-name'><?php 
                echo $one_post_connector_added_field["name"];
                ?>
</div>
											<input type='hidden' name='added_fields[<?php 
                echo $one_post_connector_added_field["id"];
                ?>
][id]' value='<?php 
                echo $one_post_connector_added_field["id"];
                ?>
' />
											<input type='hidden' name='added_fields[<?php 
                echo $one_post_connector_added_field["id"];
                ?>
][name]' value='<?php 
                echo $one_post_connector_added_field["name"];
                ?>
' />
											<input type='hidden' name='added_fields[<?php 
                echo $one_post_connector_added_field["id"];
                ?>
][deleted]' value='0' class="simple-fields-post-connector-added-field-deleted" />
											<div class="simple-fields-post-connector-addded-fields-options">
												<?php 
                _e('Context', 'simple-fields');
                ?>
												<select name='added_fields[<?php 
                echo $one_post_connector_added_field["id"];
                ?>
][context]' class="simple-fields-post-connector-addded-fields-option-context">
													<option <?php 
                echo "normal" == $one_post_connector_added_field["context"] ? " selected='selected' " : "";
                ?>
 value="normal"><?php 
                _e('normal');
                ?>
</option>
													<option <?php 
                echo "advanced" == $one_post_connector_added_field["context"] ? " selected='selected' " : "";
                ?>
 value="advanced"><?php 
                _e('advanced');
                ?>
</option>
													<option <?php 
                echo "side" == $one_post_connector_added_field["context"] ? " selected='selected' " : "";
                ?>
 value="side"><?php 
                _e('side');
                ?>
</option>
												</select>
												
												<?php 
                _e('Priority', 'simple-fields');
                ?>
												<select name='added_fields[<?php 
                echo $one_post_connector_added_field["id"];
                ?>
][priority]' class="simple-fields-post-connector-addded-fields-option-priority">
													<option <?php 
                echo "low" == $one_post_connector_added_field["priority"] ? " selected='selected' " : "";
                ?>
 value="low"><?php 
                _e('low');
                ?>
</option>
													<option <?php 
                echo "high" == $one_post_connector_added_field["priority"] ? " selected='selected' " : "";
                ?>
 value="high"><?php 
                _e('high');
                ?>
</option>
												</select>
											</div>
											<a href='#' class='simple-fields-post-connector-addded-fields-delete'><?php 
                _e('Delete', 'simple-fields');
                ?>
</a>
										</li>
										<?php 
            }
            ?>
								</ul>
							</td>
						</tr>
						
						<tr>
							<th><?php 
            _e('Options', 'simple-fields');
            ?>
</th>
							<td><input
								 type="checkbox" 
								 <?php 
            echo $post_connector_in_edit["hide_editor"] == TRUE ? " checked='checked' " : "";
            ?>
								 name="hide_editor" 
								 class="" 
								 value="1" />
								 <?php 
            _e('Hide the built in editor', 'simple-fields');
            ?>
							</td>
						</tr>
						
						<tr>
							<th>
								<?php 
            _e('Available for post types', 'simple-fields');
            ?>
							</th>
							<td>
								<table>
									<?php 
            global $wp_post_types;
            $arr_post_types_to_ignore = array("attachment", "revision", "nav_menu_item");
            foreach ($wp_post_types as $one_post_type) {
                if (!in_array($one_post_type->name, $arr_post_types_to_ignore)) {
                    ?>
											<tr>
												<td>
													<input <?php 
                    echo in_array($one_post_type->name, $post_connector_in_edit["post_types"]) ? " checked='checked' " : "";
                    ?>
 type="checkbox" name="post_types[]" value="<?php 
                    echo $one_post_type->name;
                    ?>
" />
													<?php 
                    echo $one_post_type->name;
                    ?>
												</td>
												<?php 
                    /*
                    <!-- <td>
                    	<input <?php echo (in_array($one_post_type->name, $post_connector_in_edit["post_types_type_default"]) ? " checked='checked' " : "") ?> type="checkbox" name="post_types_type_default[]" value="<?php echo $one_post_type->name ?>" />
                    	Default connector for post type <?php echo $one_post_type->name ?>
                    </td> -->
                    */
                    ?>
											</tr>
											<?php 
                }
            }
            ?>
								</table>
							</td>
						</tr>
	
					</table>
					<p class="submit">
						<input class="button-primary" type="submit" value="<?php 
            _e('Save Changes', 'simple-fields');
            ?>
" />
						<input type="hidden" name="action" value="update" />
						<!-- <input type="hidden" name="page_options" value="field_group_name" /> -->
						<input type="hidden" name="post_connector_id" value="<?php 
            echo $post_connector_in_edit["id"];
            ?>
" />
						or 
						<a href="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
"><?php 
            _e('cancel', 'simple-fields');
            ?>
</a>
					</p>
					<p class="simple-fields-post-connector-delete">
						<a href="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
&amp;action=delete-post-connector&amp;connector-id=<?php 
            echo $post_connector_in_edit["id"];
            ?>
"><?php 
            _e('Delete');
            ?>
</a>
					</p>
	
				</form>
				<?php 
        }
        /**
         * Edit new or existing Field Group
         */
        if ("edit-field-group" == $action) {
            $field_group_id = isset($_GET["group-id"]) ? intval($_GET["group-id"]) : false;
            $highest_field_id = 0;
            // check if field group is new or existing
            if ($field_group_id === 0) {
                // new: save it as unnamed, and then set to edit that
                $field_group_in_edit = simple_fields_register_field_group();
                simple_fields::debug("Added new field group", $field_group_in_edit);
            } else {
                // existing: get highest field id
                foreach ($field_groups[$field_group_id]["fields"] as $one_field) {
                    if ($one_field["id"] > $highest_field_id) {
                        $highest_field_id = $one_field["id"];
                    }
                }
                $field_group_in_edit = $field_groups[$field_group_id];
            }
            ?>
				<form method="post" action="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
&amp;action=edit-field-group-save">
		            <h3><?php 
            _e('Field group details', 'simple-fields');
            ?>
</h3>
		            <table class="form-table">
		            	<tr>
		            		<th>
		            			<label for="field_group_name"><?php 
            _e('Name', 'simple-fields');
            ?>
</label>
		            		</th>
		            		<td>
		            			<input type="text" name="field_group_name" id="field_group_name" class="regular-text" value="<?php 
            echo esc_html($field_group_in_edit["name"]);
            ?>
" required />
							</td>
						</tr>
						<tr>
							<th>
								<label for="field_group_description"><?php 
            _e('Description', 'simple-fields');
            ?>
</label>
							</th>
							<td>
								<input 	type="text" name="field_group_description" id="field_group_description" class="regular-text" 
										value="<?php 
            echo esc_html(@$field_group_in_edit["description"]);
            ?>
"
										 />
							</td>
						</th>

						<tr>
							<th>
								<label for="field_group_slug"><?php 
            _e('Slug', 'simple-fields');
            ?>
</label>
							</th>
							<td>
								<input 	type="text" name="field_group_slug" id="field_group_slug" class="regular-text" 
										value="<?php 
            echo esc_html(@$field_group_in_edit["slug"]);
            ?>
"
										pattern='<?php 
            echo $this->get_slug_pattern();
            ?>
'
										title='<?php 
            echo $this->get_slug_title();
            ?>
'
										required
										title="<?php 
            _e("Allowed chars: a-z and underscore.", 'simple-fields');
            ?>
"
										 />
								 <br>
								 <span class="description"><?php 
            echo __("A unique identifier for this field group.", 'simple-fields');
            ?>
</span>
							</td>
						</tr>

						<tr>
							<th>
								<?php 
            echo __("Options", 'simple-fields');
            ?>
							</th>
							<td>
		            			<label for="field_group_repeatable">
									<input type="checkbox" <?php 
            echo $field_group_in_edit["repeatable"] == true ? "checked='checked'" : "";
            ?>
 value="1" id="field_group_repeatable" name="field_group_repeatable" />
									<?php 
            _e('Repeatable', 'simple-fields');
            ?>
								</label>								
		            		</td>
		            	</tr>
		            	<tr>
		            		<th><?php 
            _e('Fields', 'simple-fields');
            ?>
</th>
		            		<td>
		            			<div id="simple-fields-field-group-existing-fields">
		            				<ul class='simple-fields-edit-field-groups-added-fields'>
										<?php 
            foreach ($field_group_in_edit["fields"] as $oneField) {
                if (!$oneField["deleted"]) {
                    echo $this->field_group_add_field_template($oneField["id"], $field_group_in_edit);
                }
            }
            ?>
		            				</ul>
		            			</div>
		            			<p><a href="#" id="simple-fields-field-group-add-field">+ <?php 
            _e('Add field', 'simple-fields');
            ?>
</a></p>
		            		</td>
		            	</tr>			
					</table>
	
					<p class="submit">
						<input class="button-primary" type="submit" value="<?php 
            _e('Save Changes', 'simple-fields');
            ?>
" />
						<input type="hidden" name="action" value="update" />
						<input type="hidden" name="page_options" value="field_group_name" />
						<input type="hidden" name="field_group_id" value="<?php 
            echo $field_group_in_edit["id"];
            ?>
" />
						<?php 
            _e('or', 'simple-fields');
            ?>
 
						<a href="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
"><?php 
            _e('cancel', 'simple-fields');
            ?>
</a>
					</p>
					<p class="simple-fields-field-group-delete">
						<a href="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
&amp;action=delete-field-group&amp;group-id=<?php 
            echo $field_group_in_edit["id"];
            ?>
"><?php 
            _e('Delete', 'simple-fields');
            ?>
</a>
					</p>
					
				</form>
		
				<script type="text/javascript">
					var simple_fields_highest_field_id = <?php 
            echo (int) $highest_field_id;
            ?>
;
				</script>
		
				<?php 
        }
        // view debug information
        if ("simple-fields-view-debug-info" == $action) {
            echo "<h3>Post Connectors</h3>\n";
            echo "<p>Called with function <code>get_post_connectors()</code>";
            sf_d($this->get_post_connectors());
            echo "<hr>";
            echo "<h3>Field Groups</h3>\n";
            echo "<p>Called with function <code>get_field_groups()</code>";
            sf_d($this->get_field_groups());
            echo "<hr>";
            echo "<h3>simple_fields_post_type_defaults</h3>";
            echo '<p>Called with: get_option("simple_fields_post_type_defaults")';
            sf_d($this->get_post_type_defaults());
        }
        // Save options
        if ("edit-options-save" == $action) {
            $this->save_options(array("debug_type" => (int) $_POST["debug_type"]));
            $action = "";
            $simple_fields_did_save_options = TRUE;
        }
        // overview, if no action
        if (!$action) {
            /**
             * view post connectors
             */
            $post_connector_count = 0;
            foreach ($post_connectors as $onePostConnector) {
                if (!$onePostConnector["deleted"]) {
                    $post_connector_count++;
                }
            }
            /**
             * view existing field groups
             */
            ?>
				<div class="simple-fields-edit-field-groups">
	
					<h3><?php 
            _e('Field groups', 'simple-fields');
            ?>
</h3>
	
					<?php 
            // Show messages, like "saved" and so on
            if (isset($simple_fields_did_save) && $simple_fields_did_save) {
                ?>
<div id="message" class="updated"><p><?php 
                _e('Field group saved', 'simple-fields');
                ?>
</p></div><?php 
            } elseif (isset($simple_fields_did_delete) && $simple_fields_did_delete) {
                ?>
<div id="message" class="updated"><p><?php 
                _e('Field group deleted', 'simple-fields');
                ?>
</p></div><?php 
            } elseif (isset($simple_fields_did_delete_post_connector) && $simple_fields_did_delete_post_connector) {
                ?>
<div id="message" class="updated"><p><?php 
                _e('Post connector deleted', 'simple-fields');
                ?>
</p></div><?php 
            } elseif (isset($simple_fields_did_save_post_type_defaults) && $simple_fields_did_save_post_type_defaults) {
                ?>
<div id="message" class="updated"><p><?php 
                _e('Post type defaults saved', 'simple-fields');
                ?>
</p></div><?php 
            } elseif (isset($simple_fields_did_save_options) && $simple_fields_did_save_options) {
                ?>
<div id="message" class="updated"><p><?php 
                _e('Simple Fields options saved', 'simple-fields');
                ?>
</p></div><?php 
            }
            $field_group_count = 0;
            foreach ($field_groups as $oneFieldGroup) {
                if (!$oneFieldGroup["deleted"]) {
                    $field_group_count++;
                }
            }
            if ($field_groups == $field_group_count) {
                echo "<p>" . __('No field groups yet.', 'simple-fields') . "</p>";
            } else {
                echo "<ul class=''>";
                foreach ($field_groups as $oneFieldGroup) {
                    if ($oneFieldGroup["id"] && !$oneFieldGroup["deleted"]) {
                        echo "<li>";
                        echo "<a href='" . SIMPLE_FIELDS_FILE . "&amp;action=edit-field-group&amp;group-id={$oneFieldGroup['id']}'>{$oneFieldGroup['name']}</a>";
                        if ($oneFieldGroup["fields_count"]) {
                            $format = $oneFieldGroup["repeatable"] ? _n('1 added field, repeatable', '%d added fields, repeatable', $oneFieldGroup["fields_count"]) : _n('1 added field', '%d added fields', $oneFieldGroup["fields_count"]);
                            echo "<br>" . __(sprintf($format, $oneFieldGroup["fields_count"]));
                        }
                        echo "</li>";
                    }
                }
                echo "</ul>";
            }
            echo "<p><a class='button' href='" . SIMPLE_FIELDS_FILE . "&amp;action=edit-field-group&amp;group-id=0'>+ " . __('New field group', 'simple-fields') . "</a></p>";
            ?>
				</div>
			
			
				<div class="simple-fields-edit-post-connectors">
	
					<h3><?php 
            _e('Post Connectors', 'simple-fields');
            ?>
</h3>
	
					<?php 
            if (isset($simple_fields_did_save_connector) && $simple_fields_did_save_connector === true) {
                ?>
<div id="message" class="updated"><p><?php 
                _e('Post connector saved', 'simple-fields');
                ?>
</p></div><?php 
            }
            if ($post_connector_count) {
                ?>
<ul><?php 
                foreach ($post_connectors as $one_post_connector) {
                    if ($one_post_connector["deleted"] || !$one_post_connector["id"]) {
                        continue;
                    }
                    ?>
								<li>
									<a href="<?php 
                    echo SIMPLE_FIELDS_FILE;
                    ?>
&amp;action=edit-post-connector&amp;connector-id=<?php 
                    echo $one_post_connector["id"];
                    ?>
"><?php 
                    echo $one_post_connector["name"];
                    ?>
</a>
									<?php 
                    if ($one_post_connector["field_groups_count"]) {
                        echo "<br>" . sprintf(_n('1 added field group', '%d added field groups', $one_post_connector["field_groups_count"]), $one_post_connector["field_groups_count"]);
                    }
                    ?>
								</li>
								<?php 
                }
                ?>
</ul><?php 
            } else {
                ?>
						<!-- <p>No post connectors</p> -->
						<?php 
            }
            ?>
					<p>
						<a href="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
&amp;action=edit-post-connector&amp;connector-id=0" class="button">+ <?php 
            _e('New post connector', 'simple-fields');
            ?>
</a>
					</p>
					
				</div>
	
				<div class="simple-fields-post-type-defaults">
					<h3><?php 
            _e('Post type defaults', 'simple-fields');
            ?>
</h3>
					<ul>
						<?php 
            $post_types = get_post_types();
            $arr_post_types_to_ignore = array("attachment", "revision", "nav_menu_item");
            foreach ($post_types as $one_post_type) {
                $one_post_type_info = get_post_type_object($one_post_type);
                if (!in_array($one_post_type, $arr_post_types_to_ignore)) {
                    $default_connector = $this->get_default_connector_for_post_type($one_post_type);
                    switch ($default_connector) {
                        case "__none__":
                            $default_connector_str = __('Default is to use <em>no connector</em>', 'simple-fields');
                            break;
                        case "__inherit__":
                            $default_connector_str = __('Default is to inherit from <em>parent connector</em>', 'simple-fields');
                            break;
                        default:
                            if (is_numeric($default_connector)) {
                                $connector = $this->get_connector_by_id($default_connector);
                                if ($connector !== FALSE) {
                                    $default_connector_str = sprintf(__('Default is to use connector <em>%s</em>', 'simple-fields'), $connector["name"]);
                                }
                            }
                    }
                    ?>
<li>
									<a href="<?php 
                    echo SIMPLE_FIELDS_FILE;
                    ?>
&amp;action=edit-post-type-defaults&amp;post-type=<?php 
                    echo $one_post_type;
                    ?>
">
										<?php 
                    echo $one_post_type_info->label;
                    ?>
									</a>
									<br>
									<span><?php 
                    echo $default_connector_str;
                    ?>
</span>
								</li><?php 
                }
            }
            ?>
					</ul>
				</div>	


				<div class="simple-fields-debug">
					<h3><?php 
            echo __('Debug', 'simple-fields');
            ?>
</h3>
					<?php 
            // Dropdown with debug options
            // Debug type. 0 = no debug, 1 = debug for admins only, 2 = debug for all
            $options = $this->get_options();
            $debug_type = isset($options["debug_type"]) ? (int) $options["debug_type"] : "0";
            // capability edit_themes
            ?>
					<form action="<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
&amp;action=edit-options-save" method="post">
						<?php 
            printf('
							<p>
								<select name=debug_type>
									<option value=0 %1$s>%4$s</option>
									<option value=1 %2$s>%5$s</option>
									<option value=2 %3$s>%6$s</option>
								</select>
							</p>
							', $debug_type === 0 ? "selected" : "", $debug_type === 1 ? "selected" : "", $debug_type === 2 ? "selected" : "", __("Don't enable debug output", "simple-fields"), __("Enable debug output for administrators", "simple-fields"), __("Enable debug output for all users", "simple-fields"));
            ?>
						<p class=description>
							<?php 
            _e("Automatically append information about attached fields on posts (using filter 'the_content').", "simple-fields");
            ?>
						</p>

						<p>
							<input class="button-primary" type=submit value="<?php 
            _e("Save changes", "simple-fields");
            ?>
">
						</p>
					</form> <!-- // enable debug -->
				
					<ul>
						<li><a href='<?php 
            echo SIMPLE_FIELDS_FILE;
            ?>
&amp;action=simple-fields-view-debug-info'><?php 
            echo __('View debug information', 'simple-fields');
            ?>
</a></li>
					</ul>

				</div>
				
				<?php 
        }
        // end simple_fields_options
        ?>
			</div>
		</div>	
	
		<?php 
    }
         * This is the output a regular user will see
         */
        function edit_output($saved_values, $options)
        {
            // name tex. date: simple_fields_fieldgroups[3][1][new0]
            // name denna: simple_fields_fieldgroups[3][2][new0][option1]
            // alltså ett steg till = bra för vi kan lagra fler saker med mindre problem. hej hopp.
            $output = "";
            $output .= sprintf('
					<input type="text" name="%1$s" id="%2$s" value="%3$s"><br>
					<input type="text" name="%4$s" id="%5$s" value="%6$s">
				', $this->get_options_name("option1"), $this->get_options_id("option1"), esc_attr(@$saved_values["option1"]), $this->get_options_name("option2"), $this->get_options_id("option2"), esc_attr(@$saved_values["option2"]));
            return $output;
        }
        /**
         * Before the values are returned
         */
        function return_values($values)
        {
            foreach ($values as &$one_field) {
                foreach ($one_field as $one_field_key => &$one_field_value) {
                    if ($one_field_key == "option1") {
                        $one_field_value = $one_field_value . " with some text always appended by class return method";
                    }
                }
            }
            return $values;
        }
    }
    simple_fields::register_field_type("simple_fields_field_example");
}
        function options_output($existing_vals = array())
        {
            return sprintf('
			<div class="simple-fields-field-group-one-field-row-col-first">
				<p>
					<label>%1$s</label>
				</p>
			</div>
			<div class="simple-fields-field-group-one-field-row-col-second">
				<p>
					<select name="%2$s">
						<option %6$s value="line">%4$s</option>
						<option %5$s value="white_space">%3$s</option>
					</select>
				</p>
				<!-- <p>					
					<span class="description">%7$s</span>
				</p>-->
			</div>
				', _x("Appearance", "Divider field type", "simple-fields"), $this->get_options_name("appearance"), _x("White space", "Divider field type", "simple-fields"), _x("Line", "Divider field type", "simple-fields"), isset($existing_vals["appearance"]) && $existing_vals["appearance"] == "white_space" ? " selected " : "", isset($existing_vals["appearance"]) && $existing_vals["appearance"] == "line" ? " selected " : "", _x("Select the look of the divider", "Divider field type", "simple-fields"));
        }
        function edit_output($saved_values, $options)
        {
            $output = sprintf('<div class="%1$s"></div>', $this->get_class_name($options["appearance"]));
            #sf_d($options);
            return $output;
            //return sprintf('<input type="text" name="%1$s" value="%2$s">', $this->get_options_name("name"), empty($saved_values["name"]) ? esc_attr($options["textDefaultName"]) : esc_attr($saved_values["name"]));
        }
    }
    simple_fields::register_field_type("simple_fields_field_divider");
}
}
/**
 * Edit new or existing Field Group
 */
if ("edit-field-group" == $action) {
    $field_group_id = isset($_GET["group-id"]) ? intval($_GET["group-id"]) : false;
    $highest_field_id = 0;
    $is_new_field_group = false;
    // check if field group is new or existing
    if ($field_group_id === 0) {
        // new: save it as unnamed, and then set to edit that
        $is_new_field_group = true;
        $field_group_in_edit = simple_fields_register_field_group("", array("deleted" => true, "added_with_code" => false));
        $field_group_in_edit["name"] = "";
        $field_group_in_edit["slug"] = "";
        simple_fields::debug("Added new field group", $field_group_in_edit);
    } else {
        // check that field group exists
        if (!isset($field_groups[$field_group_id])) {
            wp_die(__("Could not find field group. Perhaps it has been deleted?", "simple-fields"));
        }
        $field_group_in_edit = $field_groups[$field_group_id];
        // existing: get highest field id
        foreach ($field_groups[$field_group_id]["fields"] as $one_field) {
            if ($one_field["id"] > $highest_field_id) {
                $highest_field_id = $one_field["id"];
            }
        }
    }
    ?>
	<form method="post" action="<?php