ap_user_can_answer( mixed $question_id, boolean|integer $user_id = false )

Description #

Check if a user can answer on a question.

Parameters #

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

Changelog #

VersionDescription
4.1.0Check if $question_id argument is a valid question CPT ID. Updated to use new option post_answer_per. Also removed checking of option only_admin_can_answer. Fixed: anonymous cannot answer if allow op to answer option is unchecked.
2.4.6Introduced.

Source #

File: includes/class/roles-cap.php

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
function ap_user_can_answer( $question_id, $user_id = false ) {
    if ( false === $user_id ) {
        $user_id = get_current_user_id();
    }
 
    $question = ap_get_post( $question_id );
 
    // Return false if not a question.
    if ( ! $question || 'question' !== $question->post_type ) {
        return false;
    }
 
    if ( is_super_admin( $user_id ) ) {
        return true;
    }
 
    /**
     * Allow overriding of ap_user_can_answer.
     *
     * @param boolean|string $filter        Apply this filter, default is empty string.
     * @param integer          $question_id     Question ID.
     * @param integer          $user_id         User ID.
     * @since 2.4.6          Added 2 new arguments `$question_id` and `$user_id`.
     */
    $filter = apply_filters( 'ap_user_can_answer', '', $question->ID, $user_id );
 
    if ( true === $filter ) {
        return true;
    } elseif ( false === $filter ) {
        return false;
    }
 
    // Return if user cannot read question.
    if ( ! ap_user_can_read_question( $question_id, $user_id ) ) {
        return false;
    }
 
    // Do not allow to answer if best answer is selected.
    if ( ap_opt( 'close_selected' ) && ap_have_answer_selected( $question->ID ) ) {
        return false;
    }
 
    // Bail out if question is closed.
    if ( is_post_closed( $question ) ) {
        return false;
    }
 
    // Check if user is original poster and dont allow them to answer their own question.
    if ( is_user_logged_in() && ap_opt( 'disallow_op_to_answer' ) && ! empty( $question->post_author ) && $question->post_author == $user_id ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
        return false;
    }
 
    // Check if user already answered and if multiple answer disabled then don't allow them to answer.
    if ( is_user_logged_in() && ! ap_opt( 'multiple_answers' ) && ap_is_user_answered( $question->ID, $user_id ) ) {
        return false;
    }
 
    $option = ap_opt( 'post_answer_per' );
    if ( 'have_cap' === $option && is_user_logged_in() && user_can( $user_id, 'ap_new_answer' ) ) {
        return true;
    } elseif ( 'logged_in' === $option && is_user_logged_in() ) {
        return true;
    } elseif ( 'anyone' === $option ) {
        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