bp_media_admin_repair_media()

Repair BuddyBoss media.

Description

Source

File: bp-media/bp-media-filters.php

function bp_media_admin_repair_media() {
	global $wpdb;
	$offset 	= isset( $_POST['offset'] ) ? (int) ( $_POST['offset'] ) : 0;
	$bp 		= buddypress();

	$media_query = "SELECT id, activity_id FROM {$bp->media->table_name} WHERE activity_id != 0 LIMIT 50 OFFSET $offset ";
	$medias 	= $wpdb->get_results( $media_query );

	if ( ! empty( $medias ) ) {
		foreach ( $medias as $media ) {
			if ( ! empty( $media->id ) && ! empty( $media->activity_id ) ) {
				$activity = new BP_Activity_Activity( $media->activity_id );
				if ( ! empty( $activity->id ) ) {
					if ( 'activity_comment' === $activity->type ) {
						$activity = new BP_Activity_Activity( $activity->item_id );
					}
					if ( bp_is_active( 'groups' ) && buddypress()->groups->id === $activity->component ) {
						$update_query = "UPDATE {$bp->media->table_name} SET group_id=" . $activity->item_id . ", privacy='grouponly' WHERE id=" . $media->id . " ";
						$wpdb->query( $update_query );
					}
					if ( 'media' === $activity->privacy ) {
						if ( ! empty( $activity->secondary_item_id ) ) {
							$media_activity = new BP_Activity_Activity( $activity->secondary_item_id );
							if ( ! empty( $media_activity->id ) ) {
								if ( 'activity_comment' === $media_activity->type ) {
									$media_activity = new BP_Activity_Activity( $media_activity->item_id );
								}
								if ( bp_is_active( 'groups' ) && buddypress()->groups->id === $media_activity->component ) {
									$update_query = "UPDATE {$bp->media->table_name} SET group_id=" . $media_activity->item_id . ", privacy='grouponly' WHERE id=" . $media->id . " ";
									$wpdb->query( $update_query );
									$activity->item_id   = $media_activity->item_id;
									$activity->component = buddypress()->groups->id;
								}
							}
						}
						$activity->hide_sitewide = true;
						$activity->save();
					}
				}
			}
			$offset ++;
		}
		$records_updated = sprintf( __( '%s media updated successfully.', 'buddyboss' ), number_format_i18n( $offset ) );

		return array(
				'status'  => 'running',
				'offset'  => $offset,
				'records' => $records_updated,
		);
	} else {
		return array(
				'status'  => 1,
				'message' => __( 'Media update complete!', 'buddyboss' ),
		);
	}
}

Changelog

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