Discussion::dozent_discussion_reply_question()


Source

File: classes/Discussion.php

    public function dozent_discussion_reply_question(){
        dozent_checking_nonce();

        global $wpdb;

        $lecture_id      = (int) dozent_input_text('post_ID');
        $parent_question_id      = (int) dozent_input_text('parent_question_id');
        $question       = dozent_input_textarea('message');

        if ( empty( $question ) ){
            wp_send_json_error( [ 'message' => __('Empty question details', 'dozent') ] );
        }

        $user_id = get_current_user_id();
        $user = get_userdata($user_id);
        $date = date("Y-m-d H:i:s", dozent_time());


        $parent = $wpdb->get_row( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = {$parent_question_id} " );

        if ( $parent->user_id != $user_id ){
            $wpdb->update( $wpdb->comments,
                ['comment_approved' => 'answered'],
                ['comment_ID' => $parent_question_id]
            );
        }

        do_action('dozent_reply_question_before', $lecture_id);

        $data = apply_filters('dozent_reply_question_data', [
            'comment_post_ID'   => $lecture_id,
            'comment_author'    => $user->user_login,
            'comment_date'      => $date,
            'comment_date_gmt'  => get_gmt_from_date( $date ),
            'comment_content'   => $question,
            'comment_approved'  => 'discussion_reply',
            'comment_agent'     => 'DozentLMSPlugin',
            'comment_type'      => 'dozent_discussion',
            'comment_parent'    => $parent_question_id,
            'user_id'           => $user_id,
        ]);

        $wpdb->insert($wpdb->comments, $data);
        $comment_id = (int) $wpdb->insert_id;

        do_action('dozent_reply_question_after', $lecture_id, $comment_id, $data );

        wp_send_json_success( ['message' => __('Your reply has been added successfully', 'dozent') ] );
    }