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
 }
function ccgn_save_narrative($group_id)
{
    $post_id = isset($_POST['post_ID']) ? $_POST['post_ID'] : 0;
    // First, check the nonce
    if (!check_admin_referer('edit_group_narrative_' . $post_id)) {
        return;
    }
    //WP's update_post function does a bunch of data cleaning, so we can leave validation to that.
    $published_status = in_array($_POST['ccgn_published'], array('publish', 'draft')) ? $_POST['ccgn_published'] : 'draft';
    $title = isset($_POST['ccgn_title']) ? $_POST['ccgn_title'] : 'Draft Narrative';
    $comment_status = isset($_POST['ccgn_comment_status']) ? 'open' : 'closed';
    $args = array('post_title' => $title, 'post_content' => $_POST['ccgn_content'], 'post_name' => sanitize_title($title), 'post_type' => 'group_story', 'post_status' => $published_status, 'comment_status' => $comment_status);
    //TODO: When would this ever not be true?
    if ($post_id) {
        $args['ID'] = $post_id;
    }
    $post_id = wp_update_post($args);
    //If successful save, do some other things, like taxonomies
    if ($post_id) {
        $old_group_relations = ccgn_get_associated_group_ids($post_id);
        //Set the "related groups" terms
        $related_groups = $_POST['related-groups'] ? array_map('intval', (array) $_POST['related-groups']) : array();
        // Make sure that this group's term is always included
        $related_groups = ccgn_add_this_group_term($related_groups, ccgn_get_group_term_id($group_id));
        // Set the terms for the new post
        wp_set_object_terms($post_id, $related_groups, 'ccgn_related_groups');
        // Set some meta with the id of the group that created this post
        if ($group_id = $_POST['group_id'] ? intval($_POST['group_id']) : bp_get_current_group_id()) {
            update_post_meta($post_id, 'ccgn_origin_group', $group_id);
        }
        $new_group_relations = ccgn_get_associated_group_ids($post_id);
        // If the post was removed from a group, we remove that group's activity item.
        $removed_group_ids = array_diff($old_group_relations, $new_group_relations);
        if (!empty($removed_group_ids)) {
            foreach ($removed_group_ids as $removed_group_id) {
                bp_activity_delete(array('component' => buddypress()->groups->id, 'type' => 'group_story_created', 'item_id' => $removed_group_id, 'secondary_item_id' => $post_id));
            }
        }
        // Make sure that the each of the syndicated groups has an activity item.
        if ('publish' == $published_status) {
            ccgn_create_activity_items($post_id);
        }
    }
}