ap_user_can_delete_post( mixed $post_id, integer|false $user_id = false )

Description #

Check if user can delete AnsPress posts.

Parameters #

  • $post_id
    mixed (Required) Question or answer ID.
  • $user_id
    integer | false (Optional) User ID. Default value: false

Changelog #

VersionDescription
2.4.7Introduced.

Source #

File: includes/class/roles-cap.php

function ap_user_can_delete_post( $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 );
	$type   = $post_o->post_type;

	// Return if not question or answer post type.
	if ( ! in_array( $post_o->post_type, [ 'question', 'answer' ], true ) ) {
		return false;
	}

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

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

	// User must not able to delete post if they cannot read post.
	if ( ! ap_user_can_read_post( $post_id, $user_id ) ) {
		return false;
	}

	if ( $user_id == $post_o->post_author && user_can( $user_id, 'ap_delete_' . $type ) ) { // loose comparison ok.
		return true;
	} elseif ( user_can( $user_id, 'ap_delete_others_' . $type ) ) {
		return true;
	}

	return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Add your comment