/**
  * Get opinions for an product
  * @param integer $product_id
  * @return array
  */
 function get_opinions_for_product($product_id, $approved = '')
 {
     $returned_opinions = array();
     if (!empty($product_id)) {
         $opinions = get_comments(array('post_id' => $product_id, 'status' => $approved));
         if (!empty($opinions)) {
             foreach ($opinions as $opinion) {
                 $comment_def = array('id' => $opinion->comment_ID, 'opinion_post_ID' => $opinion->comment_post_ID, 'author_IP' => $opinion->comment_author_IP, 'author' => $opinion->comment_author, 'author_email' => $opinion->comment_author_email, 'opinion_content' => $opinion->comment_content, 'opinion_date' => $opinion->comment_date, 'author_id' => $opinion->user_id, 'opinion_approved' => $opinion->comment_approved, 'opinion_rate' => get_comment_meta($opinion->comment_ID, '_wps_customer_rate', true));
                 $o = new wps_opinion_model();
                 $o->Create($comment_def);
                 $returned_opinions[] = $o;
             }
         }
     }
     return $returned_opinions;
 }
 /**
  * AJAX - Save opinions
  */
 function wps_opinion_save_form()
 {
     $status = false;
     $response = '';
     $wps_opinion_mdl = new wps_opinion_model();
     $user_id = get_current_user_id();
     // Check if opinion for this product has been posted
     $checking_opinions = $wps_opinion_mdl->get_opinions_for_product(intval($_POST['wps-opinion-product-id']));
     $comment_exist = false;
     if (!empty($checking_opinions)) {
         foreach ($checking_opinions as $o) {
             if ($o->user_id == $user_id) {
                 $comment_exist = true;
             }
         }
     }
     if (!empty($user_id) && !$comment_exist) {
         $user_data = get_userdata($user_id);
         $data = array('opinion_post_ID' => intval($_POST['wps-opinion-product-id']), 'author_IP' => $_SERVER['REMOTE_ADDR'], 'author' => get_user_meta($user_id, 'first_name', true) . ' ' . get_user_meta($user_id, 'last_name', true), 'author_email' => !empty($user_data->user_email) ? $user_data->user_email : '', 'opinion_content' => strip_tags($_POST['wps-opinion-comment']), 'opinion_date' => current_time('mysql', 0), 'author_id' => $user_id, 'opinion_rate' => intval($_POST['wps-opinion-rate']));
         $wps_opinion_mdl->Create($data);
         $wps_opinion_mdl->Save();
         $status = true;
         $response = __('Comment has been send and must be approved by an administrator before publishing', 'wpshop');
     } else {
         $response = __('You have already post a comment for this product', 'wpshop');
     }
     echo json_encode(array('status' => $status, 'response' => $response));
     wp_die();
 }