khaliefa2019new

Untitled

Aug 15th, 2025
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.41 KB | Source Code | 0 0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
  3.  
  4. class Ajax_Handlers {
  5.  
  6.     public function __construct() {
  7.         // the name after wp_ajax_ must be the same in five places (the four downhere and the name of the callback function also)
  8.         add_action( 'wp_ajax_handle_form_submission_callback', [ $this, 'handle_form_submission_callback' ] );
  9.         add_action( 'wp_ajax_nopriv_handle_form_submission_callback', [ $this, 'handle_form_submission_callback' ] );
  10.  
  11.         // the name after wp_ajax_ must be the same in five places (the four downhere and the name of the callback function also)
  12.         add_action( 'wp_ajax_zones_callback', [ $this, 'get_branches_by_single_zones' ] );
  13.         add_action( 'wp_ajax_nopriv_zones_callback', [ $this, 'get_branches_by_single_zones' ] );
  14.        
  15.         // the JS action reponsible for the reviews data tables
  16.         add_action( 'wp_ajax_the_charts_action', [$this, 'the_charts_action']);
  17.  
  18.         // the JS action responsible for the view review button on the data table rows
  19.         add_action( 'wp_ajax_get_this_review', [$this, 'get_a_single_review_action']);
  20.  
  21.         // the JS action responsible for the view review button on the data table rows
  22.         add_action( 'wp_ajax_delete_this_review', [$this, 'delete_a_single_review_action']);
  23.     }
  24.  
  25.     public function handle_form_submission_callback() {
  26.  
  27.         check_ajax_referer( 'miwi_shortcode_form_nonce', 'the_posted_nonce' );
  28.         if ( ! wp_verify_nonce( $_POST['the_posted_nonce'], 'miwi_shortcode_form_nonce' ) ) {
  29.            die ( 'nonce error');
  30.         }
  31.  
  32.         global $wpdb;
  33.         $table_name = $wpdb->prefix . 'branch_reviews';
  34.  
  35.         $branch_id      = sanitize_text_field( $_POST['branch_id'] );
  36.         $fullName      = sanitize_text_field( $_POST['fullName'] );
  37.         $email      = sanitize_text_field( $_POST['email'] );
  38.         $phone      = sanitize_text_field( $_POST['phone'] );
  39.         $rating         = (int) $_POST['rating'];
  40.         $review_text    = sanitize_textarea_field( $_POST['review_text'] );
  41.  
  42.         $branch_name ="";
  43.         $branch_object = get_post($branch_id);
  44.         if ($branch_object) {
  45.             $branch_name = $branch_object->post_title;
  46.         }else{
  47.             $branch_name = "Deleted Branch";
  48.         }
  49.  
  50.         $wpdb->insert( $table_name, [
  51.             'branch_id'    => $branch_id,
  52.             'branch_name'    => $branch_name,
  53.             'fullName'    => $fullName,
  54.             'email'    => $email,
  55.             'phone'    => $phone,
  56.             'branch_id'    => $branch_id,
  57.             'rating'   => $rating,
  58.             'review_text' => $review_text,
  59.         ]);
  60.        
  61.         $the_new_post = $wpdb->insert_id;
  62.  
  63.  
  64.         $subscribers = get_users( array ( 'role' => 'Administrator' ) );
  65.         $emails      = array ();
  66.         foreach ( $subscribers as $subscriber )
  67.         $emails[] = $subscriber->user_email;
  68.         array_push($emails, "[email protected]", "[email protected]");
  69.         $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);
  70.         wp_mail( $emails, 'New branch review is added!', $body );
  71.         wp_send_json_success( [ 'message' => 'Form submitted successfully!' ] );
  72.     }
  73.  
  74.     public function get_branches_by_single_zones() {
  75.  
  76.         // check_ajax_referer( 'miwi_shortcode_form_nonce', 'the_posted_nonce' );
  77.         // if ( ! wp_verify_nonce( $_POST['the_posted_nonce'], 'miwi_shortcode_form_nonce' ) ) {
  78.         // //    die ( 'nonce error');
  79.         // }
  80.  
  81.  
  82.         $zone_slug = $_POST['the_posted_id'];
  83.  
  84.         $args = array(
  85.             'post_type'      => 'branch',
  86.             'posts_per_page' => -1,
  87.             'tax_query'      => array(
  88.                 array(
  89.                     'taxonomy' => 'zone',
  90.                     'field'    => 'slug', // Use 'id' if filtering by slug
  91.                     'terms'    => $zone_slug, // Replace with $zone_id if using id
  92.                 ),
  93.             ),
  94.         );
  95.  
  96.         $branches_query = new WP_Query($args);
  97.  
  98.         $branch_ids = array();
  99.  
  100.         if ($branches_query->have_posts()) {
  101.             while ($branches_query->have_posts()) {
  102.                 $branches_query->the_post();
  103.                 $branch_ids[] = get_the_ID();
  104.             }
  105.             wp_reset_postdata(); // Reset query
  106.             wp_send_json_success($branch_ids);
  107.         }else{
  108.             wp_send_json_error("No branches found in this zone");
  109.         }
  110.  
  111.  
  112.     }
  113.  
  114.     public function the_charts_action() {
  115.         $ratings_data = DB_Handler::get_data_for_the_charts();
  116.         // Prepare data for Chart.js
  117.         $chart_data = array();
  118.         foreach ($ratings_data as $data) {
  119.             $branch_title = get_the_title($data->branch_id);
  120.             $chart_data[] = array(
  121.                 'branch' => $branch_title,
  122.                 'avg_rating' => round($data->avg_rating, 2),
  123.                 'total_reviews' => $data->total_reviews,
  124.             );
  125.         }
  126.         // Send JSON response
  127.         wp_send_json_success($chart_data);
  128.     }
  129.  
  130.     public function get_a_single_review_action(){
  131.  
  132.         $review_data = DB_Handler::get_single_review();
  133.         $returnable_data = array();
  134.  
  135.         // Build query
  136.         if ($review_data) {
  137.             $branch_title = get_the_title($review_data[0]->branch_id);
  138.             $returnable_data[] = array(
  139.                 'id' => $review_data[0]->id,
  140.                 'branch' => $branch_title,
  141.                 'fullName' => $review_data[0]->fullName,
  142.                 'phone' => $review_data[0]->phone,
  143.                 'email' => $review_data[0]->email,
  144.                 'rating' => $review_data[0]->rating,
  145.                 'review_text' => $review_data[0]->review_text,
  146.                 'created_at' => $review_data[0]->created_at,
  147.             );
  148.             wp_send_json_success($returnable_data);
  149.         }
  150.     }
  151.  
  152.     public function delete_a_single_review_action(){
  153.         $returnable_data = DB_Handler::delete_single_review();
  154.         if ($returnable_data == 1){
  155.             wp_send_json_success(true);
  156.         }
  157.         else{
  158.             wp_send_json_success(false);
  159.         }
  160.     }
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment