Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/jquery.plus1.js b/jquery.plus1.js
- index a0b51a5..e9f0221 100644
- --- a/jquery.plus1.js
- +++ b/jquery.plus1.js
- @@ -4,14 +4,32 @@
- attach: function(context){
- $('.plus1-widget', context).once('plus1', function(){
- var plus1_widget = $(this);
- - plus1_widget.find('.plus1-link').attr('href', function(){ return $(this).attr('href') + '&json=true'; }).click(function(){
- - $.getJSON($(this).attr('href'), function(json){
- - if (json) {
- - var newWidget = $(json.widget);
- - newWidget.hide();
- - plus1_widget.replaceWith(newWidget);
- - newWidget.fadeIn('slow');
- - Drupal.attachBehaviors();
- + plus1_widget.find('.plus1-vote-form').attr('action', function(){ return $(this).attr('action') + '&json=true'; }).submit(function(){
- + var form = this, data = {};
- + var solution = $('input.plus1-form-captcha', form).val();
- +
- + if (solution) {
- + data['plus1_captcha'] = solution;
- + }
- +
- + jQuery.ajax({
- + 'type': 'POST',
- + 'dataType': 'json',
- + 'data': data,
- + 'url': $(this).attr('action'),
- + 'success': function (json) {
- + if (json === 'captcha_failed') {
- + $('.plus1-form-captcha', form).addClass('error');
- + } else if (typeof json == 'object' && json.widget) {
- + var key = $('input[name=plus1_key]', form).val();
- + $.cookie("Drupal.visitor.plus1_voted_" + key, "true", { path: '/', expires: 365 });
- +
- + var newWidget = $(json.widget);
- + newWidget.hide();
- + plus1_widget.replaceWith(newWidget);
- + newWidget.fadeIn('slow');
- + Drupal.attachBehaviors();
- + }
- }
- });
- return false;
- @@ -19,4 +37,4 @@
- });
- }
- };
- -})(jQuery)
- +})(jQuery)
- \ No newline at end of file
- diff --git a/plus1.module b/plus1.module
- index 51bac4a..2c475c2 100644
- --- a/plus1.module
- +++ b/plus1.module
- @@ -135,6 +135,25 @@ function plus1_vote($entity_type, $entity_id, $tag = 'plus1_node_vote') {
- }
- $voted = plus1_get_votes($entity_type, $entity_id, $user->uid, $tag);
- + $key = implode('_', array($entity_type, $entity_id, $tag));
- + if (isset($_COOKIE['Drupal_visitor_plus1_voted_' . $key]) && $_COOKIE['Drupal_visitor_plus1_voted_' . $key] == 'true') {
- + $voted = TRUE;
- + }
- +
- + // Check captcha
- + if (!user_is_logged_in()) {
- + if (!isset($_POST['plus1_captcha']) || $_POST['plus1_captcha'] != $_GET['plus1_key2'] - 42) {
- + if (isset($_GET['json'])) {
- + drupal_json_output('captcha_failed');
- + } else {
- + drupal_set_message(t('Captcha was not answered correctly. Please try again.'), 'error');
- + // go to the page where user pressed vote button
- + drupal_goto();
- + }
- + return;
- + }
- + }
- +
- $vote_source = $user->uid ? $user->uid : ip_address();
- drupal_alter('plus1_source', $vote_source);
- @@ -158,6 +177,9 @@ function plus1_vote($entity_type, $entity_id, $tag = 'plus1_node_vote') {
- $results = votingapi_select_results($criteria);
- $score = $results[0]['value'] ? $results[0]['value'] : 0;
- module_invoke_all('plus1_voted', 'vote', $entity_type, $entity_id, $tag, $score, $user);
- +
- + user_cookie_save(array('plus1_voted_' . $key => 'true'));
- +
- if (isset($_GET['json'])) {
- // Return json to client side, taking into consideration entity type.
- 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')));
- @@ -523,4 +545,4 @@ function plus1_get_cleared_destination() {
- * @return void
- */
- function hook_plus1_voted($vote_type, $entity_type, $entity_id, $tag, $score, $user) {
- -}
- +}
- \ No newline at end of file
- diff --git a/theme/theme.inc b/theme/theme.inc
- index 2670dd7..ed587c6 100644
- --- a/theme/theme.inc
- +++ b/theme/theme.inc
- @@ -14,6 +14,7 @@ function template_preprocess_plus1_widget(&$variables) {
- $entity_type = $variables['entity_type'];
- $entity_id = $variables['entity_id'];
- $tag = $variables['tag'];
- + $key = implode('_', array($entity_type, $entity_id, $tag));
- $voted = $variables['voted'];
- $logged_in = $variables['logged_in'];
- $score = $variables['score'];
- @@ -26,6 +27,13 @@ function template_preprocess_plus1_widget(&$variables) {
- $undo_vote_text = $variables['undo_vote_text'];
- $voted_text = $variables['voted_text'];
- + if (isset($_COOKIE['Drupal_visitor_plus1_voted_' . $key]) && $_COOKIE['Drupal_visitor_plus1_voted_' . $key] == 'true') {
- + $variables['voted'] = TRUE;
- + if (!$logged_in) {
- + $can_undo_vote = FALSE;
- + }
- + }
- +
- if (!$logged_in && !$can_vote) {
- $variables['widget_message'] = l(t('Log in<br />to vote'), 'user', array('html' => TRUE));
- }
- @@ -33,7 +41,11 @@ function template_preprocess_plus1_widget(&$variables) {
- // is the user can undo his vote then provide him with link
- if ($can_undo_vote) {
- if ($undo_vote_text != "") {
- - $variables['widget_message'] = l($undo_vote_text, $undo_vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
- + $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>';
- + $output_content .= '<input type="hidden" name="plus1_key" value="' . $key . '">';
- + $output_content .= '<button type="submit" class="plus1-link"><span>' . $undo_vote_text . '</span></button>';
- + $output_content .= '</div></form>';
- + $variables['widget_message'] = $output_content; //($undo_vote_text, $undo_vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
- }
- else {
- // if we don't have text for undo action, add "arrow-down" link after score.
- @@ -42,12 +54,40 @@ function template_preprocess_plus1_widget(&$variables) {
- }
- }
- else {
- - $variables['widget_message'] = $voted_text;
- + $variables['widget_message'] = '<span class="plus1-novote">' . $voted_text . '</span>';
- }
- }
- elseif ($can_vote) {
- + // Prepare captcha, put answer in query, should be sufficient to fend off bots
- + $answer = mt_rand(1, 20);
- + $link_query['plus1_key2'] = $answer + 42;
- +
- // User is eligible to vote.
- - $variables['widget_message'] = l($vote_text, $vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
- + $output_content = '<form class="plus1-vote-form" action="'. url($vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link')))) .'" method="post"><div>';
- + $output_content .= '<input type="hidden" name="plus1_key" value="' . $key . '">';
- + $output_content .= '<button type="submit" class="plus1-link"><span>' . $vote_text . '</span></button>';
- +
- + if (!$logged_in) {
- + // Add simple Math captcha
- + $x = mt_rand(1, $answer);
- + $y = $answer - $x;
- + $form = array(
- + '#type' => 'textfield',
- + '#title' => t('Math question'),
- + '#description' => t('Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.'),
- + '#field_prefix' => t('@x + @y = ', array('@x' => $x, '@y' => $y)),
- + '#size' => 4,
- + '#maxlength' => 2,
- + '#required' => TRUE,
- + '#attributes' => array('class' => array('plus1-form-captcha'), 'name' => 'plus1_captcha'),
- + );
- + $output_content .= drupal_render($form);
- + }
- +
- + $output_content .= '</div></form>';
- +
- + drupal_add_library('system', 'jquery.cookie');
- + $variables['widget_message'] = $output_content;//l($vote_text, $vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
- }
- }
- @@ -71,4 +111,4 @@ function theme_plus1_json_response($variables) {
- return array('widget' => drupal_render(plus1_build_comment_jquery_widget($variables['entity_id'], $variables['tag'])));
- break;
- }
- -}
- +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement