/** * Get the like output on site * @param $bEcho bool * @return string */ function dob_vote_content($bEcho = false) { /*{{{*/ global $wpdb; $post_id = get_the_ID(); $dob_vote = ''; // Get the posts ids where we do not need to show like functionality $allowed_posts = $excluded_posts = $excluded_categories = $excluded_sections = array(); /*{{{*/ /*$allowed_posts = explode(",", get_option('dob_vote_allowed_posts')); $excluded_posts = explode(",", get_option('dob_vote_excluded_posts')); $excluded_categories = get_option('dob_vote_excluded_categories'); $excluded_sections = get_option('dob_vote_excluded_sections'); if (empty($excluded_categories)) $excluded_categories = array(); if (empty($excluded_sections)) $excluded_sections = array();*/ /*}}}*/ // Checking for excluded section. if yes, then dont show the like/dislike option if (in_array('home', $excluded_sections) && is_home() || in_array('archive', $excluded_sections) && is_archive() || in_array($post_id, $excluded_posts)) { return; } /*{{{*/ /* Checking for excluded categories $excluded = false; $category = get_the_category(); foreach($category as $cat) { if (in_array($cat->cat_ID, $excluded_categories) && !in_array($post_id, $allowed_posts)) { $excluded = true; } } // If excluded category, then dont show the like/dislike option if ($excluded) { return; }*/ /*}}}*/ $title_text_like = 'Like'; $title_text_unlike = 'Unlike'; /*{{{*/ /* Check for title text. if empty then have the default value $title_text = ''; //get_option('dob_vote_title_text'); if (empty($title_text)) { $title_text_like = __('Like', 'wti-like-post'); $title_text_unlike = __('Unlike', 'wti-like-post'); } else { $title_text = explode('/', get_option('dob_vote_title_text')); $title_text_like = $title_text[0]; $title_text_unlike = isset( $title_text[1] ) ? $title_text[1] : ''; }*/ /*}}}*/ // Get the nonce for security purpose and create the like and unlike urls $nonce = wp_create_nonce('dob_vote_vote_nonce'); $ajax_like_link = admin_url('admin-ajax.php?action=dob_vote_process_vote&task=like&post_id=' . $post_id . '&nonce=' . $nonce); $ajax_unlike_link = admin_url('admin-ajax.php?action=dob_vote_process_vote&task=unlike&post_id=' . $post_id . '&nonce=' . $nonce); $arr_vote_count = dob_get_vote_count($post_id); $like_count = $arr_vote_count['like']; $unlike_count = $arr_vote_count['unlike']; $msg = dob_get_voted_message($post_id); $alignment = 'align-right'; //("left" == get_option('dob_vote_alignment')) ? 'align-left' : 'align-right'; $style = 'style1'; //(get_option('dob_vote_voting_style') == "") ? 'style1' : get_option('dob_vote_voting_style'); $dob_vote .= "<div class='watch-action'>"; $dob_vote .= "<div class='watch-position " . $alignment . "'>"; $dob_vote .= "<div class='action-like'>"; $dob_vote .= "<a class='lbg-" . $style . " like-" . $post_id . " jlk' href='javascript:void(0)' data-task='like' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "' rel='nofollow'>"; $dob_vote .= "<img src='" . plugins_url('assets/images/pixel.gif', __FILE__) . "' title='" . $title_text_like . "' />"; $dob_vote .= "<span class='lc-" . $post_id . " lc'>" . $like_count . "</span>"; $dob_vote .= "</a></div>"; $dob_vote .= "<div class='action-unlike'>"; $dob_vote .= "<a class='unlbg-" . $style . " unlike-" . $post_id . " jlk' href='javascript:void(0)' data-task='unlike' data-post_id='" . $post_id . "' data-nonce='" . $nonce . "' rel='nofollow'>"; $dob_vote .= "<img src='" . plugins_url('assets/images/pixel.gif', __FILE__) . "' title='" . $title_text_unlike . "' />"; $dob_vote .= "<span class='unlc-" . $post_id . " unlc'>" . $unlike_count . "</span>"; $dob_vote .= "</a></div> "; $dob_vote .= "</div> "; $dob_vote .= "<div class='status-" . $post_id . " status " . $alignment . "'>" . $msg . "</div>"; $dob_vote .= "</div><div class='wti-clear'></div>"; if ($bEcho) { echo $dob_vote; } else { return $dob_vote; } }
function dob_ajax_callback() { global $wpdb, $global_real_ip; // Get request data $post_id = (int) $_REQUEST['post_id']; $task = $_REQUEST['task']; // Check for valid access if (!wp_verify_nonce($_REQUEST['nonce'], 'dob_vote_vote_nonce')) { $error = 1; $msg = 'Invalid access'; //__( 'Invalid access', 'wti-like-post' ); } else { if (!is_user_logged_in()) { // User needs to login to vote but has not logged in $error = 1; $msg = 'plz login'; //get_option( 'wti_like_post_login_message' ); } else { #$current_user = wp_get_current_user(); #$user_id = (int)$current_user->ID; $user_id = get_current_user_id(); // get value $old_row = dob_get_voted_data($post_id, $user_id); $old_value = is_null($old_row) ? null : (int) $old_row['value']; $value = $task == "like" ? 1 : -1; $value = $old_value == $value ? 0 : $value; // check cancel vote. // INSERT dob_vote_post_log $sql = "INSERT IGNORE INTO `{$wpdb->prefix}dob_vote_post_log` SET\n\t\t\tuser_id = %d, post_id = %d, value = %d, ip = %s"; $prepare = $wpdb->prepare($sql, $user_id, $post_id, $value, $global_real_ip); $success = $wpdb->query($prepare); if (empty($success)) { // failed (duplicated) $error = 1; $msg = "DB ERROR(SQL)<br>\n: " . $sql; $msg = "TOO FAST CLICK~!! "; } else { // success == 1 (affected_rows) // UPDATE dob_vote_post_latest $table_name = $wpdb->prefix . 'dob_vote_post_latest'; if (is_null($old_value)) { $sql = "INSERT INTO `{$table_name}` SET\n\t\t\t\t\tpost_id = %d, user_id = %d, value = %d"; $prepare = $wpdb->prepare($sql, $post_id, $user_id, $value); } else { $sql = "UPDATE `{$table_name}` SET value = %d\n\t\t\t\t\tWHERE post_id = %d AND user_id = %d "; $prepare = $wpdb->prepare($sql, $value, $post_id, $user_id); } $success = $wpdb->query($prepare); if ($success) { $error = 0; $msg = 'Thanks for your vote.'; //get_option( 'wti_like_post_thank_message' ); } else { $error = 1; $msg = "DB ERROR(SQL)<br>\n: " . $sql; } } $arr_vote_count = dob_get_vote_count($post_id); } } // Check for method of processing the data if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])) { $result = array('msg' => $msg, 'error' => $error, 'old' => $old_value, 'task' => $task, 'like' => $arr_vote_count['like'], 'unlike' => $arr_vote_count['unlike']); header('Content-type: application/json'); echo json_encode($result, JSON_UNESCAPED_UNICODE); } else { header('location:' . $_SERVER['HTTP_REFERER']); } exit; }