bp_nouveau_ajax_post_update()

Processes Activity updates received via a POST request.

Description

Return

(string) JSON reply.

Source

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

function bp_nouveau_ajax_post_update() {
	$bp = buddypress();

	if ( ! is_user_logged_in() || empty( $_POST['_wpnonce_post_update'] ) || ! wp_verify_nonce( $_POST['_wpnonce_post_update'], 'post_update' ) ) {
		wp_send_json_error();
	}

	if ( !strlen( trim( $_POST['content'] ) ) ) {

		// check activity toolbar options if one of them is set, activity can be empty
		$toolbar_option = false;
		if ( bp_is_active( 'media' ) && ! empty( $_POST['media'] ) ) {
			$toolbar_option = true;
		} else if ( bp_is_activity_link_preview_active() && ! empty( $_POST['link_url'] ) ) {
			$toolbar_option = true;
		} else if ( bp_is_active( 'media' ) && ! empty( $_POST['gif_data'] ) ) {
			$toolbar_option = true;
		}

		if ( ! $toolbar_option ) {
			wp_send_json_error(
				array(
					'message' => __( 'Please enter some content to post.', 'buddyboss' ),
				)
			);
		}
	}

	$activity_id = 0;
	$item_id     = 0;
	$object      = '';
	$is_private  = false;

	// Try to get the item id from posted variables.
	if ( ! empty( $_POST['item_id'] ) ) {
		$item_id = (int) $_POST['item_id'];
	}

	// Try to get the object from posted variables.
	if ( ! empty( $_POST['object'] ) ) {
		$object = sanitize_key( $_POST['object'] );

	// If the object is not set and we're in a group, set the item id and the object
	} elseif ( bp_is_group() ) {
		$item_id = bp_get_current_group_id();
		$object  = 'group';
		$status  = groups_get_current_group()->status;
	}

	if ( 'user' === $object && bp_is_active( 'activity' ) ) {
		$content = $_POST['content'];

        if ( ! empty( $_POST['user_id'] ) && bp_get_displayed_user() && $_POST['user_id'] != bp_get_displayed_user()->id ) {
			$content = sprintf('@%s %s', bp_get_displayed_user_mentionname(), $content);
        }

        $activity_id = bp_activity_post_update( array( 'content' => $content ) );

	} elseif ( 'group' === $object ) {
		if ( $item_id && bp_is_active( 'groups' ) ) {
			// This function is setting the current group!
			$activity_id = groups_post_update(
				array(
					'content'  => $_POST['content'],
					'group_id' => $item_id,
				)
			);

			if ( empty( $status ) ) {
				if ( ! empty( $bp->groups->current_group->status ) ) {
					$status = $bp->groups->current_group->status;
				} else {
					$group  = groups_get_group( array( 'group_id' => $group_id ) );
					$status = $group->status;
				}

				$is_private = 'public' !== $status;
			}
		}

	} else {
		/** This filter is documented in bp-activity/bp-activity-actions.php */
		$activity_id = apply_filters( 'bp_activity_custom_update', false, $object, $item_id, $_POST['content'] );
	}

	if ( empty( $activity_id ) ) {
		wp_send_json_error(
			array(
				'message' => __( 'There was a problem posting your update. Please try again.', 'buddyboss' ),
			)
		);
	}

	ob_start();
	if ( bp_has_activities( array( 'include' => $activity_id, 'show_hidden' => $is_private ) ) ) {
		while ( bp_activities() ) {
			bp_the_activity();
			bp_get_template_part( 'activity/entry' );
		}
	}
	$activity = ob_get_contents();
	ob_end_clean();

	wp_send_json_success( array(
		'id'           => $activity_id,
		'message'      => esc_html__( 'Update posted.', 'buddyboss' ) . ' ' . sprintf( '<a href="%s" class="just-posted">%s</a>', esc_url( bp_activity_get_permalink( $activity_id ) ), esc_html__( 'View activity.', 'buddyboss' ) ),
		'activity'     => $activity,

		/**
		 * Filters whether or not an AJAX post update is private.
		 *
		 * @since BuddyPress 3.0.0
		 *
		 * @param string/bool $is_private Privacy status for the update.
		 */
		'is_private'   => apply_filters( 'bp_nouveau_ajax_post_update_is_private', $is_private ),
		'is_directory' => bp_is_activity_directory(),
	) );
}

Changelog

Changelog
Version Description
BuddyPress 3.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.