ap_answer_form( mixed $question_id, boolean $editing = false )
Description #
Generate answer form.
Parameters #
- $question_idmixed (Required) Question iD.
- $editingboolean (Optional) true if post is being edited. Default value: false
Changelog #
Source #
File: includes/functions.php
function ap_answer_form( $question_id, $editing = false ) {
$editing = false;
$editing_id = ap_sanitize_unslash( 'id', 'r' );
// If post_id is empty then its not editing.
if ( ! empty( $editing_id ) ) {
$editing = true;
}
if ( $editing && ! ap_user_can_edit_answer( $editing_id ) ) {
echo '<p>' . esc_attr__( 'You cannot edit this answer.', 'anspress-question-answer' ) . '</p>';
return;
}
if ( ! $editing && ! ap_user_can_answer( $question_id ) ) {
echo '<p>' . esc_attr__( 'You do not have permission to answer this question.', 'anspress-question-answer' ) . '</p>';
return;
}
$args = array(
'hidden_fields' => array(
array(
'name' => 'action',
'value' => 'ap_form_answer',
),
array(
'name' => 'question_id',
'value' => (int) $question_id,
),
),
);
$values = array();
$session_values = anspress()->session->get( 'form_answer_' . $question_id );
// Add value when editing post.
if ( $editing ) {
$answer = ap_get_post( $editing_id );
$form['editing'] = true;
$form['editing_id'] = $editing_id;
$form['submit_label'] = __( 'Update Answer', 'anspress-question-answer' );
$values['post_title'] = $answer->post_title;
$values['post_content'] = $answer->post_content;
$values['is_private'] = 'private_post' === $answer->post_status ? true : false;
if ( isset( $values['anonymous_name'] ) ) {
$fields = ap_get_post_field( 'fields', $answer );
$values['anonymous_name'] = ! empty( $fields['anonymous_name'] ) ? $fields['anonymous_name'] : '';
}
$args['hidden_fields'][] = array(
'name' => 'post_id',
'value' => (int) $editing_id,
);
} elseif ( ! empty( $session_values ) ) {
// Set last session values if not editing.
$values = $session_values;
}
anspress()->get_form( 'answer' )->set_values( $values )->generate( $args );
}
Expand full source code Collapse full source code View on GitHub: includes/functions.php:2184
Add your comment