Example #1
0
/**
 * Delete activity item(s).
 *
 * If you're looking to hook into one action that provides the ID(s) of
 * the activity/activities deleted, then use:
 *
 * add_action( 'bp_activity_deleted_activities', 'my_function' );
 *
 * The action passes one parameter that is a single activity ID or an
 * array of activity IDs depending on the number deleted.
 *
 * If you are deleting an activity comment please use bp_activity_delete_comment();
 *
 * @since BuddyPress (1.0.0)
 *
 * @see BP_Activity_Activity::get() For more information on accepted arguments.
 * @uses wp_parse_args()
 * @uses bp_activity_adjust_mention_count()
 * @uses BP_Activity_Activity::delete() {@link BP_Activity_Activity}
 * @uses do_action() To call the 'bp_before_activity_delete' hook.
 * @uses bp_get_user_meta()
 * @uses bp_delete_user_meta()
 * @uses do_action() To call the 'bp_activity_delete' hook.
 * @uses do_action() To call the 'bp_activity_deleted_activities' hook.
 * @uses wp_cache_delete()
 *
 * @param array $args To delete specific activity items, use
 *            $args = array( 'id' => $ids );
 *        Otherwise, to use filters for item deletion, the argument format is
 *        the same as BP_Activity_Activity::get(). See that method for a description.
 * @return bool True on success, false on failure.
 */
