/**
 * Deletes the blog comment when the associated activity comment is deleted.
 *
 * Note: This is hooked on the 'bp_activity_delete_comment_pre' filter instead
 * of the 'bp_activity_delete_comment' action because we need to fetch the
 * activity comment children before they are deleted.
 *
 * @since BuddyPress (2.0.0)
 *
 * @param bool $retval
 * @param int $parent_activity_id The parent activity ID for the activity comment.
 * @param int $activity_id The activity ID for the pending deleted activity comment.
 */
function bp_blogs_sync_delete_from_activity_comment( $retval, $parent_activity_id, $activity_id ) {
	// check if parent activity is a blog post
	$parent_activity = new BP_Activity_Activity( $parent_activity_id );
	if ( 'new_blog_post' != $parent_activity->type ) {
		return $retval;
	}

	// fetch the activity comments for the activity item
	$activity = bp_activity_get( array(
		'in'               => $activity_id,
		'display_comments' => 'stream',
	) );

	// get all activity comment IDs for the pending deleted item
	$activity_ids   = bp_activity_recurse_comments_activity_ids( $activity );
	$activity_ids[] = $activity_id;

	// handle multisite
	// switch to the blog where the comment was made
	switch_to_blog( $parent_activity->item_id );

	// remove associated blog comments
	bp_blogs_remove_associated_blog_comments( $activity_ids, current_user_can( 'moderate_comments' ) );

	// multisite again!
	restore_current_blog();

	// rebuild activity comment tree
	// emulate bp_activity_delete_comment()
	BP_Activity_Activity::rebuild_activity_comment_tree( $parent_activity_id );

	// we're overriding the default bp_activity_delete_comment() functionality
	// so we need to return false
	return false;
}
 function rebuild_activity_comment_tree($parent_id, $left = 1)
 {
     global $nxtdb, $bp;
     // The right value of this node is the left value + 1
     $right = $left + 1;
     // Get all descendants of this node
     $descendants = BP_Activity_Activity::get_child_comments($parent_id);
     // Loop the descendants and recalculate the left and right values
     foreach ((array) $descendants as $descendant) {
         $right = BP_Activity_Activity::rebuild_activity_comment_tree($descendant->id, $right);
     }
     // We've got the left value, and now that we've processed the children
     // of this node we also know the right value
     if (1 == $left) {
         $nxtdb->query($nxtdb->prepare("UPDATE {$bp->activity->table_name} SET mptt_left = %d, mptt_right = %d WHERE id = %d", $left, $right, $parent_id));
     } else {
         $nxtdb->query($nxtdb->prepare("UPDATE {$bp->activity->table_name} SET mptt_left = %d, mptt_right = %d WHERE type = 'activity_comment' AND id = %d", $left, $right, $parent_id));
     }
     // Return the right value of this node + 1
     return $right + 1;
 }
 static function update_recorded_time_and_comments($media, $bp_album_id, $table)
 {
     global $wpdb;
     if (function_exists('bp_activity_add')) {
         if (!is_object($media)) {
             try {
                 $media = new BPMediaHostWordpress($media);
             } catch (exception $e) {
                 return false;
             }
         }
         $activity_id = get_post_meta($media->get_id(), 'bp_media_child_activity', true);
         if ($activity_id) {
             $date_uploaded = $wpdb->get_var("SELECT date_uploaded from {$table} WHERE id = {$bp_album_id}");
             $old_activity_id = $wpdb->get_var("SELECT id from {$wpdb->base_prefix}bp_activity WHERE component = 'album' AND type = 'bp_album_picture' AND item_id = {$bp_album_id}");
             if ($old_activity_id) {
                 $comments = $wpdb->get_results("SELECT id,secondary_item_id from {$wpdb->base_prefix}bp_activity WHERE component = 'activity' AND type = 'activity_comment' AND item_id = {$old_activity_id}");
                 foreach ($comments as $comment) {
                     $update = array('item_id' => $activity_id);
                     if ($comment->secondary_item_id == $old_activity_id) {
                         $update['secondary_item_id'] = $activity_id;
                     }
                     $wpdb->update($wpdb->base_prefix . 'bp_activity', $update, array('id' => $comment->id));
                     BP_Activity_Activity::rebuild_activity_comment_tree($activity_id);
                 }
             }
             $wpdb->update($wpdb->base_prefix . 'bp_activity', array('date_recorded' => $date_uploaded), array('id' => $activity_id));
             return count($comments);
         }
         return 0;
     }
 }
