HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/shootinschool/wp-content/plugins/shootin-school-plugin/trainer_forum.php
<?php
function render_siab_trainer_forum()
{
    ob_start();
    global $wpdb;
    // echo  $_GET['id'];
    $comments = $wpdb->get_results("SELECT * FROM " . DB_FORUM . " WHERE playerID = " . $_GET['id'] . " AND forum_type = 2", ARRAY_A);
    // echo "<pre>";
    // print_r($comments);
    $user_id = get_current_user_id();
?>

    <style>
        .comment-list {
            list-style: none;
            padding: 0;
        }

        .comment {
            margin-bottom: 30px;
        }

        .comment-body {
            border: 1px solid #ddd;
            padding: 20px;
            border-radius: 5px;
            background-color: #f9f9f9;
        }

        .comment-author {
            margin-bottom: 10px;
        }

        .comment-author .comment-author {
            display: block;
            font-size: 20px;
            font-weight: bold;
            color: #333;
        }

        .comment-meta {
            color: #888;
            font-size: 14px;
        }

        p {
            font-size: 16px;
            line-height: 1.6;
        }
    </style>
    <div class="wrap">
        <div class="css_loader">Loading&#8230;</div>
        <div class="alert alert-info" role="alert">
            <?php $trainerDetails = get_userdata($_GET['id']);
            $issupervisor = get_user_meta($_GET['id'], $key = 'issupervisor');
            ?>
            <?php
            if ($issupervisor[0]== 1) { ?>
                <h3> Supervisor Forum </h3>
                <small><b>Discussion about Supervisor : <?php echo $trainerDetails->data->display_name; ?></b></small>

            <?php } else { ?>
                <h3> Trainer Forum </h3>
                <small><b>Discussion about Trainer : <?php echo $trainerDetails->data->display_name; ?></b></small>
            <?php } ?>

        </div>

        <div class="clearfix">

            <div id="comments" class="comments-area">
                <?php if (!empty($comments)) {

                ?>
                    <h4 class="comments-title">Discussions</h4>
                    <?php foreach ($comments as $comment) {
                        $commentedUser = get_userdata($comment['created_by']);

                        $commentedDate = date('M-d-Y h:i:A', strtotime($comment['created_at']));

                    ?>




                        <ul class="comment-list stm-list-duty">

                            <li class="comment byuser comment-author-sam-2 bypostauthor even thread-even depth-1">
                                <div id="div-comment-12869" class="comment-body">

                                    <div class="clearfix">


                                        <div class="comment-author vcard">
                                            <span class="comment-author heading-font"><?php echo $commentedUser->data->display_name; ?></span>
                                            <span class="comment-meta commentmetadata">
                                                <span class="date heading-font"><?php echo $commentedDate; ?></span>
                                            </span>
                                            <p><?php echo $comment['comment']; ?></p>


                                        </div>
                                    </div>


                                </div>
                            </li><!-- #comment-## -->
                        </ul>
                <?php }
                } else {
                    echo "No Records Found";
                } ?>
                <div class="clearfix"></div>




                <div id="respond" class="comment-respond">
                    <h3 id="reply-title" class="comment-reply-title">Add Your Update</h3>
                    <form id="chatForm">


                        <div class="form-group comment-form-comment">
                            <input type="hidden" value="<?php echo $_GET['id']; ?>" name="playerID" name="playerID">
                            <input type="hidden" value="<?php echo $user_id; ?>" name="customerID" name="customerID">
                            <input type="hidden" value="2" name="forum_type" name="forum_type">
                            <?php
                            $issupervisor = get_user_meta($user_id, 'issupervisor', true);

                            // Check if the meta exists
                            if ($issupervisor !== 1) {
                                // Meta exists

                                $userTypevalue = 4;
                            } else {
                                // Meta doesn't exist
                                $userTypevalue = 2;
                            }
                            ?>
                            <input type="hidden" value="<?php echo $userTypevalue; ?>" name="userType" name="userType">
                            <textarea id="comment" class="form-control" name="comment" rows="9" aria-required="true" required></textarea>
                        </div>
                        <p class="form-submit">
                            <input type="button" onclick="AddComemnt_admin()" id="btn_submit" value="Send Message">

                        </p>
                    </form>
                </div><!-- #respond -->

            </div>
        </div>
    </div>
    <script type="text/javascript">
        function AddComemnt_admin() {

            if (jQuery("#chatForm").parsley().validate()) {
                jQuery(".css_loader").show();
                jQuery('#btn_submit').prop('disabled', true);
                var formData = new FormData();
                console.log(formData);

                formData.append("data", jQuery('#chatForm').serialize());
                formData.append("action", 'add_comment_admin_trainer');
                jQuery.ajax({
                    url: ajaxurl,
                    method: "post",
                    dataType: "json",
                    processData: false,
                    contentType: false,
                    data: formData,
                    success: function(response) {
                        jQuery(".css_loader").hide();
                        jQuery('#btn_submit').prop('disabled', false);
                        if (response.status) {
                            toastr.success(response.message);
                            setTimeout(function() {
                                location.reload();
                            }, 1000);

                        } else {
                            // jQuery('#addchild_error_modalBody').html('<p><strong>'+response.message+'</strong></p>');
                            // jQuery('#addchild_error_modal').modal('show');
                        }
                    },
                    error: function(data) {
                        toastr.error(data.message);
                    }
                });
            }
        }
    </script>
<?php

}


