/** * Ajax function for the review submission */ public static function cbRatingAjaxFunction() { global $wpdb; $user_id = get_current_user_id(); //returns 0 if guest $user_info = ''; if (isset($_POST['cbRatingData']) && !empty($_POST['cbRatingData'])) { $returnedData = $_POST['cbRatingData']; //var_dump($returnedData); if (wp_verify_nonce($returnedData['cbrp_nonce'], 'cb_ratingForm_front_form_nonce_field')) { if (!empty($returnedData['values'])) { list($insertArray['form_id'], $insertArray['post_id']) = explode('-', $returnedData['rp_id']); if ($user_id == 0) { $user_session = $_COOKIE[CB_RATINGSYSTEM_COOKIE_NAME]; //this is string $user_ip = CBRatingSystem::get_ipaddress(); } elseif ($user_id > 0) { $user_session = 'user-' . $user_id; //this is string $user_ip = CBRatingSystem::get_ipaddress(); $user_info = get_userdata($user_id); } $table_name1 = CBRatingSystemData::get_user_ratings_table_name(); $table_name2 = CBRatingSystemData::get_ratingForm_settings_table_name(); $sql_get_loggin = "SELECT logging_method FROM {$table_name2} where id=" . $insertArray['form_id']; //$sql_get_loggin = maybe_unserialize($sql_get_loggin[0]->logging_method); $query_result = $wpdb->get_results($sql_get_loggin); $query_result2 = maybe_unserialize($query_result[0]->logging_method); //getting the data according to the administrative settings using IP/Cookie for the last comment if (in_array("cookie", $query_result2) && !in_array("ip", $query_result2)) { $sql = $wpdb->prepare("SELECT COUNT(ur.id) AS count FROM {$table_name1} ur WHERE ur.form_id=%d AND ur.post_id=%d AND ur.user_id=%d AND ur.user_session = %s", $insertArray['form_id'], $insertArray['post_id'], $user_id, $user_session); $count = $wpdb->get_var($sql); } else { if (!in_array("cookie", $query_result2) && in_array("ip", $query_result2)) { $sql = $wpdb->prepare("SELECT COUNT(ur.id) AS count FROM {$table_name1} ur WHERE ur.form_id=%d AND ur.post_id=%d AND ur.user_id=%d AND ur.user_ip = %s", $insertArray['form_id'], $insertArray['post_id'], $user_id, $user_ip); $count = $wpdb->get_var($sql); } else { $sql = $wpdb->prepare("SELECT COUNT(ur.id) AS count FROM {$table_name1} ur WHERE ur.form_id=%d AND ur.post_id=%d AND ur.user_id=%d AND ur.user_ip = %s", $insertArray['form_id'], $insertArray['post_id'], $user_id, $user_ip); $count = $wpdb->get_var($sql); } } if ($count < 1) { // first time rating $insertArray['post_type'] = get_post_type($insertArray['post_id']); $insertArray['created'] = time(); $question = array(); if (!empty($returnedData['question'][$insertArray['form_id']]) && is_array($returnedData['question'][$insertArray['form_id']])) { foreach ($returnedData['question'][$insertArray['form_id']] as $qID => $qValue) { if (is_array($qValue) && !empty($qValue)) { foreach ($qValue as $key => $val) { $type = $qValue[$qID . '_type']; if (isset($qValue[$type . '-' . $qID]) && !empty($qValue[$type . '-' . $qID])) { $question[$qID] = $qValue[$type . '-' . $qID]; } elseif ($key != $qID . '_type' && $key != $type . '-' . $qID && !empty($val)) { $key = str_replace($qID . '_', '', $key); if (is_numeric($key)) { $question[$qID][$key] = $val; } } } } else { $question[$qID] = $qValue; } } } $insertArray['question'] = maybe_serialize($question); $comment = esc_html(sanitize_text_field($returnedData['comment'])); if (strlen($comment) <= $returnedData['comment_limit']) { $insertArray['comment'] = $comment; } elseif (strlen($comment) > $returnedData['comment_limit']) { $insertArray['comment'] = substr($comment, 0, intval($returnedData['comment_limit'])); } if (!is_user_logged_in()) { if (!empty($returnedData['user_name'])) { $insertArray['user_name'] = sanitize_text_field(trim($returnedData['user_name'])); } else { $encoded = json_encode(array('validation' => 1, 'errorMessage' => __('Name field can\'t be left blank.', 'cbratingsystem'))); echo $encoded; die; } if (!empty($returnedData['user_email']) and is_email(trim($returnedData['user_email']))) { $insertArray['user_email'] = sanitize_text_field(trim($returnedData['user_email'])); } else { $encoded = json_encode(array('validation' => 1, 'errorMessage' => __('Please enter a valid email address.', 'cbratingsystem'))); echo $encoded; die; } } /*else { //guest user $insertArray['user_name'] = ''; $insertArray['user_email'] = $returnedData['user_email']; $insertArray['user_ip'] = $user_ip; $insertArray['user_session'] = $user_session; }*/ $insertArray['user_name'] = $user_id > 0 ? $user_info->user_login : $returnedData['user_name']; $insertArray['user_email'] = $user_id > 0 ? $user_info->user_email : $returnedData['user_email']; $insertArray['user_ip'] = $user_ip; $insertArray['user_session'] = $user_session; /* * @example: * $returnedData['values'] = array( * 0 => 100 // label_id/criteria_id => ( (Score For this label / Star count) * 100) * ) */ $insertArray['rating'] = maybe_serialize($returnedData['values']); foreach ($returnedData['values'] as $key => $val) { if (is_numeric($key)) { $average[$key] = $val; } } $hash_comment = $insertArray['user_ip'] . $insertArray['user_session'] . $insertArray['user_email'] . time(); $hash_comment = md5($hash_comment); $insertArray['average'] = array_sum($average) / count($average); //$insertArray['user_ip'] = CBRatingSystem::get_ipaddress(); //$insertArray['user_session'] = ( $user_id != 0 ) ? 'user-' . $user_id : $_COOKIE[CB_RATINGSYSTEM_COOKIE_NAME]; $insertArray['user_id'] = $user_id; $insertArray['form_id'] = (int) $insertArray['form_id']; $insertArray['post_id'] = (int) $insertArray['post_id']; $insertArray['comment_status'] = $returnedData['comment_status']; $insertArray['comment_hash'] = $hash_comment; $insertArray['allow_user_to_hide'] = isset($returnedData["hide_this_user_name"]) ? $returnedData["hide_this_user_name"] : 0; $return = CBRatingSystemData::update_rating($insertArray); if ($insertArray['comment_status'] == 'unverified' && $insertArray['comment_hash'] != '' && $insertArray['user_email'] != '') { $cb_subject = __('Verify Your email', 'cbratingsystem'); $cb_message = get_site_url() . '?cbratingemailverify=' . $insertArray['comment_hash']; $from = get_option('admin_email'); //var_dump($from); wp_mail($insertArray['user_email'], $cb_subject, $cb_message); // mail($insertArray['user_email'],$cb_subject,$cb_message,"From: $from\n"); } // buddypress post added 29-10-14 codeboxr if (isset($returnedData["buddypress_post"]) && $returnedData["buddypress_post"] == '1' || is_user_logged_in()) { if (function_exists('bp_is_active')) { // buddy press active $buddy_post = array('id' => false, 'action' => bp_core_get_userlink(bp_loggedin_user_id()) . __(' has rated this post ', 'cbratingsystem') . get_permalink($insertArray['post_id']), 'content' => $insertArray['comment'], 'component' => 'ratingcomponent', 'type' => 'ratingcomponent_activity', 'primary_link' => '', 'user_id' => bp_loggedin_user_id(), 'item_id' => false, 'secondary_item_id' => false, 'recorded_time' => bp_core_current_time(), 'hide_sitewide' => false, 'is_spam' => false); do_action('cbratingsystem_buddypress_activity_post', $buddy_post); } //end checking if buddypress is installed } $lastcommentrt = $return; // storing the last commend id for safety if ($return) { //getting the criteria rating result $ratingAverage = self::viewPerCriteriaRatingResult($insertArray['form_id'], $insertArray['post_id'], $user_id); $ratingsCount = $ratingAverage['ratingsCount'][$insertArray['form_id'] . '-' . $insertArray['post_id']]; if (!empty($ratingsCount)) { $rating = array('form_id' => $insertArray['form_id'], 'post_id' => $insertArray['post_id'], 'post_type' => $insertArray['post_type'], 'per_post_rating_count' => $ratingsCount, 'per_post_rating_summary' => number_format($ratingAverage['perPost'][$insertArray['post_id']], 2), 'custom_user_rating_summary' => maybe_serialize($ratingAverage['customUser']), 'per_criteria_rating_summary' => maybe_serialize($ratingAverage['avgPerCriteria'])); foreach ($ratingAverage['avgPerCriteria'] as $cId => $criteria) { $cCriteria['readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-count'] = count($criteria['stars']); $cCriteria['readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-value'] = $criteria['value']; } if (!empty($ratingAverage['customUser']['perCriteria']['editor'])) { foreach ($ratingAverage['customUser']['perCriteria']['editor'] as $cId => $criteria) { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-count'] = count($ratingAverage['avgPerCriteria'][$cId]['stars']); $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-value'] = $criteria['value']; } } else { foreach ($ratingAverage['avgPerCriteria'] as $cId => $criteria) { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-count'] = count($ratingAverage['avgPerCriteria'][$cId]['stars']); $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-value'] = 0; } } $cCriteria['readonly-criteria-label-' . $insertArray['form_id'] . '-post-' . $insertArray['post_id'] . '-avgvalue'] = $rating['per_post_rating_summary']; if (!empty($ratingAverage['customUser']['perCriteria']['editor'])) { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-post-' . $insertArray['post_id'] . '-avgvalue'] = $ratingAverage['customUser']['perPost']['editor']; } else { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-post-' . $insertArray['post_id'] . '-avgvalue'] = 0; } $return = CBRatingSystemData::update_rating_summary($rating); $editorCount = isset($ratingAverage['customUser']['perPostRateCount']['editor']) ? (int) $ratingAverage['customUser']['perPostRateCount']['editor'] : 0; //review part here if (empty($ratingFormArray)) { $defaultFormId = get_option('cbratingsystem_defaultratingForm'); $ratingFormId = apply_filters('rating_form_array', $defaultFormId); //getting the rating form data from here. $ratingFormArray = CBRatingSystemData::get_ratingForm($insertArray['form_id']); } //get the theme for doing frontend UI works $theme_key = get_option('cbratingsystem_theme_key'); $reviewOptions['theme'] = $theme_key; $reviewOptions['comment_status'] = $insertArray['comment_status']; $reviewOptions["hide_this_user_name"] = isset($returnedData["hide_this_user_name"]) ? $returnedData["hide_this_user_name"] : 0; $lastcomment = is_numeric($lastcommentrt) ? self::build_user_rating_review_single($reviewOptions, $ratingFormArray, $lastcommentrt) : ''; //echo $lastcomment; if ($return) { $encoded = json_encode(array('img_path' => CB_RATINGSYSTEM_PLUGIN_DIR_IMG, 'hints' => json_encode($cCriteria), 'is_rated' => 1, 'ratingsCount' => $ratingsCount, 'editorRatingsCount' => $editorCount, 'lastcomment' => $lastcomment, 'theme_key' => $reviewOptions['theme'], 'firstcomment' => true, 'comment_status' => $insertArray['comment_status'])); echo $encoded; } } } } else { //at least one rating done $summary = CBRatingSystemData::get_ratings_summary(array('form_id' => array($insertArray['form_id']), 'post_id' => array($insertArray['post_id']))); if (!empty($summary[0])) { foreach ($summary[0]['per_criteria_rating_summary'] as $cId => $criteria) { $cCriteria['readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-count'] = count($criteria['stars']); $cCriteria['readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-value'] = $criteria['value']; } if (!empty($ratingAverage['customUser']['perCriteria']['editor'])) { foreach ($ratingAverage['customUser']['perCriteria']['editor'] as $cId => $criteria) { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-count'] = count($ratingAverage['avgPerCriteria'][$cId]['stars']); $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-value'] = $criteria['value']; } } else { if (!empty($summary[0])) { foreach ($summary[0]['per_criteria_rating_summary'] as $cId => $criteria) { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-count'] = count($ratingAverage['avgPerCriteria'][$cId]['stars']); $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-stars-' . $cId . '-value'] = 0; } } } $cCriteria['readonly-criteria-label-' . $insertArray['form_id'] . '-post-' . $insertArray['post_id'] . '-avgvalue'] = $rating['per_post_rating_summary']; if (!empty($ratingAverage['customUser']['perCriteria']['editor'])) { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-post-' . $insertArray['post_id'] . '-avgvalue'] = $ratingAverage['customUser']['perPost']['editor']; } else { $cCriteria['editor-readonly-criteria-label-' . $insertArray['form_id'] . '-post-' . $insertArray['post_id'] . '-avgvalue'] = 0; } $encoded = json_encode(array('img_path' => CB_RATINGSYSTEM_PLUGIN_DIR_IMG, 'hints' => json_encode($cCriteria), 'is_rated' => 1, 'ratingsCount' => $summary[0]['per_post_rating_count'], 'errorMessage' => __('You have already rated this.', 'cbratingsystem'))); echo $encoded; } else { $encoded = json_encode(array('is_rated' => 1, 'errorMessage' => __('An error occurred while storing data. Please ensure that all data are resonable. If problem persist please contact the administrator.', 'cbratingsystem'))); echo $encoded; } } //end you already submitted your rating. } } } die; }