Esempio n. 4
0
/**
 * Delete an activity comment.
 *
 * @since BuddyPress (1.2.0)
 *
 * @uses apply_filters() To call the 'bp_activity_delete_comment_pre' hook.
 * @uses bp_activity_delete_children()
 * @uses bp_activity_delete()
 * @uses BP_Activity_Activity::rebuild_activity_comment_tree() {@link BP_Activity_Activity}
 * @uses do_action() To call the 'bp_activity_delete_comment' hook.
 * @todo Why is an activity id required? We could look this up.
 * @todo Why do we encourage users to call this function directly? We could just
 *       as easily examine the activity type in bp_activity_delete() and then
 *       call this function with the proper arguments if necessary.
 *
 * @param int $activity_id The ID of the "root" activity, ie the comment's
 *                         oldest ancestor.
 * @param int $comment_id The ID of the comment to be deleted.
 * @return bool True on success, false on failure
 */
function bp_activity_delete_comment($activity_id, $comment_id)
{
    /***
     * You may want to hook into this filter if you want to override this function and
     * handle the deletion of child comments differently. Make sure you return false.
     */
    if (!apply_filters('bp_activity_delete_comment_pre', true, $activity_id, $comment_id)) {
        return false;
    }
    // Delete any children of this comment.
    bp_activity_delete_children($activity_id, $comment_id);
    // Delete the actual comment
    if (!bp_activity_delete(array('id' => $comment_id, 'type' => 'activity_comment'))) {
        return false;
    }
    // Purge comment cache for the root activity update
    wp_cache_delete($activity_id, 'bp_activity_comments');
    // Recalculate the comment tree
    BP_Activity_Activity::rebuild_activity_comment_tree($activity_id);
    do_action('bp_activity_delete_comment', $activity_id, $comment_id);
    return true;
}
 /**
  * Rebuild nested comment tree under an activity or activity comment.
  *
  * @since 1.2.0
  *
  * @global wpdb $wpdb WordPress database object.
  *
  * @param  int $parent_id ID of an activity or activity comment.
  * @param  int $left      Node boundary start for activity or activity comment.
  * @return int Right      Node boundary of activity or activity comment.
  */
 public static function rebuild_activity_comment_tree($parent_id, $left = 1)
 {
     global $wpdb;
     $bp = buddypress();
     // The right value of this node is the left value + 1.
     $right = intval($left + 1);
     // Get all descendants of this node.
     $comments = BP_Activity_Activity::get_child_comments($parent_id);
     $descendants = wp_list_pluck($comments, 'id');
     // Loop the descendants and recalculate the left and right values.
     foreach ((array) $descendants as $descendant_id) {
         $right = BP_Activity_Activity::rebuild_activity_comment_tree($descendant_id, $right);
     }
     // We've got the left value, and now that we've processed the children
     // of this node we also know the right value.
     if (1 === $left) {
         $wpdb->query($wpdb->prepare("UPDATE {$bp->activity->table_name} SET mptt_left = %d, mptt_right = %d WHERE id = %d", $left, $right, $parent_id));
     } else {
         $wpdb->query($wpdb->prepare("UPDATE {$bp->activity->table_name} SET mptt_left = %d, mptt_right = %d WHERE type = 'activity_comment' AND id = %d", $left, $right, $parent_id));
     }
     // Return the right value of this node + 1.
     return intval($right + 1);
 }
/**
 * Deletes the blog comment when the associated activity comment is deleted.
 *
 * Note: This is hooked on the 'bp_activity_delete_comment_pre' filter instead
 * of the 'bp_activity_delete_comment' action because we need to fetch the
 * activity comment children before they are deleted.
 *
 * @since 2.0.0
 * @since 2.5.0 Add the $delected parameter
 *
 * @param bool $retval             Whether BuddyPress should continue or not.
 * @param int  $parent_activity_id The parent activity ID for the activity comment.
 * @param int  $activity_id        The activity ID for the pending deleted activity comment.
 * @param bool $deleted            Whether the comment was deleted or not.
 * @return bool
 */