add_action('wp_ajax_nopriv_add_comment_admin_trainer', 'add_comment_admin_trainer');
add_action('wp_ajax_add_comment_admin_trainer', 'add_comment_admin_trainer');
function add_comment_admin_trainer()
{

    filter_var_array($_POST, FILTER_SANITIZE_STRING);

    global $wpdb;

    $user_id = get_current_user_id();

    parse_str($_POST['data'], $form_data); //This will convert the string to array

    if (!empty($form_data)) {
        $args = array(
            "forum_type" => $form_data['forum_type'],
            "customerID" => $form_data['customerID'],
            "playerID"   => $form_data['playerID'],
            "comment"    => $form_data['comment'],
            "userType"   => $form_data['userType'],
            "staus"     => 1,
            "created_at" => date('Y-m-d H:i:s'),
            "created_by" => $user_id,
            "updated_at" => date('Y-m-d H:i:s'),
            "updated_by" => $user_id,
        );

        $sql = $wpdb->insert(DB_FORUM, $args);

        $player = $wpdb->get_row("SELECT * FROM " . DB_USERS . " WHERE id = " . $form_data['playerID']);
        $player_name = $player->display_name; // Store player name

        $email_instructor = $wpdb->get_row("SELECT * FROM " . DB_EMAILS . " WHERE id = 53", ARRAY_A);
		$msg_template = $email_instructor['body'];
        

        $args = array(
            'role'     => 'siab_supervisor',
            'orderby'  => 'created_at',
            'order'    => 'DESC',
            'fields' => array( 'ID' ),
            'meta_query' => array(
                array(
                    'key'     => 'issupervisor',
                    'value'   => 1,
                    'compare' => '=' // You can change this if necessary (e.g., '!=' for not equal)
                )
            )
        );
        $userIds = get_users( $args );
        
        $temp = array();
        foreach ($userIds as $single) {
            $temp[] = $single->ID;
        }
        $users_table = $wpdb->prefix . "users";
        if(count($temp) > 0){
            $data = $wpdb->get_results("SELECT * FROM " .$users_table." WHERE ID IN (" .implode(',', $temp). ")");
        } else{
            $data = array();
        }
        // echo json_encode(['status' => true, 'message' =>  $data]);
        // Loop through each user and send the email
        foreach ($data as $user_data) {
            
            $email = $user_data->user_email;
            $name = $user_data->display_name;
            $msg = $msg_template;
            // Replace placeholders in the message
            preg_match_all('/{(.*?)}/', $msg, $matches);
            if (in_array("customer_name", $matches[1])) {
                $msg = str_replace('{customer_name}', $name, $msg);
            }
            if (in_array("player_name", $matches[1])) {
                $msg = str_replace('{player_name}', $player_name, $msg);
            }

            // Prepare email headers
            $headers[] = 'Content-Type: text/html; charset=UTF-8';
            $headers[] = 'From: ShootInSchool <noreply@shootinschool.com>';

            // Send the email
            try {
                wp_mail($email, $email_instructor['subject'], $msg, $headers);
            } catch (Exception $th) {
                // Handle error for the current email
            }
        }

        $users_data = $wpdb->get_results("SELECT * FROM " . DB_USERS . " WHERE ID IN (3366)");
        // Loop through each user and send the email
        foreach ($users_data as $user_data) {
            $email = $user_data->user_email;
            $name = $user_data->display_name;
            $msg = $msg_template;
            // Replace placeholders in the message
            preg_match_all('/{(.*?)}/', $msg, $matches);
            if (in_array("customer_name", $matches[1])) {
                $msg = str_replace('{customer_name}', $name, $msg);
            }
            if (in_array("player_name", $matches[1])) {
                $msg = str_replace('{player_name}', $player_name, $msg);
            }

            // Prepare email headers
            $headers[] = 'Content-Type: text/html; charset=UTF-8';
            $headers[] = 'From: ShootInSchool <noreply@shootinschool.com>';

            // Send the email
            try {
                wp_mail($email, $email_instructor['subject'], $msg, $headers);
            } catch (Exception $th) {
                // Handle error for the current email
            }
        }
        

        
        echo json_encode(['status' => true, 'message' => "Comments added successfully"]);
        die();
    }
}