/** * @deprecated Dismissing support for simple field helper after adoption * of Advanced Custom Field as default custom fileds creation * plug-in */ function simple_fields_metas($post) { $connector = simple_fields_get_all_fields_and_values_for_post($post->ID); $metas = array(); $groups = $connector["field_groups"]; if (!$groups) { return array(); } foreach ($groups as $group) { $metas[$group["name"]] = array(); $fields = $group["fields"]; foreach ($fields as $field) { if (sizeof($field["saved_values"]) == 1) { $metas[$group["name"]][$field["name"]] = $field["saved_values"][0]; } else { $metas[$group["name"]][$field["name"]] = $field["saved_values"]; } } } return $metas; }
/** * Outputs the names of the post connectors attached to the post you view + outputs the values * @param string $the_content * @param bool $allow_always Set to true to bypass checks that we are inside the correct the_content-filter */ function simple_fields_content_debug_output($the_content, $args = "") { $defaults = array("always_show" => FALSE, "show_expanded" => FALSE); $args = wp_parse_args($args, $defaults); // we only want to appen the debug code when being used from get_the_content or the_content // but for example get_the_excerpt is also using filter the_content which leads to problems // so check that we are somewhere inside the right functions if ($args["always_show"] === FALSE) { $is_inside_righ_function = FALSE; $arr_trace = debug_backtrace(); $arr_trace_count = count($arr_trace); for ($i = 0; $i < $arr_trace_count; $i++) { if (isset($arr_trace[$i]["function"]) && in_array($arr_trace[$i]["function"], array("the_content", "get_the_content"))) { $is_inside_righ_function = TRUE; break; } } if (!$is_inside_righ_function) { // Don't do the debug, since we're not in the_content return $the_content; } } $output = ""; $output_all = ""; $field_count = 0; if (!isset($GLOBALS['post'])) { return $the_content; } $post_id = get_the_ID(); $post_connector_with_values = simple_fields_get_all_fields_and_values_for_post($post_id, "include_deleted=0"); if ($post_connector_with_values) { foreach ($post_connector_with_values["field_groups"] as $one_field_group) { if ($one_field_group["deleted"]) { continue; } $output_all .= "<div style='font-weight:bold;margin:1em 0 0 0;'>"; $str_is_repeatable = $one_field_group["repeatable"] ? __(" (Repeatable)", "simple-fields") : ""; $output_all .= sprintf(__('Fieldgroup %1$s %2$s', "simple-fields"), $one_field_group["name"], $str_is_repeatable); $output_all .= "</div>"; $str_all_group_fields = ""; foreach ($one_field_group["fields"] as $one_field) { if ($one_field["deleted"]) { continue; } $field_count++; $content = ""; $content .= "<ul style='background:#eee;padding:.5em;margin:0;display:block;'>"; $content .= "<li>" . __("Field", "simple-fields") . " <b>" . $one_field["name"] . "</b>"; $content .= ", " . __("type", "simple-fields") . " <b>" . $one_field["type"] . "</b>"; if (isset($one_field["slug"])) { $content .= ", slug <b>" . $one_field["slug"] . "</b>"; $str_all_group_fields .= $one_field["slug"] . ","; if ($one_field_group["repeatable"]) { $content .= "<br>Use <code><b>simple_fields_values('" . $one_field["slug"] . "')</b></code>."; /*ob_start(); sf_d( simple_fields_values($one_field["slug"]) ); $content .= ob_get_clean();*/ } else { $content .= "<br>Use <code><b>simple_fields_value('" . $one_field["slug"] . "')</b></code>."; /*ob_start(); sf_d( simple_fields_value($one_field["slug"]) ); $content .= ob_get_clean();*/ } } else { $content .= "<br>" . __("No slug for this field found (probably old field that has not been edited and saved).", "simple-fields"); } $content .= "</ul>"; $output_all .= $content; } // Show example how to get all fields in one shot // But only show if field has more than one field, otherwise it's kinda not useful if (sizeof($one_field_group["fields"]) > 1) { $str_all_group_fields = preg_replace('!,$!', '', $str_all_group_fields); $output_all .= "<ul style='background:#eee;padding:.5em;margin:0;display:block;'>"; if ($one_field_group["repeatable"]) { $content = "<li>Get all fields at once: use <code><b>simple_fields_values('" . $str_all_group_fields . "')</b></code>."; /*ob_start(); sf_d( simple_fields_values($str_all_group_fields) ); $content .= ob_get_clean();*/ } else { $content = "<li>Get all fields at once: use <code><b>simple_fields_value('" . $str_all_group_fields . "')</b></code>."; /*ob_start(); sf_d( simple_fields_value($str_all_group_fields) ); $content .= ob_get_clean();*/ } $output_all .= $content; $output_all .= "</ul>"; } } // for each field group } if ($output_all) { $str_show_fields = __("Show fields.", "simple-fields"); $str_hide_fields = __("Hide fields.", "simple-fields"); ?> <script> window.simple_fields_post_debug_show_hide = window.simple_fields_post_debug_show_hide || function(t) { var div_debug_contents = t.parentNode.parentNode.getElementsByClassName("simple-fields-post-debug-content"), new_style, link_text; if (div_debug_contents.length) { if (div_debug_contents[0].style.display === "block") { new_style = "none"; link_text = "<?php echo $str_show_fields; ?> "; } else { new_style = "block"; link_text = "<?php echo $str_hide_fields; ?> "; } div_debug_contents[0].style.display = new_style; t.innerHTML = link_text; } return false; } </script> <?php $str_show_hide = ""; $display = "block"; if ($args["show_expanded"] === FALSE) { $str_show_hide = '<a href="#" onclick="return simple_fields_post_debug_show_hide(this);">' . $str_show_fields . '</a></p>'; $display = "none"; } $output_all = sprintf(' <div class="simple-fields-post-debug-wrap" style="display:block;margin:0;padding:0;"> <p style="margin:0;padding:0;display:block;">This post has %1$s Simple Fields-fields attached. %2$s <div class="simple-fields-post-debug-content" style="display:%3$s;">%4$s</div> </div> ', $field_count, $str_show_hide, $display, $output_all); } // if a field has the slug caption the output will be [caption] and then it will crash with some shortcodes, so we try to fix that here $output_all = str_replace("[", "[", $output_all); $output_all = str_replace("]", "]", $output_all); return $the_content . $output_all; }
public function testGetAllForPost() { $post_id = 11; $all_vals = simple_fields_get_all_fields_and_values_for_post($post_id); // this test feels a bit to much, should check sub keys-stuff instead of all $vals = array('id' => 1, 'key' => 'post_connector_manually', 'slug' => 'post_connector_manually', 'name' => 'Manually added post connector', 'field_groups' => array(1 => array('id' => 1, 'name' => 'Manually added field group', 'deleted' => false, 'context' => 'normal', 'priority' => 'high', 'key' => 'field_group_manually', 'slug' => 'field_group_manually', 'description' => 'A group that is added manually from within the GUI', 'repeatable' => true, 'fields' => array(1 => array('name' => 'Text field', 'description' => 'A text field', 'slug' => 'field_text', 'type' => 'text', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '1', 'deleted' => '0', 'saved_values' => array(0 => 'Text entered in the text field', 1 => 'text in textfield 2<span>yes it is</span>'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_1_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_1_numInSet_1')), 2 => array('name' => 'Field textarea', 'description' => 'A texteara field', 'slug' => 'field_textarea', 'type' => 'textarea', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '2', 'deleted' => '0', 'saved_values' => array(0 => 'Text entered in the textarea', 1 => 'Textera with more funky text in it. <h2>Headline</h2> <ul> <li>Item 1</li> <li>Item 2</li> </ul> '), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_2_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_2_numInSet_1')), 3 => array('name' => 'Field textarea HTML', 'description' => 'A textarea field with HTML-editor enabled', 'slug' => 'field_textarea_html', 'type' => 'textarea', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_textarea_options' => array('use_html_editor' => '1'), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '3', 'deleted' => '0', 'saved_values' => array(0 => '<p>Text entered in the TinyMCE-editor.</p> ', 1 => '<p>Tiny editors are great!</p> <p>You can style the content and insert images and stuff. Groovy! Funky!</p> <h2>A list</h2> <ul> <li>List item 1</li> <li>List item 2</li> </ul> <h2>And images can be inserted</h2> <p><a href="http://unit-test.simple-fields.com/wordpress/wp-content/uploads/2012/10/product-cat-2.jpeg"><img class="alignnone wp-image-14" title="product-cat-2" src="http://unit-test.simple-fields.com/wordpress/wp-content/uploads/2012/10/product-cat-2.jpeg" alt="" width="368" height="277" /></a></p> '), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_3_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_3_numInSet_1')), 4 => array('name' => 'FIeld checkbox', 'description' => 'A checkbox field', 'slug' => 'field_checkbox', 'type' => 'checkbox', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '4', 'deleted' => '0', 'saved_values' => array(0 => '1', 1 => ''), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_4_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_4_numInSet_1')), 5 => array('name' => 'Field radioibuttons', 'description' => 'A radiobuttons field', 'slug' => 'field_radiobuttons', 'type' => 'radiobuttons', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'type_radiobuttons_options' => array('radiobutton_num_2' => array('value' => 'Radiobutton 1', 'deleted' => '0'), 'radiobutton_num_3' => array('value' => 'Radiobutton 2', 'deleted' => '0'), 'checked_by_default_num' => 'radiobutton_num_3', 'radiobutton_num_4' => array('value' => 'Radiobutton 3', 'deleted' => '0')), 'id' => '5', 'deleted' => '0', 'saved_values' => array(0 => 'radiobutton_num_4', 1 => 'radiobutton_num_2'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_5_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_5_numInSet_1')), 6 => array('name' => 'Field dropdown', 'description' => 'A dropdown field', 'slug' => 'field_dropdown', 'type' => 'dropdown', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'type_dropdown_options' => array('dropdown_num_2' => array('value' => 'Dropdown 1', 'deleted' => '0'), 'dropdown_num_3' => array('value' => 'Dropdown 2', 'deleted' => '0'), 'dropdown_num_4' => array('value' => 'Dropdown 3', 'deleted' => '0')), 'id' => '6', 'deleted' => '0', 'saved_values' => array(0 => 'dropdown_num_3', 1 => 'dropdown_num_2'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_6_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_6_numInSet_1')), 7 => array('name' => 'Field file', 'description' => 'A file field', 'slug' => 'field_file', 'type' => 'file', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '7', 'deleted' => '0', 'saved_values' => array(0 => '14', 1 => '17'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_7_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_7_numInSet_1')), 8 => array('name' => 'Field post', 'description' => 'A post field', 'slug' => 'field_post', 'type' => 'post', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('enabled_post_types' => array(0 => 'post', 1 => 'page'), 'additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '8', 'deleted' => '0', 'saved_values' => array(0 => '11', 1 => '5'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_8_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_8_numInSet_1')), 9 => array('name' => 'Field taxonomy', 'description' => 'A taxonomy field', 'slug' => 'field_taxonomy', 'type' => 'taxonomy', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomy_options' => array('enabled_taxonomies' => array(0 => 'category', 1 => 'post_tag')), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '9', 'deleted' => '0', 'saved_values' => array(0 => 'post_tag', 1 => 'category'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_9_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_9_numInSet_1')), 10 => array('name' => 'Field Taxonomy Term', 'description' => 'A taxonomy term field', 'slug' => 'field_taxonomy_term', 'type' => 'taxonomyterm', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('enabled_taxonomy' => 'category', 'additional_arguments' => ''), 'id' => '10', 'deleted' => '0', 'saved_values' => array(0 => array(0 => '1'), 1 => ''), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_10_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_10_numInSet_1')), 11 => array('name' => 'Field Color', 'description' => 'A color field', 'slug' => 'field_color', 'type' => 'color', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '11', 'deleted' => '0', 'saved_values' => array(0 => 'FF3C26', 1 => '8B33FF'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_11_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_11_numInSet_1')), 12 => array('name' => 'Field Date', 'description' => 'A date field', 'slug' => 'field_date', 'type' => 'date', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '12', 'deleted' => '0', 'saved_values' => array(0 => '12/10/2012', 1 => '15/10/2012'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_12_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_12_numInSet_1')), 13 => array('name' => 'Field user', 'description' => 'A user field', 'slug' => 'field_user', 'type' => 'user', 'options' => array('fieldExample' => array('myTextOption' => 'No value entered yet', 'mapsTextarea' => 'Enter some cool text here please!', 'funkyDropdown' => ''), 'minimalexample' => array('textDefaultName' => '')), 'type_post_options' => array('additional_arguments' => ''), 'type_taxonomyterm_options' => array('additional_arguments' => ''), 'id' => '13', 'deleted' => '0', 'saved_values' => array(0 => '1', 1 => '1'), 'meta_keys' => array(0 => '_simple_fields_fieldGroupID_1_fieldID_13_numInSet_0', 1 => '_simple_fields_fieldGroupID_1_fieldID_13_numInSet_1'))), 'fields_count' => 13)), 'post_types' => array(0 => 'post', 1 => 'page'), 'deleted' => false, 'hide_editor' => false, 'field_groups_count' => 1); // $this->assertEquals($vals, $all_vals); // perhaps spot differences in keys is a good thing? $this->assertEquals(array_keys($vals), array_keys($all_vals)); }
/** * get all values from a field group * @param int $post_id * @param name or ir $field_group_name_or_id * @param bool use_name return array with names or id as key * @param int $return_format 1|2 * @return array */ function simple_fields_get_post_group_values($post_id, $field_group_name_or_id, $use_name = true, $return_format = 1) { $fetch_by_id = true; if (is_int($field_group_name_or_id)) { $fetch_by_id = true; } $connector = simple_fields_get_all_fields_and_values_for_post($post_id); if (!$connector) { return array(); } foreach ($connector["field_groups"] as $one_field_group) { $is_found = false; if ($fetch_by_id && $one_field_group["id"] == $field_group_name_or_id) { $is_found = true; } else { if ($field_group_name_or_id == $one_field_group["name"]) { $is_found = true; } } if ($is_found) { $arr_return = array(); foreach ($one_field_group["fields"] as $one_field) { $saved_values = $one_field["saved_values"]; if (is_null($saved_values)) { // no saved values. just continue? continue; } if ($one_field["type"] == "radiobuttons" || $one_field["type"] == "dropdown") { if ($one_field["type"] == "radiobuttons") { $get_value_key = "type_radiobuttons_options"; } else { if ($one_field["type"] == "dropdown") { $get_value_key = "type_dropdown_options"; } } // if radiobutton or dropdown, get value from type_dropdown_options[<saved value>][value] // for each saved value, get value from type_dropdown_options[<saved value>] for ($saved_i = 0; $saved_i < sizeof($saved_values); $saved_i++) { $saved_values[$saved_i] = $one_field[$get_value_key][$saved_values[$saved_i]]["value"]; } } if ($use_name) { $arr_return[$one_field["name"]] = $saved_values; } else { $arr_return[$one_field["id"]] = $saved_values; } } $set_count = sizeof($one_field["saved_values"]); $arr_return2 = array(); for ($i = 0; $i < $set_count; $i++) { $arr_return2[$i] = array(); foreach ($arr_return as $key => $val) { $arr_return2[$i][$key] = $val[$i]; } } if ($return_format == 1) { return $arr_return; } elseif ($return_format == 2) { return $arr_return2; } } } }
/** * Outputs the names of the post connectors attached to the post you view + outputs the values */ function simple_fields_content_debug_output($the_content) { // we only want to appen the debug code when being used from get_the_content or the_content // but for example get_the_excerpt is also using filter the_content which leads to problems // so check that we are somewhere inside the right functions $is_inside_righ_function = FALSE; $arr_trace = debug_backtrace(); $arr_trace_count = count($arr_trace); for ($i = 0; $i < $arr_trace_count; $i++) { if (isset($arr_trace[$i]["function"]) && in_array($arr_trace[$i]["function"], array("the_content", "get_the_content"))) { $is_inside_righ_function = TRUE; break; } } if (!$is_inside_righ_function) { // Don't do the debug, since we're not in the_content return $the_content; } $output = ""; $output_all = ""; $field_count = 0; $post_connector_with_values = simple_fields_get_all_fields_and_values_for_post(get_the_ID(), "include_deleted=0"); if ($post_connector_with_values) { foreach ($post_connector_with_values["field_groups"] as $one_field_group) { if ($one_field_group["deleted"]) { continue; } $output_all .= "<div style='font-weight:bold;margin:1em 0 0 0;'>"; $str_is_repeatable = $one_field_group["repeatable"] ? __(" (Repeatable)", "simple-fields") : ""; $output_all .= sprintf(__('Fieldgroup %1$s %2$s', "simple-fields"), $one_field_group["name"], $str_is_repeatable); $output_all .= "</div>"; $str_all_group_fields = ""; foreach ($one_field_group["fields"] as $one_field) { if ($one_field["deleted"]) { continue; } $field_count++; $content = ""; $content .= "<ul style='background:#eee;padding:.5em;margin:0;display:block;'>"; $content .= "<li>Field <b>" . $one_field["name"] . "</b>"; $content .= ", type <b>" . $one_field["type"] . "</b>"; if (isset($one_field["slug"])) { $content .= ", slug <b>" . $one_field["slug"] . "</b>"; $str_all_group_fields .= $one_field["slug"] . ","; if ($one_field_group["repeatable"]) { $content .= "<br>Use <code><b>simple_fields_values('" . $one_field["slug"] . "')</b></code> to get:"; ob_start(); sf_d(simple_fields_values($one_field["slug"])); $content .= ob_get_clean(); } else { $content .= "<br>Use <code><b>simple_fields_value('" . $one_field["slug"] . "')</b></code> to get:"; ob_start(); sf_d(simple_fields_value($one_field["slug"])); $content .= ob_get_clean(); } } else { $content .= "<br>No slug for this field found (probably old field that has not been edited and saved)."; } $content .= "</ul>"; $output_all .= $content; } // Show example how to get all fields in one shot // But only show if field has more than one field, otherwise it's kinda not useful if (sizeof($one_field_group["fields"]) > 1) { $str_all_group_fields = preg_replace('!,$!', '', $str_all_group_fields); $output_all .= "<ul style='background:#eee;padding:.5em;margin:0;display:block;'>"; if ($one_field_group["repeatable"]) { $content = "<li>Get all fields at once: use <code><b>simple_fields_values('" . $str_all_group_fields . "')</b></code> to get:"; ob_start(); sf_d(simple_fields_values($str_all_group_fields)); $content .= ob_get_clean(); } else { $content = "<li>Get all fields at once: use <code><b>simple_fields_value('" . $str_all_group_fields . "')</b></code> to get:"; ob_start(); sf_d(simple_fields_value($str_all_group_fields)); $content .= ob_get_clean(); } $output_all .= $content; $output_all .= "</ul>"; } } // for each field group } if ($output_all) { $str_show_fields = __("Show fields.", "simple-fields"); $str_hide_fields = __("Hide fields.", "simple-fields"); ?> <script> window.simple_fields_post_debug_show_hide = window.simple_fields_post_debug_show_hide || function(t) { var $t = jQuery(t); var $div_wrap = $t.closest("div.simple-fields-post-debug-wrap"); var debug_content = $div_wrap.find("div.simple-fields-post-debug-content"); debug_content.toggle(); if (debug_content.is(":visible")) { $t.text("<?php echo $str_hide_fields; ?> "); } else { $t.text("<?php echo $str_show_fields; ?> "); } return false; } </script> <?php $output_all = ' <div class="simple-fields-post-debug-wrap" style="display:block;margin:0;padding:0;"> <p style="margin:0;padding:0;display:block;">This post has ' . $field_count . ' Simple Fields-fields attached. <a href="#" onclick="return simple_fields_post_debug_show_hide(this);">' . $str_show_fields . '</a></p> <div class="simple-fields-post-debug-content" style="display:none;">' . $output_all . '</div> </div> '; } // if a field has the slug caption the output will be [caption] and then it will crash with some shortcodes, so we try to fix that here $output_all = str_replace("[", "[", $output_all); $output_all = str_replace("]", "]", $output_all); return $the_content . $output_all; }
/** * Gets all values as array * @param string $field_slug or comma separated list of several slugs. can be in "slashed" format, ie.. fieldgroupslug/fieldgroup, to avoid wrong value being fetched when multiple fields have the same slug. * @param int $post_id ID of post or WP_Post object or null to use current post in loop * @param array $options Array or query string of options to send to field type * @return mixed string or array, depending on the field type and if first arg contains comma or not * * Example output if field_slug contains comma = get serveral fields from a repeatable field group * Array * ( * [0] => Array * ( * [image] => 1156 * [text] => Text 1 * [link] => /hej/ * ) * * [1] => Array * ( * [image] => 1159 * [text] => Hej svejs i ditt fejs * [link] => /hopp/ * ) * * Example output if field_slug with no comma: * Array * ( * [0] => Text 1 * [1] => Hej svejs i ditt fejs * [2] => Lorem ipsum dolor sit amet * ) */ function simple_fields_values($field_slug = NULL, $post_id = NULL, $options = NULL) { if (empty($field_slug)) { return FALSE; } if (is_null($post_id)) { $post_id = get_the_ID(); } if (is_a($post_id, "WP_Post")) { $post_id = $post_id->ID; } // if field_slug contains commas then get all fields. awe-some! if (strpos($field_slug, ",") !== FALSE) { $arr_comma_slugs_values = array(); $arr_field_slugs = explode(",", $field_slug); if ($arr_field_slugs) { foreach ($arr_field_slugs as $one_of_the_comma_separated_slug) { $one_of_the_comma_separated_slug = trim($one_of_the_comma_separated_slug); $one_slug_values = simple_fields_values($one_of_the_comma_separated_slug, $post_id, $options); // no value, don't add if (empty($one_slug_values)) { continue; } $loopnum = 0; if (!isset($arr_comma_slugs_values[$loopnum])) { $arr_comma_slugs_values[$loopnum] = array(); } // If slashed value then fetch slug part $is_slashed_slug = strpos($one_of_the_comma_separated_slug, "/") !== false ? true : false; if ($is_slashed_slug) { list($unslashed_group_slug, $unslashed_field_slug) = explode("/", $one_of_the_comma_separated_slug); $slug_for_array = $unslashed_field_slug; } else { $slug_for_array = $one_of_the_comma_separated_slug; } // create array with result foreach ($one_slug_values as $one_slug_sub_value) { $arr_comma_slugs_values[$loopnum][$slug_for_array] = $one_slug_sub_value; $loopnum++; } } } $arr_comma_slugs_values = apply_filters("simple_fields_values", $arr_comma_slugs_values, $field_slug, $post_id, $options); return $arr_comma_slugs_values; } // here we get a single field global $sf; // Post connector for this post, with lots of info $post_connector_info = simple_fields_get_all_fields_and_values_for_post($post_id, "include_deleted=0"); if ($post_connector_info === FALSE) { $return_val = apply_filters("simple_fields_values", FALSE, $field_slug, $post_id, $options); return $return_val; } $parsed_options = wp_parse_args($options); // if slug contains slash ("/") then this is a field that we want to get by both field groups and field slug (so field slug can be same for multiple fields) // lke this: fieldgroupslug/fieldslug $is_slashed_slug = strpos($field_slug, "/") !== false ? true : false; if ($is_slashed_slug) { list($unslashed_group_slug, $unslashed_field_slug) = explode("/", $field_slug); } // Loop through the field groups that this post connector has and locate the field_slug we are looking for foreach ($post_connector_info["field_groups"] as $one_field_group) { // If slug is slashed then the field group must match if ($is_slashed_slug && $unslashed_group_slug !== $one_field_group["slug"]) { continue; } // Loop the fields in this field group foreach ($one_field_group["fields"] as $one_field_group_field) { // Skip deleted fields if ($one_field_group_field["deleted"]) { continue; } if ($field_slug === $one_field_group_field["slug"] || $is_slashed_slug && $unslashed_field_slug === $one_field_group_field["slug"]) { // Detect options for the field with this slug // options are in format: // extended_output=1&file[extended_output]=1&file[anotherOptions]=yepp indeed // where the first arg is for all fields, and the one with square-brackets are for specific slugs $parsed_options_for_this_field = array(); // First check for settings saved for the field (in gui or through register_field_group) $field_options_key = "type_" . $one_field_group_field["type"] . "_options"; if (isset($one_field_group_field[$field_options_key])) { // settings exist for this field if (isset($one_field_group_field[$field_options_key]["enable_extended_return_values"]) && $one_field_group_field[$field_options_key]["enable_extended_return_values"]) { $parsed_options_for_this_field["extended_return"] = 1; } } // check for options savailable for all fields // all keys for values that are not arrays. these are args that are meant for all slugs foreach ($parsed_options as $key => $val) { if (!is_array($val)) { $parsed_options_for_this_field = array_merge($parsed_options_for_this_field, array($key => $val)); } } // check for options for just this specific slug // if our field slug is available as a key and that key is an array = value is for this field slug if (isset($parsed_options[$one_field_group_field["slug"]]) && is_array($parsed_options[$one_field_group_field["slug"]])) { $parsed_options_for_this_field = array_merge($parsed_options_for_this_field, $parsed_options[$one_field_group_field["slug"]]); } // that's it, we have the options that should be available for this field slug // echo "<br>field: " . $one_field_group_field["slug"]; // sf_d($parsed_options_for_this_field); // Slug is found. Get and return values. // If no value is set. Should we return string, null, or false? NULL as in "no value exists"? $saved_values = isset($one_field_group_field["saved_values"]) ? $one_field_group_field["saved_values"] : NULL; // If no values just return // But return an array, since that's what we except it to return // if (!sizeof($saved_values)) return array(); // no, don't return here. let the action further down run. if (!sizeof($saved_values)) { $saved_values = array(); } /* For old/core/legacy fields it's like this: Array ( [0] => Entered text into field one [1] => Entered text into field two ) For new/cool/custom field types it's like this: Array ( [0] => Array ( [option1] => Yeah [option2] => aha ) [1] => Array ( [option1] => hejhopp [option2] => snopp-pop ) ) */ // If the type is among the registered_field_types then use it //if (isset($sf->registered_field_types[$one_field_group_field["type"]]) && isset($saved_values[0]) && is_array($saved_values[0])) { if (isset($sf->registered_field_types[$one_field_group_field["type"]]) && isset($saved_values[0])) { // Use the custom field object to output this value, since we can't guess how the data is supposed to be used $custom_field_type = $sf->registered_field_types[$one_field_group_field["type"]]; $saved_values = $custom_field_type->return_values($saved_values, $parsed_options_for_this_field); } else { // legacy/core field type, uses plain $saved_values // ...but since 1.0.3 you can use extened return // $parsed_options_for_this_field // Check if field should return extended return values if (isset($parsed_options_for_this_field["extended_return"]) && (bool) $parsed_options_for_this_field["extended_return"]) { // check if current field type supports this if (in_array($one_field_group_field["type"], array("file", "radiobuttons", "dropdown", "post", "user", "taxonomy", "taxonomyterm", "date"))) { foreach ($saved_values as $one_saved_value_key => $one_saved_value) { $saved_values[$one_saved_value_key] = $sf->get_extended_return_values_for_field($one_field_group_field, $one_saved_value); } } } } /* // You can modify the return values by adding a filter for the field type you want to modify // Example: adds "appended text" to all text values upon retrieval add_filter("simple-fields-return-values-text", function($values) { $values[0] = $values[0] . " appended text"; return $values; }); */ $saved_values = apply_filters("simple-fields-return-values-" . $one_field_group_field["type"], $saved_values); $saved_values = apply_filters("simple_fields_values", $saved_values, $field_slug, $post_id, $options, $one_field_group_field["type"]); return $saved_values; } } } }