bp_nouveau_ajax_media_save()

Save media

Description

Return

(string) HTML

Source

File: bp-templates/bp-nouveau/includes/media/ajax.php

function bp_nouveau_ajax_media_save() {
	$response = array(
		'feedback' => sprintf(
			'<div class="bp-feedback error bp-ajax-message"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
			esc_html__( 'There was a problem performing this action. Please try again.', 'buddyboss' )
		),
	);

	// Bail if not a POST action.
	if ( ! bp_is_post_request() ) {
		wp_send_json_error( $response );
	}

	if ( empty( $_POST['_wpnonce'] ) ) {
		wp_send_json_error( $response );
	}

	// Use default nonce
	$nonce = $_POST['_wpnonce'];
	$check = 'bp_nouveau_media';

	// Nonce check!
	if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
		wp_send_json_error( $response );
	}

	if ( empty( $_POST['medias'] ) ) {
		$response['feedback'] = sprintf(
			'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
			esc_html__( 'Please upload media before saving.', 'buddyboss' )
		);

		wp_send_json_error( $response );
	}

//	if ( empty( $_POST['content'] ) ) {
//		$response['feedback'] = sprintf(
//			'<div class="bp-feedback error">%s</div>',
//			esc_html__( 'Please write something about media.', 'buddyboss' )
//		);
//
//		wp_send_json_error( $response );
//	}

	$main_activity_id = false;
	// make an activity
	if ( bp_is_active( 'activity' ) ) {

		/**
		 * Filters the content provided in the activity input field.
		 *
		 * @since BuddyPress 1.2.0
		 *
		 * @param string $value Activity message being posted.
		 */
		$content = apply_filters( 'bp_activity_post_update_content', $_POST['content'] );

		$main_activity_id = bp_activity_post_update( array( 'content' => $content ) );
	}

	$album_privacy = 'public';
	$medias        = $_POST['medias'];
	$media_ids     = array();

	// save media
	foreach( $medias as $media ) {

		$activity_id = false;
		// make an activity for the media
		if ( bp_is_active( 'activity' ) ) {
			$activity_id = bp_activity_post_update( array( 'hide_sitewide' => true, 'privacy' => 'media' ) );
			if ( $activity_id ) {
				// update activity meta
				bp_activity_update_meta( $activity_id, 'bp_media_activity', '1' );
			}
		}

		if ( ! empty( $media['album_id'] ) ) {
			$albums        = bp_album_get_specific( array( 'album_ids' => array( $media['album_id'] ) ) );
			if ( ! empty( $albums['albums'] ) ) {
				$album         = array_pop( $albums['albums'] );
				$album_privacy = $album->privacy;
			}
		}

		$media_id = bp_media_add( array(
			'attachment_id' => $media['id'],
			'title'         => $media['name'],
			'activity_id'   => $activity_id,
			'album_id'      => $media['album_id'],
			'group_id'      => $media['group_id'],
			'privacy'       => $album_privacy,
			'error_type'    => 'wp_error'
		) );

		if ( is_wp_error( $media_id ) ) {
			$response['feedback'] = sprintf(
				'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
				esc_html__( 'There was a problem when trying to add the media.', 'buddyboss' )
			);

			wp_send_json_error( $response );
		}

		//save media is saved in attahchment
        update_post_meta( $media['id'], 'bp_media_saved', true );

		//save media meta for activity
		if ( ! empty( $main_activity_id ) ) {
			update_post_meta( $media['id'], 'bp_media_parent_activity_id', $main_activity_id );
			update_post_meta( $media['id'], 'bp_media_activity_id', $activity_id );
		}

		$media_ids[] = $media_id;
	}

	$media = '';
	if ( ! empty( $media_ids ) ) {
		$media_ids = implode( ',', $media_ids );

		//save media meta for activity
		if ( ! empty( $main_activity_id ) ) {
			bp_activity_update_meta( $main_activity_id, 'bp_media_ids', $media_ids );

			$main_activity = new BP_Activity_Activity( $main_activity_id );
			if ( ! empty( $main_activity ) ) {
				$main_activity->privacy = $album_privacy;
				$main_activity->save();
			}
		}

		ob_start();
		if ( bp_has_media( array( 'include' => $media_ids ) ) ) {
			while ( bp_media() ) {
				bp_the_media();
				bp_get_template_part( 'media/entry' );
			}
		}
		$media = ob_get_contents();
		ob_end_clean();
	}

	wp_send_json_success( array(
		'media' => $media,
	) );
}

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.