function bp_activity_delete($args = '')
{
    // Pass one or more the of following variables to delete by those variables
    $args = bp_parse_args($args, array('id' => false, 'action' => false, 'content' => false, 'component' => false, 'type' => false, 'primary_link' => false, 'user_id' => false, 'item_id' => false, 'secondary_item_id' => false, 'date_recorded' => false, 'hide_sitewide' => false));
    do_action('bp_before_activity_delete', $args);
    // Adjust the new mention count of any mentioned member
    bp_activity_adjust_mention_count($args['id'], 'delete');
    $activity_ids_deleted = BP_Activity_Activity::delete($args);
    if (empty($activity_ids_deleted)) {
        return false;
    }
    // Check if the user's latest update has been deleted
    $user_id = empty($args['user_id']) ? bp_loggedin_user_id() : $args['user_id'];
    $latest_update = bp_get_user_meta($user_id, 'bp_latest_update', true);
    if (!empty($latest_update)) {
        if (in_array((int) $latest_update['id'], (array) $activity_ids_deleted)) {
            bp_delete_user_meta($user_id, 'bp_latest_update');
        }
    }
    do_action('bp_activity_delete', $args);
    do_action('bp_activity_deleted_activities', $activity_ids_deleted);
    wp_cache_delete('bp_activity_sitewide_front', 'bp');
    return true;
}
 /**
  * @group delete
  */
 public function test_delete_with_secondary_item_id()
 {
     $a1 = $this->factory->activity->create(array('secondary_item_id' => 523));
     $a2 = $this->factory->activity->create(array('secondary_item_id' => 1888));
     $activity = BP_Activity_Activity::delete(array('secondary_item_id' => 523));
     $this->assertEquals(array($a1), $activity);
 }
 /**
  * Delete activity items from the database.
  *
  * To delete a specific activity item, pass an 'id' parameter.
  * Otherwise use the filters.
  *
  * @since 1.2.0
  *
  * @param array $args {
  *     @int    $id                Optional. The ID of a specific item to delete.
  *     @string $action            Optional. The action to filter by.
  *     @string $content           Optional. The content to filter by.
  *     @string $component         Optional. The component name to filter by.
  *     @string $type              Optional. The activity type to filter by.
  *     @string $primary_link      Optional. The primary URL to filter by.
  *     @int    $user_id           Optional. The user ID to filter by.
  *     @int    $item_id           Optional. The associated item ID to filter by.
  *     @int    $secondary_item_id Optional. The secondary associated item ID to filter by.
  *     @string $date_recorded     Optional. The date to filter by.
  *     @int    $hide_sitewide     Optional. Default: false.
  * }
  * @return array|bool An array of deleted activity IDs on success, false on failure.
  */
 public static function delete($args = array())
 {
     global $wpdb;
     $bp = buddypress();
     $r = wp_parse_args($args, array('id' => false, 'action' => false, 'content' => false, 'component' => false, 'type' => false, 'primary_link' => false, 'user_id' => false, 'item_id' => false, 'secondary_item_id' => false, 'date_recorded' => false, 'hide_sitewide' => false));
     // Setup empty array from where query arguments.
     $where_args = array();
     // ID.
     if (!empty($r['id'])) {
         $where_args[] = $wpdb->prepare("id = %d", $r['id']);
     }
     // User ID.
     if (!empty($r['user_id'])) {
         $where_args[] = $wpdb->prepare("user_id = %d", $r['user_id']);
     }
     // Action.
     if (!empty($r['action'])) {
         $where_args[] = $wpdb->prepare("action = %s", $r['action']);
     }
     // Content.
     if (!empty($r['content'])) {
         $where_args[] = $wpdb->prepare("content = %s", $r['content']);
     }
     // Component.
     if (!empty($r['component'])) {
         $where_args[] = $wpdb->prepare("component = %s", $r['component']);
     }
     // Type.
     if (!empty($r['type'])) {
         $where_args[] = $wpdb->prepare("type = %s", $r['type']);
     }
     // Primary Link.
     if (!empty($r['primary_link'])) {
         $where_args[] = $wpdb->prepare("primary_link = %s", $r['primary_link']);
     }
     // Item ID.
     if (!empty($r['item_id'])) {
         $where_args[] = $wpdb->prepare("item_id = %d", $r['item_id']);
     }
     // Secondary item ID.
     if (!empty($r['secondary_item_id'])) {
         $where_args[] = $wpdb->prepare("secondary_item_id = %d", $r['secondary_item_id']);
     }
     // Date Recorded.
     if (!empty($r['date_recorded'])) {
         $where_args[] = $wpdb->prepare("date_recorded = %s", $r['date_recorded']);
     }
     // Hidden sitewide.
     if (!empty($r['hide_sitewide'])) {
         $where_args[] = $wpdb->prepare("hide_sitewide = %d", $r['hide_sitewide']);
     }
     // Bail if no where arguments.
     if (empty($where_args)) {
         return false;
     }
     // Join the where arguments for querying.
     $where_sql = 'WHERE ' . join(' AND ', $where_args);
     // Fetch all activities being deleted so we can perform more actions.
     $activities = $wpdb->get_results("SELECT * FROM {$bp->activity->table_name} {$where_sql}");
     /**
      * Action to allow intercepting activity items to be deleted.
      *
      * @since 2.3.0
      *
      * @param array $activities Array of activities.
      * @param array $r          Array of parsed arguments.
      */
     do_action_ref_array('bp_activity_before_delete', array($activities, $r));
     // Attempt to delete activities from the database.
     $deleted = $wpdb->query("DELETE FROM {$bp->activity->table_name} {$where_sql}");
     // Bail if nothing was deleted.
     if (empty($deleted)) {
         return false;
     }
     /**
      * Action to allow intercepting activity items just deleted.
      *
      * @since 2.3.0
      *
      * @param array $activities Array of activities.
      * @param array $r          Array of parsed arguments.
      */
     do_action_ref_array('bp_activity_after_delete', array($activities, $r));
     // Pluck the activity IDs out of the $activities array.
     $activity_ids = wp_parse_id_list(wp_list_pluck($activities, 'id'));
     // Handle accompanying activity comments and meta deletion.
     if (!empty($activity_ids)) {
         // Delete all activity meta entries for activity items.
         BP_Activity_Activity::delete_activity_meta_entries($activity_ids);
         // Setup empty array for comments.
         $comment_ids = array();
         // Loop through activity ids and attempt to delete comments.
         foreach ($activity_ids as $activity_id) {
             // Attempt to delete comments.
             $comments = BP_Activity_Activity::delete(array('type' => 'activity_comment', 'item_id' => $activity_id));
             // Merge IDs together.
             if (!empty($comments)) {
                 $comment_ids = array_merge($comment_ids, $comments);
             }
         }
         // Merge activity IDs with any deleted comment IDs.
         if (!empty($comment_ids)) {
             $activity_ids = array_unique(array_merge($activity_ids, $comment_ids));
         }
     }
     return $activity_ids;
 }
