public function settings_screen_save($group_id = null)
 {
     // First, set up a new taxonomy term if this group doesn't already have one. Kind of painful since we want to keep this list hierarchical.
     // Two options: update existing term or create new. (Updating could be useful for fixing hierarchy problems.)
     if ($_POST["ccgn_is_enabled"]) {
         // Are we using BP Group Hierarchy?
         $hierarchy_active = class_exists('BP_Groups_Hierarchy');
         // Create a group object, using BP Group Hierarchy or not.
         $group_object = $hierarchy_active ? new BP_Groups_Hierarchy($group_id) : groups_get_group(array('group_id' => $group_id));
         $group_name = $group_object->name;
         $term_args['description'] = 'Group narratives associated with ' . $group_name;
         // Check for a term for this group's parent group, set a value for the term's 'parent' arg
         // Depends on BP_Group_Hierarchy being active
         if (($parent_group_id = $group_object->vars['parent_id']) && ($parent_group_term = get_term_by('slug', ccgn_create_taxonomy_slug($parent_group_id), 'ccgn_related_groups'))) {
             $term_args['parent'] = (int) $parent_group_term->term_id;
         }
         if ($existing_term_id = ccgn_get_group_term_id($group_id)) {
             $term_args['name'] = $group_name;
             $term_array = wp_update_term($existing_term_id, 'ccgn_related_groups', $term_args);
         } else {
             $term_args['slug'] = ccgn_create_taxonomy_slug($group_id);
             $term_array = wp_insert_term($group_name, 'ccgn_related_groups', $term_args);
         }
     }
     // End "is_enabled" check
     // $towrite = PHP_EOL . 'submitted: ' . print_r($_POST['tax_input']['ccgn_related_groups'], TRUE);
     // $towrite .= PHP_EOL . 'this group term: ' . print_r($term_array['term_id'], TRUE);
     // $fp = fopen('narrative-taxonomy.txt', 'a');
     // fwrite($fp, $towrite);
     // fclose($fp);
     // Next, handle relating the group to the right taxonomy terms
     // Make sure that this group is always included - otherwise once the narrative is created it might disappear from the front end
     // TODO: If only site admins can add terms, we may not want to change this unless the submitter is a site admin.
     $group_relations = ccgn_add_this_group_term($_POST['tax_input']['ccgn_related_groups'], $term_array['term_id']);
     // $towrite = PHP_EOL . 'ready to save: ' . print_r($group_relations, TRUE);
     // $fp = fopen('narrative-taxonomy.txt', 'a');
     // fwrite($fp, $towrite);
     // fclose($fp);
     if (!ccgn_update_categories($group_id, $group_relations) || !ccgn_update_groupmeta($group_id)) {
         // @TODO: This might be returning a false positive error - meta and cat updates are weird.
         bp_core_add_message(__('There was an error updating the Group Narratives settings, please try again.', 'ccgn'), 'error');
     } else {
         bp_core_add_message(__('Group Narratives settings were successfully updated.', 'ccgn'));
     }
     // Redirect to the (possibly) new admin slug
 }
/**
 * Get the taxonomy term specific to group.
 *
 * @return integer
 */
function ccgn_get_group_term_id($group_id = false)
{
    $group_id = !$group_id ? bp_get_current_group_id() : $group_id;
    if ($term = get_term_by('slug', ccgn_create_taxonomy_slug($group_id), 'ccgn_related_groups')) {
        return $term->term_id;
    } else {
        return false;
    }
}