function bp_blogs_sync_delete_from_activity_comment($retval, $parent_activity_id, $activity_id, &$deleted)
{
    // Check if parent activity is a blog post.
    $parent_activity = new BP_Activity_Activity($parent_activity_id);
    // if parent activity isn't a post type having the buddypress-activity support, stop now!
    if (!bp_activity_type_supports($parent_activity->type, 'post-type-comment-tracking')) {
        return $retval;
    }
    // Fetch the activity comments for the activity item.
    $activity = bp_activity_get(array('in' => $activity_id, 'display_comments' => 'stream', 'spam' => 'all'));
    // Get all activity comment IDs for the pending deleted item.
    $activity_ids = bp_activity_recurse_comments_activity_ids($activity);
    $activity_ids[] = $activity_id;
    // Handle multisite
    // switch to the blog where the comment was made.
    switch_to_blog($parent_activity->item_id);
    // Remove associated blog comments.
    bp_blogs_remove_associated_blog_comments($activity_ids, current_user_can('moderate_comments'));
    // Multisite again!
    restore_current_blog();
    // Rebuild activity comment tree
    // emulate bp_activity_delete_comment().
    BP_Activity_Activity::rebuild_activity_comment_tree($parent_activity_id);
    // Avoid the error message although the comments were successfully deleted
    $deleted = true;
    // We're overriding the default bp_activity_delete_comment() functionality
    // so we need to return false.
    return false;
}
/**
 * Delete an activity comment.
 *
 * @since 1.2.0
 *
 * @uses apply_filters() To call the 'bp_activity_delete_comment_pre' hook.
 * @uses bp_activity_delete_children()
 * @uses bp_activity_delete()
 * @uses BP_Activity_Activity::rebuild_activity_comment_tree() {@link BP_Activity_Activity}
 * @uses do_action() To call the 'bp_activity_delete_comment' hook.
 * @todo Why is an activity id required? We could look this up.
 * @todo Why do we encourage users to call this function directly? We could just
 *       as easily examine the activity type in bp_activity_delete() and then
 *       call this function with the proper arguments if necessary.
 *
 * @param int $activity_id The ID of the "root" activity, ie the comment's
 *                         oldest ancestor.
 * @param int $comment_id  The ID of the comment to be deleted.
 * @return bool True on success, false on failure.
 */
function bp_activity_delete_comment($activity_id, $comment_id)
{
    $deleted = false;
    /**
     * Filters whether BuddyPress should delete an activity comment or not.
     *
     * You may want to hook into this filter if you want to override this function and
     * handle the deletion of child comments differently. Make sure you return false.
     *
     * @since 1.2.0
     * @since 2.5.0 Add the deleted parameter (passed by reference)
     *
     * @param bool $value       Whether BuddyPress should continue or not.
     * @param int  $activity_id ID of the root activity item being deleted.
     * @param int  $comment_id  ID of the comment being deleted.
     * @param bool $deleted     Whether the activity comment has been deleted or not.
     */
    if (!apply_filters_ref_array('bp_activity_delete_comment_pre', array(true, $activity_id, $comment_id, &$deleted))) {
        return $deleted;
    }
    // Delete any children of this comment.
    bp_activity_delete_children($activity_id, $comment_id);
    // Delete the actual comment.
    if (!bp_activity_delete(array('id' => $comment_id, 'type' => 'activity_comment'))) {
        return false;
    } else {
        $deleted = true;
    }
    // Purge comment cache for the root activity update.
    wp_cache_delete($activity_id, 'bp_activity_comments');
    // Recalculate the comment tree.
    BP_Activity_Activity::rebuild_activity_comment_tree($activity_id);
    /**
     * Fires at the end of the deletion of an activity comment, before returning success.
     *
     * @since 1.2.0
     *
     * @param int $activity_id ID of the activity that has had a comment deleted from.
     * @param int $comment_id  ID of the comment that was deleted.
     */
    do_action('bp_activity_delete_comment', $activity_id, $comment_id);
    return $deleted;
}
/**
 * Remove a blog comment activity item from the activity stream.
 *
 * @param int $comment_id ID of the comment to be removed.
 */