/**
 * Delete activity item(s).
 *
 * If you're looking to hook into one action that provides the ID(s) of
 * the activity/activities deleted, then use:
 *
 * add_action( 'bp_activity_deleted_activities', 'my_function' );
 *
 * The action passes one parameter that is a single activity ID or an
 * array of activity IDs depending on the number deleted.
 *
 * If you are deleting an activity comment please use bp_activity_delete_comment();
 *
 * @since 1.0.0
 *
 * @see BP_Activity_Activity::get() For more information on accepted arguments.
 * @uses wp_parse_args()
 * @uses bp_activity_adjust_mention_count()
 * @uses BP_Activity_Activity::delete() {@link BP_Activity_Activity}
 * @uses do_action() To call the 'bp_before_activity_delete' hook.
 * @uses bp_get_user_meta()
 * @uses bp_delete_user_meta()
 * @uses do_action() To call the 'bp_activity_delete' hook.
 * @uses do_action() To call the 'bp_activity_deleted_activities' hook.
 * @uses wp_cache_delete()
 *
 * @param array|string $args To delete specific activity items, use
 *                           $args = array( 'id' => $ids ); Otherwise, to use
 *                           filters for item deletion, the argument format is
 *                           the same as BP_Activity_Activity::get().
 *                           See that method for a description.
 * @return bool True on success, false on failure.
 */
function bp_activity_delete($args = '')
{
    // Pass one or more the of following variables to delete by those variables.
    $args = bp_parse_args($args, array('id' => false, 'action' => false, 'content' => false, 'component' => false, 'type' => false, 'primary_link' => false, 'user_id' => false, 'item_id' => false, 'secondary_item_id' => false, 'date_recorded' => false, 'hide_sitewide' => false));
    /**
     * Fires before an activity item proceeds to be deleted.
     *
     * @since 1.5.0
     *
     * @param array $args Array of arguments to be used with the activity deletion.
     */
    do_action('bp_before_activity_delete', $args);
    // Adjust the new mention count of any mentioned member.
    bp_activity_adjust_mention_count($args['id'], 'delete');
    $activity_ids_deleted = BP_Activity_Activity::delete($args);
    if (empty($activity_ids_deleted)) {
        return false;
    }
    // Check if the user's latest update has been deleted.
    $user_id = empty($args['user_id']) ? bp_loggedin_user_id() : $args['user_id'];
    $latest_update = bp_get_user_meta($user_id, 'bp_latest_update', true);
    if (!empty($latest_update)) {
        if (in_array((int) $latest_update['id'], (array) $activity_ids_deleted)) {
            bp_delete_user_meta($user_id, 'bp_latest_update');
        }
    }
    /**
     * Fires after the activity item has been deleted.
     *
     * @since 1.0.0
     *
     * @param array $args Array of arguments used with the activity deletion.
     */
    do_action('bp_activity_delete', $args);
    /**
     * Fires after the activity item has been deleted.
     *
     * @since 1.2.0
     *
     * @param array $activity_ids_deleted Array of affected activity item IDs.
     */
    do_action('bp_activity_deleted_activities', $activity_ids_deleted);
    wp_cache_delete('bp_activity_sitewide_front', 'bp');
    return true;
}
Example #5
0
function bp_activity_delete( $args = '' ) {
	global $bp;

	/* Pass one or more the of following variables to delete by those variables */
	$defaults = array(
		'id' => false,
		'action' => false,
		'content' => false,
		'component' => false,
		'type' => false,
		'primary_link' => false,
		'user_id' => false,
		'item_id' => false,
		'secondary_item_id' => false,
		'date_recorded' => false,
		'hide_sitewide' => false
	);

	$args = wp_parse_args( $args, $defaults );

	if ( !$activity_ids_deleted = BP_Activity_Activity::delete( $args ) )
		return false;

	/* Check if the user's latest update has been deleted */
	if ( empty( $args['user_id'] ) )
		$user_id = $bp->loggedin_user->id;
	else
		$user_id = $args['user_id'];

	$latest_update = get_user_meta( $user_id, 'bp_latest_update', true );
	if ( !empty( $latest_update ) ) {
		if ( in_array( (int)$latest_update['id'], (array)$activity_ids_deleted ) )
			delete_user_meta( $user_id, 'bp_latest_update' );
	}

	do_action( 'bp_activity_delete', $args );
	do_action( 'bp_activity_deleted_activities', $activity_ids_deleted );

	wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );

	return true;
}
Example #6
0
function bp_activity_delete($item_id, $component_name, $component_action, $user_id, $secondary_item_id)
{
    if (!BP_Activity_Activity::delete($item_id, $component_name, $component_action, $user_id, $secondary_item_id)) {
        return false;
    }
    do_action('bp_activity_delete', $item_id, $component_name, $component_action, $user_id, $secondary_item_id);
    return true;
}