Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
- class Ajax_Handlers {
- public function __construct() {
- // the name after wp_ajax_ must be the same in five places (the four downhere and the name of the callback function also)
- add_action( 'wp_ajax_handle_form_submission_callback', [ $this, 'handle_form_submission_callback' ] );
- add_action( 'wp_ajax_nopriv_handle_form_submission_callback', [ $this, 'handle_form_submission_callback' ] );
- // the name after wp_ajax_ must be the same in five places (the four downhere and the name of the callback function also)
- add_action( 'wp_ajax_zones_callback', [ $this, 'get_branches_by_single_zones' ] );
- add_action( 'wp_ajax_nopriv_zones_callback', [ $this, 'get_branches_by_single_zones' ] );
- // the JS action reponsible for the reviews data tables
- add_action( 'wp_ajax_the_charts_action', [$this, 'the_charts_action']);
- // the JS action responsible for the view review button on the data table rows
- add_action( 'wp_ajax_get_this_review', [$this, 'get_a_single_review_action']);
- // the JS action responsible for the view review button on the data table rows
- add_action( 'wp_ajax_delete_this_review', [$this, 'delete_a_single_review_action']);
- }
- public function handle_form_submission_callback() {
- check_ajax_referer( 'miwi_shortcode_form_nonce', 'the_posted_nonce' );
- if ( ! wp_verify_nonce( $_POST['the_posted_nonce'], 'miwi_shortcode_form_nonce' ) ) {
- die ( 'nonce error');
- }
- global $wpdb;
- $table_name = $wpdb->prefix . 'branch_reviews';
- $branch_id = sanitize_text_field( $_POST['branch_id'] );
- $fullName = sanitize_text_field( $_POST['fullName'] );
- $email = sanitize_text_field( $_POST['email'] );
- $phone = sanitize_text_field( $_POST['phone'] );
- $rating = (int) $_POST['rating'];
- $review_text = sanitize_textarea_field( $_POST['review_text'] );
- $branch_name ="";
- $branch_object = get_post($branch_id);
- if ($branch_object) {
- $branch_name = $branch_object->post_title;
- }else{
- $branch_name = "Deleted Branch";
- }
- $wpdb->insert( $table_name, [
- 'branch_id' => $branch_id,
- 'branch_name' => $branch_name,
- 'fullName' => $fullName,
- 'email' => $email,
- 'phone' => $phone,
- 'branch_id' => $branch_id,
- 'rating' => $rating,
- 'review_text' => $review_text,
- ]);
- $the_new_post = $wpdb->insert_id;
- $subscribers = get_users( array ( 'role' => 'Administrator' ) );
- $emails = array ();
- foreach ( $subscribers as $subscriber )
- $emails[] = $subscriber->user_email;
- $body = sprintf( 'Hey there is a new review for a branch! <br> Review ID: %s <br> Branch Name: %s <br> Customer Full Name: %s <br> Customer Email: $s <br> Customer Rating: %s <br> Customer Review %s', $the_new_post, $branch_name, $fullName, $email,$rating,$review_text);
- wp_mail( $emails, 'New branch review is added!', $body );
- wp_send_json_success( [ 'message' => 'Form submitted successfully!' ] );
- }
- public function get_branches_by_single_zones() {
- // check_ajax_referer( 'miwi_shortcode_form_nonce', 'the_posted_nonce' );
- // if ( ! wp_verify_nonce( $_POST['the_posted_nonce'], 'miwi_shortcode_form_nonce' ) ) {
- // // die ( 'nonce error');
- // }
- $zone_slug = $_POST['the_posted_id'];
- $args = array(
- 'post_type' => 'branch',
- 'posts_per_page' => -1,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'zone',
- 'field' => 'slug', // Use 'id' if filtering by slug
- 'terms' => $zone_slug, // Replace with $zone_id if using id
- ),
- ),
- );
- $branches_query = new WP_Query($args);
- $branch_ids = array();
- if ($branches_query->have_posts()) {
- while ($branches_query->have_posts()) {
- $branches_query->the_post();
- $branch_ids[] = get_the_ID();
- }
- wp_reset_postdata(); // Reset query
- wp_send_json_success($branch_ids);
- }else{
- wp_send_json_error("No branches found in this zone");
- }
- }
- public function the_charts_action() {
- $ratings_data = DB_Handler::get_data_for_the_charts();
- // Prepare data for Chart.js
- $chart_data = array();
- foreach ($ratings_data as $data) {
- $branch_title = get_the_title($data->branch_id);
- $chart_data[] = array(
- 'branch' => $branch_title,
- 'avg_rating' => round($data->avg_rating, 2),
- 'total_reviews' => $data->total_reviews,
- );
- }
- // Send JSON response
- wp_send_json_success($chart_data);
- }
- public function get_a_single_review_action(){
- $review_data = DB_Handler::get_single_review();
- $returnable_data = array();
- // Build query
- if ($review_data) {
- $branch_title = get_the_title($review_data[0]->branch_id);
- $returnable_data[] = array(
- 'id' => $review_data[0]->id,
- 'branch' => $branch_title,
- 'fullName' => $review_data[0]->fullName,
- 'phone' => $review_data[0]->phone,
- 'email' => $review_data[0]->email,
- 'rating' => $review_data[0]->rating,
- 'review_text' => $review_data[0]->review_text,
- 'created_at' => $review_data[0]->created_at,
- );
- wp_send_json_success($returnable_data);
- }
- }
- public function delete_a_single_review_action(){
- $returnable_data = DB_Handler::delete_single_review();
- if ($returnable_data == 1){
- wp_send_json_success(true);
- }
- else{
- wp_send_json_success(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment