ap_user_can_edit_answer( integer $post_id, boolean|integer $user_id = false )

Description #

Check if a user can edit answer on a question.

Parameters #

  • $post_id
    integer (Required) Answer id.
  • $user_id
    boolean | integer (Optional) User id. Default value: false

Changelog #

VersionDescription
2.4.7Introduced.

Source #

File: includes/class/roles-cap.php

function ap_user_can_edit_answer( $post_id, $user_id = false ) {
	if ( false === $user_id ) {
		$user_id = get_current_user_id();
	}

	if ( user_can( $user_id, 'ap_edit_others_answer' ) || is_super_admin( $user_id ) ) {
		return true;
	}

	$answer = ap_get_post( $post_id );

	/**
	 * Filter to hijack ap_user_can_edit_answer. This filter will be applied if filter
	 * returns a boolean value. To bypass return an empty string.
	 *
	 * @param string|boolean    $filter         Apply this filter.
	 * @param integer           $question_id    Question ID.
	 * @param integer           $user_id        User ID.
	 */
	$filter = apply_filters( 'ap_user_can_edit_answer', '', $answer->ID, $user_id );

	if ( true === $filter ) {
		return true;
	} elseif ( false === $filter ) {
		return false;
	}

	// Do not allow to edit if moderate.
	if ( 'moderate' === $answer->post_status ) {
		return false;
	}

	// No point to let user edit answer if they cannot read.
	if ( ! ap_user_can_read_answer( $answer->ID, $user_id ) ) {
		return false;
	}

	if ( $user_id == $answer->post_author && user_can( $user_id, 'ap_edit_answer' ) ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
		return true;
	}

	return false;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment