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

Description #

Check if user can edit a question.

Parameters #

  • $post_id
    boolean | integer (Optional) Question ID. Default value: false
  • $user_id
    boolean | integer (Optional) User ID. Default value: false

Changelog #

VersionDescription
4.1.8Fixed: user is not able to edit their own question.
4.1.5Check if valid post type.
2.4.7Introduced.

Source #

File: includes/class/roles-cap.php

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

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

	if ( false !== $post_id ) {
		$question = ap_get_post( $post_id );
	} else {
		global $post;
		$question = $post;
	}

	// Check post_type.
	if ( ! $question || 'question' !== $question->post_type ) {
		return false;
	}

	/**
	 * Filter to hijack ap_user_can_edit_question. 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_question', '', $question->ID, $user_id );

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

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

	if ( ! ap_user_can_read_question( $question->ID, $user_id ) ) {
		return false;
	}

	if ( $user_id == $question->post_author && user_can( $user_id, 'ap_edit_question' ) ) { // 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