public function testRegisterFunctions() { $arr_return = simple_fields_register_field_group("my_new_field_group", array('name' => 'Test field group', 'description' => "Test field description", 'repeatable' => 1, 'fields' => array(array('name' => 'A new text field', 'description' => 'Enter some text in my new text field', 'type' => 'text', 'slug' => "my_new_textfield")))); $expected_return = array('id' => 4, 'key' => 'my_new_field_group', 'slug' => 'my_new_field_group', 'name' => 'Test field group', 'description' => 'Test field description', 'repeatable' => 1, 'fields' => array(0 => array('name' => 'A new text field', 'slug' => 'my_new_textfield', 'description' => 'Enter some text in my new text field', 'type' => 'text', 'type_post_options' => array('enabled_post_types' => array(), 'additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => 0, 'deleted' => 0, "field_group" => array("id" => 4, "name" => "Test field group", "slug" => "my_new_field_group", "description" => "Test field description", "repeatable" => 1, "fields_count" => 1))), 'deleted' => false, "fields_count" => 1); #echo "xxx";print_r($arr_return); $this->assertEquals($expected_return, $arr_return); // generate arr with all field types $arr_field_types = array(); $field_types = explode(",", "text,textarea,checkbox,radiobutton,dropdown,file,post,taxonomy,taxonomyterm,color,date,user"); foreach ($field_types as $field_type) { $arr_field_types[] = array('name' => "A new field of type {$field_type}", 'description' => "Description for field of type {$field_type}", 'type' => $field_type, 'slug' => "slug_fieldtype_{$field_type}"); } $arr_return = simple_fields_register_field_group("my_new_field_group_all_fields", array('name' => 'Test field group with all fields', 'description' => "Test field description", 'repeatable' => 1, 'fields' => $arr_field_types)); // something like this anyway. we can check keys by it anyway $expected_return = array('id' => 3, 'key' => 'my_new_field_group_all_fields', 'slug' => 'my_new_field_group_all_fields', 'name' => 'Test field group with all fields', 'description' => 'Test field description', 'repeatable' => 1, 'fields' => array(0 => array('name' => 'A new field of type text', 'slug' => 'slug_fieldtype_text', 'description' => 'Description for field of type text', 'type' => 'text', 'type_post_options' => array('enabled_post_types' => array(), 'additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => 0, 'deleted' => 0, "field_group" => array())), 'deleted' => false, "fields_count" => 1); $this->assertEquals(array_keys($expected_return), array_keys($arr_return)); // @todo: add test of values here also foreach ($arr_return["fields"] as $arr_one_field) { $this->assertEquals(array_keys($expected_return["fields"][0]), array_keys($arr_one_field)); } /* simple_fields_register_post_connector($unique_name = "", $new_post_connector = array()) simple_fields_register_post_type_default($connector_id_or_special_type = "", $post_type = "post") */ // test manually added fields again to make sure nothing broke // does this work btw? $this->testManuallyAddedFields(); /* left to write tests for: simple_fields_query_posts function simple_fields_set_value($post_id, $field_slug, $new_numInSet = null, $new_post_connector = null, $new_value) { get_field_group($group_id) get_field_in_group($field_group, $field_id) Extension API */ }
public function testRegisterFunctions() { $arr_return = simple_fields_register_field_group("my_new_field_group", array('name' => 'Test field group', 'description' => "Test field description", 'repeatable' => 1, 'fields' => array(array('name' => 'A new text field', 'description' => 'Enter some text in my new text field', 'type' => 'text', 'slug' => "my_new_textfield")))); $expected_return = array('id' => 1, 'key' => 'my_new_field_group', 'slug' => 'my_new_field_group', 'name' => 'Test field group', 'description' => 'Test field description', 'repeatable' => 1, 'fields' => array(0 => array('name' => 'A new text field', 'slug' => 'my_new_textfield', 'description' => 'Enter some text in my new text field', 'type' => 'text', 'type_post_options' => array('enabled_post_types' => array(), 'additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'type_text_options' => array(), 'id' => 0, 'deleted' => 0, "options" => array("text" => array()), "field_group" => array("id" => 1, "name" => "Test field group", "slug" => "my_new_field_group", "description" => "Test field description", "repeatable" => 1, "fields_count" => 1))), 'deleted' => false, "fields_count" => 1, "added_with_code" => true); // check that all keys in expected_return and it's values exist in arr_return foreach ($expected_return as $expected_return_key => $expected_return_value) { $this->assertArrayHasKey($expected_return_key, $arr_return); $this->assertEquals($expected_return_value, $arr_return[$expected_return_key]); } foreach ($expected_return["fields"][0] as $expected_return_key => $expected_return_value) { $this->assertArrayHasKey($expected_return_key, $arr_return["fields"][0]); } // generate arr with all field types $arr_field_types = array(); $field_types = explode(",", "text,textarea,checkbox,radiobutton,dropdown,file,post,taxonomy,taxonomyterm,color,date,user"); foreach ($field_types as $field_type) { $arr_field_types[] = array('name' => "A new field of type {$field_type}", 'description' => "Description for field of type {$field_type}", 'type' => $field_type, 'slug' => "slug_fieldtype_{$field_type}"); } $arr_return = simple_fields_register_field_group("my_new_field_group_all_fields", array('name' => 'Test field group with all fields', 'description' => "Test field description", 'repeatable' => 1, 'fields' => $arr_field_types)); // something like this anyway. we can check keys by it anyway $expected_return = array('id' => 2, 'key' => 'my_new_field_group_all_fields', 'slug' => 'my_new_field_group_all_fields', 'name' => 'Test field group with all fields', 'description' => 'Test field description', 'repeatable' => 1, 'fields' => array(0 => array('name' => 'A new field of type text', 'slug' => 'slug_fieldtype_text', 'description' => 'Description for field of type text', 'type' => 'text', 'type_post_options' => array('enabled_post_types' => array(), 'additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => 0, 'deleted' => 0, "field_group" => array(), "options" => array())), 'deleted' => false, "fields_count" => 1, "added_with_code" => true, "gui_view" => "list"); unset($arr_return["fields_by_slug"]); ksort($expected_return); ksort($arr_return); #print_r(array_keys($arr_return)); #print_r(array_keys($expected_return)); $this->assertEquals(array_keys($expected_return), array_keys($arr_return)); // @todo: add test of values here also foreach ($arr_return["fields"] as $arr_one_field) { // cheating a bit, because laziness unset($arr_one_field["type_text_options"], $arr_one_field["type_textarea_options"], $arr_one_field["type_checkbox_options"], $arr_one_field["type_dropdown_options"], $arr_one_field["type_file_options"], $arr_one_field["type_taxonomy_options"], $arr_one_field["type_color_options"], $arr_one_field["type_date_options"], $arr_one_field["type_user_options"]); ksort($expected_return["fields"][0]); ksort($arr_one_field); $this->assertEquals(array_keys($expected_return["fields"][0]), array_keys($arr_one_field)); } /* simple_fields_register_post_connector($unique_name = "", $new_post_connector = array()) simple_fields_register_post_type_default($connector_id_or_special_type = "", $post_type = "post") */ // Test post connectors $connector_return1 = simple_fields_register_post_connector('test_connector', array('name' => "A test connector", 'field_groups' => array(array('slug' => 'my_new_field_group')), 'post_types' => array('post', "page"))); $connector_return2 = simple_fields_register_post_connector('another_connector', array('name' => "Another connector", 'field_groups' => array(array('slug' => 'my_new_field_group_all_fields'), array('slug' => 'my_new_field_group')), 'post_types' => array('post', "page"))); $connector_return1_expected = array('id' => 1, 'key' => 'test_connector', 'slug' => 'test_connector', 'name' => 'A test connector', 'field_groups' => array(1 => array('id' => 1, 'slug' => 'my_new_field_group', 'key' => 'my_new_field_group', 'name' => 'Test field group', 'deleted' => 0, 'context' => 'normal', 'priority' => 'low')), 'post_types' => array(0 => 'post', 1 => 'page'), 'deleted' => false, 'hide_editor' => false, 'field_groups_count' => 1, "added_with_code" => true); $this->assertEquals($connector_return1_expected, $connector_return1); $connector_return2_expected = array('id' => 2, 'key' => 'another_connector', 'slug' => 'another_connector', 'name' => 'Another connector', 'field_groups' => array(2 => array('id' => 2, 'slug' => 'my_new_field_group_all_fields', 'key' => 'my_new_field_group_all_fields', 'name' => 'Test field group with all fields', 'deleted' => 0, 'context' => 'normal', 'priority' => 'low'), 1 => array('id' => 1, 'slug' => 'my_new_field_group', 'key' => 'my_new_field_group', 'name' => 'Test field group', 'deleted' => 0, 'context' => 'normal', 'priority' => 'low')), 'post_types' => array(0 => 'post', 1 => 'page'), 'deleted' => false, 'hide_editor' => false, 'field_groups_count' => 2, "added_with_code" => true); $this->assertEquals($connector_return2_expected, $connector_return2); // test manually added fields again to make sure nothing broke // does this work btw? // $this->testManuallyAddedFields(); // Some more texts with addings fields // Added 14 jan 2013 $new_field_group_fields = array('name' => 'Attachments', 'description' => "Add some attachments to this post", 'repeatable' => 1, 'fields' => array(array('slug' => "attachment_file", 'name' => 'A file', 'description' => 'Select a file, for example an image', 'type' => 'file', "type_file_options" => array("enable_extended_return_values" => 1)))); $added_field_group = simple_fields_register_field_group('attachments', $new_field_group_fields); // check most important things //$this->assertEquals( array_keys($expected_return), array_keys($arr_return) ); $this->assertArrayHasKey("slug", $added_field_group); foreach ($new_field_group_fields as $field_key => $field_val) { $this->assertArrayHasKey($field_key, $added_field_group); } foreach ($new_field_group_fields["fields"][0] as $field_key => $field_val) { $this->assertArrayHasKey($field_key, $added_field_group["fields"][0]); } // Change some small things, like adding another fields after the first $new_field_group_fields_modified1 = $new_field_group_fields; $new_field_group_fields_modified1["name"] = "Attachments changed text"; $new_field_group_fields_modified1["description"] = "Attachments changed description"; $new_field_group_fields_modified1["fields"][] = array('slug' => "attachment_description", 'name' => 'A description', 'description' => 'bla bla bla', 'type' => 'text', "type_file_options" => array("enable_extended_return_values" => 1)); $added_field_group_after_modified1 = simple_fields_register_field_group('attachments', $new_field_group_fields_modified1); foreach ($expected_return as $field_key => $field_val) { $this->assertArrayHasKey($field_key, $added_field_group_after_modified1); } $this->assertCount(2, $added_field_group_after_modified1["fields"]); $this->assertEquals($added_field_group_after_modified1["fields"][0]["slug"], "attachment_file"); $this->assertEquals($added_field_group_after_modified1["fields"][1]["slug"], "attachment_description"); // Update an existing field with, with as little code as possible $added_field_group_after_updated_field_with_little_code = simple_fields_register_field_group('attachments', array('key' => 'attachments', 'fields' => array(array('slug' => "attachment_file", 'name' => 'A file, updated', 'description' => 'Select a file, for example an image, updated')))); $this->assertEquals($added_field_group_after_updated_field_with_little_code["fields"][0]["slug"], "attachment_file"); $this->assertEquals($added_field_group_after_updated_field_with_little_code["fields"][1]["slug"], "attachment_description"); // Update an existing field with, with as little code as possible // after this attachment_description should be the first key in fields $added_field_group_after_updated_field_with_little_code = simple_fields_register_field_group('attachments', array('key' => 'attachments', 'fields' => array(array('slug' => "attachment_description")))); $this->assertEquals($added_field_group_after_updated_field_with_little_code["fields"][0]["slug"], "attachment_file"); $this->assertEquals($added_field_group_after_updated_field_with_little_code["fields"][1]["slug"], "attachment_description"); $this->assertEquals(array(1, 0), array_keys($added_field_group_after_updated_field_with_little_code["fields"])); /* left to write tests for: simple_fields_query_posts function simple_fields_set_value($post_id, $field_slug, $new_numInSet = null, $new_post_connector = null, $new_value) { get_field_group($group_id) get_field_in_group($field_group, $field_id) Extension API */ }
/** * 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; ?> &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; ?> &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; ?> &action=delete-post-connector&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; ?> &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; ?> &action=delete-field-group&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 . "&action=edit-field-group&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 . "&action=edit-field-group&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; ?> &action=edit-post-connector&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; ?> &action=edit-post-connector&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; ?> &action=edit-post-type-defaults&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; ?> &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; ?> &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 }
</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; $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"]; } }