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

Delete albums from the database.

Description

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

Parameters

$args

(array) (Optional)

Default value: array()

Return

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

Source

File: bp-media/classes/class-bp-media-album.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 media albums being deleted so we can perform more actions.
		$albums = $wpdb->get_results( "SELECT * FROM {$bp->media->table_name_albums} {$where_sql}" );

		/**
		 * Action to allow intercepting albums to be deleted.
		 *
		 * @since BuddyBoss 1.0.0
		 *
		 * @param array $albums Array of media albums.
		 * @param array $r          Array of parsed arguments.
		 */
		do_action_ref_array( 'bp_media_album_before_delete', array( $albums, $r ) );

		// Attempt to delete media albums from the database.
		$deleted = $wpdb->query( "DELETE FROM {$bp->media->table_name_albums} {$where_sql}" );

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

		/**
		 * Action to allow intercepting albums just deleted.
		 *
		 * @since BuddyBoss 1.0.0
		 *
		 * @param array $albums     Array of media albums.
		 * @param array $r          Array of parsed arguments.
		 */
		do_action_ref_array( 'bp_media_album_after_delete', array( $albums, $r ) );

		// Pluck the media albums IDs out of the $albums array.
		$album_ids      = wp_parse_id_list( wp_list_pluck( $albums, 'id' ) );

		// delete the media associated with album
		if ( ! empty( $album_ids ) ) {
			foreach( $album_ids as $album_id ) {
				BP_Media::delete( array( 'album_id' => $album_id ) );
			}
		}

		return $album_ids;
	}

Changelog

Changelog
Version Description
BuddyBoss 1.0.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.