/**
 *	Do shortcode
 *	@param array $args Attributes from shortcode
 *	@return string Field content
 */
function jcf_do_shortcode($args)
{
    extract(shortcode_atts(array('field' => '', 'post_id' => ''), $args));
    //init registered fields
    jcf_shortcode_init_fields();
    //get post id
    $post_id = !empty($args['post_id']) ? $args['post_id'] : get_the_ID();
    //get post type
    $post_type = get_post_type($post_id);
    //set post type for fields
    jcf_set_post_type($post_type);
    //get field settings
    $field_settings = jcf_field_settings_get();
    //get field id
    foreach ($field_settings as $key_field => $field) {
        if (strcmp($args['field'], $field['slug']) === 0) {
            $field_id = $key_field;
            break;
        }
    }
    // init field object and do shortcode
    if ($field_id) {
        $field_obj = jcf_init_field_object($field_id);
        $field_obj->set_post_ID($post_id);
        unset($args['field']);
        return $field_obj->do_shortcode($args);
    } else {
        return false;
    }
}
/**
 *	Save fields from import to file config or db
 *	@param array $data Array with fieldsets and fields settings from import file
 *	@return boolean|int Return save status
 */
function jcf_admin_save_settings($data)
{
    foreach ($data as $key => $post_type) {
        jcf_set_post_type($key);
        if (is_array($post_type) && !empty($post_type['fieldsets'])) {
            foreach ($post_type['fieldsets'] as $fieldset_id => $fieldset) {
                $status_fieldset = jcf_import_add_fieldset($fieldset['title'], $fieldset_id);
                if (empty($status_fieldset)) {
                    $notice = array('error' => 'Error! Please check <strong>import file</strong>');
                    do_action('admin_notices', $notice);
                    return false;
                } else {
                    $fieldset_id = $status_fieldset;
                    if (!empty($fieldset['fields'])) {
                        $old_fields = jcf_field_settings_get();
                        if (!empty($old_fields)) {
                            foreach ($old_fields as $old_field_id => $old_field) {
                                $old_slugs[] = $old_field['slug'];
                                $old_field_ids[$old_field['slug']] = $old_field_id;
                            }
                        }
                        foreach ($fieldset['fields'] as $field_id => $field) {
                            $slug_checking = !empty($old_slugs) ? in_array($field['slug'], $old_slugs) : false;
                            if ($slug_checking) {
                                $status_field = jcf_import_add_field($old_field_ids[$field['slug']], $fieldset_id, $field);
                            } else {
                                $status_field = jcf_import_add_field($field_id, $fieldset_id, $field);
                            }
                        }
                    }
                }
            }
            if (!empty($status_fieldset)) {
                if ($_POST['file_name']) {
                    unlink($_POST['file_name']);
                }
            }
        }
    }
    return $status_fieldset;
}
/**
 *	Fields UI page
 */
function jcf_admin_fields_page($post_type)
{
    jcf_set_post_type($post_type->name);
    $fieldsets = jcf_fieldsets_get();
    $field_settings = jcf_field_settings_get();
    //pa($fieldsets);
    //pa($field_settings,1);
    // load template
    include JCF_ROOT . '/templates/fields_ui.tpl.php';
}
/**
 *	callback function for "save_post" action
 */
function jcf_post_save_custom_fields($post_ID = 0, $post = null)
{
    // do not save anything on autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if (!wp_verify_nonce($_POST['justcustomfields_noncename'], plugin_basename(__FILE__))) {
        return;
    }
    // check permissions
    $permission = 'page' == $_POST['post_type'] ? 'edit_page' : 'edit_post';
    if (!current_user_can($permission, $post_ID)) {
        return;
    }
    // OK, we're authenticated: we need to find and save the data
    // set global post type
    jcf_set_post_type($_POST['post_type']);
    // get fieldsets
    $fieldsets = jcf_fieldsets_get();
    // create field class objects and call save function
    foreach ($fieldsets as $f_id => $fieldset) {
        foreach ($fieldset['fields'] as $field_id => $tmp) {
            $field_obj = jcf_init_field_object($field_id, $fieldset['id']);
            $field_obj->set_post_ID($post->ID);
            $field_obj->do_save();
        }
    }
    //pa('stop',1);
    return false;
}
/**
 *	Fields UI page
 */
function jcf_admin_fields_page($post_type)
{
    jcf_set_post_type($post_type->name);
    $jcf_read_settings = jcf_get_read_settings();
    if ($jcf_read_settings == JCF_CONF_SOURCE_DB) {
        $fieldsets = jcf_fieldsets_get();
        $field_settings = jcf_field_settings_get();
    } else {
        $jcf_settings = jcf_get_all_settings_from_file();
        $fieldsets = $jcf_settings['fieldsets'][$post_type->name];
        $field_settings = $jcf_settings['field_settings'][$post_type->name];
    }
    // load template
    include JCF_ROOT . '/templates/fields_ui.tpl.php';
}