ap_user_can_change_status( integer|object $post_id, integer|boolean $user_id = false )

Description #

Check if current user can change post status i.e. private_post, moderate, closed.

Parameters #

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

Changelog #

VersionDescription
4.1.0Do not allow post author to change their own post status regardless of moderator role.
2.4.7Added new argument $user_id.
2.1Introduced.

Source #

File: includes/class/roles-cap.php

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

	if ( is_super_admin( $user_id ) ) {
		return true;
	}

	$post_o = ap_get_post( $post_id );

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

	/**
	 * Filter to hijack ap_user_can_change_status.
	 *
	 * @param  boolean|string   $apply_filter   Apply current filter, empty string by default.
	 * @param  integer          $post_id        Post ID.
	 * @param  integer          $user_id        User ID.
	 * @return boolean
	 * @since  2.4.6
	 */
	$filter = apply_filters( 'ap_user_can_change_status', '', $post_o->ID, $user_id );

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

	// Do not allow post author to change status if current status is moderate,
	// regardless of moderator user role.
	if ( 'moderate' === $post_o->post_status && $post_o->post_author == $user_id ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
		return false;
	}

	if ( user_can( $user_id, 'ap_change_status_other' ) ) {
		return true;
	}

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