ap_user_can_edit_post( mixed $post = null, boolean|integer $user_id = false )

Description #

Check if a user can edit answer on a question.

Parameters #

  • $post
    mixed (Optional) Post. Default value: null
  • $user_id
    boolean | integer (Optional) User id. Default value: false

Source #

File: includes/class/roles-cap.php

function ap_user_can_edit_post( $post = null, $user_id = false ) {
	if ( false === $user_id ) {
		$user_id = get_current_user_id();
	}

	$_post = ap_get_post( $post );

	if ( ! in_array( $_post->post_type, array( 'question', 'answer' ), true ) ) {
		return false;
	}

	if ( user_can( $user_id, 'ap_edit_others_' . $_post->post_type ) || is_super_admin( $user_id ) ) {
		return true;
	}

	/**
	 * Filter to hijack ap_user_can_edit_post. This filter will be applied if filter
	 * returns a boolean value. To baypass 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_post', '', $_post->ID, $user_id );

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

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

	if ( ! ap_user_can_read_post( $_post, $user_id ) ) {
		return false;
	}

	if ( $user_id == $_post->post_author && user_can( $user_id, 'ap_edit_' . $_post->post_type ) ) { // 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