ap_user_can_vote_on_post( integer|object $post_id, string $type, boolean|integer $user_id = false, boolean $wp_error = false )
Description #
Check if user is allowed to cast a vote on post.
Parameters #
- $post_idinteger | object (Required) Post ID or Object.
- $typestring (Required) Vote type. vote_up or vote_down.
- $user_idboolean | integer (Optional) User ID. Default value: false
- $wp_errorboolean (Optional) Return WP_Error object. Default value: false
Source #
File: includes/class/roles-cap.php
function ap_user_can_vote_on_post( $post_id, $type, $user_id = false, $wp_error = false ) {
if ( false === $user_id ) {
$user_id = get_current_user_id();
}
// Return true if super admin.
if ( is_super_admin( $user_id ) ) {
return true;
}
$type = 'vote_up' === $type ? 'vote_up' : 'vote_down';
$post_o = ap_get_post( $post_id );
/**
* Filter to hijack ap_user_can_vote_on_post.
*
* @param boolean|string $apply_filter Apply current filter, empty string by default.
* @param integer|object $post_id Post ID or object.
* @param string $type Vote type, vote_up or vote_down.
* @param integer $user_id User ID.
* @return boolean
* @since 2.4.6
*/
$filter = apply_filters( 'ap_user_can_vote_on_post', '', $post_o->ID, $type, $user_id );
if ( true === $filter ) {
return true;
} elseif ( false === $filter ) {
return false;
}
// Do not allow post author to vote on self posts.
if ( $post_o->post_author == $user_id ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
if ( $wp_error ) {
return new WP_Error( 'cannot_vote_own_post', __( 'Voting on own post is not allowed', 'anspress-question-answer' ) );
}
return false;
}
// Check if user can read question/answer, if not then they are not allowed to vote.
if ( ! ap_user_can_read_post( $post_id, $user_id ) ) {
if ( $wp_error ) {
return new WP_Error( 'you_cannot_vote_on_restricted', __( 'Voting on restricted posts are not allowed.', 'anspress-question-answer' ) );
}
return false;
}
if ( user_can( $user_id, 'ap_' . $type ) ) {
return true;
}
if ( $wp_error ) {
return new WP_Error( 'no_permission', __( 'You do not have permission to vote.', 'anspress-question-answer' ) );
}
return false;
}
Expand full source code Collapse full source code View on GitHub: includes/class/roles-cap.php:1312
Add your comment