BP_Document_Folder::delete( array $args = array() )

Delete folders from the database.

Description

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

Parameters

$args

(Optional)

Default value: array()

Return

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

Source

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

	public static function delete( $args = array() ) {
		global $wpdb;

		$bp = buddypress();
		$r  = wp_parse_args(
			$args,
			array(
				'id'           => false,
				'user_id'      => false,
				'group_id'     => 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'] );
		}

		// User ID.
		if ( ! empty( $r['user_id'] ) ) {
			$where_args[] = $wpdb->prepare( 'user_id = %d', $r['user_id'] );
		}

		// Group ID.
		if ( ! empty( $r['group_id'] ) ) {
			$where_args[] = $wpdb->prepare( 'group_id = %d', $r['group_id'] );
		}

		// 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 folders being deleted so we can perform more actions.
		$folders = $wpdb->get_results( "SELECT * FROM {$bp->document->table_name_folder} {$where_sql}" ); // db call ok; no-cache ok;

		if ( ! empty( $r['id'] ) && empty( $r['date_created'] ) && empty( $r['group_id'] ) && empty( $r['user_id'] ) ) {
			$recursive_folders = $wpdb->get_results( "SELECT * FROM {$bp->document->table_name_folder} WHERE FIND_IN_SET(ID,(SELECT GROUP_CONCAT(lv SEPARATOR ',') FROM ( SELECT @pv:=(SELECT GROUP_CONCAT(id SEPARATOR ',') FROM {$bp->document->table_name_folder} WHERE parent IN (@pv)) AS lv FROM {$bp->document->table_name_folder} JOIN (SELECT @pv:= {$r['id']})tmp WHERE parent IN (@pv)) a))" ); // db call ok; no-cache ok;
			$folders           = array_merge( $folders, $recursive_folders );
		}

		/**
		 * Action to allow intercepting folders to be deleted.
		 *
		 * @param array $folders Array of document folders.
		 * @param array $r       Array of parsed arguments.
		 *
		 * @since BuddyBoss 1.4.0
		 */
		do_action_ref_array( 'bp_document_folder_before_delete', array( $folders, $r ) );

		if ( ! empty( $r['id'] ) && empty( $r['date_created'] ) && empty( $r['group_id'] ) && empty( $r['user_id'] ) ) {
			$recursive_folders = $wpdb->get_results( "SELECT * FROM {$bp->document->table_name_folder} WHERE FIND_IN_SET(ID,(SELECT GROUP_CONCAT(lv SEPARATOR ',') FROM ( SELECT @pv:=(SELECT GROUP_CONCAT(id SEPARATOR ',') FROM {$bp->document->table_name_folder} WHERE parent IN (@pv)) AS lv FROM {$bp->document->table_name_folder} JOIN (SELECT @pv:= {$r['id']})tmp WHERE parent IN (@pv)) a))" ); // db call ok; no-cache ok;
			$folders           = array_merge( $folders, $recursive_folders );

			// Pluck the document folders IDs out of the $folders array.
			$foldr_ids = wp_parse_id_list( wp_list_pluck( $folders, 'id' ) );

			// delete the document associated with folder.
			if ( ! empty( $foldr_ids ) ) {
				foreach ( $foldr_ids as $folder_id ) {
					// Attempt to delete document folders from the database.
					$deleted = $wpdb->query( "DELETE FROM {$bp->document->table_name_folder} where id = {$folder_id}" ); // db call ok; no-cache ok;
				}
			}
		} else {
			// Attempt to delete document folders from the database.
			$deleted = $wpdb->query( "DELETE FROM {$bp->document->table_name_folder} {$where_sql}" ); // db call ok; no-cache ok;
		}

		// Bail if nothing was deleted.
		if ( empty( $deleted ) ) {
			return false;
		}

		/**
		 * Action to allow intercepting folders just deleted.
		 *
		 * @param array $folders Array of document folders.
		 * @param array $r       Array of parsed arguments.
		 *
		 * @since BuddyBoss 1.4.0
		 */
		do_action_ref_array( 'bp_document_folder_after_delete', array( $folders, $r ) );

		// Pluck the document folders IDs out of the $folders array.
		$foldr_ids = wp_parse_id_list( wp_list_pluck( $folders, 'id' ) );

		// delete the document associated with folder.
		if ( ! empty( $foldr_ids ) ) {
			foreach ( $foldr_ids as $folder_id ) {
				bp_document_delete( array( 'folder_id' => $folder_id ) );
			}
		}

		if ( ! empty( $foldr_ids ) ) {
			// Delete all folder meta entries for folder items.
			self::delete_document_folder_meta_entries( wp_list_pluck( $folders, 'id' ) );
		}

		// delete all child folders.
		if ( ! empty( $foldr_ids ) ) {

			foreach ( $foldr_ids as $folder_id ) {

				// Get child folders.
				$get_children = bp_document_get_folder_children( $folder_id );
				if ( $get_children ) {
					foreach ( $get_children as $child ) {
						// Delete all documents of folder.
						bp_document_delete( array( 'folder_id' => $child ) );

						// Delete Child folder.
						bp_folder_delete( array( 'id' => $child ) );
					}
				}
			}
		}

		return $foldr_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.