Permalink
Browse files

close #1

  • Loading branch information...
1 parent 82460d8 commit 079924b2b1c9afaee913af2a493a3743ae67661a @fumikito committed Mar 24, 2012
Showing with 61 additions and 27 deletions.
  1. +31 −4 anyway-feedback.class.php
  2. +4 −0 assets/afb-style.css
  3. +24 −22 assets/anyway-feedback-handler.js
  4. +2 −1 functions.php
View
@@ -12,7 +12,7 @@ class Anyway_Feedback{
* Version of this plugin
* @var float
*/
- var $version = 0.5;
+ var $version = 0.6;
/**
* Version of database
@@ -33,6 +33,12 @@ class Anyway_Feedback{
var $dir;
/**
+ * Name of Session key
+ * @var string
+ */
+ private $session = 'afb_session';
+
+ /**
* Domain name for i18n
* @var string
*/
@@ -289,6 +295,13 @@ function Anyway_Feedback(){
*/
function __construct(){
global $wpdb;
+ //Start Session
+ if(!isset($_SESSION)){
+ session_start();
+ }
+ if(!isset($_SESSION[$this->session]) || empty($_SESSION[$this->session])){
+ $_SESSION[$this->session] = array();
+ }
//Set directory
$this->dir = dirname(__FILE__);
//option
@@ -343,7 +356,7 @@ function ajax(){
"success" => true,
"message" => $this->_("Thank you for your feedback.")
);
- if(wp_verify_nonce($nonce, "anyway_feedback")){
+ if(wp_verify_nonce($nonce, "anyway_feedback") && !$this->does_current_user_posted($_REQUEST['post_type'], $_REQUEST['object_id'])){
//Feedback request is valid.
switch($_POST["class_name"]){
case "good":
@@ -362,6 +375,7 @@ function ajax(){
if(!$this->update($_POST["object_id"], $_POST["post_type"], $affirmative)){
$this->add($_POST["object_id"], $_POST["post_type"], $affirmative);
}
+ $_SESSION[$this->session][] = (string)$_POST['post_type'].'_'.$_POST['object_id'];
}
}else{
//Error.
@@ -500,9 +514,10 @@ function get_conroller_tag($object_id, $post_type){
$usefull = $this->_("Usefull");
$userless = $this->_("Useless");
$url = $post_type == "comment" ? get_permalink() : get_permalink($object_id);
+ $already_posted = $this->does_current_user_posted($post_type, $object_id) ? ' afb_posted' : '';
$before = <<<EOS
<!-- Anyway Feedback Container //-->
-<div class="afb_container" id="afb_comment_container_{$object_id}">
+<div class="afb_container{$already_posted}" id="afb_comment_container_{$object_id}">
EOS;
if(empty($this->option["controller"])){
$before .= <<<EOS
@@ -535,7 +550,19 @@ function get_conroller_tag($object_id, $post_type){
return $before.$after;
}
-
+ /**
+ * 現在のユーザーが回答済みか否か
+ * @param string $post_type
+ * @param int $object_id
+ * @return boolean
+ */
+ function does_current_user_posted($post_type, $object_id){
+ if(isset($_SESSION[$this->session])){
+ return (false !== array_search("{$post_type}_{$object_id}", $_SESSION[$this->session]));
+ }else{
+ return false;
+ }
+ }
/**
* Add controller panel to the_content()
View
@@ -30,4 +30,8 @@
}
.afb_container .status{
font-size:10px; margin-left:20px;
+}
+.afb_posted .good,
+.afb_posted .bad{
+ display: none;
}
@@ -1,27 +1,29 @@
jQuery(document).ready(function($){
$('.afb_container').find('a').click(function(e){
- var endpoint = this.href;
- var data = {
- action: "anyway_feedback",
- object_id: $(this).nextAll("input[name=object_id]").val(),
- post_type: $(this).nextAll('input[name=post_type]').val(),
- nonce: $(this).nextAll("input[name=nonce]").val(),
- class_name: $(this).attr('class')
- };
- var target = $(this).parent(".afb_container");
- $.post(
- endpoint,
- data,
- function(response){
- if(response.success){
- target.find('a, .input').remove();
- target.find('.message').addClass('success').text(response.message);
- }else{
- target.find('a, .input').remove();
- target.find('.message').addClass('error').text(response.message);
+ e.preventDefault();
+ if(!$(this).parents('.afb_container').hasClass('afb_posted')){
+ var endpoint = this.href;
+ var data = {
+ action: "anyway_feedback",
+ object_id: $(this).nextAll("input[name=object_id]").val(),
+ post_type: $(this).nextAll('input[name=post_type]').val(),
+ nonce: $(this).nextAll("input[name=nonce]").val(),
+ class_name: $(this).attr('class')
+ };
+ var target = $(this).parent(".afb_container");
+ $.post(
+ endpoint,
+ data,
+ function(response){
+ if(response.success){
+ target.find('a, .input').remove();
+ target.find('.message').addClass('success').text(response.message);
+ }else{
+ target.find('a, .input').remove();
+ target.find('.message').addClass('error').text(response.message);
+ }
}
- }
- );
- return false;
+ );
+ }
});
});
View
@@ -13,7 +13,8 @@
* @retun void
*/
function afb_display(){
- global $afb, $post; if($post){
+ global $afb, $post;
+ if($post){
echo $afb->get_conroller_tag(get_the_ID(), get_post_type());
}
}

0 comments on commit 079924b

Please sign in to comment.