function bp_blogs_remove_comment($comment_id)
{
    global $wpdb;
    // activity comments are disabled for blog posts
    // which means that individual activity items exist for blog comments
    if (bp_disable_blogforum_comments()) {
        // Delete the individual activity stream item
        bp_blogs_delete_activity(array('item_id' => $wpdb->blogid, 'secondary_item_id' => $comment_id, 'type' => 'new_blog_comment'));
        // activity comments are enabled for blog posts
        // remove the associated activity item
    } else {
        // get associated activity ID from comment meta
        $activity_id = get_comment_meta($comment_id, 'bp_activity_comment_id', true);
        // delete the associated activity comment
        //
        // also removes child post comments and associated activity comments
        if (!empty($activity_id) && bp_is_active('activity')) {
            // fetch the activity comments for the activity item
            $activity = bp_activity_get(array('in' => $activity_id, 'display_comments' => 'stream'));
            // get all activity comment IDs for the pending deleted item
            if (!empty($activity['activities'])) {
                $activity_ids = bp_activity_recurse_comments_activity_ids($activity);
                $activity_ids[] = $activity_id;
                // delete activity items
                foreach ($activity_ids as $activity_id) {
                    bp_activity_delete(array('id' => $activity_id));
                }
                // remove associated blog comments
                bp_blogs_remove_associated_blog_comments($activity_ids);
                // rebuild activity comment tree
                BP_Activity_Activity::rebuild_activity_comment_tree($activity['activities'][0]->item_id);
            }
        }
    }
    /**
     * Fires after a blog comment activity item was removed from activity stream.
     *
     * @since BuddyPress (1.0.0)
     *
     * @param int $blogid     Item ID for the blog associated with the removed comment.
     * @param int $comment_id ID of the comment being removed.
     * @param int $value      ID of the current logged in user.
     */
    do_action('bp_blogs_remove_comment', $wpdb->blogid, $comment_id, bp_loggedin_user_id());
}
/**
 * Remove a synced activity comment from the activity stream.
 *
 * @since 2.5.0
 *
 * @param bool   $deleted              True when a comment post type activity was successfully removed.
 * @param int    $comment_id           ID of the comment to be removed.
 * @param object $activity_post_object The post type tracking args object.
 * @param string $activity_type        The post type comment activity type.
 *
 * @return bool True on success. False on error.
 */
function bp_blogs_post_type_remove_comment($deleted, $comment_id, $activity_post_object, $activity_type = '')
{
    // Remove synced activity comments, if needed.
    if (!bp_disable_blogforum_comments()) {
        // Get associated activity ID from comment meta
        $activity_id = get_comment_meta($comment_id, 'bp_activity_comment_id', true);
        /**
         * Delete the associated activity comment & also remove
         * child post comments and associated activity comments.
         */
        if (!empty($activity_id)) {
            // fetch the activity comments for the activity item
            $activity = bp_activity_get(array('in' => $activity_id, 'display_comments' => 'stream', 'spam' => 'all'));
            // get all activity comment IDs for the pending deleted item
            if (!empty($activity['activities'])) {
                $activity_ids = bp_activity_recurse_comments_activity_ids($activity);
                $activity_ids[] = $activity_id;
                // delete activity items
                foreach ($activity_ids as $activity_id) {
                    bp_activity_delete(array('id' => $activity_id));
                }
                // remove associated blog comments
                bp_blogs_remove_associated_blog_comments($activity_ids);
                // rebuild activity comment tree
                BP_Activity_Activity::rebuild_activity_comment_tree($activity['activities'][0]->item_id);
                // Set the result
                $deleted = true;
            }
        }
    }
    // Backcompat for comments about the 'post' post type.
    if ('new_blog_comment' === $activity_type) {
        /**
         * Fires after a blog comment activity item was removed from activity stream.
         *
         * @since 1.0.0
         *
         * @param int $value      ID for the blog associated with the removed comment.
         * @param int $comment_id ID of the comment being removed.
         * @param int $value      ID of the current logged in user.
         */
        do_action('bp_blogs_remove_comment', get_current_blog_id(), $comment_id, bp_loggedin_user_id());
    }
    return $deleted;
}