BP_Document::delete( array $args = array(), bool $from = false )

Delete document items from the database.

Description

To delete a specific document item, pass an ‘id’ parameter. Otherwise use the filters.

Parameters

$args

(Optional)

Default value: array()

$from

(Optional) Context of deletion from. ex. attachment, activity etc.

Default value: false

Return

(array|bool) An array of deleted document IDs on success, false on failure.

Source

File: bp-document/classes/class-bp-document.php

1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
    public static function delete( $args = array(), $from = false ) {
        global $wpdb;
 
        $bp = buddypress();
        $r  = wp_parse_args(
            $args,
            array(
                'id'            => false,
                'blog_id'       => false,
                'attachment_id' => false,
                'user_id'       => false,
                'title'         => false,
                'folder_id'     => false,
                'activity_id'   => false,
                'group_id'      => false,
                'privacy'       => false,
                'date_created'  => false,
            )
        );
 
        // Setup empty array from where query arguments.
        $where_args = array();
 
        // ID.
        if ( ! empty( $r['id'] ) ) {
            $where_args[] = $wpdb->prepare( 'id = %d', $r['id'] );
        }
 
        // blog ID.
        if ( ! empty( $r['blog_id'] ) ) {
            $where_args[] = $wpdb->prepare( 'blog_id = %d', $r['blog_id'] );
        }
 
        // attachment ID.
        if ( ! empty( $r['attachment_id'] ) ) {
            $where_args[] = $wpdb->prepare( 'attachment_id = %d', $r['attachment_id'] );
        }
 
        // User ID.
        if ( ! empty( $r['user_id'] ) ) {
            $where_args[] = $wpdb->prepare( 'user_id = %d', $r['user_id'] );
        }
 
        // title.
        if ( ! empty( $r['title'] ) ) {
            $where_args[] = $wpdb->prepare( 'title = %s', $r['title'] );
        }
 
        // folder ID.
        if ( ! empty( $r['folder_id'] ) ) {
            $where_args[] = $wpdb->prepare( 'folder_id = %d', $r['folder_id'] );
        }
 
        // activity ID.
        if ( ! empty( $r['activity_id'] ) ) {
            $where_args[] = $wpdb->prepare( 'activity_id = %d', $r['activity_id'] );
        }
 
        // group ID.
        if ( ! empty( $r['group_id'] ) ) {
            $where_args[] = $wpdb->prepare( 'group_id = %d', $r['group_id'] );
        }
 
        // privacy.
        if ( ! empty( $r['privacy'] ) ) {
            $where_args[] = $wpdb->prepare( 'privacy = %s', $r['privacy'] );
        }
 
        // Date created.
        if ( ! empty( $r['date_created'] ) ) {
            $where_args[] = $wpdb->prepare( 'date_created = %s', $r['date_created'] );
        }
 
        // 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 document being deleted so we can perform more actions.
        $documents = $wpdb->get_results( "SELECT * FROM {$bp->document->table_name} {$where_sql}" ); // db call ok; no-cache ok;
 
        /**
         * Action to allow intercepting document items to be deleted.
         *
         * @param array $documents Array of document.
         * @param array $r         Array of parsed arguments.
         *
         * @since BuddyBoss 1.4.0
         */
        do_action_ref_array( 'bp_document_before_delete', array( $documents, $r ) );
 
        // Attempt to delete document from the database.
        $deleted = $wpdb->query( "DELETE FROM {$bp->document->table_name} {$where_sql}" );           // db call ok; no-cache ok;
 
        // Bail if nothing was deleted.
        if ( empty( $deleted ) ) {
            return false;
        }
 
        /**
         * Action to allow intercepting document items just deleted.
         *
         * @param array $documents Array of document.
         * @param array $r         Array of parsed arguments.
         *
         * @since BuddyBoss 1.4.0
         */
        do_action_ref_array( 'bp_document_after_delete', array( $documents, $r ) );
 
        // Pluck the document IDs out of the $documents array.
        $document_ids   = wp_parse_id_list( wp_list_pluck( $documents, 'id' ) );
        $activity_ids   = wp_parse_id_list( wp_list_pluck( $documents, 'activity_id' ) );
        $attachment_ids = wp_parse_id_list( wp_list_pluck( $documents, 'attachment_id' ) );
 
        // Delete preview attachment.
        foreach ( $document_ids as $document_delete ) {
            $preview_id = bp_document_get_meta( $document_delete, 'preview_attachment_id', true );
            if ( $preview_id ) {
                wp_delete_attachment( $preview_id, true );
            }
        }
 
//      if ( ! empty( $document_ids ) ) {
//          // Loop through attachment ids and attempt to delete.
//          foreach ( $document_ids as $document ) {
//              $preview_attachment_id = bp_document_get_meta( $document, 'preview_attachment_id', true );
//              if ( $preview_attachment_id ) {
//                  wp_delete_attachment( $preview_attachment_id, true );
//              }
//          }
//      }
 
        // Delete meta.
        if ( ! empty( $document_ids ) ) {
            // Delete all document meta entries for document items.
            self::delete_document_meta_entries( wp_list_pluck( $documents, 'id' ) );
        }
 
        // Handle accompanying attachments and meta deletion.
        if ( ! empty( $attachment_ids ) ) {
 
            // Loop through attachment ids and attempt to delete.
            foreach ( $attachment_ids as $attachment_id ) {
 
                if ( bp_is_active( 'activity' ) ) {
                    $parent_activity_id = get_post_meta( $attachment_id, 'bp_document_parent_activity_id', true );
                    if ( ! empty( $parent_activity_id ) ) {
                        $activity_document_ids = bp_activity_get_meta( $parent_activity_id, 'bp_document_ids', true );
                        if ( ! empty( $activity_document_ids ) ) {
                            $activity_document_ids = explode( ',', $activity_document_ids );
                            $activity_document_ids = array_diff( $activity_document_ids, $document_ids );
                            if ( ! empty( $activity_document_ids ) ) {
                                $activity_document_ids = implode( ',', $activity_document_ids );
                                bp_activity_update_meta( $parent_activity_id, 'bp_document_ids', $activity_document_ids );
                            } else {
                                $activity_ids[] = $parent_activity_id;
                            }
                        }
                    }
                }
 
                if ( empty( $from ) ) {
                    wp_delete_attachment( $attachment_id, true );
                }
            }
        }
 
        // delete related activity.
        if ( ! empty( $activity_ids ) && bp_is_active( 'activity' ) ) {
 
            foreach ( $activity_ids as $activity_id ) {
                $activity = new BP_Activity_Activity( (int) $activity_id );
 
                // Check access.
                if ( bp_activity_user_can_delete( $activity ) ) {
                    /** This action is documented in bp-activity/bp-activity-actions.php */
                    do_action( 'bp_activity_before_action_delete_activity', $activity->id, $activity->user_id );
 
                    // Deleting an activity comment.
                    if ( 'activity_comment' === $activity->type ) {
                        if ( bp_activity_delete_comment( $activity->item_id, $activity->id ) ) {
                            /** This action is documented in bp-activity/bp-activity-actions.php */
                            do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id );
                        }
 
                        // Deleting an activity.
                    } else {
                        if ( bp_activity_delete(
                            array(
                                'id'      => $activity->id,
                                'user_id' => $activity->user_id,
                            )
                        ) ) {
                            /** This action is documented in bp-activity/bp-activity-actions.php */
                            do_action( 'bp_activity_action_delete_activity', $activity->id, $activity->user_id );
                        }
                    }
                }
            }
        }
 
        return $document_ids;
    }

Changelog

Changelog
Version Description
BuddyBoss 1.4.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.