BP_REST_Reply_Endpoint::update_item( WP_REST_Request $request )

Update/Edit a reply.

Description

Parameters

$request

(WP_REST_Request) (Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

File: bp-forums/classes/class-bp-rest-reply-endpoint.php

	public function update_item( $request ) {
		$reply_new = $this->prepare_reply_for_database( $request );

		// Define local variable(s).
		$revisions_removed = false;
		$reply             = 0;
		$reply_id          = 0;
		$reply_to          = 0;
		$reply_author      = 0;
		$reply_title       = '';
		$reply_content     = '';
		$reply_edit_reason = '';
		$anonymous_data    = array();

		/** Reply */
		// Reply id was not passed.
		if ( empty( $reply_new->bbp_reply_id ) ) {
			return new WP_Error(
				'bp_rest_bbp_edit_reply_id',
				__( 'Reply ID not found.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);

			// Reply id was passed.
		} elseif ( is_numeric( $reply_new->bbp_reply_id ) ) {
			$reply_id = (int) $reply_new->bbp_reply_id;
			$reply    = bbp_get_reply( $reply_id );
		}

		// Reply does not exist.
		if ( empty( $reply ) ) {
			return new WP_Error(
				'bp_rest_bbp_edit_reply_not_found',
				__( 'The reply you want to edit was not found.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);

			// Reply exists.
		} else {

			// Check users ability to create new reply.
			if ( ! bbp_is_reply_anonymous( $reply_id ) ) {

				// User cannot edit this reply.
				if ( ! current_user_can( 'edit_reply', $reply_id ) ) {
					return new WP_Error(
						'bp_rest_bbp_edit_reply_permissions',
						__( 'You do not have permission to edit that reply.', 'buddyboss' ),
						array(
							'status' => rest_authorization_required_code(),
						)
					);
				}

				// Set reply author.
				$reply_author = bbp_get_reply_author_id( $reply_id );

				// It is an anonymous post.
			} else {

				$anonymous_args = array(
					'bbp_anonymous_name'    => ! empty( $request['anonymous_name'] ) ? sanitize_text_field( $request['anonymous_name'] ) : '',
					'bbp_anonymous_email'   => ! empty( $request['anonymous_email'] ) ? sanitize_email( $request['anonymous_email'] ) : '',
					'bbp_anonymous_website' => ! empty( $request['anonymous_website'] ) ? sanitize_text_field( $request['anonymous_website'] ) : '',
				);

				// Filter anonymous data.
				$anonymous_data = bbp_filter_anonymous_post_data( $anonymous_args );
			}
		}

		// Remove kses filters from title and content for capable users.
		if ( current_user_can( 'unfiltered_html' ) ) {
			remove_filter( 'bbp_edit_reply_pre_title', 'wp_filter_kses' );
			remove_filter( 'bbp_edit_reply_pre_content', 'bbp_encode_bad', 10 );
			remove_filter( 'bbp_edit_reply_pre_content', 'bbp_filter_kses', 30 );
		}

		/** Reply Topic */
		$topic_id = bbp_get_reply_topic_id( $reply_id );

		/** Topic Forum */
		$forum_id = bbp_get_topic_forum_id( $topic_id );

		// Forum exists.
		if ( ! empty( $forum_id ) && ( bbp_get_reply_forum_id( $reply_id ) !== $forum_id ) ) {

			// Forum is a category.
			if ( bbp_is_forum_category( $forum_id ) ) {
				return new WP_Error(
					'bp_rest_bbp_edit_reply_forum_category',
					__( 'This forum is a category. No replies can be created in this forum.', 'buddyboss' ),
					array(
						'status' => 400,
					)
				);

				// Forum is not a category.
			} else {

				// Forum is closed and user cannot access.
				if ( bbp_is_forum_closed( $forum_id ) && ! current_user_can( 'edit_forum', $forum_id ) ) {
					return new WP_Error(
						'bp_rest_bbp_edit_reply_forum_closed',
						__( 'This forum has been closed to new replies.', 'buddyboss' ),
						array(
							'status' => 400,
						)
					);
				}

				/**
				 * Added logic for group forum
				 * Current user is part of that group or not.
				 * We need to check manually because bbpress updating that caps only on group forum page and
				 * in API those conditional tag will not work.
				 */
				$group_ids = bbp_get_forum_group_ids( $forum_id );
				if ( ! empty( $group_ids ) ) {
					$is_member = false;
					foreach ( $group_ids as $group_id ) {
						if ( groups_is_user_member( get_current_user_id(), $group_id ) ) {
							$is_member = true;
							break;
						}
					}
				}

				// Forum is private and user cannot access.
				if ( bbp_is_forum_private( $forum_id ) ) {
					if (
						( empty( $group_ids ) && ! current_user_can( 'read_private_forums' ) )
						|| ( ! empty( $group_ids ) && ! $is_member )
					) {
						return new WP_Error(
							'bp_rest_bbp_edit_reply_forum_private',
							__( 'This forum is private and you do not have the capability to read or create new replies in it.', 'buddyboss' ),
							array(
								'status' => rest_authorization_required_code(),
							)
						);
					}

					// Forum is hidden and user cannot access.
				} elseif ( bbp_is_forum_hidden( $forum_id ) ) {
					if (
						( empty( $group_ids ) && ! current_user_can( 'read_hidden_forums' ) )
						|| ( ! empty( $group_ids ) && ! $is_member )
					) {
						return new WP_Error(
							'bp_rest_bbp_edit_reply_forum_hidden',
							__( 'This forum is hidden and you do not have the capability to read or create new replies in it.', 'buddyboss' ),
							array(
								'status' => rest_authorization_required_code(),
							)
						);
					}
				}
			}
		}

		/** Reply Title */
		if ( ! empty( $reply_new->bbp_reply_title ) ) {
			$reply_title = esc_attr( wp_strip_all_tags( $reply_new->bbp_reply_title ) );
		}

		// Filter and sanitize.
		$reply_title = apply_filters( 'bbp_edit_reply_pre_title', $reply_title, $reply_id );

		/** Reply Content */
		if ( ! empty( $reply_new->bbp_reply_content ) ) {
			$reply_content = $reply_new->bbp_reply_content;
		}

		// Filter and sanitize.
		$reply_content = apply_filters( 'bbp_edit_reply_pre_content', $reply_content, $reply_id );

		// No reply content.
		if ( empty( $reply_content ) ) {
			return new WP_Error(
				'bp_rest_bbp_edit_reply_content',
				__( 'Your reply cannot be empty.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		/** Reply Blacklist */
		if ( ! bbp_check_for_blacklist( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) {
			return new WP_Error(
				'bp_rest_bbp_reply_blacklist',
				__( 'Sorry, Your reply cannot be edited at this time.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		/** Reply Status */
		// Maybe put into moderation.
		if ( ! bbp_check_for_moderation( $anonymous_data, $reply_author, $reply_title, $reply_content ) ) {

			// Set post status to pending if public.
			if ( bbp_get_public_status_id() === $reply->post_status ) {
				$reply_status = bbp_get_pending_status_id();
			}

			// Use existing post_status.
		} else {
			$reply_status = $reply->post_status;
		}

		/** Reply To */
		// Handle Reply To of the reply; $_REQUEST for non-JS submissions.
		if ( isset( $reply_new->bbp_reply_to ) ) {
			$reply_to = bbp_validate_reply_to( $reply_new->bbp_reply_to );
		}

		/** Topic Tags */
		// Either replace terms.
		if ( bbp_allow_topic_tags() && current_user_can( 'assign_topic_tags' ) && ! empty( $reply_new->bbp_topic_tags ) ) {
			$terms = esc_attr( wp_strip_all_tags( $reply_new->bbp_topic_tags ) );

			// ...or remove them.
		} elseif ( isset( $reply_new->bbp_topic_tags ) ) {
			$terms = '';

			// Existing terms.
		} else {
			$terms = bbp_get_topic_tag_names( $topic_id );
		}

		/** Additional Actions (Before Save) */
		do_action( 'bbp_edit_reply_pre_extras', $reply_id );

		/** No Errors */
		// Add the content of the form to $reply_data as an array.
		// Just in time manipulation of reply data before being edited.
		$reply_data = apply_filters(
			'bbp_edit_reply_pre_insert',
			array(
				'ID'           => $reply_id,
				'post_title'   => $reply_title,
				'post_content' => $reply_content,
				'post_status'  => $reply_status,
				'post_parent'  => $topic_id,
				'post_author'  => $reply_author,
				'post_type'    => bbp_get_reply_post_type(),
			)
		);

		// Toggle revisions to avoid duplicates.
		if ( post_type_supports( bbp_get_reply_post_type(), 'revisions' ) ) {
			$revisions_removed = true;
			remove_post_type_support( bbp_get_reply_post_type(), 'revisions' );
		}

		// Insert topic.
		$reply_id = wp_update_post( $reply_data );

		// Toggle revisions back on.
		if ( true === $revisions_removed ) {
			$revisions_removed = false;
			add_post_type_support( bbp_get_reply_post_type(), 'revisions' );
		}

		/** Topic Tags */
		// Just in time manipulation of reply terms before being edited.
		$terms = apply_filters( 'bbp_edit_reply_pre_set_terms', $terms, $topic_id, $reply_id );

		// Insert terms.
		$terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), true );

		// Term error.
		if ( is_wp_error( $terms ) ) {
			return new WP_Error(
				'bp_rest_bbp_reply_tags',
				__( 'There was a problem adding the tags to the topic.', 'buddyboss' ),
				array(
					'status' => 400,
				)
			);
		}

		if ( empty( $reply_id ) || is_wp_error( $reply_id ) ) {
			$append_error = (
				( is_wp_error( $reply_id ) && $reply_id->get_error_message() )
				? __( 'The following problem(s) have been found with your reply: ', 'buddyboss' ) . $reply_id->get_error_message() . __( 'Please try again.', 'buddyboss' )
				: __( 'We are facing a problem to updating a reply.', 'buddyboss' )
			);

			return new WP_Error(
				'bp_rest_bbp_reply_error',
				$append_error,
				array(
					'status' => 400,
				)
			);
		}

		// Update counts, etc...
		do_action( 'bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author, true, $reply_to );

		/** Revisions */
		// Update locks.
		update_post_meta( $reply_id, '_edit_last', bbp_get_current_user_id() );
		delete_post_meta( $reply_id, '_edit_lock' );

		// Revision Reason.
		if ( ! empty( $reply_new->bbp_reply_edit_reason ) ) {
			$reply_edit_reason = esc_attr( wp_strip_all_tags( $reply_new->bbp_reply_edit_reason ) );
		}

		// Update revision log.
		if ( ! empty( $reply_new->bbp_log_reply_edit ) && ( true === $reply_new->bbp_log_reply_edit ) ) {
			$revision_id = wp_save_post_revision( $reply_id );
			if ( ! empty( $revision_id ) ) {
				bbp_update_reply_revision_log(
					array(
						'reply_id'    => $reply_id,
						'revision_id' => $revision_id,
						'author_id'   => bbp_get_current_user_id(),
						'reason'      => $reply_edit_reason,
					)
				);
			}
		}

		/** Additional Actions (After Save) */
		do_action( 'bbp_edit_reply_post_extras', $reply_id );

		$reply         = get_post( $reply_id );
		$fields_update = $this->update_additional_fields_for_object( $reply, $request );

		if ( is_wp_error( $fields_update ) ) {
			return $fields_update;
		}

		/**
		 * Fires after a reply is edited via the REST API.
		 *
		 * @param array           $reply   Edited Reply.
		 * @param WP_REST_Request $request The request sent to the API.
		 *
		 * @since 0.1.0
		 */
		do_action( 'bp_rest_reply_update_item', $reply, $request );

		return $this->get_item(
			array(
				'id'      => $reply_id,
				'context' => 'view',
			)
		);

	}

Changelog

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