Advertisement
Guest User

Untitled

a guest
May 29th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.14 KB | None | 0 0
  1. diff --git a/jquery.plus1.js b/jquery.plus1.js
  2. index a0b51a5..e9f0221 100644
  3. --- a/jquery.plus1.js
  4. +++ b/jquery.plus1.js
  5. @@ -4,14 +4,32 @@
  6. attach: function(context){
  7. $('.plus1-widget', context).once('plus1', function(){
  8. var plus1_widget = $(this);
  9. - plus1_widget.find('.plus1-link').attr('href', function(){ return $(this).attr('href') + '&json=true'; }).click(function(){
  10. - $.getJSON($(this).attr('href'), function(json){
  11. - if (json) {
  12. - var newWidget = $(json.widget);
  13. - newWidget.hide();
  14. - plus1_widget.replaceWith(newWidget);
  15. - newWidget.fadeIn('slow');
  16. - Drupal.attachBehaviors();
  17. + plus1_widget.find('.plus1-vote-form').attr('action', function(){ return $(this).attr('action') + '&json=true'; }).submit(function(){
  18. + var form = this, data = {};
  19. + var solution = $('input.plus1-form-captcha', form).val();
  20. +
  21. + if (solution) {
  22. + data['plus1_captcha'] = solution;
  23. + }
  24. +
  25. + jQuery.ajax({
  26. + 'type': 'POST',
  27. + 'dataType': 'json',
  28. + 'data': data,
  29. + 'url': $(this).attr('action'),
  30. + 'success': function (json) {
  31. + if (json === 'captcha_failed') {
  32. + $('.plus1-form-captcha', form).addClass('error');
  33. + } else if (typeof json == 'object' && json.widget) {
  34. + var key = $('input[name=plus1_key]', form).val();
  35. + $.cookie("Drupal.visitor.plus1_voted_" + key, "true", { path: '/', expires: 365 });
  36. +
  37. + var newWidget = $(json.widget);
  38. + newWidget.hide();
  39. + plus1_widget.replaceWith(newWidget);
  40. + newWidget.fadeIn('slow');
  41. + Drupal.attachBehaviors();
  42. + }
  43. }
  44. });
  45. return false;
  46. @@ -19,4 +37,4 @@
  47. });
  48. }
  49. };
  50. -})(jQuery)
  51. +})(jQuery)
  52. \ No newline at end of file
  53. diff --git a/plus1.module b/plus1.module
  54. index 51bac4a..2c475c2 100644
  55. --- a/plus1.module
  56. +++ b/plus1.module
  57. @@ -135,6 +135,25 @@ function plus1_vote($entity_type, $entity_id, $tag = 'plus1_node_vote') {
  58. }
  59. $voted = plus1_get_votes($entity_type, $entity_id, $user->uid, $tag);
  60.  
  61. + $key = implode('_', array($entity_type, $entity_id, $tag));
  62. + if (isset($_COOKIE['Drupal_visitor_plus1_voted_' . $key]) && $_COOKIE['Drupal_visitor_plus1_voted_' . $key] == 'true') {
  63. + $voted = TRUE;
  64. + }
  65. +
  66. + // Check captcha
  67. + if (!user_is_logged_in()) {
  68. + if (!isset($_POST['plus1_captcha']) || $_POST['plus1_captcha'] != $_GET['plus1_key2'] - 42) {
  69. + if (isset($_GET['json'])) {
  70. + drupal_json_output('captcha_failed');
  71. + } else {
  72. + drupal_set_message(t('Captcha was not answered correctly. Please try again.'), 'error');
  73. + // go to the page where user pressed vote button
  74. + drupal_goto();
  75. + }
  76. + return;
  77. + }
  78. + }
  79. +
  80. $vote_source = $user->uid ? $user->uid : ip_address();
  81. drupal_alter('plus1_source', $vote_source);
  82.  
  83. @@ -158,6 +177,9 @@ function plus1_vote($entity_type, $entity_id, $tag = 'plus1_node_vote') {
  84. $results = votingapi_select_results($criteria);
  85. $score = $results[0]['value'] ? $results[0]['value'] : 0;
  86. module_invoke_all('plus1_voted', 'vote', $entity_type, $entity_id, $tag, $score, $user);
  87. +
  88. + user_cookie_save(array('plus1_voted_' . $key => 'true'));
  89. +
  90. if (isset($_GET['json'])) {
  91. // Return json to client side, taking into consideration entity type.
  92. drupal_json_output(theme('plus1_json_response__' . $entity_type . '__' . $tag, array('entity_type' => $entity_type, 'entity_id' => $entity_id, 'tag' => $tag, 'score' => $score, 'vote_type' => 'vote')));
  93. @@ -523,4 +545,4 @@ function plus1_get_cleared_destination() {
  94. * @return void
  95. */
  96. function hook_plus1_voted($vote_type, $entity_type, $entity_id, $tag, $score, $user) {
  97. -}
  98. +}
  99. \ No newline at end of file
  100. diff --git a/theme/theme.inc b/theme/theme.inc
  101. index 2670dd7..ed587c6 100644
  102. --- a/theme/theme.inc
  103. +++ b/theme/theme.inc
  104. @@ -14,6 +14,7 @@ function template_preprocess_plus1_widget(&$variables) {
  105. $entity_type = $variables['entity_type'];
  106. $entity_id = $variables['entity_id'];
  107. $tag = $variables['tag'];
  108. + $key = implode('_', array($entity_type, $entity_id, $tag));
  109. $voted = $variables['voted'];
  110. $logged_in = $variables['logged_in'];
  111. $score = $variables['score'];
  112. @@ -26,6 +27,13 @@ function template_preprocess_plus1_widget(&$variables) {
  113. $undo_vote_text = $variables['undo_vote_text'];
  114. $voted_text = $variables['voted_text'];
  115.  
  116. + if (isset($_COOKIE['Drupal_visitor_plus1_voted_' . $key]) && $_COOKIE['Drupal_visitor_plus1_voted_' . $key] == 'true') {
  117. + $variables['voted'] = TRUE;
  118. + if (!$logged_in) {
  119. + $can_undo_vote = FALSE;
  120. + }
  121. + }
  122. +
  123. if (!$logged_in && !$can_vote) {
  124. $variables['widget_message'] = l(t('Log in<br />to vote'), 'user', array('html' => TRUE));
  125. }
  126. @@ -33,7 +41,11 @@ function template_preprocess_plus1_widget(&$variables) {
  127. // is the user can undo his vote then provide him with link
  128. if ($can_undo_vote) {
  129. if ($undo_vote_text != "") {
  130. - $variables['widget_message'] = l($undo_vote_text, $undo_vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
  131. + $output_content = '<form class="plus1-vote-form" action="'. url($undo_vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link')))) .'" method="post"><div>';
  132. + $output_content .= '<input type="hidden" name="plus1_key" value="' . $key . '">';
  133. + $output_content .= '<button type="submit" class="plus1-link"><span>' . $undo_vote_text . '</span></button>';
  134. + $output_content .= '</div></form>';
  135. + $variables['widget_message'] = $output_content; //($undo_vote_text, $undo_vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
  136. }
  137. else {
  138. // if we don't have text for undo action, add "arrow-down" link after score.
  139. @@ -42,12 +54,40 @@ function template_preprocess_plus1_widget(&$variables) {
  140. }
  141. }
  142. else {
  143. - $variables['widget_message'] = $voted_text;
  144. + $variables['widget_message'] = '<span class="plus1-novote">' . $voted_text . '</span>';
  145. }
  146. }
  147. elseif ($can_vote) {
  148. + // Prepare captcha, put answer in query, should be sufficient to fend off bots
  149. + $answer = mt_rand(1, 20);
  150. + $link_query['plus1_key2'] = $answer + 42;
  151. +
  152. // User is eligible to vote.
  153. - $variables['widget_message'] = l($vote_text, $vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
  154. + $output_content = '<form class="plus1-vote-form" action="'. url($vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link')))) .'" method="post"><div>';
  155. + $output_content .= '<input type="hidden" name="plus1_key" value="' . $key . '">';
  156. + $output_content .= '<button type="submit" class="plus1-link"><span>' . $vote_text . '</span></button>';
  157. +
  158. + if (!$logged_in) {
  159. + // Add simple Math captcha
  160. + $x = mt_rand(1, $answer);
  161. + $y = $answer - $x;
  162. + $form = array(
  163. + '#type' => 'textfield',
  164. + '#title' => t('Math question'),
  165. + '#description' => t('Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.'),
  166. + '#field_prefix' => t('@x + @y = ', array('@x' => $x, '@y' => $y)),
  167. + '#size' => 4,
  168. + '#maxlength' => 2,
  169. + '#required' => TRUE,
  170. + '#attributes' => array('class' => array('plus1-form-captcha'), 'name' => 'plus1_captcha'),
  171. + );
  172. + $output_content .= drupal_render($form);
  173. + }
  174. +
  175. + $output_content .= '</div></form>';
  176. +
  177. + drupal_add_library('system', 'jquery.cookie');
  178. + $variables['widget_message'] = $output_content;//l($vote_text, $vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
  179. }
  180. }
  181.  
  182. @@ -71,4 +111,4 @@ function theme_plus1_json_response($variables) {
  183. return array('widget' => drupal_render(plus1_build_comment_jquery_widget($variables['entity_id'], $variables['tag'])));
  184. break;
  185. }
  186. -}
  187. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement