/**
 * Generates the possible syndication groups taxonomy list on the post create/edit screen.
 *
 * @return Unordered list of checkboxes showing terms available for assignment.
 */
function ccgn_related_group_checkboxes($group_id, $post_id)
{
    // Get terms that can be used by this group
    $possibly_related_groups = ccgn_get_categories($group_id);
    //Get ids of terms that _are_ related to this post
    //TODO: If mutiple groups can edit a post, you could lose "related groups" association if group 2 can't associate with group 1. Editing should always take place in the origin group.
    $related_groups = wp_get_post_terms($post_id, 'ccgn_related_groups', array("fields" => "ids"));
    if (empty($possibly_related_groups)) {
        _e('This group has no categories associated with it. To post to group blog, first associate one or more categories with it.', 'bcg');
        return;
    } else {
        // Get this group's term; we'll always want it to be checked for UI transparency
        $home_group_term_id = ccgn_get_group_term_id($group_id);
        ?>
        <ul class="ccgn-related-groups">
        <?php 
        foreach ((array) $possibly_related_groups as $possible_relation) {
            $checked = !empty($related_groups) && in_array($possible_relation, $related_groups) || $possible_relation == $home_group_term_id ? true : false;
            $term = get_term($possible_relation, 'ccgn_related_groups');
            ?>
            <li>
                <label for="related-group-<?php 
            echo $possible_relation;
            ?>
"> <input type="checkbox" name="related-groups[]" id="related-group-<?php 
            echo $possible_relation;
            ?>
" value="<?php 
            echo $possible_relation;
            ?>
" <?php 
            if ($checked) {
                echo 'checked="checked"';
            }
            ?>
 /> <?php 
            echo $term->name;
            ?>
</label>
            </li>
            <?php 
        }
        ?>
        </ul>
        <?php 
    }
}
 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);
        }
    }
}
 /**
  * Add filter to catch removal of a story from a group
  *
  * @since    1.0.0
  */
 public function remove_story_from_group()
 {
     // Fires on bp_init action, so this is a catch-action type of filter.
     // Bail out if this isn't the narrative component.
     if (!ccgn_is_component()) {
         return false;
     }
     // Set up an array of BP's action variables
     $action_variables = bp_action_variables();
     //Handle delete actions: Removing story from group
     if (bp_is_action_variable('remove-story', 0)) {
         // Is the nonce good?
         if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'ccgn-remove-story-from-group')) {
             return false;
         }
         // Parse the URI to create our necessary variables
         $post_id = bp_action_variable(1);
         // Is this user allowed to remove this item?
         if (!ccgn_current_user_can_moderate($post_id)) {
             return false;
         }
         // Get this group's term and disassociate it from the post
         if ($group_term_id = ccgn_get_group_term_id()) {
             $success = wp_remove_object_terms($post_id, $group_term_id, 'ccgn_related_groups');
         }
         // On successful removal, we should delete related activity items.
         if ($success) {
             if ($group_id = bp_get_current_group_id()) {
                 bp_activity_delete(array('component' => buddypress()->groups->id, 'type' => 'group_story_created', 'item_id' => $group_id, 'secondary_item_id' => $post_id));
             }
         }
         if ($success && !is_wp_error($success)) {
             bp_core_add_message(__('Successfully removed the item.', $this->plugin_slug));
         } else {
             bp_core_add_message(__('Could not remove the item.', $this->plugin_slug), 'error');
         }
         // Redirect and exit
         bp_core_redirect(wp_get_referer());
         return false;
     }
 }