/**
 * Saves an idea entry in posts table
 *
 * @package WP Idea Stream
 * @subpackage ideas/functions
 *
 * @since 2.0.0
 *
 * @param  array  $ideaarr the posted arguments
 * @uses   wp_idea_stream_users_current_user_id() to get current user ID
 * @uses   wp_idea_stream_ideas_insert_status() to get the status of the idea
 * @uses   wp_parse_id_list() to sanitize a list of ids
 * @uses   wp_idea_stream_get_category() to get the category taxonomy identifier
 * @uses   wp_idea_stream_get_tag() to get the tag taxonomy identifier
 * @uses   WP_Idea_Stream_Idea->save() to insert the idea
 * @uses   do_action() call 'wp_idea_stream_ideas_before_idea_save' to perform custom actions
 *                     before the idea is saved
 *                     call 'wp_idea_stream_ideas_after_{$hook}_idea' to perform custom actions
 *                     after the idea is saved
 * @return int    the ID of the created or updated idea
 */
function wp_idea_stream_ideas_save_idea($ideaarr = array())
{
    if (!is_array($ideaarr)) {
        return false;
    }
    if (empty($ideaarr['_the_title']) || empty($ideaarr['_the_content'])) {
        return false;
    }
    // Init update vars
    $update = false;
    $old_taxonomies = array();
    $old_metas = array();
    if (!empty($ideaarr['_the_id'])) {
        /**
         * Passing the id attribute to WP_Idea_Stream_Idea will get the previous version of the idea
         * In this case we don't need to set the author or status
         */
        $idea = new WP_Idea_Stream_Idea(absint($ideaarr['_the_id']));
        if (!empty($idea->id)) {
            $update = true;
            // Get old metas
            if (!empty($idea->metas['keys'])) {
                $old_metas = $idea->metas['keys'];
            }
            // Get old taxonomies
            if (!empty($idea->taxonomies)) {
                $old_taxonomies = $idea->taxonomies;
            }
            // If we don't find the idea, stop!
        } else {
            return false;
        }
    } else {
        $idea = new WP_Idea_Stream_Idea();
        $idea->author = wp_idea_stream_users_current_user_id();
        $idea->status = wp_idea_stream_ideas_insert_status($ideaarr);
    }
    // Set the title and description of the idea
    $idea->title = $ideaarr['_the_title'];
    $idea->description = $ideaarr['_the_content'];
    // Handling categories
    if (!empty($ideaarr['_the_category']) && is_array($ideaarr['_the_category'])) {
        $categories = wp_parse_id_list($ideaarr['_the_category']);
        $idea->taxonomies = array(wp_idea_stream_get_category() => $categories);
        // In case of an update, we need to eventually remove all categories
    } else {
        if (empty($ideaarr['_the_category']) && !empty($old_taxonomies[wp_idea_stream_get_category()])) {
            // Reset categories if some were set
            if (is_array($idea->taxonomies)) {
                $idea->taxonomies[wp_idea_stream_get_category()] = array();
            } else {
                $idea->taxonomies = array(wp_idea_stream_get_category() => array());
            }
        }
    }
    // Handling tags
    if (!empty($ideaarr['_the_tags']) && is_array($ideaarr['_the_tags'])) {
        $tags = array_map('strip_tags', $ideaarr['_the_tags']);
        $tags = array(wp_idea_stream_get_tag() => join(',', $tags));
        if (!empty($idea->taxonomies)) {
            $idea->taxonomies = array_merge($idea->taxonomies, $tags);
        } else {
            $idea->taxonomies = $tags;
        }
        // In case of an update, we need to eventually remove all tags
    } else {
        if (empty($ideaarr['_the_tags']) && !empty($old_taxonomies[wp_idea_stream_get_tag()])) {
            // Reset tags if some were set
            if (is_array($idea->taxonomies)) {
                $idea->taxonomies[wp_idea_stream_get_tag()] = '';
            } else {
                $idea->taxonomies = array(wp_idea_stream_get_tag() => '');
            }
        }
    }
    // Handling metas. By default none, but can be useful for plugins or
    // when playing with BuddyPress groups.
    if (!empty($ideaarr['_the_metas']) && is_array($ideaarr['_the_metas'])) {
        $idea->metas = $ideaarr['_the_metas'];
    }
    // Check if some metas need to be deleted
    if (!empty($old_metas) && is_array($idea->metas)) {
        $to_delete = array_diff($old_metas, array_keys($idea->metas));
        if (!empty($to_delete)) {
            $to_delete = array_fill_keys($to_delete, 0);
            $idea->metas = array_merge($idea->metas, $to_delete);
        }
    }
    /**
     * Do stuff before the idea is saved
     *
     * @param  array $ideaarr the posted values
     * @param  bool  $update  whether it's an update or not
     */
    do_action('wp_idea_stream_ideas_before_idea_save', $ideaarr, $update);
    $saved_id = $idea->save();
    if (!empty($saved_id)) {
        $hook = 'insert';
        if (!empty($update)) {
            $hook = 'update';
        }
        /**
         * Do stuff after the idea was saved
         *
         * Call wp_idea_stream_ideas_after_insert_idea for a new idea
         * Call wp_idea_stream_ideas_after_update_idea for an updated idea
         *
         * @param  int    $inserted_id the inserted id
         * @param  object $idea the idea
         */
        do_action("wp_idea_stream_ideas_after_{$hook}_idea", $saved_id, $idea);
    }
    return $saved_id;
}
Example #2
0
 /**
  * Remove ideas of a banned / removed user from group
  *
  * @package WP Idea Stream
  * @subpackage buddypress/groups
  *
  * @since  2.0.0
  *
  * @param  int $group_id the ID of the group
  * @param  int $user_id the ID of the user
  * @uses   apply_filters() call 'wp_idea_stream_buddypress_group_removed_user_ideas' to force ideas to be kept in group
  * @uses   add_filter() to temporarly include all post stati
  * @uses   wp_idea_stream_ideas_get_ideas() to get user's ideas posted in the group
  * @uses   remove_filter() to remove the filter
  * @uses   bp_loggedin_user_id() to get current user ID
  * @uses   WP_Idea_Stream_Idea()->save to update the idea
  * @uses   wp_idea_stream_user_can() to check for user's capacity
  * @uses   delete_post_meta() to remove the attached group
  * @uses   WP_Idea_Stream_Group::bulk_edit_ideas_status to bulk edit the ideas stati
  * @uses   do_action() call 'wp_idea_stream_buddypress_user_removed_from_group' to perform custom actions
  */
 public function user_removed_from_group($group_id = 0, $user_id = 0)
 {
     if (empty($group_id) || empty($user_id)) {
         return false;
     }
     /**
      * Use this filter if you want to keep the ideas the user posted in the group
      * even if he was banned / removed from the group or if he left the group.
      *
      * @param bool          true to remove user's ideas, false otherwise
      * @param int $group_id the group id the user left/was removed, banned from
      * @param int $user_id  the user id
      */
     $remove_user_ideas = apply_filters('wp_idea_stream_buddypress_group_removed_user_ideas', true, $group_id, $user_id);
     if (empty($remove_user_ideas)) {
         return;
     }
     add_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
     // Get user's ideas posted in the group
     $user_ideas = wp_idea_stream_ideas_get_ideas(array('per_page' => -1, 'author' => $user_id, 'meta_query' => array(array('key' => '_ideastream_group_id', 'value' => $group_id, 'compare' => '='))));
     remove_filter('wp_idea_stream_ideas_get_status', 'wp_idea_stream_ideas_get_all_status', 10, 1);
     if (empty($user_ideas['ideas'])) {
         return;
     }
     $ideas = array();
     $leaving_group = doing_action('groups_leave_group');
     // Remove user's ideas from group
     foreach ($user_ideas['ideas'] as $idea) {
         if (!empty($leaving_group) && $idea->post_author == bp_loggedin_user_id()) {
             // Edit each idea's status and reset their group id
             $edit_idea = new WP_Idea_Stream_Idea($idea->ID);
             $edit_idea->status = 'publish';
             $edit_idea->metas['group_id'] = 0;
             // Update the idea
             $edit_idea->save();
             // Else prepare remove from group
         } else {
             if (wp_idea_stream_user_can('remove_group_ideas')) {
                 delete_post_meta($idea->ID, '_ideastream_group_id');
                 $ideas[] = $idea->ID;
             }
         }
     }
     if (!empty($ideas)) {
         // Bulk edit ideas to reset status to publish
         self::bulk_edit_ideas_status(array('status' => 'publish', 'ideas' => $ideas));
         /**
          * Use this action to perform custom ones, after the user ideas are removed as the user was banned
          * or removed from the group
          *
          * @param  int   $user_id    the user id
          * @param  array $user_ideas list of WP_Post idea objects
          * @param  int   $group_id   the group ID
          */
         do_action('wp_idea_stream_buddypress_user_removed_from_group', $user_id, $ideas, $group_id);
     }
 }